package com.mortals.xhx.module.perform.web;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.model.Context;
import com.mortals.framework.model.OrderCol;
import com.mortals.framework.model.PageInfo;
import com.mortals.framework.model.Result;
import com.mortals.framework.web.BaseCRUDJsonBodyMappingController;
import com.mortals.xhx.base.system.param.service.ParamService;
import com.mortals.xhx.common.code.*;
import com.mortals.xhx.module.check.model.*;
import com.mortals.xhx.module.check.service.*;
import com.mortals.xhx.module.perform.model.PerformAttendAppealEntity;
import com.mortals.xhx.module.perform.model.PerformAttendAppealQuery;
import com.mortals.xhx.module.perform.model.PerformErrorMessageEntity;
import com.mortals.xhx.module.perform.service.PerformAttendAppealService;
import com.mortals.xhx.module.perform.service.PerformErrorMessageService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/**
*
* 绩效异常信息
*
* @author zxfei
* @date 2024-04-28
*/
@RestController
@RequestMapping("perform/error/message")
public class PerformErrorMessageController extends BaseCRUDJsonBodyMappingController<PerformErrorMessageService,PerformErrorMessageEntity,Long> {

    @Autowired
    private ParamService paramService;
    @Autowired
    private CheckAttendRecordService checkAttendRecordService;
    @Autowired
    private CheckEffectRecordService checkEffectRecordService;
    @Autowired
    private CheckGoworkRecordService checkGoworkRecordService;
    @Autowired
    private CheckReviewRecordService checkReviewRecordService;
    @Autowired
    private CheckComplainRecordService checkComplainRecordService;
    @Autowired
    private CheckOtherRecordService checkOtherRecordService;
    @Autowired
    private PerformAttendAppealService performAttendAppealService;

    public PerformErrorMessageController(){
        super.setModuleDesc( "绩效异常信息");
    }

    @Override
    protected void init(Map<String, Object> model, Context context) {
        this.addDict(model, "subAddType", SubAddTypeEnum.getEnumMap());
        this.addDict(model, "checkStatus", CheckStatusEnum.getEnumMap());
        this.addDict(model, "appealStatus", AppealStatusEnum.getEnumMap());
        this.addDict(model, "performType", PerformTypeEnum.getEnumMap(PerformTypeEnum.全部.getValue()));
        Map<String, String> data = new HashMap<>();
        data.put("0","未读");
        data.put("1","已读");
        this.addDict(model, "readStatus", data);
        super.init(model, context);
    }

