package com.microservices.pms.project.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.microservices.common.core.exception.ServiceException;
import com.microservices.common.core.utils.DateUtils;
import com.microservices.common.core.utils.JSONUtils;
import com.microservices.common.core.utils.PageUtils;
import com.microservices.common.core.utils.bean.BeanUtils;
import com.microservices.common.core.web.page.GenericsTableDataInfo;
import com.microservices.common.security.utils.SecurityUtils;
import com.microservices.pms.common.service.IPmsCommonService;
import com.microservices.pms.enterprise.domain.PmsEnterprise;
import com.microservices.pms.enterprise.service.IPmsEnterpriseService;
import com.microservices.pms.product.domain.PmsProductModule;
import com.microservices.pms.product.domain.PmsProductRequirement;
import com.microservices.pms.product.domain.enums.ProductReqStatus;
import com.microservices.pms.product.domain.vo.ProductReqSpecsAssociatedTestcaseDataVo;
import com.microservices.pms.product.service.IPmsProductModuleService;
import com.microservices.pms.product.service.IPmsProductRequirementService;
import com.microservices.pms.product.service.IPmsProductRequirementTagService;
import com.microservices.pms.project.domain.*;
import com.microservices.pms.project.domain.vo.PmsProjectTestcaseBatchUpdateVo;
import com.microservices.pms.project.domain.vo.PmsProjectTestcaseDataVo;
import com.microservices.pms.project.domain.vo.PmsProjectTestcaseDetailVo;
import com.microservices.pms.project.mapper.PmsProjectTestcaseMapper;
import com.microservices.pms.project.mapper.PmsProjectTestsheetCasesMapper;
import com.microservices.pms.project.service.IPmsProjectService;
import com.microservices.pms.project.service.IPmsProjectTestcaseService;
import com.microservices.pms.project.service.IPmsProjectTestcaseStepService;
import com.microservices.pms.project.service.IPmsProjectTestcaseTypeService;
import com.microservices.pms.utils.PmsConstants;
import com.microservices.pms.utils.PmsUtils;
import com.microservices.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static com.microservices.common.core.utils.PageUtils.startPage;

/**
 * 测试用例管理Service业务层处理
 *
 * @author otto
 * @date 2023-11-22
 */
@Service
public class PmsProjectTestcaseServiceImpl implements IPmsProjectTestcaseService {
    @Autowired
    private PmsProjectTestcaseMapper pmsProjectTestcaseMapper;
    @Autowired
    private IPmsProjectTestcaseTypeService pmsProjectTestcaseTypeService;
    @Autowired
    private IPmsCommonService pmsCommonService;
    @Autowired
    private IPmsProductModuleService pmsProductModuleService;
    @Autowired
    private IPmsProjectTestcaseStepService pmsProjectTestcaseStepService;
    @Autowired
    private IPmsEnterpriseService pmsEnterpriseService;
    @Autowired
    private IPmsProjectService pmsProjectService;
    @Autowired
    private IPmsProductRequirementTagService pmsProductRequirementTagService;
    @Autowired
    private PmsProjectTestsheetCasesMapper pmsProjectTestsheetCasesMapper;
    @Autowired
    private IPmsProductRequirementService pmsProductRequirementService;

    /**
     * 查询测试用例管理
     *
     * @param id 测试用例管理主键
     * @return 测试用例管理
     */
    @Override
    public PmsProjectTestcase selectPmsProjectTestcaseById(Long id) {
        PmsProjectTestcase pmsProjectTestcase = pmsProjectTestcaseMapper.selectPmsProjectTestcaseById(id);
        if (pmsProjectTestcase == null) {
            throw new ServiceException("该测试用例不存在(测试用例ID[%s])", id);
        }
        pmsProjectService.selectPmsProjectById(pmsProjectTestcase.getPmsProjectId());
        return pmsProjectTestcase;
    }

    /**
     * 查询测试用例管理列表
     *
     * @param pmsProjectTestcase 测试用例管理
     * @return 测试用例管理
     */
    @Override
    public GenericsTableDataInfo<PmsProjectTestcaseDataVo> selectPmsProjectTestcasePageList(PmsProjectTestcase pmsProjectTestcase) {
        pmsProjectService.selectPmsProjectById(pmsProjectTestcase.getPmsProjectId());
        startPage();
        List<PmsProjectTestcase> pmsProjectTestcaseList = pmsProjectTestcaseMapper.selectPmsProjectTestcaseList(pmsProjectTestcase);
        HashMap<String, Object> hashMap = new HashMap<>();
        return PageUtils.toPage(pmsProjectTestcaseList, x -> toPmsProjectTestcaseDataVo(x, hashMap));
    }

