package com.mortals.xhx.module.perform.service.impl;
import com.mortals.xhx.common.code.*;
import com.mortals.xhx.common.utils.BeanUtil;
import com.mortals.xhx.module.check.model.CheckOtherRecordEntity;
import com.mortals.xhx.module.check.service.CheckOtherRecordService;
import com.mortals.xhx.module.perform.model.PerformGoworkRecordEntity;
import com.mortals.xhx.module.perform.model.PerformRulesEntity;
import com.mortals.xhx.module.perform.service.PerformRulesService;
import com.mortals.xhx.module.perform.service.PerformStaffConfService;
import com.mortals.xhx.module.staff.model.StaffEntity;
import com.mortals.xhx.module.staff.service.StaffService;
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.perform.dao.PerformOtherRecordDao;
import com.mortals.xhx.module.perform.model.PerformOtherRecordEntity;
import com.mortals.xhx.module.perform.service.PerformOtherRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;

import java.util.Date;

/**
* PerformOtherRecordService
* 其它绩效记录信息 service实现
*
* @author zxfei
* @date 2023-07-11
*/
@Service("performOtherRecordService")
@Slf4j
public class PerformOtherRecordServiceImpl extends AbstractCRUDServiceImpl<PerformOtherRecordDao, PerformOtherRecordEntity, Long> implements PerformOtherRecordService {

    @Autowired
    private CheckOtherRecordService checkOtherRecordService;

    @Autowired
    private PerformRulesService rulesService;
    @Autowired
    private StaffService staffService;
    @Autowired
    private PerformStaffConfService performStaffConfService;

    @Override
    protected void saveBefore(PerformOtherRecordEntity entity, Context context) throws AppException {
        updateStaffRuleNames(entity,context);
    }

    @Override
    protected void updateBefore(PerformOtherRecordEntity entity, Context context) throws AppException {
        updateStaffRuleNames(entity,context);
    }


    private void updateStaffRuleNames(PerformOtherRecordEntity entity, Context context) {
        if (!ObjectUtils.isEmpty(entity.getRuleId())) {
            PerformRulesEntity rulesEntity = rulesService.getCache(entity.getRuleId().toString());
            entity.setRuleName(rulesEntity.getName());
            entity.setRuleDesc(rulesEntity.getContent());
            entity.setSubAddType(rulesEntity == null ? 1 : rulesEntity.getSubAddType());
        }
        if (!ObjectUtils.isEmpty(entity.getStaffId())) {
            StaffEntity staffCache = staffService.getCache(entity.getStaffId().toString());
            entity.setStaffName(staffCache == null ? "" : staffCache.getName());
            entity.setDeptName(staffCache == null ? "" : staffCache.getDeptName());
            entity.setSalaId(staffCache == null ? null : staffCache.getSalaId());
            entity.setSalaName(staffCache == null ? "" : staffCache.getSalaName());
        }

        entity.setDeductTime(new Date());

        if(ObjectUtils.isEmpty(entity.getDeductPerson())){
            entity.setDeductPerson(context == null ? "" : context.getUser() == null ? "" : context.getUser().getRealName());
        }

    }

    @Override
    protected void saveAfter(PerformOtherRecordEntity entity, Context context) throws AppException {
        CheckOtherRecordEntity checkOtherRecordEntity = new CheckOtherRecordEntity();
        checkOtherRecordEntity.initAttrValue();
        BeanUtils.copyProperties(entity,checkOtherRecordEntity, BeanUtil.getNullPropertyNames(entity));
        checkOtherRecordEntity.setId(null);
        checkOtherRecordEntity.setRecordId(entity.getId());
        if(performStaffConfService.checkRules(entity.getStaffId(),entity.getDeptId(),entity.getRuleId())){
            checkOtherRecordEntity.setCheckStatus(CheckStatusEnum.已处理.getValue());
            if(checkOtherRecordEntity.getSubAddType()== SubAddTypeEnum.增加.getValue()){
                checkOtherRecordEntity.setCheckResult(CheckResultAddEnum.核查加分.getValue());
            }else {
                checkOtherRecordEntity.setCheckResult(CheckResultSubEnum.核查扣分.getValue());
            }
            checkOtherRecordEntity.setUpdateTime(new Date());
        }else {
            checkOtherRecordEntity.setCheckStatus(CheckStatusEnum.未处理.getValue());      //非自动扣分相设置为未处理
        }
        checkOtherRecordService.save(checkOtherRecordEntity,context);
    }

    @Override
    public void updateProcessStatus(Long id, Integer status) throws AppException {
        if(id==null){
            throw new AppException("其它绩效记录Id不能为空");
        }
        PerformOtherRecordEntity entity = this.get(id);
        if(entity==null){
            throw new AppException("无效的其它绩效记录Id");
        }
        if(status==null){
            status=1; //处理状态(1.未核查,2.已核查)
        }
        PerformOtherRecordEntity update = new PerformOtherRecordEntity();
        update.setId(id);
        update.setProcessStatus(status);
        update.setUpdateTime(new Date());
        dao.update(update);
    }

}