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

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.cert.vo.IotMeasureCertPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.qhse.cert.vo.IotMeasureCertSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.qhse.cert.IotMeasureCertDO;
import cn.iocoder.yudao.module.pms.dal.mysql.qhse.cert.IotMeasureCertMapper;
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;

/**
 * 计量器具-证书管理 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class IotMeasureCertServiceImpl implements IotMeasureCertService {

    @Resource
    private IotMeasureCertMapper iotMeasureCertMapper;
    @Autowired
    private DeptService deptService;

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

    @Override
    public void updateIotMeasureCert(IotMeasureCertSaveReqVO updateReqVO) {
        // 校验存在
        validateIotMeasureCertExists(updateReqVO.getId());
        // 更新
        IotMeasureCertDO updateObj = BeanUtils.toBean(updateReqVO, IotMeasureCertDO.class);
        LocalDateTime certExpire = updateObj.getCertExpire();
        //证书已过期的话，更新expired为true
        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);
        }
        iotMeasureCertMapper.updateById(updateObj);
    }

    @Override
    public void deleteIotMeasureCert(Long id) {
        // 校验存在
        validateIotMeasureCertExists(id);
        // 删除
        iotMeasureCertMapper.deleteById(id);
    }

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

    @Override
    public IotMeasureCertDO getIotMeasureCert(Long id) {
        return iotMeasureCertMapper.selectById(id);
    }

    @Override
    public PageResult<IotMeasureCertDO> getIotMeasureCertPage(IotMeasureCertPageReqVO pageReqVO) {
        Set<Long> ids = new HashSet<>();
        if (Objects.nonNull(pageReqVO.getDeptId())) {
            ids = deptService.getChildDeptIdListFromCache(pageReqVO.getDeptId());
            ids.add(pageReqVO.getDeptId());
        }
        return iotMeasureCertMapper.selectPage(pageReqVO, ids);
    }

}