    private PmsProjectTestcaseDataVo toPmsProjectTestcaseDataVo(PmsProjectTestcase pmsProjectTestcase, HashMap<String, Object> hashMap) {
        PmsProjectTestcaseDataVo pmsProjectTestcaseDataVo = pmsProjectTestcase.toPmsProjectTestcaseDataVo();
        // 设置用例维护人
        pmsProjectTestcaseDataVo.setAssigneeUser(
                PmsUtils.getSelectObjectInCache(
                        hashMap
                        , pmsCommonService::getSimpleSysUserByGitlinkUserId
                        , pmsProjectTestcase.getAssigneeGitlinkId()));
        // 设置关联项目
        pmsProjectTestcaseDataVo.setPmsProject(
                PmsUtils.getSelectObjectInCache(
                        hashMap
                        , pmsProjectService::selectAndCheckPmsProjectById
                        , pmsProjectTestcase.getPmsProjectId()));
        // 设置用例类型
        pmsProjectTestcaseDataVo.setType(
                PmsUtils.getSelectObjectInCache(
                        hashMap
                        , pmsProjectTestcaseTypeService::selectPmsProjectTestcaseTypeById
                        , pmsProjectTestcase.getTypeId()));
        // 设置标记列表
        pmsProjectTestcaseDataVo.setTagList(
                PmsUtils.getSelectObjectInCache(
                        hashMap
                        , pmsProductRequirementTagService::selectTagListByIds
                        , pmsProjectTestcase.getTagIds()));
        // 设置用例模块
        if (pmsProjectTestcase.getPmsModuleId() != null) {
            pmsProjectTestcaseDataVo.setPmsModule(
                    PmsUtils.getSelectObjectInCache(
                            hashMap
                            , x -> pmsProductModuleService.selectPmsProductModuleById(x, false)
                            , pmsProjectTestcase.getPmsModuleId()));
        }
        // 设置需求创建人
        pmsProjectTestcaseDataVo.setCreateBy(
                PmsUtils.getSelectObjectInCache(
                        hashMap
                        , pmsCommonService::getNickNameByUsername
                        , pmsProjectTestcase.getCreateBy())
        );
        // 设置需求更新人
        pmsProjectTestcaseDataVo.setUpdateBy(
                PmsUtils.getSelectObjectInCache(
                        hashMap
                        , pmsCommonService::getNickNameByUsername
                        , pmsProjectTestcase.getUpdateBy())
        );
        // 设置产品需求规格
        if (pmsProjectTestcase.getProductReqSpecsId() != null) {
            pmsProjectTestcaseDataVo.setProductReqSpecs(
                    PmsUtils.getSelectObjectInCache(
                            hashMap
                            , x -> pmsProductRequirementService.selectPmsProductRequirementById(x, false)
                            , pmsProjectTestcase.getProductReqSpecsId())
            );
        }
        return pmsProjectTestcaseDataVo;
    }

    @Override
    public PmsProjectTestcaseDetailVo toPmsProjectTestcaseDetailVo(PmsProjectTestcase pmsProjectTestcase) {
        PmsProjectTestcaseDataVo pmsProjectTestcaseDataVo = toPmsProjectTestcaseDataVo(pmsProjectTestcase, null);
        PmsProjectTestcaseDetailVo pmsProjectTestcaseDetailVo = pmsProjectTestcaseDataVo.toPmsProjectTestcaseDetailVo();
        pmsProjectTestcaseDetailVo.setPreconditions(pmsProjectTestcase.getPreconditions());
        pmsProjectTestcaseDetailVo.setRemark(pmsProjectTestcase.getRemark());
        // 设置测试步骤列表
        PmsProjectTestcaseStep pmsProjectTestcaseStepSearch = new PmsProjectTestcaseStep();
        pmsProjectTestcaseStepSearch.setPmsTestcaseId(pmsProjectTestcase.getId());
        pmsProjectTestcaseDetailVo.setTestcaseStepList(pmsProjectTestcaseStepService.selectPmsProjectTestcaseStepList(pmsProjectTestcaseStepSearch));

        // 设置附件列表
        pmsProjectTestcaseDetailVo.setFileList(pmsCommonService.getAllFileByFileIdentifiers(pmsProjectTestcase.getFileIdentifiers()));
        pmsCommonService.setBaseEntityVo(pmsProjectTestcaseDetailVo, pmsProjectTestcase);
        return pmsProjectTestcaseDetailVo;
    }

