package com.mortals.xhx.module.check.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.service.ICacheService;
import com.mortals.framework.service.IUser;
import com.mortals.framework.util.StringUtils;
import com.mortals.xhx.base.system.param.service.ParamService;
import com.mortals.xhx.common.code.CheckStatusEnum;
import com.mortals.xhx.common.code.CheckTypeEnum;
import com.mortals.xhx.common.pdu.WeightPdu;
import com.mortals.xhx.common.utils.BeanUtil;
import com.mortals.xhx.module.check.model.CheckWindowPerformEntity;
import com.mortals.xhx.module.check.model.vo.StaffCheckSummaryQuery;
import com.mortals.xhx.module.check.model.vo.StaffCheckSummaryVo;
import com.mortals.xhx.module.dept.model.DeptEntity;
import com.mortals.xhx.module.dept.model.DeptPerformStatEntity;
import com.mortals.xhx.module.dept.model.DeptPerformStatQuery;
import com.mortals.xhx.module.staff.model.StaffPerformStatEntity;
import com.mortals.xhx.module.staff.model.StaffPerformStatQuery;
import com.mortals.xhx.module.staff.model.StaffPerformSummaryEntity;
import com.mortals.xhx.module.staff.model.StaffPerformSummaryQuery;
import com.mortals.xhx.module.staff.service.StaffPerformSummaryService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.xhx.module.check.dao.CheckWindowWorkmanPerformDao;
import com.mortals.xhx.module.check.model.CheckWindowWorkmanPerformEntity;
import com.mortals.xhx.module.check.service.CheckWindowWorkmanPerformService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import static com.mortals.xhx.common.key.ParamKey.SYS_PARAM_WEIGHT;
import static com.mortals.xhx.common.key.RedisKey.KEY_CHECK_SUMMARY_CACHE;

/**
* CheckWindowWorkmanPerformService
* 窗口人员考核汇总核查 service实现
*
* @author zxfei
* @date 2024-01-17
*/
@Service("checkWindowWorkmanPerformService")
@Slf4j
public class CheckWindowWorkmanPerformServiceImpl extends AbstractCRUDServiceImpl<CheckWindowWorkmanPerformDao, CheckWindowWorkmanPerformEntity, Long> implements CheckWindowWorkmanPerformService {

    @Autowired
    private StaffPerformSummaryService staffPerformSummaryService;
    @Autowired
    private ParamService paramService;
    @Autowired
    private ICacheService cacheService;

    @Override
    public void examine(CheckWindowWorkmanPerformEntity entity, Context context) throws AppException {
        if (entity.getId() == null) {
            throw new AppException("核查记录ID不能为空");
        }
        CheckWindowWorkmanPerformEntity temp = this.get(entity.getId());
        if (temp == null) {
            throw new AppException("核查记录ID不正确");
        }
        if (context != null && context.getUser() != null) {
            IUser user = context.getUser();
            entity.setUpdateUserId(user.getId());
            entity.setManageCheckPerson(user.getRealName());
        }

        entity.setManageCheckTime(new Date());
        entity.setUpdateTime(new Date());
        entity.setCheckStatus(CheckStatusEnum.已处理.getValue()); //处理状态(1.未处理,2.已处理)
        dao.update(entity);
        StaffCheckSummaryQuery query = new StaffCheckSummaryQuery();
        query.setRecordId(temp.getRecordId());
        query.setYear(temp.getYear());
        query.setMonth(temp.getMonth());
        query.setCheckType(CheckTypeEnum.其它绩效.getValue());
        cacheService.lpush(KEY_CHECK_SUMMARY_CACHE, query);
        //summaryCheck(query);
    }

