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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.annotation.UnAuth;
import com.mortals.framework.common.Rest;
import com.mortals.framework.exception.AppException;
import com.mortals.xhx.busiz.h5.req.*;
import com.mortals.xhx.module.perform.model.*;
import com.mortals.xhx.module.perform.service.*;
import com.mortals.xhx.module.staff.model.StaffEntity;
import com.mortals.xhx.module.staff.model.StaffQuery;
import com.mortals.xhx.module.staff.service.StaffService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
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.Date;

@RestController
@Slf4j
@RequestMapping("/api/v1/web/")
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;
    @Autowired
    private StaffService staffService;
    @Autowired
    private PerformRulesCategoryService categoryService;


    /**
     * 考勤新增
     *
     * @param req
     * @return
     */
    @PostMapping("attend/save")
    @UnAuth
    public Rest<Void> attendSave(@RequestBody AttendSaveReq req) {
        log.info("web Api req==>{}", JSON.toJSONString(req));
        String busiDesc = "外部考勤新增";
        Rest rest = Rest.ok(busiDesc + "成功！");
        try {
            if (ObjectUtils.isEmpty(req.getPhone())) throw new AppException("手机号码不能为空！");
            if (ObjectUtils.isEmpty(req.getRuleCode())) throw new AppException("绩效规则编码不能为空！");
            boolean bool = attend(req);
            if (bool) throw new AppException(busiDesc + "失败！");
        } catch (Exception e) {
            log.error(busiDesc, e);
            return Rest.fail(e.getMessage());
        }
        log.info("响应【{}】【响应体】--> {} ", busiDesc, JSONObject.toJSONString(rest));
        return rest;
    }

    /**
     * 评价新增
     *
     * @param req
     * @return
     */
    @PostMapping("review/save")
    @UnAuth
    public Rest<Void> reviewSave(@RequestBody ReviewSaveReq req) {
        log.info("web Api req==>{}", JSON.toJSONString(req));
        String busiDesc = "外部评价新增";
        Rest rest = Rest.ok(busiDesc + "成功！");
        try {
            if (ObjectUtils.isEmpty(req.getPhone())) throw new AppException("手机号码不能为空！");
            if (ObjectUtils.isEmpty(req.getRuleCode())) throw new AppException("绩效规则编码不能为空！");
            boolean bool = review(req);
            if (bool) throw new AppException(busiDesc + "失败！");
        } catch (Exception e) {
            log.error(busiDesc, e);
            return Rest.fail(e.getMessage());
        }
        log.info("响应【{}】【响应体】--> {} ", busiDesc, JSONObject.toJSONString(rest));
        return rest;
    }

    /**
     * 投诉新增
     *
     * @param req
     * @return
     */
    @PostMapping("complain/save")
    @UnAuth
    public Rest<Void> complainSave(@RequestBody ComplainSaveReq req) {
        log.info("web Api req==>{}", JSON.toJSONString(req));
        String busiDesc = "外部投诉新增";
        Rest rest = Rest.ok(busiDesc + "成功！");
        try {
            if (ObjectUtils.isEmpty(req.getPhone())) throw new AppException("手机号码不能为空！");
            if (ObjectUtils.isEmpty(req.getRuleCode())) throw new AppException("绩效规则编码不能为空！");
            boolean bool = complain(req);
            if (bool) throw new AppException(busiDesc + "失败！");
        } catch (Exception e) {
            log.error(busiDesc, e);
            return Rest.fail(e.getMessage());
        }
        log.info("响应【{}】【响应体】--> {} ", busiDesc, JSONObject.toJSONString(rest));
        return rest;
    }

    /**
     * 办件新增
     *
     * @param req
     * @return
     */
    @PostMapping("gowork/save")
    @UnAuth
    public Rest<Void> goworkSave(@RequestBody GoWorkSaveReq req) {
        log.info("web Api req==>{}", JSON.toJSONString(req));
        String busiDesc = "外部办件新增";
        Rest rest = Rest.ok(busiDesc + "成功！");
        try {
            if (ObjectUtils.isEmpty(req.getPhone())) throw new AppException("手机号码不能为空！");
            if (ObjectUtils.isEmpty(req.getRuleCode())) throw new AppException("绩效规则编码不能为空！");
            boolean bool = gowork(req);
            if (bool) throw new AppException(busiDesc + "失败！");
        } catch (Exception e) {
            log.error(busiDesc, e);
            return Rest.fail(e.getMessage());
        }
        log.info("响应【{}】【响应体】--> {} ", busiDesc, JSONObject.toJSONString(rest));
        return rest;
    }

    /**
     * 效能新增
     *
     * @param req
     * @return
     */
    @PostMapping("effect/save")
    @UnAuth
    public Rest<Void> effectSave(@RequestBody EffectSaveReq req) {

        log.info("web Api req==>{}", JSON.toJSONString(req));
        String busiDesc = "外部效能新增";
        Rest rest = Rest.ok(busiDesc + "成功！");
        try {
            if (ObjectUtils.isEmpty(req.getPhone())) throw new AppException("手机号码不能为空！");
            if (ObjectUtils.isEmpty(req.getRuleCode())) throw new AppException("绩效规则编码不能为空！");
            boolean bool = effect(req);
            if (bool) throw new AppException(busiDesc + "失败！");
        } catch (Exception e) {
            log.error(busiDesc, e);
            return Rest.fail(e.getMessage());
        }
        log.info("响应【{}】【响应体】--> {} ", busiDesc, JSONObject.toJSONString(rest));
        return rest;
    }

    /**
     * 其它新增
     *
     * @param req
     * @return
     */
    @PostMapping("other/save")
    @UnAuth
    public Rest<Void> otherSave(@RequestBody OtherSaveReq req) {
        log.info("web Api req==>{}", JSON.toJSONString(req));
        String busiDesc = "外部其它绩效新增";
        Rest rest = Rest.ok(busiDesc + "成功！");
        try {
            if (ObjectUtils.isEmpty(req.getPhone())) throw new AppException("手机号码不能为空！");
            if (ObjectUtils.isEmpty(req.getRuleCode())) throw new AppException("绩效规则编码不能为空！");
            boolean bool = other(req);
            if (bool) throw new AppException(busiDesc + "失败！");
        } catch (Exception e) {
            log.error(busiDesc, e);
            return Rest.fail(e.getMessage());
        }
        log.info("响应【{}】【响应体】--> {} ", busiDesc, JSONObject.toJSONString(rest));
        return rest;
    }


    private boolean attend(AttendSaveReq req) throws AppException {
        //考勤保存
        //通过手机号码查询员工属性
        PerformRulesEntity rule = getRule(req.getRuleCode());
        StaffEntity staffEntity = getStaff(req.getPhone());

        PerformAttendRecordEntity recordEntity = new PerformAttendRecordEntity();
        recordEntity.initAttrValue();
        BeanUtils.copyProperties(req, recordEntity);
        recordEntity.setStaffId(staffEntity.getId());
        recordEntity.setStaffName(staffEntity.getName());
        recordEntity.setDeptId(staffEntity.getDeptId());
        recordEntity.setDeptName(staffEntity.getDeptName());

        recordEntity.setSubAddType(rule.getSubAddType());
        recordEntity.setScore(rule.getScore());
        recordEntity.setRuleId(rule.getId());
        recordEntity.setRuleName(rule.getName());
        recordEntity.setCategoryId(rule.getCategoryId());
        recordEntity.setCategoryName(rule.getCategoryName());
        recordEntity.setDeductPerson("系统管理员");
        recordEntity.setDeductTime(new Date());
        recordEntity.setCreateUserId(1L);
        recordEntity.setCreateTime(new Date());
        recordEntity.setWorkNum(staffEntity.getWorkNum());
        PerformRulesCategoryEntity categoryEntity = categoryService.get(rule.getCategoryId());
        recordEntity.setCategoryName(categoryEntity == null ? "" : categoryEntity.getName());


        PerformAttendRecordEntity save = attendRecordService.save(recordEntity);

        return save.newEntity();

    }


    private boolean review(ReviewSaveReq req) throws AppException {
        //评价保存
        StaffEntity staffEntity = getStaff(req.getPhone());
        PerformRulesEntity rule = getRule(req.getRuleCode());

        PerformReviewRecordEntity recordEntity = new PerformReviewRecordEntity();

        recordEntity.initAttrValue();
        BeanUtils.copyProperties(req, recordEntity);
        recordEntity.setStaffId(staffEntity.getId());
        recordEntity.setStaffName(staffEntity.getName());
        recordEntity.setDeptId(staffEntity.getDeptId());
        recordEntity.setDeptName(staffEntity.getDeptName());
        recordEntity.setSubAddType(rule.getSubAddType());
        recordEntity.setScore(rule.getScore());
        recordEntity.setRuleId(rule.getId());
        recordEntity.setRuleName(rule.getName());
        recordEntity.setCategoryId(rule.getCategoryId());
        recordEntity.setCategoryName(rule.getCategoryName());
        recordEntity.setDeductPerson("系统管理员");
        recordEntity.setDeductTime(new Date());
        recordEntity.setCreateUserId(1L);
        recordEntity.setCreateTime(new Date());
        recordEntity.setWorkNum(staffEntity.getWorkNum());
        recordEntity.setWindowNum(req.getWindowNum());
        PerformRulesCategoryEntity categoryEntity = categoryService.get(rule.getCategoryId());
        recordEntity.setCategoryName(categoryEntity == null ? "" : categoryEntity.getName());
        PerformReviewRecordEntity save = reviewRecordService.save(recordEntity);
        return save.newEntity();

    }

    private boolean complain(ComplainSaveReq req) throws AppException {
        //投诉保存
        StaffEntity staffEntity = getStaff(req.getPhone());
        PerformRulesEntity rule = getRule(req.getRuleCode());

        PerformComplainRecordEntity recordEntity = new PerformComplainRecordEntity();

        recordEntity.initAttrValue();
        BeanUtils.copyProperties(req, recordEntity);
        recordEntity.setStaffId(staffEntity.getId());
        recordEntity.setStaffName(staffEntity.getName());
        recordEntity.setDeptId(staffEntity.getDeptId());
        recordEntity.setDeptName(staffEntity.getDeptName());
        recordEntity.setSubAddType(rule.getSubAddType());
        recordEntity.setScore(rule.getScore());
        recordEntity.setRuleId(rule.getId());
        recordEntity.setRuleName(rule.getName());
        recordEntity.setCategoryId(rule.getCategoryId());
        recordEntity.setCategoryName(rule.getCategoryName());
        recordEntity.setDeductPerson("系统管理员");
        recordEntity.setDeductTime(new Date());
        recordEntity.setWorkNum(staffEntity.getWorkNum());

        recordEntity.setCreateUserId(1L);
        recordEntity.setCreateTime(new Date());
        PerformRulesCategoryEntity categoryEntity = categoryService.get(rule.getCategoryId());
        recordEntity.setCategoryName(categoryEntity == null ? "" : categoryEntity.getName());
        PerformComplainRecordEntity save = complainRecordService.save(recordEntity);
        return save.newEntity();

    }

    private boolean gowork(GoWorkSaveReq req) throws AppException {
        //办件保存
        StaffEntity staffEntity = getStaff(req.getPhone());
        PerformRulesEntity rule = getRule(req.getRuleCode());

        PerformGoworkRecordEntity recordEntity = new PerformGoworkRecordEntity();

        recordEntity.initAttrValue();
        BeanUtils.copyProperties(req, recordEntity);
        recordEntity.setStaffId(staffEntity.getId());
        recordEntity.setStaffName(staffEntity.getName());
        recordEntity.setDeptId(staffEntity.getDeptId());
        recordEntity.setDeptName(staffEntity.getDeptName());
        recordEntity.setSubAddType(rule.getSubAddType());
        recordEntity.setScore(rule.getScore());
        recordEntity.setRuleId(rule.getId());
        recordEntity.setRuleName(rule.getName());
        recordEntity.setCategoryId(rule.getCategoryId());
        recordEntity.setCategoryName(rule.getCategoryName());
        recordEntity.setDeductPerson("系统管理员");
        recordEntity.setDeductTime(new Date());

        recordEntity.setWorkNum(staffEntity.getWorkNum());

        recordEntity.setCreateUserId(1L);
        recordEntity.setCreateTime(new Date());
        PerformRulesCategoryEntity categoryEntity = categoryService.get(rule.getCategoryId());
        recordEntity.setCategoryName(categoryEntity == null ? "" : categoryEntity.getName());
        PerformGoworkRecordEntity save = performGoworkRecordService.save(recordEntity);
        return save.newEntity();

    }

    private boolean effect(EffectSaveReq req) throws AppException {
        //效能保存
        StaffEntity staffEntity = getStaff(req.getPhone());
        PerformRulesEntity rule = getRule(req.getRuleCode());

        PerformEffectRecordEntity recordEntity = new PerformEffectRecordEntity();

        recordEntity.initAttrValue();
        BeanUtils.copyProperties(req, recordEntity);
        recordEntity.setStaffId(staffEntity.getId());
        recordEntity.setStaffName(staffEntity.getName());
        recordEntity.setDeptId(staffEntity.getDeptId());
        recordEntity.setDeptName(staffEntity.getDeptName());
        recordEntity.setSubAddType(rule.getSubAddType());
        recordEntity.setScore(rule.getScore());
        recordEntity.setRuleId(rule.getId());
        recordEntity.setRuleName(rule.getName());
        recordEntity.setCategoryId(rule.getCategoryId());
        recordEntity.setCategoryName(rule.getCategoryName());
        recordEntity.setDeductPerson("系统管理员");
        recordEntity.setDeductTime(new Date());
        recordEntity.setWorkNum(staffEntity.getWorkNum());

        recordEntity.setWindowNum(req.getWindowNum());

        recordEntity.setCreateUserId(1L);
        recordEntity.setCreateTime(new Date());
        PerformRulesCategoryEntity categoryEntity = categoryService.get(rule.getCategoryId());
        recordEntity.setCategoryName(categoryEntity == null ? "" : categoryEntity.getName());
        PerformEffectRecordEntity save = effectRecordService.save(recordEntity);
        return save.newEntity();
    }

    private boolean other(OtherSaveReq req) throws AppException {
        //其它保存

        StaffEntity staffEntity = getStaff(req.getPhone());
        PerformRulesEntity rule = getRule(req.getRuleCode());

        PerformOtherRecordEntity recordEntity = new PerformOtherRecordEntity();

        recordEntity.initAttrValue();
        BeanUtils.copyProperties(req, recordEntity);
        recordEntity.setStaffId(staffEntity.getId());
        recordEntity.setStaffName(staffEntity.getName());
        recordEntity.setDeptId(staffEntity.getDeptId());
        recordEntity.setDeptName(staffEntity.getDeptName());
        recordEntity.setSubAddType(rule.getSubAddType());
        recordEntity.setScore(rule.getScore());
        recordEntity.setRuleId(rule.getId());
        recordEntity.setRuleName(rule.getName());
        recordEntity.setCategoryId(rule.getCategoryId());
        recordEntity.setCategoryName(rule.getCategoryName());
        recordEntity.setWorkNum(staffEntity.getWorkNum());

        recordEntity.setDeductPerson("系统管理员");
        recordEntity.setDeductTime(new Date());

        recordEntity.setCreateUserId(1L);
        recordEntity.setCreateTime(new Date());
        PerformRulesCategoryEntity categoryEntity = categoryService.get(rule.getCategoryId());
        recordEntity.setCategoryName(categoryEntity == null ? "" : categoryEntity.getName());
        PerformOtherRecordEntity save = otherRecordService.save(recordEntity);
        return save.newEntity();
    }


    private PerformRulesEntity getRule(String ruleCode) {
        PerformRulesEntity rule = rulesService.getCache(ruleCode);
        if (ObjectUtils.isEmpty(rule))
            throw new AppException(String.format("当前手机号码未找到匹配的员工！rule:%s", ruleCode));

        return rule;
    }

    private StaffEntity getStaff(String phone) {
        StaffEntity staffEntity = staffService.selectOne(new StaffQuery().phoneNumber(phone));
        if (ObjectUtils.isEmpty(staffEntity))
            throw new AppException(String.format("当前手机号码未找到匹配的员工！phone:%s", phone));

        return staffEntity;
    }


    public static void main(String[] args) {

    }

}
