package com.microservices.pms.product.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.common.service.IPmsCommonService;
import com.microservices.pms.product.domain.PmsProductReqOperationRecord;
import com.microservices.pms.product.domain.PmsProductRequirement;
import com.microservices.pms.product.domain.enums.ProductReqOperationType;
import com.microservices.pms.product.domain.enums.ProductReqStatus;
import com.microservices.pms.product.domain.vo.ProductReqOperationRecordDataVo;
import com.microservices.pms.product.mapper.PmsProductReqOperationRecordMapper;
import com.microservices.pms.product.service.IPmsProductReqOperationRecordService;
import com.microservices.pms.product.service.IPmsProductRequirementService;
import com.microservices.system.api.domain.SimpleSysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

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

/**
 * 项目管理-产品需求操作记录Service业务层处理
 *
 * @author otto
 * @date 2024-11-29
 */
@Service
public class PmsProductReqOperationRecordServiceImpl implements IPmsProductReqOperationRecordService {
    @Autowired
    private PmsProductReqOperationRecordMapper pmsProductReqOperationRecordMapper;
    @Autowired
    private IPmsCommonService commonService;
    @Autowired
    @Lazy
    private IPmsProductRequirementService pmsProductRequirementService;

    /**
     * 查询项目管理-产品需求操作记录
     *
     * @param id 项目管理-产品需求操作记录主键
     * @return 项目管理-产品需求操作记录
     */
    @Override
    public PmsProductReqOperationRecord selectPmsProductReqOperationRecordById(Long id) {
        return pmsProductReqOperationRecordMapper.selectPmsProductReqOperationRecordById(id);
    }

    /**
     * 查询项目管理-产品需求操作记录列表
     *
     * @param pmsProductReqOperationRecord 项目管理-产品需求操作记录
     * @return 项目管理-产品需求操作记录
     */
    @Override
    public List<PmsProductReqOperationRecord> selectPmsProductReqOperationRecordList(PmsProductReqOperationRecord pmsProductReqOperationRecord) {
        return pmsProductReqOperationRecordMapper.selectPmsProductReqOperationRecordList(pmsProductReqOperationRecord);
    }

    /**
     * 新增项目管理-产品需求操作记录
     *
     * @param pmsProductReqOperationRecord 项目管理-产品需求操作记录
     * @return 结果
     */
    @Override
    public int insertPmsProductReqOperationRecord(PmsProductReqOperationRecord pmsProductReqOperationRecord) {
        pmsProductReqOperationRecord.setCreateBy(SecurityUtils.getUsername());
        pmsProductReqOperationRecord.setCreateTime(DateUtils.getNowDate());
        return pmsProductReqOperationRecordMapper.insertPmsProductReqOperationRecord(pmsProductReqOperationRecord);
    }

    /**
     * 修改项目管理-产品需求操作记录
     *
     * @param pmsProductReqOperationRecord 项目管理-产品需求操作记录
     * @return 结果
     */
    @Override
    public int updatePmsProductReqOperationRecord(PmsProductReqOperationRecord pmsProductReqOperationRecord) {
        pmsProductReqOperationRecord.setUpdateBy(SecurityUtils.getUsername());
        pmsProductReqOperationRecord.setUpdateTime(DateUtils.getNowDate());
        return pmsProductReqOperationRecordMapper.updatePmsProductReqOperationRecord(pmsProductReqOperationRecord);
    }

    /**
     * 批量删除项目管理-产品需求操作记录
     *
     * @param ids 需要删除的项目管理-产品需求操作记录主键
     * @return 结果
     */
    @Override
    public int deletePmsProductReqOperationRecordByIds(Long[] ids) {
        return pmsProductReqOperationRecordMapper.deletePmsProductReqOperationRecordByIds(ids);
    }

    /**
     * 删除项目管理-产品需求操作记录信息
     *
     * @param id 项目管理-产品需求操作记录主键
     * @return 结果
     */
    @Override
    public int deletePmsProductReqOperationRecordById(Long id) {
        return pmsProductReqOperationRecordMapper.deletePmsProductReqOperationRecordById(id);
    }

    @Override
    public PmsProductReqOperationRecord selectUserReqLatestReviewRecord(Long reqId) {
        return pmsProductReqOperationRecordMapper.selectUserReqLatestReviewRecord(reqId);
    }

    @Override
    public List<ProductReqOperationRecordDataVo> selectUserReqChangeRecordList(Long userReqId) {
        PmsProductRequirement userReq = pmsProductRequirementService.selectPmsProductRequirementById(userReqId, false);
        if (!ProductReqStatus.isUserReqStatus(userReq.getStatus())) {
            throw new ServiceException("需求规格变更记录请使用查询需求规格变更记录接口");
        }
        PmsProductReqOperationRecord recordSearch = new PmsProductReqOperationRecord();
        recordSearch.setProductRequirementId(userReqId);
        List<PmsProductReqOperationRecord> recordList = pmsProductReqOperationRecordMapper.selectPmsProductReqOperationRecordList(recordSearch);
        return getRecordDataList(recordList);
    }

    @Override
    public List<ProductReqOperationRecordDataVo> selectReqSpecsChangeRecordList(Long reqSpecsId) {
        PmsProductRequirement reqSpecs = pmsProductRequirementService.selectPmsProductRequirementById(reqSpecsId, false);
        if (ProductReqStatus.isUserReqStatus(reqSpecs.getStatus())) {
            throw new ServiceException("用户需求变更记录请使用查询用户需求变更记录接口");
        }
        // 根据需求规格id和fromId查询需求规格相关的所有需求规格相关的变更记录列表
        PmsProductReqOperationRecord recordSearch = new PmsProductReqOperationRecord();
        List<PmsProductReqOperationRecord> recordList = new ArrayList<>();
        while (reqSpecs.getFromId() != null && !ProductReqStatus.isUserReqStatus(reqSpecs.getStatus())) {
            recordSearch.setProductRequirementId(reqSpecs.getId());
            recordList.addAll(pmsProductReqOperationRecordMapper.selectPmsProductReqOperationRecordList(recordSearch));
            reqSpecs = pmsProductRequirementService.selectPmsProductRequirementById(reqSpecs.getFromId(), false);
        }

        return getRecordDataList(recordList);
    }

    @Override
    public PmsProductReqOperationRecord selectReqSpecsLatestChangeRecord(Long reqSpecsId) {
        return pmsProductReqOperationRecordMapper.selectReqSpecsLatestChangeRecord(reqSpecsId);
    }

    @Override
    public PmsProductReqOperationRecord selectReqSpecsLatestReviewRecord(Long reqSpecsId) {
        return pmsProductReqOperationRecordMapper.selectReqSpecsLatestReviewRecord(reqSpecsId);
    }

    private List<ProductReqOperationRecordDataVo> getRecordDataList(List<PmsProductReqOperationRecord> recordList) {
        List<ProductReqOperationRecordDataVo> recordDataList = new ArrayList<>();
        for (PmsProductReqOperationRecord record : recordList) {
            ProductReqOperationRecordDataVo recordData = new ProductReqOperationRecordDataVo();
            recordData.setContent(record.getContent());
            SimpleSysUser simpleSysUser = commonService.getSimpleSysUserByUsername(record.getOperator());
            recordData.setRecord(ProductReqOperationType.genRecordStr(simpleSysUser, record));
            recordDataList.add(recordData);
        }
        return recordDataList;
    }
}
