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

import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.model.Context;
import com.mortals.framework.web.BaseCRUDJsonBodyMappingController;
import com.mortals.xhx.base.system.param.service.ParamService;
import com.mortals.xhx.common.code.AppealStatusEnum;
import com.mortals.xhx.common.code.CheckStatusEnum;
import com.mortals.xhx.common.code.PerformTypeEnum;
import com.mortals.xhx.common.code.SubAddTypeEnum;
import com.mortals.xhx.module.check.model.*;
import com.mortals.xhx.module.check.service.*;
import com.mortals.xhx.module.perform.model.PerformErrorMessageEntity;
import com.mortals.xhx.module.perform.service.PerformErrorMessageService;
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.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;

    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("checkRecordData", checkAttendRecordEntity);
                }
                if(entity.getPerformType().equals(PerformTypeEnum.效能绩效.getValue())){
                    CheckEffectRecordEntity checkEffectRecordEntity = checkEffectRecordService.get(entity.getCheckRecordId());
                    ret.put("checkRecordData", checkEffectRecordEntity);
                }
                if(entity.getPerformType().equals(PerformTypeEnum.办件绩效.getValue())){
                    CheckGoworkRecordEntity checkGoworkRecordEntity = checkGoworkRecordService.get(entity.getCheckRecordId());
                    ret.put("checkRecordData", checkGoworkRecordEntity);
                }
                if(entity.getPerformType().equals(PerformTypeEnum.评价差评绩效.getValue())){
                    CheckReviewRecordEntity checkReviewRecordEntity = checkReviewRecordService.get(entity.getCheckRecordId());
                    ret.put("checkRecordData", checkReviewRecordEntity);
                }
                if(entity.getPerformType().equals(PerformTypeEnum.评价投诉绩效.getValue())){
                    CheckComplainRecordEntity checkComplainRecordEntity = checkComplainRecordService.get(entity.getCheckRecordId());
                    ret.put("checkRecordData", checkComplainRecordEntity);
                }
                if(entity.getPerformType().equals(PerformTypeEnum.其它绩效.getValue())){
                    CheckOtherRecordEntity checkOtherRecordEntity = checkOtherRecordService.get(entity.getCheckRecordId());
                    ret.put("checkRecordData", checkOtherRecordEntity);
                }
                if (!ObjectUtils.isEmpty(context) && !ObjectUtils.isEmpty(context.getUser())) {
                    this.recordSysLog(this.request, busiDesc + " 【成功】");
                }
            } 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();
        }
    }
}