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

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.microservices.common.core.utils.PageUtils;
import com.microservices.common.core.web.page.GenericsTableDataInfo;
import com.microservices.pms.common.service.IPmsCommonService;
import com.microservices.pms.enums.TestsheetStepStatus;
import com.microservices.pms.project.domain.PmsProjectTestcase;
import com.microservices.pms.project.domain.PmsProjectTestsheet;
import com.microservices.pms.project.domain.PmsProjectTestsheetCases;
import com.microservices.pms.project.domain.PmsProjectTestsheetIssues;
import com.microservices.pms.project.domain.vo.*;
import com.microservices.pms.project.mapper.PmsProjectTestsheetCasesMapper;
import com.microservices.pms.project.mapper.PmsProjectTestsheetIssuesMapper;
import com.microservices.pms.project.mapper.PmsProjectTestsheetMapper;
import com.microservices.pms.project.service.IPmsProjectTestcaseService;
import com.microservices.pms.project.service.IPmsProjectTestsheetIssuesService;
import com.microservices.pms.project.service.PmsProjectIssuesService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 测试单执行用例关联缺陷Service业务层处理
 *
 * @author microservices
 * @date 2023-11-27
 */
@Service
public class PmsProjectTestsheetIssuesServiceImpl implements IPmsProjectTestsheetIssuesService
{
    @Autowired
    private PmsProjectTestsheetIssuesMapper pmsProjectTestsheetIssuesMapper;

    @Autowired
    private PmsProjectIssuesService pmsProjectIssuesService;

    @Autowired
    private PmsProjectTestsheetCasesMapper pmsProjectTestsheetCasesMapper;

    @Autowired
    private PmsProjectTestsheetMapper pmsProjectTestsheetMapper;

    @Autowired
    private IPmsProjectTestcaseService pmsProjectTestcaseService;

    @Autowired
    private IPmsCommonService pmsCommonService;

    /**
     * 查询测试单执行用例关联缺陷
     *
     * @param id 测试单执行用例关联缺陷主键
     * @return 测试单执行用例关联缺陷
     */
    @Override
    public PmsProjectTestsheetIssues selectPmsProjectTestsheetIssuesById(Long id)
    {
        return pmsProjectTestsheetIssuesMapper.selectPmsProjectTestsheetIssuesById(id);
    }

    /**
     * 查询测试单执行用例关联缺陷列表
     *
     * @param pmsProjectTestsheetIssues 测试单执行用例关联缺陷
     * @return 测试单执行用例关联缺陷
     */
    @Override
    public List<PmsProjectTestsheetIssues> selectPmsProjectTestsheetIssuesList(PmsProjectTestsheetIssues pmsProjectTestsheetIssues)
    {
        return pmsProjectTestsheetIssuesMapper.selectPmsProjectTestsheetIssuesList(pmsProjectTestsheetIssues);
    }

    /**
     * 新增测试单执行用例关联缺陷
     *
     * @param pmsProjectTestsheetIssues 测试单执行用例关联缺陷
     * @return 结果
     */
    @Override
    public int insertPmsProjectTestsheetIssues(PmsProjectTestsheetIssues pmsProjectTestsheetIssues)
    {
        return pmsProjectTestsheetIssuesMapper.insertPmsProjectTestsheetIssues(pmsProjectTestsheetIssues);
    }

    @Override
    public int insertPmsProjectTestsheetIssuesVo(PmsProjectTestsheetIssuesInputVo issuesInputVo) {

        PmsProjectIssuesInputVo pmsProjectIssuesInputVo = new PmsProjectIssuesInputVo();
        BeanUtils.copyProperties(issuesInputVo, pmsProjectIssuesInputVo);
        JSONObject result =  pmsProjectIssuesService.insertPmsProjectIssues(pmsProjectIssuesInputVo);
        PmsProjectTestsheetIssues pmsProjectTestsheetIssues = new PmsProjectTestsheetIssues();
        pmsProjectTestsheetIssues.setProjectTestsheetCasesId(issuesInputVo.getProjectTestsheetCasesId());
        pmsProjectTestsheetIssues.setProjectIssueId(result.getLongValue("id"));
        pmsProjectTestsheetIssuesMapper.insertPmsProjectTestsheetIssues(pmsProjectTestsheetIssues);
        return 1;
    }