    @RequestMapping(value = {"info"},method = {RequestMethod.POST, RequestMethod.GET})
    @Override
    public String info(Long id) {
        Map<String, Object> model = new HashMap();
        if (id == null) {
            return this.createFailJsonResp("请选择待查看" + this.getModuleDesc() + "信息");
        } else {
            JSONObject ret = new JSONObject();
            String busiDesc = "查看" + this.getModuleDesc();
            Context context = this.getContext();

            try {
                this.infoBefore(id, model, context);
                PerformErrorMessageEntity entity = this.service.get(id, context);
                this.infoAfter(id, model, entity, context);
                if (entity == null) {
                    throw new Exception(busiDesc + "，不存在或已被删除");
                }
                ret.put("data", entity);
                if(entity.getPerformType().equals(PerformTypeEnum.考勤绩效.getValue())){
                    CheckAttendRecordEntity checkAttendRecordEntity = checkAttendRecordService.get(entity.getCheckRecordId());
                    ret.put(PerformTypeEnum.考勤绩效.getValue(), checkAttendRecordEntity);
                    this.addDict(model, "checkStatus", CheckStatusEnum.getEnumMap());
                    this.addDict(model, "subMethod", SubMethodEnum.getEnumMap());
                    this.addDict(model, "subAddType", SubAddTypeEnum.getEnumMap());
                    this.addDict(model, "checkType", CheckTypeEnum.getEnumMap());
                    this.addDict(model, "checkResult", CheckResultEnum.getEnumMap());
                    this.addDict(model, "checkResultAdd", CheckResultAddEnum.getEnumMap());
                    this.addDict(model, "checkResultSub", CheckResultSubEnum.getEnumMap());

                }
                if(entity.getPerformType().equals(PerformTypeEnum.效能绩效.getValue())){
                    CheckEffectRecordEntity checkEffectRecordEntity = checkEffectRecordService.get(entity.getCheckRecordId());
                    ret.put(PerformTypeEnum.效能绩效.getValue(), checkEffectRecordEntity);
                    this.addDict(model, "irregularType", IrregularTypeEnum.getEnumMap());
                    this.addDict(model, "checkStatus", CheckStatusEnum.getEnumMap());
                    this.addDict(model, "subMethod", SubMethodEnum.getEnumMap(SubMethodEnum.大厅巡查.getValue(),SubMethodEnum.申诉冲销.getValue()));
                    this.addDict(model, "subAddType", SubAddTypeEnum.getEnumMap());
                    this.addDict(model, "checkType", CheckTypeEnum.getEnumMap());
                    this.addDict(model, "checkResult", CheckResultEnum.getEnumMap());
                    this.addDict(model, "checkResultAdd", CheckResultAddEnum.getEnumMap());
                    this.addDict(model, "checkResultSub", CheckResultSubEnum.getEnumMap());
                    this.addDict(model, "processStatus", ProcessStatusEnum.getEnumMap());
                }
                if(entity.getPerformType().equals(PerformTypeEnum.办件绩效.getValue())){
                    CheckGoworkRecordEntity checkGoworkRecordEntity = checkGoworkRecordService.get(entity.getCheckRecordId());
                    ret.put(PerformTypeEnum.办件绩效.getValue(), checkGoworkRecordEntity);
                    this.addDict(model, "checkStatus", CheckStatusEnum.getEnumMap());
                    this.addDict(model, "subMethod", SubMethodEnum.getEnumMap());
                    this.addDict(model, "subAddType", SubAddTypeEnum.getEnumMap());
                    this.addDict(model, "checkType", CheckTypeEnum.getEnumMap());
                    this.addDict(model, "checkResult", CheckResultEnum.getEnumMap());
                    this.addDict(model, "checkResultAdd", CheckResultAddEnum.getEnumMap());
                    this.addDict(model, "checkResultSub", CheckResultSubEnum.getEnumMap());
                }
                if(entity.getPerformType().equals(PerformTypeEnum.评价差评绩效.getValue())){
                    CheckReviewRecordEntity checkReviewRecordEntity = checkReviewRecordService.get(entity.getCheckRecordId());
                    ret.put(PerformTypeEnum.评价差评绩效.getValue(), checkReviewRecordEntity);
                    this.addDict(model, "reviewResult", paramService.getParamBySecondOrganize("CheckReviewRecord", "reviewResult"));
                    this.addDict(model, "reviewSource", paramService.getParamBySecondOrganize("CheckReviewRecord", "reviewSource"));

                    this.addDict(model, "checkStatus", CheckStatusEnum.getEnumMap());
                    this.addDict(model, "subMethod", SubMethodEnum.getEnumMap());
                    this.addDict(model, "subAddType", SubAddTypeEnum.getEnumMap());
                    this.addDict(model, "checkType", CheckTypeEnum.getEnumMap());

                    this.addDict(model, "checkResult", CheckResultEnum.getEnumMap());
                    this.addDict(model, "checkResultAdd", CheckResultAddEnum.getEnumMap());
                    this.addDict(model, "checkResultSub", CheckResultSubEnum.getEnumMap());

                    this.addDict(model, "reviewSource", ReviewSourceEnum.getEnumMap());
                }
                if(entity.getPerformType().equals(PerformTypeEnum.评价投诉绩效.getValue())){
                    CheckComplainRecordEntity checkComplainRecordEntity = checkComplainRecordService.get(entity.getCheckRecordId());
                    ret.put(PerformTypeEnum.评价投诉绩效.getValue(), checkComplainRecordEntity);
                    this.addDict(model, "checkStatus", CheckStatusEnum.getEnumMap());
                    this.addDict(model, "subMethod", SubMethodEnum.getEnumMap());
                    this.addDict(model, "subAddType", SubAddTypeEnum.getEnumMap());
                    this.addDict(model, "checkType", CheckTypeEnum.getEnumMap());
                    this.addDict(model, "checkResult", CheckResultEnum.getEnumMap());
                    this.addDict(model, "checkResultAdd", CheckResultAddEnum.getEnumMap());
                    this.addDict(model, "checkResultSub", CheckResultSubEnum.getEnumMap());
                    this.addDict(model, "complainSource", ReviewSourceEnum.getEnumMap());
                }
                if(entity.getPerformType().equals(PerformTypeEnum.其它绩效.getValue())){
                    CheckOtherRecordEntity checkOtherRecordEntity = checkOtherRecordService.get(entity.getCheckRecordId());
                    ret.put(PerformTypeEnum.其它绩效.getValue(), checkOtherRecordEntity);
                    this.addDict(model, "irregularOtherType", paramService.getParamBySecondOrganize("CheckOtherRecord","irregularOtherType"));
                    this.addDict(model, "checkStatus", CheckStatusEnum.getEnumMap());
                    this.addDict(model, "subMethod", SubMethodEnum.getEnumMap());
                    this.addDict(model, "subAddType", SubAddTypeEnum.getEnumMap());
                    this.addDict(model, "checkType", CheckTypeEnum.getEnumMap());
                    this.addDict(model, "checkResult", CheckResultEnum.getEnumMap());
                    this.addDict(model, "checkResultAdd", CheckResultAddEnum.getEnumMap());
                    this.addDict(model, "checkResultSub", CheckResultSubEnum.getEnumMap());
                }
                if (!ObjectUtils.isEmpty(context) && !ObjectUtils.isEmpty(context.getUser())) {
                    this.recordSysLog(this.request, busiDesc + " 【成功】");
                }
                if(entity.getAppealStatus()!=0) {
                    PerformAttendAppealQuery appealQuery = new PerformAttendAppealQuery();
                    appealQuery.setCheckRecordId(entity.getCheckRecordId());
                    appealQuery.setPerformType(entity.getPerformType());
                    appealQuery.setOrderColList(Arrays.asList(new OrderCol("appealTime", OrderCol.DESCENDING)));
                    LocalDateTime currentDate = LocalDateTime.now();
                    LocalDateTime firstDayOfMonth = currentDate.withDayOfMonth(1);
                    appealQuery.setAppealTimeStart(DateUtil.format(firstDayOfMonth,"yyyy-MM-dd"));
                    PageInfo pageInfo = new PageInfo(-1);
                    Result<PerformAttendAppealEntity> result = performAttendAppealService.getDao().getListExt(appealQuery,pageInfo);
                    if(CollectionUtils.isNotEmpty(result.getList())) {
                        ret.put("appealData", result.getList().get(0));
                    }
                }
            } catch (Exception var8) {
                this.doException(this.request, busiDesc, model, var8);
                Object msg = model.get("message_info");
                return this.createFailJsonResp(msg == null ? "系统异常" : msg.toString());
            }

            this.init(model, context);
            ret.put("code", 1);
            ret.put("msg", model.remove("message_info"));
            ret.put("dict", model.remove("dict"));
            return ret.toJSONString();
        }
    }
}