    @Override
    public List<StaffCheckSummaryVo> summaryCheck(StaffCheckSummaryQuery query) throws AppException {
        List<StaffCheckSummaryVo> summaryVoList = dao.summaryCheck(query);
        if (CollectionUtils.isNotEmpty(summaryVoList)) {
            String value = paramService.getValueByKey(SYS_PARAM_WEIGHT);
            WeightPdu weightPdu;
            if (ObjectUtils.isEmpty(value)){
                weightPdu = new WeightPdu();
            }else {
                weightPdu = JSONObject.parseObject(value,WeightPdu.class);
            }
            for (StaffCheckSummaryVo vo : summaryVoList) {
                StaffPerformSummaryEntity staffPerformSummaryEntity = new StaffPerformSummaryEntity();
                staffPerformSummaryEntity.initAttrValue();
                BeanUtils.copyProperties(vo, staffPerformSummaryEntity, BeanUtil.getNullPropertyNames(vo));
                staffPerformSummaryEntity.setOtherScore(vo.getSumScore());
                StaffPerformSummaryQuery summaryQuery = new StaffPerformSummaryQuery();
                summaryQuery.setStaffId(vo.getStaffId());
                summaryQuery.setYear(vo.getYear());
                summaryQuery.setMonth(vo.getMonth());
                StaffPerformSummaryEntity temp = staffPerformSummaryService.selectOne(summaryQuery);
                if (temp != null) {
                    if (temp.getReviewScore() != null) {
                        staffPerformSummaryEntity.setReviewScore(temp.getReviewScore());
                    }
                    if (temp.getAttendScore() != null) {
                        staffPerformSummaryEntity.setAttendScore(temp.getAttendScore());
                    }
                    if (temp.getGoworkScore() != null) {
                        staffPerformSummaryEntity.setGoworkScore(temp.getGoworkScore());
                    }
                    if (temp.getEffectScore() != null) {
                        staffPerformSummaryEntity.setEffectScore(temp.getEffectScore());
                    }
                    if (temp.getComplainScore() != null) {
                        staffPerformSummaryEntity.setComplainScore(temp.getComplainScore());
                    }
                    BigDecimal erro = new BigDecimal(0);
                    erro = erro.add(staffPerformSummaryEntity.getReviewScore());
                    erro = erro.add(staffPerformSummaryEntity.getAttendScore());
                    //erro = erro.add(staffPerformSummaryEntity.getOtherScore());
                    erro = erro.add(staffPerformSummaryEntity.getGoworkScore());
                    erro = erro.add(staffPerformSummaryEntity.getEffectScore());
                    erro = erro.add(staffPerformSummaryEntity.getComplainScore());
                    staffPerformSummaryEntity.setErrorScore(erro);
                    computeSummary(staffPerformSummaryEntity,weightPdu);
                    staffPerformSummaryEntity.setId(temp.getId());
                    staffPerformSummaryEntity.setUpdateTime(new Date());
                    staffPerformSummaryService.update(staffPerformSummaryEntity);
                    staffPerformSummaryEntity.setId(temp.getId());
                    staffPerformSummaryEntity.setUpdateTime(new Date());
                    staffPerformSummaryService.update(staffPerformSummaryEntity);
                } else {
                    staffPerformSummaryEntity.setAttendScore(new BigDecimal(0));
                    staffPerformSummaryEntity.setReviewScore(new BigDecimal(0));
                    staffPerformSummaryEntity.setGoworkScore(new BigDecimal(0));
                    staffPerformSummaryEntity.setEffectScore(new BigDecimal(0));
                    staffPerformSummaryEntity.setComplainScore(new BigDecimal(0));
                    staffPerformSummaryEntity.setErrorScore(vo.getSumScore());
                    computeSummary(staffPerformSummaryEntity,weightPdu);
                    staffPerformSummaryEntity.setCreateUserId(1l);
                    staffPerformSummaryEntity.setCreateTime(new Date());
                    staffPerformSummaryService.save(staffPerformSummaryEntity);
                }
            }
        }

        return summaryVoList;
    }

    private void computeSummary(StaffPerformSummaryEntity staffPerformSummaryEntity,WeightPdu weightPdu){
        BigDecimal total = new BigDecimal(100);
        BigDecimal reviewScore = total.add(staffPerformSummaryEntity.getReviewScore());  //评价
        reviewScore = reviewScore.multiply(weightPdu.reviewWeight());
        BigDecimal attendScore = total.add(staffPerformSummaryEntity.getAttendScore()); //考勤
        attendScore = attendScore.multiply(weightPdu.attendWeight());
        BigDecimal otherScore = staffPerformSummaryEntity.getOtherScore();  //自评不用加100
        otherScore = otherScore.multiply(weightPdu.selfWeight());
        BigDecimal goworkScore = total.add(staffPerformSummaryEntity.getGoworkScore());  //办件
        goworkScore = goworkScore.multiply(weightPdu.goworkWeight());
        BigDecimal effectScore = total.add(staffPerformSummaryEntity.getEffectScore());  //效能
        effectScore = effectScore.multiply(weightPdu.effectWeight());
//        BigDecimal complainScore = complainScore = total.add(staffPerformSummaryEntity.getComplainScore());  //投诉
        BigDecimal summary = new BigDecimal(0);
        summary = summary.add(reviewScore);
        summary = summary.add(attendScore);
        summary = summary.add(otherScore);
        summary = summary.add(goworkScore);
        summary = summary.add(effectScore);
        staffPerformSummaryEntity.setTotalScore(summary);
    }

}