    @Override
    public List<PmsProjectTestcase> selectPmsProjectTestcaseForSelectList(PmsProjectTestcase pmsProjectTestcase) {
        return pmsProjectTestcaseMapper.selectPmsProjectTestcaseForSelectList(pmsProjectTestcase);
    }

    @Override
    public List<PmsProjectTestcase> selectPmsProjectTestcaseForTestsheetList(PmsProjectTestcase pmsProjectTestcase) {
        return pmsProjectTestcaseMapper.selectPmsProjectTestcaseForTestsheetList(pmsProjectTestcase);
    }

    @Override
    public List<PmsProjectTestcase> selectPmsProjectTestcaseList(PmsProjectTestcase pmsProjectTestcaseSearch) {
        return pmsProjectTestcaseMapper.selectPmsProjectTestcaseList(pmsProjectTestcaseSearch);
    }

    @Override
    public List<ProductReqSpecsAssociatedTestcaseDataVo> selectReqSpecsAssociatedTestcaseList(Long reqSpecsId) {
        List<ProductReqSpecsAssociatedTestcaseDataVo> reqSpecsTestcaseDataVoList = new ArrayList<>();
        PmsProductRequirement pmsProductRequirement = pmsProductRequirementService.selectPmsProductRequirementById(reqSpecsId, false);
        if (ProductReqStatus.isUserReqStatus(pmsProductRequirement.getStatus())) {
            throw new ServiceException("用户需求无法查询关联的测试用例");
        }
        PmsProjectTestcase testcaseSearch = new PmsProjectTestcase();
        testcaseSearch.setProductReqSpecsId(reqSpecsId);
        List<PmsProjectTestcase> reqSpecsTestcaseList = pmsProjectTestcaseMapper.selectPmsProjectTestcaseList(testcaseSearch);
        HashMap<String, Object> hashMap = new HashMap<>();
        for (PmsProjectTestcase pmsProjectTestcase : reqSpecsTestcaseList) {
            ProductReqSpecsAssociatedTestcaseDataVo reqSpecsTestcaseDataVo = new ProductReqSpecsAssociatedTestcaseDataVo();
            BeanUtils.copyProperties(pmsProjectTestcase, reqSpecsTestcaseDataVo);
            reqSpecsTestcaseDataVo.setAssigneeUser(
                    PmsUtils.getSelectObjectInCache(
                            hashMap
                            , pmsCommonService::getSimpleSysUserByGitlinkUserId
                            , pmsProjectTestcase.getAssigneeGitlinkId()));
            PmsProjectTestsheetCases projectTestSheetCasesSearch = new PmsProjectTestsheetCases();
            projectTestSheetCasesSearch.setProjectTestcaseId(pmsProjectTestcase.getId());
            List<PmsProjectTestsheetCases> testsheetCasesList = pmsProjectTestsheetCasesMapper.selectPmsProjectTestsheetCasesList(projectTestSheetCasesSearch);
            reqSpecsTestcaseDataVo.setCountData(testsheetCasesList);
            reqSpecsTestcaseDataVoList.add(reqSpecsTestcaseDataVo);
        }
        return reqSpecsTestcaseDataVoList;
    }

    /**
     * 新增测试用例管理
     *
     * @param pmsProjectTestcase 测试用例管理
     * @return 结果
     */
    @Override
    public Long insertPmsProjectTestcase(PmsProjectTestcase pmsProjectTestcase) {
        pmsProjectService.selectPmsProjectById(pmsProjectTestcase.getPmsProjectId());
        if (pmsProjectTestcase.getAssigneeGitlinkId() == null) {
            // 未设置维护用户时默认为当前操作用户
            pmsProjectTestcase.setAssigneeGitlinkId(SecurityUtils.getGitlinkUserId());
        }
        if (pmsProjectTestcase.getTypeId() == null) {
            // 设置类型为默认值
            PmsProjectTestcaseType defaultType = pmsProjectTestcaseTypeService.selectDefaultPmsProjectTestcaseType();
            pmsProjectTestcase.setTypeId(defaultType.getId());
        }

        checkAndSetDefaultTestcase(pmsProjectTestcase, null);

        // 获取当前项目下测试用例的最大索引值
        Long maxIndex = pmsProjectTestcaseMapper.selectPmsProjectTestcaseMaxIndexByProjectId(pmsProjectTestcase.getPmsProjectId());
        if (maxIndex == null) {
            maxIndex = 1L;
        }
        pmsProjectTestcase.setIndex(++maxIndex);
        pmsProjectTestcase.setCreateBy(SecurityUtils.getUsername());
        pmsProjectTestcase.setCreateTime(DateUtils.getNowDate());
        pmsProjectTestcaseMapper.insertPmsProjectTestcase(pmsProjectTestcase);
        // 批量新增测试步骤
        pmsProjectTestcaseStepService.batchInsertPmsProjectTestcaseStep(pmsProjectTestcase.getId(), pmsProjectTestcase.getTestcaseStepList());
        return pmsProjectTestcase.getId();
    }

