package com.test.test.service.impl;

import com.test.common.utils.DateUtils;
import com.test.common.utils.SecurityUtils;
import com.test.common.utils.bean.BeanUtils;
import com.test.test.domain.TestReport;
import com.test.test.domain.qo.TestReportAddQO;
import com.test.test.domain.vo.TestReportVo;
import com.test.test.mapper.TestPlanReportMapper;
import com.test.test.mapper.TestReportMapper;
import com.test.test.service.ITestReportService;
import jakarta.annotation.Resource;
import java.util.List;
import java.util.UUID;
import org.springframework.stereotype.Service;

/**
 * 测试报告Service业务层实现
 *
 * @author test
 * @date 2025-04-25
 */
@Service
public class TestReportServiceImpl implements ITestReportService {

    @Resource
    private TestReportMapper testReportMapper;

    @Resource
    private TestPlanReportMapper testPlanReportMapper;

    /**
     * 查询测试报告列表
     * @param planId
     * @return
     */
    @Override
    public List<TestReportVo> selectTestReportList(Long planId) {
        return testReportMapper.selectTestReportList(planId);
    }

    /**
     * 新增测试报告
     * @param testReportAddQO
     * @return
     */
    @Override
    public int addTestReport(TestReportAddQO testReportAddQO) {
        TestReport testReport = new TestReport();
        BeanUtils.copyProperties(testReportAddQO,testReport);
        testReport.setSerialNumber(generateSerialNumber());
        testReport.setResult("0");
        testReport.setStatus("0");
        testReport.setCreateTime(DateUtils.getNowDate());
        testReport.setDelFlag("0");
        testReport.setUpdateBy(SecurityUtils.getUsername());
        testReportMapper.addTestReport(testReport);
        testReportAddQO.setReportId(testReport.getId());
        testReportAddQO.setCreateTime(DateUtils.getNowDate());
        testReportAddQO.setDelFlag("0");
        return testPlanReportMapper.insertTestPlanReport(testReportAddQO);
    }

    /**
     * 生成随机序列号
     * @return
     */
    private String generateSerialNumber() {
        return UUID.randomUUID().toString().replaceAll("-", "").substring(0, 16);
    }
}
