package com.jeelowcode.module.biz.enhance;

import cn.hutool.json.JSONUtil;
import com.jeelowcode.core.framework.config.aspect.enhance.model.EnhanceContext;
import com.jeelowcode.core.framework.config.aspect.enhance.plugin.AfterAdvicePlugin;
import com.jeelowcode.core.framework.controller.BaseController;
import com.jeelowcode.core.framework.service.IFormService;
import com.jeelowcode.framework.utils.tool.NumberUtil;
import com.jeelowcode.module.biz.listener.ApproveStatusListener;
import com.jeelowcode.module.biz.service.IDrillPlanService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 删除前校验
 */
@Slf4j
@Component("afterDrillTaskEditEnhance")
public class AfterDrillTaskEditEnhance extends BaseController implements AfterAdvicePlugin {

    @Autowired
    private IDrillPlanService iDrillPlanService;

    @Autowired
    IFormService dbFormService;

    private static AfterDrillTaskEditEnhance ADTE;

    @PostConstruct
    public void init(){
        ADTE = this;
    }

    @Override
    public void execute(EnhanceContext enhanceContext) {

        Map<String, Object> params = enhanceContext.getParam().getParams();
        if(!params.containsKey("id")){
            return;
        }

        Long id = NumberUtil.toLong(params.get("id").toString());
        String taskTableName="lc_drill_task";
        String planTableName="lc_drill_plan";
        Long taskFormId = ADTE.dbFormService.getDbFormIdByTableName(taskTableName);
        Long planFormId = ADTE.dbFormService.getDbFormIdByTableName(planTableName);

        // 获取当前任务
        Map<String, Object> currentTask=ADTE.iDrillPlanService.getDrillTaskById(id);

        if(!Objects.isNull(currentTask)){
            // 根据当前任务 drill_plan_id 获取当前计划下所有任务
            Long planId = NumberUtil.toLong(currentTask.get("drill_plan_id").toString());
            List<Map<String, Object>> allTasks=ADTE.iDrillPlanService.getDrillTaskByPlanId(planId);
            // 根据所以任务状态 设置 计划状态
            if(!allTasks.isEmpty()){
                Integer allTaskNum=allTasks.size();
                Integer doneTaskNum=0;
                Integer planStatus=2;
                for(Map<String, Object> task:allTasks){
                    if (!Objects.isNull(task))
                    {
                        if(task.get("task_status").equals("3")){
                            doneTaskNum++;
                        }
                    }
                }

                if(doneTaskNum.equals(allTaskNum))
                {
                    planStatus=5; //已完成
                }else if(doneTaskNum>0){
                    planStatus=3; //执行中
                }

                // 更新 计划状态
                Map<String, Object> currentPlan=ADTE.iDrillPlanService.getDrillPlanById(planId);
                currentPlan.put("drill_plan_status_type", planStatus);
                super.editJsonData(planFormId, JSONUtil.parseObj(currentPlan));
            }
        }

    }
}