    /**
     * 校验测试用例数据是否正确并设置默认值
     *
     * @param pmsProjectTestcase 测试用例
     */
    private void checkAndSetDefaultTestcase(PmsProjectTestcase pmsProjectTestcase, PmsProjectTestcase oldPmsProjectTestcase) {
        PmsProject pmsProject = pmsProjectService.selectAndCheckPmsProjectById(pmsProjectTestcase.getPmsProjectId());
        if (pmsProjectTestcase.getAssigneeGitlinkId() != null) {
            if (oldPmsProjectTestcase == null
                    || !pmsProjectTestcase.getAssigneeGitlinkId().equals(oldPmsProjectTestcase.getAssigneeGitlinkId())) {
                // 检查维护用户id是否正确
                checkUserInEnterprise(pmsProjectTestcase.getAssigneeGitlinkId(), pmsProject);
            }
        }
        // 检查测试模块是否正确
        if (pmsProjectTestcase.getPmsModuleId() != null) {
            PmsProductModule pmsProductModule = pmsProductModuleService.selectPmsProductModuleById(pmsProjectTestcase.getPmsModuleId(), false);
            if (!pmsProductModule.getPmsProjectId().equals(pmsProjectTestcase.getPmsProjectId())) {
                throw new ServiceException("该模块不属于当前项目(模块名称[%s],项目名称[%s])", pmsProductModule.getModuleName(), pmsProject.getProjectName());
            }
        }
        // 检查测试用例类型是否正确
        if (pmsProjectTestcase.getTypeId() != null) {
            pmsProjectTestcaseTypeService.selectPmsProjectTestcaseTypeById(pmsProjectTestcase.getTypeId());
        }

        // 检查附件列表是否有异常数据
        pmsCommonService.getAllFileByFileIdentifiers(pmsProjectTestcase.getFileIdentifiers());

        // 校验标记是否存在
        pmsProductRequirementTagService.checkTags(pmsProjectTestcase.getTags(), PmsConstants.TESTCASE_TAG_AND_MODULE_TYPE, pmsProject.getPmsEnterpriseId());
        pmsProjectTestcase.setTagIds(pmsProjectTestcase.getTags());

        if (pmsProjectTestcase.getProductReqSpecsId() != null) {
            PmsProductRequirement pmsProductRequirement = pmsProductRequirementService.selectPmsProductRequirementById(pmsProjectTestcase.getProductReqSpecsId(), false);
            if (ProductReqStatus.isUserReqStatus(pmsProductRequirement.getStatus())) {
                throw new ServiceException("该需求处于用户需求状态，不能关联测试用例");
            }
        }
    }

    private void checkUserInEnterprise(Long assigneeGitlinkId, PmsProject pmsProject) {
        SysUser sysUser = pmsCommonService.getSysUserByGitlinkId(assigneeGitlinkId);
        // 检查该用户是否在企业中
        boolean isExist = pmsEnterpriseService.checkUserInEnterprise(pmsProject.getPmsEnterpriseId(), sysUser.getUserId());
        if (!isExist) {
            PmsEnterprise pmsEnterprise = pmsEnterpriseService.selectPmsEnterpriseById(pmsProject.getPmsEnterpriseId());
            throw new ServiceException("该测试用例的维护人不在当前企业中(企业名称[%s],用户名[%s])", pmsEnterprise.getEnterpriseName(), sysUser.getUserName());
        }
    }

