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

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.devicecert.vo.QhseDeviceCertPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.qhse.devicecert.vo.QhseDeviceCertSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.qhse.devicecert.QhseDeviceCertDO;
import cn.iocoder.yudao.module.pms.dal.mysql.qhse.devicecert.QhseDeviceCertMapper;
import cn.iocoder.yudao.module.system.service.dept.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
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 java.util.HashSet;
import java.util.Objects;
import java.util.Set;

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

/**
 * QHSE应检设备证书 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class QhseDeviceCertServiceImpl implements QhseDeviceCertService {

    @Resource
    private QhseDeviceCertMapper qhseDeviceCertMapper;
    @Autowired
    private DeptService deptService;

    @Override
    public Long createQhseDeviceCert(QhseDeviceCertSaveReqVO createReqVO) {
        // 插入
        QhseDeviceCertDO qhseDeviceCert = BeanUtils.toBean(createReqVO, QhseDeviceCertDO.class);
        qhseDeviceCert.setDeleted(false);
        qhseDeviceCert.setExpired(false);
        qhseDeviceCert.setAlertWarn(false);
        LocalDateTime certExpire = qhseDeviceCert.getCertExpire();
        if (certExpire.isBefore(LocalDateTime.now())) {
            //证书已过期的话，更新expired为true
            qhseDeviceCert.setExpired(true);
        } else  {
            if (ChronoUnit.DAYS.between(LocalDateTime.now(), certExpire) < 60) {
                qhseDeviceCert.setAlertWarn(true);//设置为60天预警过期
            } else {
                qhseDeviceCert.setAlertWarn(false);
            }
            qhseDeviceCert.setExpired(false);
        }
        qhseDeviceCertMapper.insert(qhseDeviceCert);
        // 返回
        return qhseDeviceCert.getId();
    }

    @Override
    public void updateQhseDeviceCert(QhseDeviceCertSaveReqVO updateReqVO) {
        // 校验存在
        validateQhseDeviceCertExists(updateReqVO.getId());
        // 更新
        QhseDeviceCertDO updateObj = BeanUtils.toBean(updateReqVO, QhseDeviceCertDO.class);
        LocalDateTime certExpire = updateObj.getCertExpire();
        if (certExpire.isBefore(LocalDateTime.now())) {
            //证书已过期的话，更新expired为true
            updateObj.setExpired(true);
        } else  {
            if (ChronoUnit.DAYS.between(LocalDateTime.now(), certExpire) < 60) {
                updateObj.setAlertWarn(true);//设置为60天预警过期
            }else {
                updateObj.setAlertWarn(false);
            }
            updateObj.setExpired(false);
        }
        qhseDeviceCertMapper.updateById(updateObj);
    }

    @Override
    public void deleteQhseDeviceCert(Long id) {
        // 校验存在
        validateQhseDeviceCertExists(id);
        // 删除
        qhseDeviceCertMapper.deleteById(id);
    }

    private void validateQhseDeviceCertExists(Long id) {
        if (qhseDeviceCertMapper.selectById(id) == null) {
            throw exception(new ErrorCode(2,"不存在"));
        }
    }

    @Override
    public QhseDeviceCertDO getQhseDeviceCert(Long id) {
        return qhseDeviceCertMapper.selectById(id);
    }

    @Override
    public PageResult<QhseDeviceCertDO> getQhseDeviceCertPage(QhseDeviceCertPageReqVO pageReqVO) {
        Set<Long> ids = new HashSet<>();
        if (Objects.nonNull(pageReqVO.getDeptId())) {
            ids = deptService.getChildDeptIdListFromCache(pageReqVO.getDeptId());
            ids.add(pageReqVO.getDeptId());
        }
        return qhseDeviceCertMapper.selectPage(pageReqVO, ids);
    }

}