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.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.utils.model.ResultDataModel;
import com.jeelowcode.service.system.entity.AdminUserDO;
import com.jeelowcode.service.system.service.IAdminUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

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

    @Autowired
    private IAdminUserService userService;

    private static BeforeRiskAssessmentExportEnhance ADTE;

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

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

        params.put("billNo","FX" + System.currentTimeMillis());

        //评估人
        if(params.containsKey("assessorName")) {
            String assessorName = Func.getMap2Str(params, "assessorName");
            AdminUserDO user = ADTE.userService.selectByNickname(assessorName);
            if (user != null) {
                params.put("assessorId", user.getId());
            }
        }

        //部门负责人
        if(params.containsKey("deptChargerName")) {
            String deptChargerName = Func.getMap2Str(params, "deptChargerName");
            AdminUserDO user = ADTE.userService.selectByNickname(deptChargerName);
            if (user != null) {
                params.put("deptChargerId", user.getId());
            }
        }
    }
}
