package com.jeelowcode.module.biz.enhance;

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.model.ResultDataModel;
import com.jeelowcode.framework.utils.tool.CollectionUtil;
import com.jeelowcode.framework.utils.tool.NumberUtil;
import com.jeelowcode.framework.utils.utils.FuncBase;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.time.LocalDate;

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

    @Autowired
    private IFormService dbFormService;

    private static AfterDrillPlanListEnhance ADTE;

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


    @Override
    public void execute(EnhanceContext enhanceContext) {
        log.info("进入=======>studentAfterEnhancePage=======>execute");
        List<Map<String, Object>> records = enhanceContext.getResult().getRecords();
        if (FuncBase.isEmpty(records)) {
            return;
        }
        String taskTableName="lc_drill_task";
        Long taskFormId = ADTE.dbFormService.getDbFormIdByTableName(taskTableName);
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        records.forEach(record -> {
            // 已下发待执行 和 执行中 的计划才进行逾期判断
            if(record.get("drill_plan_status_type").equals("2")
                    ||record.get("drill_plan_status_type").equals("3")){
                // 获取参数 计划ID
                Long planId = NumberUtil.toLong(record.get("id").toString());

                boolean isAfter = false;
                // 获取该计划下的所有任务
                Map<String, Object> params = new HashMap<>();
                params.put("drill_plan_id", planId);
                ResultDataModel allTask = super.getDataPage(taskFormId, params);

                // 遍历allTask 如果有一个逾期，设置计划为逾期状态
                if (CollectionUtil.isNotEmpty(allTask.getRecords())) {
                    for (Map<String, Object> task : allTask.getRecords()) {
                        if (!task.get("task_status").equals("3")) {
                            // 对比当前日期和任务日期
                            LocalDate eDate = LocalDate.parse(task.get("eDate").toString(), formatter);
                            // 比较日期并更新状态
                            if (currentDate.isAfter(eDate)) {
                                isAfter = true;
                                break; // 找到逾期任务，立即跳出循环
                            }
                        }
                    }
                }

                if (isAfter) {
                    // 设置计划为逾期状态
                    record.put("drill_plan_status_type", "4"); // 设置为已经逾期
                }
            }
        });
    }
}
