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

import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.framework.service.IUser;
import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
import com.mortals.framework.util.StringUtils;
import com.mortals.xhx.common.code.CheckStatusEnum;
import com.mortals.xhx.module.check.dao.CheckWindowPerformDao;
import com.mortals.xhx.module.check.model.CheckWindowPerformEntity;
import com.mortals.xhx.module.check.service.CheckWindowPerformService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
* CheckWindowPerformService
* 窗口考核核查 service实现
*
* @author zxfei
* @date 2024-01-17
*/
@Service("checkWindowPerformService")
@Slf4j
public class CheckWindowPerformServiceImpl extends AbstractCRUDServiceImpl<CheckWindowPerformDao, CheckWindowPerformEntity, Long> implements CheckWindowPerformService {

    @Override
    public void examine(CheckWindowPerformEntity entity, Context context) throws AppException {
        if (entity.getId() == null) {
            throw new AppException("核查记录ID不能为空");
        }
        CheckWindowPerformEntity temp = this.get(entity.getId());
        if (temp == null) {
            throw new AppException("核查记录ID不正确");
        }
        if(StringUtils.isNotEmpty(entity.getLeaderCheckResult())){
            if (context != null && context.getUser() != null) {
                IUser user = context.getUser();
                entity.setUpdateUserId(user.getId());
                entity.setLeaderCheckPerson(user.getRealName());
            }
            entity.setLeaderCheckTime(new Date());
        }
        if(StringUtils.isNotEmpty(entity.getManageCheckResult())){
            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());
        if(StringUtils.isNotEmpty(entity.getLeaderCheckResult())&&StringUtils.isNotEmpty(entity.getManageCheckResult())) {
            entity.setCheckStatus(CheckStatusEnum.已处理.getValue()); //处理状态(1.未处理,2.已处理)
        }
        dao.update(entity);
    }
}