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.service.IWorkItemService;
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("afterItemResultEditEnhance")
public class AfterItemResultEditEnhance extends BaseController implements AfterAdvicePlugin {

    @Autowired
    private IWorkItemService workItemService;

    @Autowired
    private IFormService dbFormService;

    private static AfterItemResultEditEnhance 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_item_result";
        String planTableName="lc_work_item";
        Long taskFormId = ADTE.dbFormService.getDbFormIdByTableName(taskTableName);
        Long planFormId = ADTE.dbFormService.getDbFormIdByTableName(planTableName);

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

        if(!Objects.isNull(currentTask)){
            // 根据当前任务 workItemId 获取当前计划下所有任务
            Long workItemId = NumberUtil.toLong(currentTask.get("workitemid").toString());
            List<Map<String, Object>> allTasks=ADTE.workItemService.getItemResultByItemId(workItemId);
            // 根据所以任务状态 设置 计划状态
            if(!allTasks.isEmpty()){
                Integer allTaskNum=allTasks.size();
                Integer doneTaskNum=0;
                Integer itemStatus =1;
                for(Map<String, Object> task:allTasks){
                    if (!Objects.isNull(task))
                    {
                        if(task.get("resultstatus").equals("1")){
                            doneTaskNum++;
                        }
                    }
                }

                if(doneTaskNum.equals(allTaskNum))
                {
                    itemStatus =3; //已完成
                }else if(doneTaskNum>0){
                    itemStatus =2; //进行中
                }

                // 更新 计划状态
                Map<String, Object> currentItem =ADTE.workItemService.getWorkItemById(workItemId);
                currentItem.put("itemSattus", itemStatus);
                super.editJsonData(planFormId, JSONUtil.parseObj(currentItem));
            }

            // 发送已办消息
            if(currentTask.get("resultstatus").equals("1"))
            {
                // 发送已办消息
                ADTE.workItemService.pushTodoWorkItemWorkflow(workItemId,id,"WORK_ITEM_DONE");

            }
        }

    }
}
