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

import cn.iocoder.yudao.framework.common.exception.ErrorCode;
import cn.iocoder.yudao.framework.common.exception.ServiceException;
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.measure.vo.IotMeasureDetectPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.qhse.measure.vo.IotMeasureDetectSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.qhse.measure.IotMeasureBookDO;
import cn.iocoder.yudao.module.pms.dal.dataobject.qhse.measure.IotMeasureDetectDO;
import cn.iocoder.yudao.module.pms.dal.mysql.qhse.measure.IotMeasureBookMapper;
import cn.iocoder.yudao.module.pms.dal.mysql.qhse.measure.IotMeasureDetectMapper;
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 IotMeasureDetectServiceImpl implements IotMeasureDetectService {

    @Resource
    private IotMeasureDetectMapper iotMeasureDetectMapper;
    @Autowired
    private DeptService deptService;
    @Autowired
    private IotMeasureBookMapper iotMeasureBookMapper;

    @Override
    public Long createIotMeasureDetect(IotMeasureDetectSaveReqVO createReqVO) {
        // 插入
        IotMeasureDetectDO iotMeasureDetect = BeanUtils.toBean(createReqVO, IotMeasureDetectDO.class);
        iotMeasureDetect.setDeleted(false);
        LocalDateTime validityPeriod = createReqVO.getValidityPeriod();
        IotMeasureBookDO iotMeasureBookDO = iotMeasureBookMapper.selectById(createReqVO.getMeasureId());
        if (Objects.isNull(iotMeasureBookDO)) {
            throw new ServiceException(new ErrorCode(3, "不存在"));
        }
        if (validityPeriod.isBefore(LocalDateTime.now())) {
            iotMeasureBookDO.setExpired(true);
        } else {
            iotMeasureBookDO.setExpired(false);
        }
        //如果相差不到90天了就进行预警
        if (validityPeriod.isAfter(LocalDateTime.now()) && ChronoUnit.DAYS.between(LocalDateTime.now(), validityPeriod) < 90) {
            iotMeasureBookDO.setAlertWarn(true);
            iotMeasureBookDO.setExpired(false);
        }
        iotMeasureBookMapper.updateById(iotMeasureBookDO);
        iotMeasureDetectMapper.insert(iotMeasureDetect);
        // 返回
        return iotMeasureDetect.getId();
    }

    @Override
    public void updateIotMeasureDetect(IotMeasureDetectSaveReqVO updateReqVO) {
        // 校验存在
        validateIotMeasureDetectExists(updateReqVO.getId());
        // 更新
        IotMeasureDetectDO updateObj = BeanUtils.toBean(updateReqVO, IotMeasureDetectDO.class);
        LocalDateTime validityPeriod = updateReqVO.getValidityPeriod();
        IotMeasureBookDO iotMeasureBookDO = iotMeasureBookMapper.selectById(updateReqVO.getMeasureId());
        if (Objects.isNull(iotMeasureBookDO)) {
            throw new ServiceException(new ErrorCode(3, "不存在"));
        }
        if (validityPeriod.isBefore(LocalDateTime.now())) {
            iotMeasureBookDO.setExpired(true);
        } else {
            iotMeasureBookDO.setExpired(false);
        }
        //如果相差不到90天了就进行预警
        if (validityPeriod.isAfter(LocalDateTime.now()) && ChronoUnit.DAYS.between(LocalDateTime.now(), validityPeriod) < 90) {
            iotMeasureBookDO.setAlertWarn(true);
            iotMeasureBookDO.setExpired(false);
        } else {
            iotMeasureBookDO.setAlertWarn(false);
        }
        iotMeasureBookMapper.updateById(iotMeasureBookDO);
        iotMeasureDetectMapper.updateById(updateObj);
    }

    @Override
    public void deleteIotMeasureDetect(Long id) {
        // 校验存在
        validateIotMeasureDetectExists(id);
        // 删除
        iotMeasureDetectMapper.deleteById(id);
    }

    private void validateIotMeasureDetectExists(Long id) {
        if (iotMeasureDetectMapper.selectById(id) == null) {
            throw exception(new ErrorCode(1,"不存在"));
        }
    }

    @Override
    public IotMeasureDetectDO getIotMeasureDetect(Long id) {
        return iotMeasureDetectMapper.selectById(id);
    }

    @Override
    public PageResult<IotMeasureDetectDO> getIotMeasureDetectPage(IotMeasureDetectPageReqVO pageReqVO) {
        Set<Long> ids = new HashSet<>();
        if (Objects.nonNull(pageReqVO.getDeptId())) {
            ids = deptService.getChildDeptIdListFromCache(pageReqVO.getDeptId());
            ids.add(pageReqVO.getDeptId());
        }
        return iotMeasureDetectMapper.selectPage(pageReqVO, ids);
    }

}