package cn.iocoder.yudao.module.pms.service.qhse.emergency;

import cn.iocoder.yudao.framework.common.exception.ErrorCode;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.pms.controller.admin.qhse.emergency.vo.QhseEmergencyCertPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.qhse.emergency.vo.QhseEmergencyCertSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.qhse.emergency.QhseEmergencyCertDO;
import cn.iocoder.yudao.module.pms.dal.mysql.qhse.emergency.QhseEmergencyCertMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * QHSE应急物资检测证书 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class QhseEmergencyCertServiceImpl implements QhseEmergencyCertService {

    @Resource
    private QhseEmergencyCertMapper qhseEmergencyCertMapper;

    @Override
    public Long createQhseEmergencyCert(QhseEmergencyCertSaveReqVO createReqVO) {
        // 插入
        QhseEmergencyCertDO qhseEmergencyCert = BeanUtils.toBean(createReqVO, QhseEmergencyCertDO.class);
        qhseEmergencyCert.setDeleted(false);
        qhseEmergencyCert.setExpired(false);
        qhseEmergencyCert.setAlertWarn(false);
        LocalDateTime certExpire = qhseEmergencyCert.getCertExpire();
        if (certExpire.isBefore(LocalDateTime.now())) {
            qhseEmergencyCert.setExpired(true);
        } else {
            qhseEmergencyCert.setExpired(false);
        }
        //如果相差不到90天了就进行预警
        if (certExpire.isAfter(LocalDateTime.now()) && ChronoUnit.DAYS.between(LocalDateTime.now(), certExpire) < 90) {
            qhseEmergencyCert.setAlertWarn(true);
            qhseEmergencyCert.setExpired(false);
        }
        qhseEmergencyCertMapper.insert(qhseEmergencyCert);
        // 返回
        return qhseEmergencyCert.getId();
    }

    @Override
    public void updateQhseEmergencyCert(QhseEmergencyCertSaveReqVO updateReqVO) {
        // 校验存在
        validateQhseEmergencyCertExists(updateReqVO.getId());
        // 更新
        QhseEmergencyCertDO updateObj = BeanUtils.toBean(updateReqVO, QhseEmergencyCertDO.class);
        LocalDateTime certExpire = updateObj.getCertExpire();
        if (certExpire.isBefore(LocalDateTime.now())) {
            updateObj.setExpired(true);
        } else {
            updateObj.setExpired(false);
        }
        //如果相差不到90天了就进行预警
        if (certExpire.isAfter(LocalDateTime.now()) && ChronoUnit.DAYS.between(LocalDateTime.now(), certExpire) < 90) {
            updateObj.setAlertWarn(true);
            updateObj.setExpired(false);
        }
        qhseEmergencyCertMapper.updateById(updateObj);
    }

    @Override
    public void deleteQhseEmergencyCert(Long id) {
        // 校验存在
        validateQhseEmergencyCertExists(id);
        // 删除
        qhseEmergencyCertMapper.deleteById(id);
    }

    private void validateQhseEmergencyCertExists(Long id) {
        if (qhseEmergencyCertMapper.selectById(id) == null) {
            throw exception(new ErrorCode(3, "不存在"));
        }
    }

    @Override
    public QhseEmergencyCertDO getQhseEmergencyCert(Long id) {
        return qhseEmergencyCertMapper.selectById(id);
    }

    @Override
    public PageResult<QhseEmergencyCertDO> getQhseEmergencyCertPage(QhseEmergencyCertPageReqVO pageReqVO) {
        return qhseEmergencyCertMapper.selectPage(pageReqVO);
    }

}