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

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzsmart.qomo.kn.cost.manage.entity.AppSceneCostCount;
import com.zzsmart.qomo.kn.cost.manage.entity.CostStandardVersion;
import com.zzsmart.qomo.kn.cost.manage.mapper.AppSceneCostCountMapper;
import com.zzsmart.qomo.kn.cost.manage.service.IAppSceneCostCountService;
import com.zzsmart.qomo.kn.cost.manage.service.ICostStandardVersionService;
import com.zzsmart.qomo.service.IDataFlowInstanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @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 ICostStandardVersionService costStandardVersionService;
    @Autowired
    private IDataFlowInstanceService flowInstanceService;

    /**
     * 查看关键缺失部件
     *
     * @param id
     * @return
     */
    @Override
    public AppSceneCostCount queryMissingComponentById(String id) {
        return null;
    }

    /**
     * 重新计算
     *
     * @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.新增版本号记录成功后
        CostStandardVersion costStandardVersion = CostStandardVersion.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(appSceneCostCount.getCostVersion()).status(0).createTime(new Date()).build();
            boolean flag = save(appSceneCostCount1);
            if (flag) {
                //执行标准成本计算
                executeStandardCost(appSceneCostCount1.getId(), appSceneCostCount1.getFlowDefinitionId());
            }
        }
    }
}
