package com.mortals.xhx.busiz.h5.web;

import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.model.Context;
import com.mortals.framework.util.DateUtils;
import com.mortals.framework.util.StringUtils;
import com.mortals.xhx.busiz.h5.req.PerformReq;
import com.mortals.xhx.common.code.AppealStatusEnum;
import com.mortals.xhx.module.perform.model.PerformErrorMessageEntity;
import com.mortals.xhx.module.perform.service.PerformErrorMessageService;
import com.mortals.xhx.module.staff.model.StaffPerformSummaryEntity;
import com.mortals.xhx.module.staff.service.StaffPerformSummaryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
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.text.SimpleDateFormat;
import java.util.*;

@RestController
@Slf4j
@RequestMapping("/api/v1/perform/error/message")
public class PerformErrorMessagePerformApiController extends AbstractBaseController<PerformReq>{

    @Autowired
    private StaffPerformSummaryService staffPerformSummaryService;
    @Autowired
    private PerformErrorMessageService performErrorMessageService;

    /**
     * 个人绩效汇总分数信息
     * @param query
     * @return
     */
    @PostMapping({"summary"})
    public String getSummary(@RequestBody PerformErrorMessageEntity query){
        JSONObject ret = new JSONObject();
        String busiDesc = "查看个人绩效汇总";
        Context context = this.getContext();
        Map<String, Object> model = new HashMap();
        if(query.getStaffId()==null){
            return this.createFailJsonResp("绩效人员信息不能为空");
        }else {
            StaffPerformSummaryEntity summaryQuery = new StaffPerformSummaryEntity();
            summaryQuery.setStaffId(query.getStaffId());
            if(StringUtils.isNotEmpty(query.getErrorTimeStart())){
                try {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    Date date = sdf.parse(query.getErrorTimeStart());
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(date);
                    summaryQuery.setYear(calendar.get(Calendar.YEAR));
                    summaryQuery.setMonth(calendar.get(Calendar.MONTH)+1);
                }catch (Exception e){

                }
            }else {
                query.setErrorTimeStart(DateUtils.getCurrDateTime("yyyy-MM")+"-01");
                Calendar calendar = Calendar.getInstance();
                summaryQuery.setYear(calendar.get(Calendar.YEAR));
                summaryQuery.setMonth(calendar.get(Calendar.MONTH)+1);
            }
            StaffPerformSummaryEntity staffPerformSummaryEntity = staffPerformSummaryService.selectOne(summaryQuery);
            if(staffPerformSummaryEntity!=null) {
                model.put("totalScore",staffPerformSummaryEntity.getTotalScore());
            }
            int errorCount = performErrorMessageService.count(query,null);
            model.put("errorCount", errorCount);
            List<Integer> appeal = Arrays.asList(1,2,3);
            query.setAppealStatusList(appeal);
            int appealCount = performErrorMessageService.count(query,null);
            model.put("appealCount", appealCount);
            ret.put("data", model);
            ret.put("code", 1);
            ret.put("msg", model.remove("message_info"));
            ret.put("dict", model.remove("dict"));
            return ret.toJSONString();
        }
    }

    /**
     * 个人绩效申诉情况
     * @param query
     * @return
     */
    @PostMapping({"appeal"})
    public String getAppeal(@RequestBody PerformErrorMessageEntity query){
        JSONObject ret = new JSONObject();
        String busiDesc = "查看个人申诉情况";
        Context context = this.getContext();
        Map<String, Object> model = new HashMap();
        if(query.getStaffId()==null){
            return this.createFailJsonResp("绩效人员信息不能为空");
        }else {
            if(CollectionUtils.isEmpty(query.getAppealStatusList())) {
                List<Integer> appeal = Arrays.asList(1, 2, 3);
                query.setAppealStatusList(appeal);
            }
            List<PerformErrorMessageEntity> list = performErrorMessageService.find(query);
            int appeal1 = 0;
            int appeal2 = 0;
            int appeal3 = 0;
            for(PerformErrorMessageEntity item:list){
                if(item.getAppealStatus()==AppealStatusEnum.申诉中.getValue()){
                    appeal1++;
                }
                if(item.getAppealStatus()==AppealStatusEnum.申诉拒绝.getValue()){
                    appeal2++;
                }
                if(item.getAppealStatus()==AppealStatusEnum.申诉通过.getValue()){
                    appeal3++;
                }
            }
            model.put("shensuzhong", appeal1);
            model.put("shensujujue", appeal2);
            model.put("shensutongguo", appeal3);
//            int errorCount = performErrorMessageService.count(query,null);
//            model.put("errorCount", errorCount);
//
//            int appealCount = performErrorMessageService.count(query,null);
//            model.put("appealCount", appealCount);
            ret.put("data", model);
            ret.put("code", 1);
            ret.put("msg", model.remove("message_info"));
            ret.put("dict", model.remove("dict"));
            return ret.toJSONString();
        }
    }
}
