package com.mortals.xhx.module.staff.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.framework.model.PageInfo;
import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
import com.mortals.framework.util.StringUtils;
import com.mortals.xhx.base.system.param.service.ParamService;
import com.mortals.xhx.common.pdu.WeightPdu;
import com.mortals.xhx.common.utils.StaffPerformUtil;
import com.mortals.xhx.module.staff.dao.StaffPerformSummaryDao;
import com.mortals.xhx.module.staff.model.StaffEntity;
import com.mortals.xhx.module.staff.model.StaffPerformSummaryEntity;
import com.mortals.xhx.module.staff.service.StaffPerformSummaryService;
import com.mortals.xhx.module.staff.service.StaffService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.List;

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

/**
* StaffPerformSummaryService
* 员工绩效统计 service实现
*
* @author zxfei
* @date 2023-07-05
*/
@Service("staffPerformSummaryService")
@Slf4j
public class StaffPerformSummaryServiceImpl extends AbstractCRUDServiceImpl<StaffPerformSummaryDao, StaffPerformSummaryEntity, Long> implements StaffPerformSummaryService {

    @Autowired
    private StaffService staffService;
    @Autowired
    private ParamService paramService;

    @Override
    protected StaffPerformSummaryEntity findBefore(StaffPerformSummaryEntity params, PageInfo pageInfo, Context context) throws AppException {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        if(params.getYear()==null){
            params.setYear(year);
        }
        if(params.getMonth()==null){
            params.setMonth(month);
        }
        return params;
    }

    @Override
    protected void findAfter(StaffPerformSummaryEntity params, PageInfo pageInfo, Context context, List<StaffPerformSummaryEntity> list) throws AppException {
        if(CollectionUtils.isNotEmpty(list)){
            String value = paramService.getValueByKey(SYS_PARAM_WEIGHT);
            WeightPdu weightPdu;
            if (ObjectUtils.isEmpty(value)){
                weightPdu = new WeightPdu();
            }else {
                weightPdu = JSONObject.parseObject(value,WeightPdu.class);
            }
            for(StaffPerformSummaryEntity item:list) {
                StaffEntity staffEntity = staffService.getCache(item.getStaffId().toString());
                if(staffEntity!=null){
                    item.setWorkNum(staffEntity.getWorkNum());
                    item.setPhoneNumber(staffEntity.getPhoneNumber());
                }
                computeSummary(item,weightPdu);
                if(StringUtils.isNotEmpty(item.getRemarkAdd())){
                    List<JSONObject> jList = JSONArray.parseArray(item.getRemarkAdd(),JSONObject.class);
                    String desc = "";
                    for (JSONObject jsonObject:jList){
                        desc+=jsonObject.getString("name")+"("+jsonObject.getString("score")+");";
                    }
                    item.setRemarkAddDesc(desc);
                }
            }
        }
    }

    private void computeSummary(StaffPerformSummaryEntity staffPerformSummaryEntity, WeightPdu weightPdu){

        BigDecimal reviewScore = weightPdu.reviewWeight().add(staffPerformSummaryEntity.getReviewScore());  //评价
        if(reviewScore.compareTo(BigDecimal.ZERO)<0){
            reviewScore = BigDecimal.ZERO;
        }
        staffPerformSummaryEntity.setReviewScore(reviewScore.setScale(2,BigDecimal.ROUND_DOWN));
        BigDecimal attendScore = weightPdu.attendWeight().add(staffPerformSummaryEntity.getAttendScore()); //考勤
        if(attendScore.compareTo(BigDecimal.ZERO)<0){
            attendScore = BigDecimal.ZERO;
        }
        staffPerformSummaryEntity.setAttendScore(attendScore.setScale(2,BigDecimal.ROUND_DOWN));
        BigDecimal complainScore = staffPerformSummaryEntity.getComplainScore();  //自评不用加100
        if(complainScore.compareTo(BigDecimal.ZERO)==0){
            complainScore = StaffPerformUtil.SCORE100;
        }
        complainScore = complainScore.multiply(weightPdu.selfWeight());
        staffPerformSummaryEntity.setComplainScore(complainScore.setScale(2,BigDecimal.ROUND_DOWN));
        BigDecimal goworkScore = weightPdu.goworkWeight().add(staffPerformSummaryEntity.getGoworkScore());  //办件
        if(goworkScore.compareTo(BigDecimal.ZERO)<0){
            goworkScore = BigDecimal.ZERO;
        }
        staffPerformSummaryEntity.setGoworkScore(goworkScore.setScale(2,BigDecimal.ROUND_DOWN));
        BigDecimal effectScore = weightPdu.effectWeight().add(staffPerformSummaryEntity.getEffectScore());  //效能
        if(effectScore.compareTo(BigDecimal.ZERO)<0){
            effectScore = BigDecimal.ZERO;
        }
        staffPerformSummaryEntity.setEffectScore(effectScore.setScale(2,BigDecimal.ROUND_DOWN));

    }

    @Override
    protected void updateBefore(StaffPerformSummaryEntity entity, Context context) throws AppException {
        super.updateBefore(entity,context);
        if(entity.getAddTotalScore()!=null){
            entity.setSumScore(entity.getTotalScore().add(entity.getAddTotalScore()));
        }
    }
}
