package com.test.test.service.impl;

import com.test.common.utils.DateUtils;
import com.test.common.utils.SecurityUtils;
import com.test.test.domain.TestPlanCase;
import com.test.test.domain.qo.TestPlanCaseQO;
import com.test.test.mapper.TestPlanCaseMapper;
import com.test.test.service.ITestPlanCaseService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 测试计划用例关联Service业务层处理
 *
 * @author test
 * @date 2025-04-25
 */
@Service
public class TestPlanCaseServiceImpl implements ITestPlanCaseService
{
    @Resource
    private TestPlanCaseMapper testPlanCaseMapper;

    /**
     * 查询测试计划用例关联
     *
     * @param id 测试计划用例关联主键
     * @return 测试计划用例关联
     */
    @Override
    public TestPlanCase selectTestPlanCaseById(Long id)
    {
        return testPlanCaseMapper.selectTestPlanCaseById(id);
    }

    /**
     * 查询测试计划用例关联列表
     *
     * @param testPlanCase 测试计划用例关联
     * @return 测试计划用例关联
     */
    @Override
    public List<TestPlanCase> selectTestPlanCaseList(TestPlanCase testPlanCase)
    {
        return testPlanCaseMapper.selectTestPlanCaseList(testPlanCase);
    }

    /**
     * 保存测试计划用例关联列表
     *
     * @param testPlanCaseQO 测试计划用例关联
     * @return
     */
    @Override
    public void saveRelate(TestPlanCaseQO testPlanCaseQO)
    {
        List<Long> testCaseIdList = testPlanCaseQO.getTestCaseIdList();
        for (Long testCaseId : testCaseIdList) {
            TestPlanCase testPlanCase = new TestPlanCase();
            testPlanCase.setPlanId(testPlanCaseQO.getPlanId());
            testPlanCase.setCaseId(testCaseId);
            testPlanCase.setType(testPlanCaseQO.getType());
            List<TestPlanCase> testPlanCaseList = selectTestPlanCaseList(testPlanCase);
            if (!CollectionUtils.isEmpty(testPlanCaseList)) {
                // 防止重复
                continue;
            }
            testPlanCase.setCreateBy(SecurityUtils.getUsername());
            testPlanCase.setCreateTime(DateUtils.getNowDate());
            testPlanCaseMapper.insertTestPlanCase(testPlanCase);
        }
    }

    /**
     * 新增测试计划用例关联
     *
     * @param testPlanCase 测试计划用例关联
     * @return 结果
     */
    @Override
    public int insertTestPlanCase(TestPlanCase testPlanCase)
    {
        testPlanCase.setCreateTime(DateUtils.getNowDate());
        return testPlanCaseMapper.insertTestPlanCase(testPlanCase);
    }

    /**
     * 修改测试计划用例关联
     *
     * @param testPlanCase 测试计划用例关联
     * @return 结果
     */
    @Override
    public int updateTestPlanCase(TestPlanCase testPlanCase)
    {
        testPlanCase.setUpdateTime(DateUtils.getNowDate());
        return testPlanCaseMapper.updateTestPlanCase(testPlanCase);
    }

    /**
     * 批量删除测试计划用例关联
     *
     * @param ids 需要删除的测试计划用例关联主键
     * @return 结果
     */
    @Override
    public int deleteTestPlanCaseByIds(Long[] ids)
    {
        return testPlanCaseMapper.deleteTestPlanCaseByIds(ids);
    }

    /**
     * 删除测试计划用例关联信息
     *
     * @param id 测试计划用例关联主键
     * @return 结果
     */
    @Override
    public int deleteTestPlanCaseById(Long id)
    {
        return testPlanCaseMapper.deleteTestPlanCaseById(id);
    }
}
