package com.jeelowcode.module.biz.enhance;

import com.jeelowcode.core.framework.config.aspect.enhance.model.EnhanceContext;
import com.jeelowcode.core.framework.config.aspect.enhance.model.EnhanceResult;
import com.jeelowcode.core.framework.config.aspect.enhance.plugin.BeforeAdvicePlugin;
import com.jeelowcode.core.framework.controller.BaseController;
import com.jeelowcode.core.framework.service.IFormService;
import com.jeelowcode.core.framework.utils.Func;
import com.jeelowcode.framework.exception.JeeLowCodeException;
import com.jeelowcode.framework.utils.model.ResultDataModel;
import com.jeelowcode.service.system.controller.vo.auth.AuthPermissionInfoRespVO;
import com.jeelowcode.service.system.entity.AdminUserDO;
import com.jeelowcode.service.system.service.IAdminUserService;
import com.jeelowcode.tool.framework.common.pojo.CommonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

/**
 * 风险隐患数据导入前
 * 检查园区 Name 反向给 检查园区 ID 赋值
 */
@Slf4j
@Component("beforeRiskExportEnhance")
public class BeforeRiskExportEnhance extends BaseController implements BeforeAdvicePlugin {
    @Autowired
    private IFormService dbFormService;

    @Autowired
    private IAdminUserService userService;

    private static BeforeRiskExportEnhance ADTE;

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

    @Override
    public void execute(EnhanceContext enhanceContext) {
        Map<String, Object> params = enhanceContext.getParam().getParams();



        // 获取所有园区信息
        String campusTableName="campus_info";
        Long campusFormId = ADTE.dbFormService.getDbFormIdByTableName(campusTableName);
        Map<String, Object> param = new HashMap<>();
        ResultDataModel allCampus = super.getDataPage(campusFormId, param);

        // 隐患来源 固定为 1
        params.put("source", "1");
        params.put("billNo","FX" + System.currentTimeMillis());

        if(params.containsKey("parkName")) {
            String name = Func.getMap2Str(params, "parkName");

            // 根据name查找id
            Map<String, Object> depMap = allCampus.getRecords().stream()
                    .filter(t -> t.get("campus_name")
                            .equals(name)).findFirst().orElse(null);
            if (Objects.nonNull(depMap)) {
                params.put("parkId", depMap.get("campus_id"));
            }
        }

        //检查人员
        if(params.containsKey("check_people")) {
            String check_people = Func.getMap2Str(params, "check_people");
            AdminUserDO user = ADTE.userService.selectByNickname(check_people);
            if (user != null) {
                params.put("check_people_id", user.getId());
            }
        }

        //整改责任人
        if(params.containsKey("corrective_charge_people")) {
            String corrective_charge_people = Func.getMap2Str(params, "corrective_charge_people");
            AdminUserDO user = ADTE.userService.selectByNickname(corrective_charge_people);
            if (user != null) {
                params.put("corrective_charge_people_id", user.getId());
            }
        }

        //整改确认人
        if(params.containsKey("corrective_confirm_people")) {
            String corrective_confirm_people = Func.getMap2Str(params, "corrective_confirm_people");
            AdminUserDO user = ADTE.userService.selectByNickname(corrective_confirm_people);
            if (user != null) {
                params.put("corrective_confirm_people_id", user.getId());
            }
        }
    }
}
