package com.mortals.xhx.busiz.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.exception.AppException;
import com.mortals.xhx.base.framework.annotation.ApiUserAuth;
import com.mortals.xhx.busiz.req.PerformReq;
import com.mortals.xhx.busiz.req.PerformSaveReq;
import com.mortals.xhx.busiz.rsp.ApiResp;
import com.mortals.xhx.common.code.ApiRespCodeEnum;
import com.mortals.xhx.common.code.PerformTypeEnum;
import com.mortals.xhx.module.perform.service.*;
import lombok.extern.slf4j.Slf4j;
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 javax.servlet.http.HttpServletRequest;

@RestController
@Slf4j
@RequestMapping("/api/v1/web/perform")
public class ApiWebPerformController extends AbstractBaseController<PerformReq> {

    @Autowired
    private PerformAttendRecordService attendRecordService;
    @Autowired
    private PerformReviewRecordService reviewRecordService;
    @Autowired
    private PerformComplainRecordService complainRecordService;
    @Autowired
    private PerformGoworkRecordService performGoworkRecordService;
    @Autowired
    private PerformEffectRecordService effectRecordService;
    @Autowired
    private PerformOtherRecordService otherRecordService;
    @Autowired
    private PerformRulesService rulesService;




    /**
     * 绩效外部新增
     *
     * @param req
     * @return
     */
    @PostMapping("save")
    @ApiUserAuth
    public String receive(HttpServletRequest request, @RequestBody PerformSaveReq req) {
        ApiResp<String> rsp = new ApiResp<>();
        rsp.setMsg(ApiRespCodeEnum.SUCCESS.getLabel());
        rsp.setCode(ApiRespCodeEnum.SUCCESS.getValue());
        StringBuilder message = new StringBuilder();
        message.append(String.format("【外部请求】类型【%s】 内容:%s", PerformTypeEnum.getByValue(req.getPerformType()).getDesc(), JSONObject.toJSONString(req)));
        recordSysLog(request, message.toString());
        try {
            switch (PerformTypeEnum.getByValue(req.getPerformType())) {
                case 考勤绩效:
                    attend(req);
                    break;
                case 评价差评绩效:
                    review(req);
                    break;
                case 评价投诉绩效:
                    complain(req);
                    break;
                case 办件绩效:
                    gowork(req);
                    break;
                case 效能绩效:
                    effect(req);
                    break;
                case 其它绩效:
                    other(req);
                    break;
            }
        } catch (AppException e) {
            log.error("接收数据失败", e);
            rsp.setCode(e.getCode());
            rsp.setMsg(e.getMessage());
            return JSON.toJSONString(rsp);
        } catch (Exception e) {
            log.error("接收数据失败", e);
            rsp.setCode(ApiRespCodeEnum.FAILED.getValue());
            rsp.setMsg(e.getMessage());
            return JSON.toJSONString(rsp);
        }
        log.info("响应【设备接收】【响应体】-->  " + JSONObject.toJSONString(rsp));
        return JSON.toJSONString(rsp);
    }
    private void attend(PerformSaveReq req) throws AppException {
        //考勤保存
    }

    private void review(PerformSaveReq req) throws AppException {
        //评价保存
    }

    private void complain(PerformSaveReq req) throws AppException {
        //投诉保存
    }

    private void gowork(PerformSaveReq req) throws AppException {
        //办件保存
    }

    private void effect(PerformSaveReq req) throws AppException {
        //效能保存
    }

    private void other(PerformSaveReq req) throws AppException {
        //其它保存
    }


    public static void main(String[] args) {

    }

}
