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.CheckGoworkRecordEntity;
import com.mortals.xhx.module.check.service.CheckGoworkRecordService;
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.PerformGoworkRecordDao;
import com.mortals.xhx.module.perform.model.PerformGoworkRecordEntity;
import com.mortals.xhx.module.perform.service.PerformGoworkRecordService;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;

/**
* PerformGoworkRecordService
* 办件绩效记录信息 service实现
*
* @author zxfei
* @date 2023-07-11
*/
@Service("performGoworkRecordService")
@Slf4j
public class PerformGoworkRecordServiceImpl extends AbstractCRUDServiceImpl<PerformGoworkRecordDao, PerformGoworkRecordEntity, Long> implements PerformGoworkRecordService {

    @Autowired
    private CheckGoworkRecordService checkGoworkRecordService;

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

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

}