    @Override
    public int batchInsertPmsProjectTestsheetIssuesVo(PmsProjectTestsheetIssuesBatchInputVo batchInputVo) {
        for (Long linkIssueId : batchInputVo.getLinkIssueIds()) {
            PmsProjectTestsheetIssues pmsProjectTestsheetIssues = new PmsProjectTestsheetIssues();
            pmsProjectTestsheetIssues.setProjectTestsheetCasesId(batchInputVo.getProjectTestsheetCasesId());
            pmsProjectTestsheetIssues.setProjectIssueId(linkIssueId);
            pmsProjectTestsheetIssuesMapper.insertPmsProjectTestsheetIssues(pmsProjectTestsheetIssues);
        }
        return 1;
    }

    private void addPmsProjectTestsheetIssues(){

    }

    /**
     * 修改测试单执行用例关联缺陷
     *
     * @param pmsProjectTestsheetIssues 测试单执行用例关联缺陷
     * @return 结果
     */
    @Override
    public int updatePmsProjectTestsheetIssues(PmsProjectTestsheetIssues pmsProjectTestsheetIssues)
    {
        return pmsProjectTestsheetIssuesMapper.updatePmsProjectTestsheetIssues(pmsProjectTestsheetIssues);
    }

    /**
     * 批量删除测试单执行用例关联缺陷
     *
     * @param ids 需要删除的测试单执行用例关联缺陷主键
     * @return 结果
     */
    @Override
    public int deletePmsProjectTestsheetIssuesByIds(Long[] ids)
    {
        return pmsProjectTestsheetIssuesMapper.deletePmsProjectTestsheetIssuesByIds(ids);
    }

    /**
     * 批量删除测试单执行用例关联缺陷
     *
     * @param projectTestsheetCasesId 测试单执行用例id
     * @param ids 需要删除的缺陷主键
     * @return 结果
     */
    @Override
    public int deleteByProjectTestsheetCasesIdAndIssueIds(Long projectTestsheetCasesId, Long[] ids)
    {
        return pmsProjectTestsheetIssuesMapper.deleteByProjectTestsheetCasesIdAndIssueIds(projectTestsheetCasesId, ids);
    }


    /**
     * 删除测试单执行用例关联缺陷信息
     *
     * @param id 测试单执行用例关联缺陷主键
     * @return 结果
     */
    @Override
    public int deletePmsProjectTestsheetIssuesById(Long id)
    {
        return pmsProjectTestsheetIssuesMapper.deletePmsProjectTestsheetIssuesById(id);
    }

