package com.zzsmart.qomo.kn.cost.manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzsmart.qomo.dao.entity.FlowDefinition;
import com.zzsmart.qomo.kn.cost.manage.entity.AppSceneCostCount;
import com.zzsmart.qomo.kn.cost.manage.entity.AppSceneCostPartMissingInfo;
import com.zzsmart.qomo.kn.cost.manage.entity.AppSceneCostStandardVersion;
import com.zzsmart.qomo.kn.cost.manage.enums.CostCountStatusEnum;
import com.zzsmart.qomo.kn.cost.manage.mapper.AppSceneCostCountMapper;
import com.zzsmart.qomo.kn.cost.manage.mapper.AppSceneCostPartMissingInfoMapper;
import com.zzsmart.qomo.kn.cost.manage.service.IAppSceneCostCountService;
import com.zzsmart.qomo.kn.cost.manage.service.IAppSceneCostStandardVersionService;
import com.zzsmart.qomo.kn.cost.manage.vo.MissingComponentVO;
import com.zzsmart.qomo.service.IDataFlowDefinitionService;
import com.zzsmart.qomo.service.IDataFlowInstanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * @Description: app_scene_cost_count
 * @Author: jeecg-boot
 * @Date: 2024-07-29
 * @Version: V1.0
 */
@Service
public class AppSceneCostCountServiceImpl extends ServiceImpl<AppSceneCostCountMapper, AppSceneCostCount> implements IAppSceneCostCountService {
    @Autowired
    private IAppSceneCostStandardVersionService costStandardVersionService;
    @Autowired
    private IDataFlowInstanceService flowInstanceService;
    @Autowired
    private AppSceneCostPartMissingInfoMapper costPartMissingInfoMapper;

    /**
     * 查看关键缺失部件
     *
     * @param id
     * @return
     */
    @Override
    public List<MissingComponentVO> queryMissingComponentById(String id) {
        List<MissingComponentVO> list = new ArrayList<>();
        if (id != null) {
            QueryWrapper<AppSceneCostPartMissingInfo> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("costStandardId", id);
            List<AppSceneCostPartMissingInfo> costPartMissingInfos = costPartMissingInfoMapper.selectList(queryWrapper);
            if (costPartMissingInfos != null && costPartMissingInfos.size() > 0) {
                for (int i = 0; i < costPartMissingInfos.size(); i++) {
                    AppSceneCostPartMissingInfo costPartMissingInfo = costPartMissingInfos.get(i);
                    if (costPartMissingInfo.getMissingInfo() != null) {
                        String[] split = costPartMissingInfo.getMissingInfo().split(";");
                        for (int j = 0; j < split.length; j++) {
                            MissingComponentVO temp = MissingComponentVO.builder().materialNumber(costPartMissingInfo.getMaterialNumber()).missingInfo(split[j]).versionNumber(costPartMissingInfo.getVersionNumber()).build();
                            list.add(temp);
                        }
                    }
                }
            }
        }
        return list;
    }

    /**
     * 重新计算
     *
     * @param id
     * @return
     */
    @Override
    public AppSceneCostCount recountById(Integer id) {
        if (id != null) {
            AppSceneCostCount appSceneCostCount = getById(id);
            if (appSceneCostCount != null) {
                //执行标准成本计算
                executeStandardCost(id, appSceneCostCount.getFlowDefinitionId());
            }
        }
        return null;
    }

    /**
     * 执行标准成本计算
     *
     * @param id
     * @param flowDefinitionId
     */
    private void executeStandardCost(Integer id, Integer flowDefinitionId) {
        //TODO 执行标准成本计算(参数传递)
        flowInstanceService.execute(flowDefinitionId, null, null);
    }

    /**
     * 添加标准成本计算任务
     *
     * @param appSceneCostCount
     */
    @Override
    public void addCostCountTask(AppSceneCostCount appSceneCostCount) {
        //1.新增版本号记录成功后
        AppSceneCostStandardVersion costStandardVersion = AppSceneCostStandardVersion.builder().figureNumber(appSceneCostCount.getDrawingNo()).materialNumber(appSceneCostCount.getMaterialNo()).stage(appSceneCostCount.getStage()).versionNumber(appSceneCostCount.getCostVersion()).versionYear(appSceneCostCount.getYear()).build();
        boolean save = costStandardVersionService.save(costStandardVersion);
        //2.新增标准成本计算任务，并立即执行标准成本计算
        if (save) {
            AppSceneCostCount appSceneCostCount1 = AppSceneCostCount.builder().materialNo(appSceneCostCount.getMaterialNo()).drawingNo(appSceneCostCount.getDrawingNo()).stage(appSceneCostCount.getStage()).flowDefinitionId(appSceneCostCount.getFlowDefinitionId()).year(appSceneCostCount.getYear()).costVersion(costStandardVersion.getVersionNumber()).status(CostCountStatusEnum.Executing.getValue()).createTime(new Date()).build();
            boolean flag = save(appSceneCostCount1);
            if (flag) {
                //发送更新成本计算状态的队列消息（队列名:updateCostCountStatus）

                //执行标准成本计算
                executeStandardCost(appSceneCostCount1.getId(), appSceneCostCount1.getFlowDefinitionId());
                //TODO 新增部件缺失信息记录
                AppSceneCostPartMissingInfo costPartMissingInfo = AppSceneCostPartMissingInfo.builder().costStandardId(appSceneCostCount1.getId().toString()).materialNumber(appSceneCostCount1.getMaterialNo()).versionNumber(appSceneCostCount1.getCostVersion()).missingInfo("电源线;绝缘层涂料").build();
                costPartMissingInfoMapper.insert(costPartMissingInfo);
            }
        }
    }
}
