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

import com.microservices.common.core.exception.ServiceException;
import com.microservices.common.core.utils.DateUtils;
import com.microservices.common.security.utils.SecurityUtils;
import com.microservices.pms.project.domain.PmsProjectTestcaseStep;
import com.microservices.pms.project.domain.vo.PmsProjectTestcaseStepDataVo;
import com.microservices.pms.project.mapper.PmsProjectTestcaseStepMapper;
import com.microservices.pms.project.service.IPmsProjectTestcaseService;
import com.microservices.pms.project.service.IPmsProjectTestcaseStepService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 测试用例步骤管理Service业务层处理
 *
 * @author otto
 * @date 2023-11-22
 */
@Service
public class PmsProjectTestcaseStepServiceImpl implements IPmsProjectTestcaseStepService {
    @Autowired
    private PmsProjectTestcaseStepMapper pmsProjectTestcaseStepMapper;
    @Autowired
    @Lazy
    private IPmsProjectTestcaseService pmsProjectTestcaseService;

    /**
     * 查询测试用例步骤管理
     *
     * @param id 测试用例步骤管理主键
     * @return 测试用例步骤管理
     */
    @Override
    public PmsProjectTestcaseStep selectPmsProjectTestcaseStepById(Long id) {
        PmsProjectTestcaseStep pmsProjectTestcaseStep = pmsProjectTestcaseStepMapper.selectPmsProjectTestcaseStepById(id);
        if (pmsProjectTestcaseStep == null) {
            throw new ServiceException("该测试用例步骤不存在(测试用例步骤ID[%s])", id);
        }
        return pmsProjectTestcaseStep;
    }

    /**
     * 查询测试用例步骤管理列表
     *
     * @param pmsProjectTestcaseStep 测试用例步骤管理
     * @return 测试用例步骤管理
     */
    @Override
    public List<PmsProjectTestcaseStepDataVo> selectPmsProjectTestcaseStepList(PmsProjectTestcaseStep pmsProjectTestcaseStep) {
        List<PmsProjectTestcaseStep> pmsProjectTestcaseStepList = pmsProjectTestcaseStepMapper.selectPmsProjectTestcaseStepList(pmsProjectTestcaseStep);
        if (pmsProjectTestcaseStepList == null) {
            return new ArrayList<>();
        }
        return pmsProjectTestcaseStepList.stream().map(PmsProjectTestcaseStep::toPmsProjectTestcaseStepDataVo).collect(Collectors.toList());
    }

    /**
     * 新增测试用例步骤管理
     *
     * @param pmsProjectTestcaseStep 测试用例步骤管理
     * @return 结果
     */
    @Override
    public int insertPmsProjectTestcaseStep(PmsProjectTestcaseStep pmsProjectTestcaseStep) {
        pmsProjectTestcaseStep.setCreateBy(SecurityUtils.getUsername());
        pmsProjectTestcaseStep.setCreateTime(DateUtils.getNowDate());
        return pmsProjectTestcaseStepMapper.insertPmsProjectTestcaseStep(pmsProjectTestcaseStep);
    }

    /**
     * 修改测试用例步骤管理
     *
     * @param pmsProjectTestcaseStep 测试用例步骤管理
     * @return 结果
     */
    @Override
    public int updatePmsProjectTestcaseStep(PmsProjectTestcaseStep pmsProjectTestcaseStep) {
        selectPmsProjectTestcaseStepById(pmsProjectTestcaseStep.getId());
        pmsProjectTestcaseStep.setUpdateBy(SecurityUtils.getUsername());
        pmsProjectTestcaseStep.setUpdateTime(DateUtils.getNowDate());
        return pmsProjectTestcaseStepMapper.updatePmsProjectTestcaseStep(pmsProjectTestcaseStep);
    }

    /**
     * 批量删除测试用例步骤管理
     *
     * @param ids 需要删除的测试用例步骤管理主键
     * @return 结果
     */
    @Override
    public int deletePmsProjectTestcaseStepByIds(Long[] ids) {
        return pmsProjectTestcaseStepMapper.deletePmsProjectTestcaseStepByIds(ids);
    }

    /**
     * 删除测试用例步骤管理信息
     *
     * @param id 测试用例步骤管理主键
     * @return 结果
     */
    @Override
    public int deletePmsProjectTestcaseStepById(Long id) {
        return pmsProjectTestcaseStepMapper.deletePmsProjectTestcaseStepById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchInsertPmsProjectTestcaseStep(Long testcaseId, List<PmsProjectTestcaseStep> testcaseStepList) {
        if (testcaseStepList == null) {
            return;
        }
        pmsProjectTestcaseService.selectPmsProjectTestcaseById(testcaseId);
        // 删除原测试步骤，创建新的测试步骤
        deletePmsProjectTestcaseStepByTestcaseId(testcaseId);
        for (PmsProjectTestcaseStep pmsProjectTestcaseStep : testcaseStepList) {
            pmsProjectTestcaseStep.setPmsTestcaseId(testcaseId);
            insertPmsProjectTestcaseStep(pmsProjectTestcaseStep);
        }
    }

    @Override
    public void deletePmsProjectTestcaseStepByTestcaseId(Long testcaseId) {
        pmsProjectTestcaseStepMapper.deletePmsProjectTestcaseStepByTestcaseId(testcaseId);
    }

    @Override
    public void batchUpdatePmsProjectTestcaseStep(Long testcaseId, List<PmsProjectTestcaseStep> testcaseStepList) {
        if (testcaseStepList == null) {
            return;
        }
        PmsProjectTestcaseStep pmsProjectTestcaseStepSearch = new PmsProjectTestcaseStep();
        pmsProjectTestcaseStepSearch.setPmsTestcaseId(testcaseId);
        List<PmsProjectTestcaseStep> oldTestcaseStepList = pmsProjectTestcaseStepMapper.selectPmsProjectTestcaseStepList(pmsProjectTestcaseStepSearch);
        List<Long> testcaseStepIdList = testcaseStepList.stream().map(PmsProjectTestcaseStep::getId).collect(Collectors.toList());
        // 删除原步骤多余步骤
        Long[] deleteStepIdList = oldTestcaseStepList.stream().map(PmsProjectTestcaseStep::getId).filter(x -> !testcaseStepIdList.contains(x)).toArray(Long[]::new);
        if (deleteStepIdList.length > 0) {
            deletePmsProjectTestcaseStepByIds(deleteStepIdList);
        }
        for (PmsProjectTestcaseStep pmsProjectTestcaseStep : testcaseStepList) {
            if (pmsProjectTestcaseStep.getId() != null) {
                updatePmsProjectTestcaseStep(pmsProjectTestcaseStep);
            } else {
                pmsProjectTestcaseStep.setPmsTestcaseId(testcaseId);
                insertPmsProjectTestcaseStep(pmsProjectTestcaseStep);
            }
        }
    }
}