    /**
     * 根据缺陷ID查询关联测试用例
     *
     * @param projectIssueId
     * @return
     */
    @Override
    public GenericsTableDataInfo<PmsProjectTestsheetCaseVo> getProjectIssueLinkTestcaseList(Long projectIssueId, Long pmProjectId) {
        // 1.查询测试单关联测试单执行用例
        PmsProjectTestsheetIssues pmsProjectTestsheetIssues = new PmsProjectTestsheetIssues();
        pmsProjectTestsheetIssues.setProjectIssueId(projectIssueId);
        List<PmsProjectTestsheetIssues> testsheetIssuesList = pmsProjectTestsheetIssuesMapper.selectPmsProjectTestsheetIssuesList(pmsProjectTestsheetIssues);
        List<Long> testsheetCasesIds = Optional.ofNullable(testsheetIssuesList).orElse(Collections.emptyList()).stream().map(PmsProjectTestsheetIssues::getProjectTestsheetCasesId).collect(Collectors.toList());
        if (testsheetIssuesList.isEmpty()) {
            return new GenericsTableDataInfo<>();
        }
        // 2.查询测试单执行用例关联测试用例
        PmsProjectTestsheetCases pmsProjectTestsheetCases = new PmsProjectTestsheetCases();
        pmsProjectTestsheetCases.setIdsList(testsheetCasesIds);
        List<PmsProjectTestsheetCases> testsheetCasesList = pmsProjectTestsheetCasesMapper.selectPmsProjectTestsheetCasesList(pmsProjectTestsheetCases);
        List<Long> testcaseIds = Optional.ofNullable(testsheetCasesList).orElse(Collections.emptyList()).stream().map(PmsProjectTestsheetCases::getProjectTestcaseId).collect(Collectors.toList());
        if (testsheetCasesList.isEmpty()) {
            return new GenericsTableDataInfo<>();
        }
        return PageUtils.toPage(testsheetCasesList, item -> this.toPmsProjectTestsheetCaseDataVo(item));
    }

    @Override
    public List<Long> selectAllDistinctIssueId() {
        return pmsProjectTestsheetIssuesMapper.selectAllDistinctIssueId();
    }

    @Override
    public void deletePmsProjectTestsheetIssuesByIssueIds(Long[] issueIds) {
        pmsProjectTestsheetIssuesMapper.deletePmsProjectTestsheetIssuesByIssueIds(issueIds);
    }


    private PmsProjectTestsheetCaseVo toPmsProjectTestsheetCaseDataVo(PmsProjectTestsheetCases pmsProjectTestsheetCases) {
        PmsProjectTestsheetCaseVo dataVo = new PmsProjectTestsheetCaseVo();
        // 构建用例对象
        PmsProjectTestcase pmsProjectTestcase = pmsProjectTestcaseService.selectPmsProjectTestcaseById(pmsProjectTestsheetCases.getProjectTestcaseId());
        dataVo.setTestcaseDataVo(pmsProjectTestcaseService.toPmsProjectTestcaseDetailVo(pmsProjectTestcase));
        dataVo.setProjectTestsheetId(pmsProjectTestsheetCases.getProjectTestsheetId());
        dataVo.setProjectTestsheetName(Optional.ofNullable(pmsProjectTestsheetMapper.selectPmsProjectTestsheetById(pmsProjectTestsheetCases.getProjectTestsheetId())).orElse(new PmsProjectTestsheet()).getName());
        dataVo.setProjectTestcaseId(pmsProjectTestcase.getId());
        // 匹配测试单中是否有该用例，存在用例加载执行结果
        BeanUtils.copyProperties(pmsProjectTestsheetCases, dataVo);
        // 关联缺陷数量
        dataVo.setTestIssueCount(pmsProjectTestsheetIssuesMapper.selectCountByTestsheetCasesId(pmsProjectTestsheetCases.getId()));
        // 设置用例执行人
        dataVo.setTesterUser(pmsCommonService.getSimpleSysUserByGitlinkUserId(pmsProjectTestsheetCases.getTesterId()));
        // 设置测试步骤结果
        List<PmsProjectTestsheetCaseStepVo> stepVoList = pmsProjectTestsheetCases.getStepStatus() == null ? Collections.emptyList() : JSON.parseArray(pmsProjectTestsheetCases.getStepStatus(), PmsProjectTestsheetCaseStepVo.class);
        Optional.ofNullable(dataVo.getTestcaseDataVo().getTestcaseStepList()).orElse(Collections.emptyList()).forEach(step -> {
            PmsProjectTestsheetCaseStepVo stepResult = stepVoList.stream().filter(item -> item.getStepId() != null && item.getStepId().equals(step.getId())).findFirst().orElse(null);
            step.setExecResult(stepResult != null ? stepResult.getStepStatus() : TestsheetStepStatus.INIT.ordinal());
        });
        return dataVo;
    }
}
