package com.test.test.service.impl;

import java.util.Collections;
import java.util.List;
import com.test.common.utils.DateUtils;
import com.test.common.utils.SecurityUtils;
import com.test.test.domain.PerformanceTest;
import com.test.test.domain.PerformanceTestCase;
import com.test.test.domain.TestCase;
import com.test.test.domain.qo.PerformanceTestCaseQO;
import com.test.test.domain.qo.PerformanceTestQO;
import com.test.test.domain.vo.PerformanceTestCaseVO;
import com.test.test.domain.vo.PerformanceTestVO;
import com.test.test.mapper.PerformanceTestCaseMapper;
import com.test.test.mapper.PerformanceTestMapper;
import com.test.test.mapper.TestCaseMapper;
import com.test.test.service.IPerformanceTestService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;


/**
 * 性能测试Service业务层处理
 * 
 * @author test
 * @date 2025-04-14
 */
@Service
public class PerformanceTestServiceImpl implements IPerformanceTestService
{
    @Resource
    private PerformanceTestMapper performanceTestMapper;
    @Resource
    private PerformanceTestCaseMapper performanceTestCaseMapper;
    @Resource
    private TestCaseMapper testCaseMapper;

    /**
     * 查询性能测试
     * 
     * @param id 性能测试主键
     * @return 性能测试
     */
    @Override
    public PerformanceTestVO selectPerformanceTestById(Long id)
    {
        PerformanceTestVO performanceTestVO = new PerformanceTestVO();
        PerformanceTest performanceTest = performanceTestMapper.selectPerformanceTestById(id);
        BeanUtils.copyProperties(performanceTest, performanceTestVO);
        //通过id查询performance_test_case信息
        PerformanceTestCase performanceTestCase = new PerformanceTestCase();
        performanceTestCase.setPerformanceId(performanceTest.getId());
        List<PerformanceTestCase> performanceTestCaseList = performanceTestCaseMapper.selectPerformanceTestCaseList(performanceTestCase);
        List<PerformanceTestCaseVO> performanceTestCaseVOList = performanceTestVO.getPerformanceTestCaseVOList();
        for (PerformanceTestCase testCase : performanceTestCaseList) {
            PerformanceTestCaseVO performanceTestCaseVO = new PerformanceTestCaseVO();
            BeanUtils.copyProperties(testCase, performanceTestCaseVO);
            TestCase caseEntity = testCaseMapper.selectTestCaseById(performanceTestCaseVO.getTestCaseId());
            if (caseEntity!=null){
                performanceTestCaseVOList.add(performanceTestCaseVO);
                performanceTestCaseVO.setTestCaseName(caseEntity.getName());
            }
        }
        return performanceTestVO;
    }

    /**
     * 查询性能测试列表
     * 
     * @param performanceTest 性能测试
     * @return 性能测试
     */
    @Override
    public List<PerformanceTest> selectPerformanceTestList(PerformanceTest performanceTest)
    {
        //默认显示未删除
        performanceTest.setDelFlag("0");
        return performanceTestMapper.selectPerformanceTestList(performanceTest);
    }

    /**
     * 新增性能测试
     * 
     * @param performanceTestQO 性能测试
     * @return 结果
     */
    @Override
    @Transactional
    public Long insertPerformanceTest(PerformanceTestQO performanceTestQO)
    {
        PerformanceTest performanceTest = new PerformanceTest();
        //默认未删除
        performanceTestQO.setDelFlag("0");
        performanceTestQO.setCreateTime(DateUtils.getNowDate());
        performanceTestQO.setCreateBy(SecurityUtils.getUsername());
        performanceTestQO.setUpdateBy(SecurityUtils.getUsername());
        BeanUtils.copyProperties(performanceTestQO,performanceTest);
        //新增performance_test
        performanceTestMapper.insertPerformanceTest(performanceTest);

        //新增后的主键
        Long id = performanceTest.getId();
        List<PerformanceTestCaseQO> performanceTestCaseQOList = performanceTestQO.getPerformanceTestCaseVOList();
        if (CollectionUtils.isEmpty(performanceTestCaseQOList)){
            return id;
        }
        for (PerformanceTestCaseQO performanceTestCaseQO : performanceTestCaseQOList) {
            PerformanceTestCase performanceTestCase = new PerformanceTestCase();
            performanceTestCaseQO.setPerformanceId(id);
            BeanUtils.copyProperties(performanceTestCaseQO,performanceTestCase);
            performanceTestCaseMapper.insertPerformanceTestCase(performanceTestCase);
        }
        return id;
    }

    /**
     * 修改性能测试
     * 
     * @param performanceTestQO 性能测试
     * @return 结果
     */
    @Override
    @Transactional
    public Long updatePerformanceTest(PerformanceTestQO performanceTestQO)
    {
        Long id = performanceTestQO.getId();
        PerformanceTest performanceTest = new PerformanceTest();
        performanceTestQO.setUpdateTime(DateUtils.getNowDate());
        performanceTestQO.setUpdateBy(SecurityUtils.getUsername());
        BeanUtils.copyProperties(performanceTestQO,performanceTest);
        performanceTestMapper.updatePerformanceTest(performanceTest);
        //先删除 performance_test_case
        performanceTestCaseMapper.deletePerformanceTestCaseById(id);
        //再新增
        List<PerformanceTestCaseQO> performanceTestCaseQOList = performanceTestQO.getPerformanceTestCaseVOList();
        if (CollectionUtils.isEmpty(performanceTestCaseQOList)){
            return id;
        }
        for (PerformanceTestCaseQO performanceTestCaseQO : performanceTestCaseQOList) {
            PerformanceTestCase performanceTestCase = new PerformanceTestCase();
            performanceTestCaseQO.setPerformanceId(id);
            performanceTestCaseQO.setStatus(performanceTestCaseQO.getStatus());
            BeanUtils.copyProperties(performanceTestCaseQO,performanceTestCase);
            performanceTestCaseMapper.insertPerformanceTestCase(performanceTestCase);
        }
        return id;
    }

    /**
     * 批量删除性能测试
     * 
     * @param ids 需要删除的性能测试主键
     * @return 结果
     */
    @Override
    public int deletePerformanceTestByIds(Long[] ids)
    {
        return performanceTestMapper.deletePerformanceTestByIds(ids);
    }

    /**
     * 删除性能测试信息
     * 
     * @param id 性能测试主键
     * @return 结果
     */
    @Override
    public int deletePerformanceTestById(Long id)
    {
        return performanceTestMapper.deletePerformanceTestById(id);
    }
}
