package com.test.test.service.impl;

import com.test.common.utils.DateUtils;
import com.test.common.utils.bean.BeanUtils;
import com.test.test.domain.TestPlan;
import com.test.test.domain.qo.TestPlanAddQO;
import com.test.test.domain.qo.TestPlanListQO;
import com.test.test.domain.vo.TestPlanListVO;
import com.test.test.mapper.TestPlanMapper;
import com.test.test.mapper.TestProjectPlanMapper;
import com.test.test.service.ITestPlanService;
import jakarta.annotation.Resource;
import java.util.List;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 测试计划Service实现类
 *
 * @author pfl
 */
@Service
public class TestPlanServiceImpl implements ITestPlanService {

    @Resource
    private TestPlanMapper testPlanMapper;

    @Resource
    private TestProjectPlanMapper testProjectPlanMapper;

    /**
     * 查询测试计划列表
     * @param qo
     * @return
     */
    @Override
    public List<TestPlanListVO> getPlanList(TestPlanListQO qo) {
        return testPlanMapper.selectTestPlanList(qo);
    }

    /**
     * 新增测试计划
     * @param testPlanAddQO
     * @return
     */
    @Transactional
    @Override
    public int insertTestPlan(TestPlanAddQO testPlanAddQO) {
        TestPlan testPlan = new TestPlan();
        BeanUtils.copyProperties(testPlanAddQO,testPlan);
        testPlan.setCreateTime(DateUtils.getNowDate());
        testPlan.setDelFlag("0");
        testPlan.setStatus("0");
        // 新增测试计划
        testPlanMapper.insertTestPlan(testPlan);
        testPlanAddQO.setCreateTime(DateUtils.getNowDate());
        Long planId = testPlanMapper.selectPlanId(testPlanAddQO.getSerialNumber());
        testPlanAddQO.setPlanId(planId);
        testPlanAddQO.setDelFlag("0");
        //新增测试计划需求关联表
        return testProjectPlanMapper.insertTestProjectPlan(testPlanAddQO);
    }


    /**
     * 查询测试计划详情
     * @param id
     * @return
     */
    @Override
    public TestPlan selectTestPlanById(Long id) {
        return testPlanMapper.selectTestPlanById(id);
    }

    /**
     * 修改测试计划
     * @param testPlan
     * @return
     */
    @Override
    public int updateTestPlan(TestPlan testPlan) {
        testPlan.setUpdateTime(DateUtils.getNowDate());
        return testPlanMapper.updateTestPlan(testPlan);
    }
}
