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

import com.mortals.xhx.common.code.CheckStatusEnum;
import com.mortals.xhx.common.code.SubMethodEnum;
import com.mortals.xhx.common.utils.BeanUtil;
import com.mortals.xhx.module.check.model.CheckAttendRecordEntity;
import com.mortals.xhx.module.check.service.CheckAttendRecordService;
import com.mortals.xhx.module.dept.model.DeptEntity;
import com.mortals.xhx.module.dept.service.DeptService;
import com.mortals.xhx.module.perform.model.PerformRulesEntity;
import com.mortals.xhx.module.perform.service.PerformRulesService;
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.PerformAttendRecordDao;
import com.mortals.xhx.module.perform.model.PerformAttendRecordEntity;
import com.mortals.xhx.module.perform.service.PerformAttendRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;

import java.util.Date;

/**
 * PerformAttendRecordService
 * 考勤绩效记录信息 service实现
 *
 * @author zxfei
 * @date 2023-07-10
 */
@Service("performAttendRecordService")
@Slf4j
public class PerformAttendRecordServiceImpl extends AbstractCRUDServiceImpl<PerformAttendRecordDao, PerformAttendRecordEntity, Long> implements PerformAttendRecordService {

    @Autowired
    private CheckAttendRecordService checkAttendRecordService;
    @Autowired
    private DeptService deptService;
    @Autowired
    private PerformRulesService rulesService;
    @Autowired
    private StaffService staffService;

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

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


    private void updateStaffRuleNames(PerformAttendRecordEntity entity) {
        if (!ObjectUtils.isEmpty(entity.getRuleId())) {
            PerformRulesEntity rulesEntity = rulesService.getCache(entity.getRuleId().toString());
            entity.setRuleName(rulesEntity.getName());
        }
        if (!ObjectUtils.isEmpty(entity.getStaffId())) {
            StaffEntity staffCache = staffService.getCache(entity.getStaffId().toString());
            entity.setStaffName(staffCache == null ? "" : staffCache.getName());
            entity.setDeptName(staffCache == null ? "" : staffCache.getDeptName());
        }
    }

    @Override
    protected void saveAfter(PerformAttendRecordEntity entity, Context context) throws AppException {
        CheckAttendRecordEntity checkAttendRecordEntity = new CheckAttendRecordEntity();
        checkAttendRecordEntity.initAttrValue();
        BeanUtils.copyProperties(entity, checkAttendRecordEntity, BeanUtil.getNullPropertyNames(entity));
        checkAttendRecordEntity.setId(null);
        checkAttendRecordEntity.setRecordId(entity.getId());
        if (entity.getSubMethod() == SubMethodEnum.系统自动.getValue()) {
            checkAttendRecordEntity.setCheckStatus(CheckStatusEnum.已处理.getValue());     //自动扣分相设置为已处理
            checkAttendRecordEntity.setCheckTime(new Date());
        } else {
            checkAttendRecordEntity.setCheckStatus(CheckStatusEnum.未处理.getValue());      //非自动扣分相设置为未处理
        }
        checkAttendRecordService.save(checkAttendRecordEntity, context);
    }

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

}