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

import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.annotation.UnAuth;
import com.mortals.framework.common.Rest;
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.module.staff.model.StaffPerformStatEntity;
import com.mortals.xhx.module.staff.model.vo.StaffSummaryTopQuery;
import com.mortals.xhx.module.staff.service.StaffPerformStatService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
*
* 员工绩效统计
*
* @author zxfei
* @date 2023-05-17
*/
@RestController
@RequestMapping("staff/perform/stat")
public class StaffPerformStatController extends BaseCRUDJsonBodyMappingController<StaffPerformStatService,StaffPerformStatEntity,Long> {

    @Autowired
    private ParamService paramService;

    public StaffPerformStatController(){
        super.setModuleDesc( "员工绩效统计");
    }

    @Override
    protected void init(Map<String, Object> model, Context context) {
        this.addDict(model, "month", paramService.getParamBySecondOrganize("StaffPerformStat","month"));
        this.addDict(model, "day", paramService.getParamBySecondOrganize("StaffPerformStat","day"));
        super.init(model, context);
    }

    /**
     * 个人绩效排行榜
     * @param query
     * @return
     */
    @PostMapping({"summary/top"})
    @UnAuth
    public Rest<Object> getSummaryTopList(@RequestBody StaffSummaryTopQuery query) {
        Rest<Object> ret = new Rest();
        Map<String, Object> model = new HashMap();
        Context context = this.getContext();
        String busiDesc = "查询个人绩效排行榜";
        int code = 1;
        try {
            List<StaffPerformStatEntity> result = this.getService().getSummaryTopList(query);
            model.put("data", result);
            model.put("message_info", busiDesc + "成功");
            if (!ObjectUtils.isEmpty(context) && !ObjectUtils.isEmpty(context.getUser())) {
                this.recordSysLog(this.request, busiDesc + " 【成功】");
            }
        } catch (Exception var9) {
            code = -1;
            this.doException(this.request, busiDesc, model, var9);
        }
        this.init(model, context);
        ret.setCode(code);
        ret.setData(model.get("data"));
        ret.setMsg(model.get("message_info") == null ? "" : model.remove("message_info").toString());
        return ret;
    }

    /**
     * 异常分值统计
     * @param query
     * @return
     */
    @PostMapping({"summary/exception"})
    @UnAuth
    public Rest<Object> getExceptionList(@RequestBody StaffSummaryTopQuery query) {
        Rest<Object> ret = new Rest();
        Map<String, Object> model = new HashMap();
        Context context = this.getContext();
        String busiDesc = "查询异常分值统计";
        int code = 1;
        try {
            List<StaffPerformStatEntity> result = this.getService().getExceptionList(query);
            List<JSONObject> data = new ArrayList<>();
            if(CollectionUtils.isNotEmpty(result)) {
                StaffPerformStatEntity entity = result.get(0);
                JSONObject jsonObject1 = new JSONObject();
                jsonObject1.put("name", "考勤绩效");
                jsonObject1.put("add", entity.getAttendScoreAdd());
                jsonObject1.put("sub", entity.getAttendScoreSub());
                data.add(jsonObject1);
                JSONObject jsonObject2 = new JSONObject();
                jsonObject2.put("name", "评价绩效");
                jsonObject2.put("add", entity.getReviewScoreAdd());
                jsonObject2.put("sub", entity.getReviewScoreSub());
                data.add(jsonObject2);
                JSONObject jsonObject3 = new JSONObject();
                jsonObject3.put("name", "投诉绩效");
                jsonObject3.put("add", entity.getComplainScoreAdd());
                jsonObject3.put("sub", entity.getComplainScoreSub());
                data.add(jsonObject3);
                JSONObject jsonObject4 = new JSONObject();
                jsonObject4.put("name", "办件绩效");
                jsonObject4.put("add", entity.getGoworkScoreAdd());
                jsonObject4.put("sub", entity.getGoworkScoreSub());
                data.add(jsonObject4);
                JSONObject jsonObject5 = new JSONObject();
                jsonObject5.put("name", "效能绩效");
                jsonObject5.put("add", entity.getEffectScoreAdd());
                jsonObject5.put("sub", entity.getEffectScoreSub());
                data.add(jsonObject5);
                JSONObject jsonObject6 = new JSONObject();
                jsonObject6.put("name", "其它绩效");
                jsonObject6.put("add", entity.getOtherScoreAdd());
                jsonObject6.put("sub", entity.getOtherScoreSub());
                data.add(jsonObject6);
            }
            model.put("data", data);
            model.put("message_info", busiDesc + "成功");
            if (!ObjectUtils.isEmpty(context) && !ObjectUtils.isEmpty(context.getUser())) {
                this.recordSysLog(this.request, busiDesc + " 【成功】");
            }
        } catch (Exception var9) {
            code = -1;
            this.doException(this.request, busiDesc, model, var9);
        }
        this.init(model, context);
        ret.setCode(code);
        ret.setData(model.get("data"));
        ret.setMsg(model.get("message_info") == null ? "" : model.remove("message_info").toString());
        return ret;
    }

}