    /**
     * 修改测试用例管理
     *
     * @param pmsProjectTestcase 测试用例管理
     * @return 结果
     */
    @Override
    public int updatePmsProjectTestcase(PmsProjectTestcase pmsProjectTestcase) {
        PmsProjectTestcase oldPmsProjectTestcase = selectPmsProjectTestcaseById(pmsProjectTestcase.getId());
        pmsProjectTestcase.setPmsProjectId(oldPmsProjectTestcase.getPmsProjectId());
        checkAndSetDefaultTestcase(pmsProjectTestcase, oldPmsProjectTestcase);

        pmsProjectTestcase.setUpdateBy(SecurityUtils.getUsername());
        pmsProjectTestcase.setUpdateTime(DateUtils.getNowDate());

        // 批量更新测试步骤
        pmsProjectTestcaseStepService.batchUpdatePmsProjectTestcaseStep(pmsProjectTestcase.getId(), pmsProjectTestcase.getTestcaseStepList());
        return pmsProjectTestcaseMapper.updatePmsProjectTestcase(pmsProjectTestcase);
    }

    /**
     * 批量删除测试用例管理
     *
     * @param ids 需要删除的测试用例管理主键
     * @return 结果
     */
    @Override
    public int deletePmsProjectTestcaseByIds(Long[] ids) {
        int success = 0;
        for (Long id : ids) {
            success += deletePmsProjectTestcaseById(id);
        }
        return success;
    }

    /**
     * 删除测试用例管理信息
     *
     * @param id 测试用例管理主键
     * @return 结果
     */
    @Override
    public int deletePmsProjectTestcaseById(Long id) {
        PmsProjectTestcase pmsProjectTestcase = selectPmsProjectTestcaseById(id);
        // 删除测试用例下所有测试步骤
        pmsProjectTestcaseStepService.deletePmsProjectTestcaseStepByTestcaseId(pmsProjectTestcase.getId());
        // 删除测试用例关联测试单执行用例
        pmsProjectTestsheetCasesMapper.deletePmsProjectTestsheetCasesByTestcaseId(id);
        return pmsProjectTestcaseMapper.deletePmsProjectTestcaseById(id);
    }

    @Override
    public Integer selectPmsProjectTestcaseCount(PmsProjectTestcase pmsProjectTestcase) {
        pmsProjectService.selectPmsProjectById(pmsProjectTestcase.getPmsProjectId());
        return pmsProjectTestcaseMapper.selectPmsProjectTestcaseCount(pmsProjectTestcase);
    }

    @Override
    public PmsProjectTestcaseDataVo selectPmsProjectTestcaseDataVoById(Long id) {
        return toPmsProjectTestcaseDetailVo(selectPmsProjectTestcaseById(id));
    }

    @Override
    public void updatePmsProjectTestcaseToNoModule(Long moduleId) {
        pmsProjectTestcaseMapper.updatePmsProjectTestcaseToNoModule(moduleId, SecurityUtils.getUsername(), DateUtils.getNowDate());
    }

    @Override
    public int batchUpdatePmsProjectTestcase(JSONObject batchUpdateVo) {
        List<Long> idList = JSONUtils.getLongListByJsonArray(batchUpdateVo.getJSONArray("idList"));
        checkBatchUpdateInTheSameProject(idList);
        return batchUpdatePmsProjectTestcaseByJsonObject(idList, batchUpdateVo);
    }

    /**
     * 检查批量更新的测试用例是否属于同一个项目
     *
     * @param idList id列表
     */
    private void checkBatchUpdateInTheSameProject(List<Long> idList) {
        List<Long> projectIdList = pmsProjectTestcaseMapper.selectProjectIdListInTestcaseIdList(idList);
        if (projectIdList != null && projectIdList.size() == 1) {
            // 校验数据权限
            pmsProjectService.selectPmsProjectById(projectIdList.get(0));
            return;
        }
        throw new ServiceException("批量编辑测试用例时仅允许编辑同一项目下的测试用例");
    }


    private int batchUpdatePmsProjectTestcaseByJsonObject(List<Long> idList, JSONObject jsonObject) {
        PmsProjectTestcaseBatchUpdateVo pmsProjectTestcaseBatchUpdateVo = new PmsProjectTestcaseBatchUpdateVo();
        pmsProjectTestcaseBatchUpdateVo.setIdList(idList);
        if (jsonObject.containsKey("assigneeGitlinkId")) {
            pmsProjectTestcaseBatchUpdateVo.setAssigneeGitlinkId(jsonObject.getLong("assigneeGitlinkId"));
            pmsProjectTestcaseBatchUpdateVo.setAssigneeGitlinkIdIsChange(true);
        }
        if (jsonObject.containsKey("typeId")) {
            pmsProjectTestcaseBatchUpdateVo.setTypeId(jsonObject.getLong("typeId"));
            pmsProjectTestcaseBatchUpdateVo.setTypeIdIsChange(true);
        }
        if (jsonObject.containsKey("pmsModuleId")) {
            pmsProjectTestcaseBatchUpdateVo.setPmsModuleId(jsonObject.getLong("pmsModuleId"));
            pmsProjectTestcaseBatchUpdateVo.setPmsModuleIdIsChange(true);
        }
        if (jsonObject.containsKey("tags")) {
            pmsProjectTestcaseBatchUpdateVo.setTagIds(jsonObject.getString("tags"));
            pmsProjectTestcaseBatchUpdateVo.setTagIdsIsChange(true);
        }
        if (jsonObject.containsKey("productReqSpecsId")) {
            pmsProjectTestcaseBatchUpdateVo.setProductReqSpecsId(jsonObject.getLong("productReqSpecsId"));
            pmsProjectTestcaseBatchUpdateVo.setProductReqSpecsIdIsChange(true);
        }
        if (pmsProjectTestcaseBatchUpdateVo.thisIsChange()) {
            checkPmsProjectTestcaseBatchUpdateVo(pmsProjectTestcaseBatchUpdateVo);
            pmsProjectTestcaseBatchUpdateVo.setUpdateBy(SecurityUtils.getUsername());
            pmsProjectTestcaseBatchUpdateVo.setUpdateTime(DateUtils.getNowDate());
            return pmsProjectTestcaseMapper.batchUpdatePmsProjectTestcase(pmsProjectTestcaseBatchUpdateVo);
        }
        return 1;
    }

    private void checkPmsProjectTestcaseBatchUpdateVo(PmsProjectTestcaseBatchUpdateVo pmsProjectTestcaseBatchUpdateVo) {
        if (pmsProjectTestcaseBatchUpdateVo.getIdList() == null || pmsProjectTestcaseBatchUpdateVo.getIdList().isEmpty()) {
            return;
        }
        PmsProjectTestcase oldPmsProjectTestcase =
                pmsProjectTestcaseMapper.selectPmsProjectTestcaseById(pmsProjectTestcaseBatchUpdateVo.getIdList().get(0));
        PmsProject pmsProject = pmsProjectService.selectAndCheckPmsProjectById(oldPmsProjectTestcase.getPmsProjectId());
        if (pmsProjectTestcaseBatchUpdateVo.getAssigneeGitlinkIdIsChange()
                && pmsProjectTestcaseBatchUpdateVo.getAssigneeGitlinkId() != null) {
            // 检查维护用户id是否正确
            checkUserInEnterprise(pmsProjectTestcaseBatchUpdateVo.getAssigneeGitlinkId(), pmsProject);
        }

        // 检查测试模块是否正确
        if (pmsProjectTestcaseBatchUpdateVo.getPmsModuleIdIsChange()
                && pmsProjectTestcaseBatchUpdateVo.getPmsModuleId() != null) {
            PmsProductModule pmsProductModule = pmsProductModuleService.selectPmsProductModuleById(pmsProjectTestcaseBatchUpdateVo.getPmsModuleId(), false);
            if (!pmsProductModule.getPmsProjectId().equals(pmsProject.getId())) {
                throw new ServiceException("该模块不属于当前项目(模块名称[%s],项目名称[%s])", pmsProductModule.getModuleName(), pmsProject.getProjectName());
            }
        }
        // 检查测试用例类型是否正确
        if (pmsProjectTestcaseBatchUpdateVo.getTypeIdIsChange()
                && pmsProjectTestcaseBatchUpdateVo.getTypeId() != null) {
            pmsProjectTestcaseTypeService.selectPmsProjectTestcaseTypeById(pmsProjectTestcaseBatchUpdateVo.getTypeId());
        }

        if (pmsProjectTestcaseBatchUpdateVo.getTagIdsIsChange()) {
            // 校验标记是否存在
            pmsProductRequirementTagService.checkTags(
                    pmsProjectTestcaseBatchUpdateVo.getTagIds(),
                    PmsConstants.TESTCASE_TAG_AND_MODULE_TYPE,
                    pmsProject.getPmsEnterpriseId());
        }

        if (pmsProjectTestcaseBatchUpdateVo.getProductReqSpecsIdIsChange()) {
            PmsProductRequirement pmsProductRequirement = pmsProductRequirementService.selectPmsProductRequirementById(pmsProjectTestcaseBatchUpdateVo.getProductReqSpecsId(), false);
            if (ProductReqStatus.isUserReqStatus(pmsProductRequirement.getStatus())) {
                throw new ServiceException("该需求处于用户需求状态，不能关联测试用例");
            }
        }

    }
}
