package cn.iocoder.yudao.module.mes.service.energydevice;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.collection.MapUtils;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.mes.controller.admin.energydevice.vo.EnergyDeviceCheckRecordPageReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.energydevice.vo.EnergyDeviceCheckRecordRespVO;
import cn.iocoder.yudao.module.mes.controller.admin.energydevice.vo.EnergyDeviceCheckRecordSaveReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.energydevice.EnergyDeviceCheckRecordDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.energydevice.EnergyDeviceDO;
import cn.iocoder.yudao.module.mes.dal.mysql.energydevice.EnergyDeviceCheckRecordMapper;
import cn.iocoder.yudao.module.mes.dal.mysql.energydevice.EnergyDeviceMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.ENERGY_DEVICE_CHECK_RECORD_NOT_EXISTS;

/**
 * 抄表记录 Service 实现类
 *
 * @author 内蒙必硕
 */
@Service
@Validated
public class EnergyDeviceCheckRecordServiceImpl implements EnergyDeviceCheckRecordService {

    @Resource
    private EnergyDeviceCheckRecordMapper energyDeviceCheckRecordMapper;
    @Resource
    private EnergyDeviceMapper energyDeviceMapper;
    @Resource
    private EnergyDeviceService energyDeviceService;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createEnergyDeviceCheckRecord(EnergyDeviceCheckRecordSaveReqVO createReqVO) {
        if(createReqVO.getCheckTime()==null)
            createReqVO.setCheckTime(LocalDateTime.now());
        EnergyDeviceDO energyDeviceDO = energyDeviceMapper.selectById(createReqVO.getDeviceId());
        // 插入
        EnergyDeviceCheckRecordDO energyDeviceCheckRecord = BeanUtils.toBean(createReqVO, EnergyDeviceCheckRecordDO.class);
        energyDeviceCheckRecord.setLastCheckTime(energyDeviceDO.getLastCheckTime());
        energyDeviceCheckRecord.setLastCheckValue(energyDeviceDO.getLastCheckValue());
        BigDecimal last = energyDeviceDO.getLastCheckValue()==null? BigDecimal.ZERO : energyDeviceDO.getLastCheckValue();
        BigDecimal diff = energyDeviceCheckRecord.getCheckValue().subtract(last).abs();
        energyDeviceCheckRecord.setDiffValue(diff);
        energyDeviceCheckRecordMapper.insert(energyDeviceCheckRecord);

        energyDeviceDO.setLastCheckTime(createReqVO.getCheckTime());
        energyDeviceDO.setLastCheckValue(createReqVO.getCheckValue());
        energyDeviceMapper.updateById(energyDeviceDO);
        // 返回
        return energyDeviceCheckRecord.getId();
    }

    @Override
    public void updateEnergyDeviceCheckRecord(EnergyDeviceCheckRecordSaveReqVO updateReqVO) {
        // 校验存在
        validateEnergyDeviceCheckRecordExists(updateReqVO.getId());
        // 更新
        EnergyDeviceCheckRecordDO updateObj = BeanUtils.toBean(updateReqVO, EnergyDeviceCheckRecordDO.class);
        energyDeviceCheckRecordMapper.updateById(updateObj);
    }

    @Override
    public void deleteEnergyDeviceCheckRecord(Long id) {
        // 校验存在
        validateEnergyDeviceCheckRecordExists(id);
        // 删除
        energyDeviceCheckRecordMapper.deleteById(id);
    }

    private void validateEnergyDeviceCheckRecordExists(Long id) {
        if (energyDeviceCheckRecordMapper.selectById(id) == null) {
            throw exception(ENERGY_DEVICE_CHECK_RECORD_NOT_EXISTS);
        }
    }

    @Override
    public EnergyDeviceCheckRecordDO getEnergyDeviceCheckRecord(Long id) {
        return energyDeviceCheckRecordMapper.selectById(id);
    }

    @Override
    public PageResult<EnergyDeviceCheckRecordDO> getEnergyDeviceCheckRecordPage(EnergyDeviceCheckRecordPageReqVO pageReqVO) {
        return energyDeviceCheckRecordMapper.selectPage(pageReqVO);
    }

    @Override
    public List<EnergyDeviceCheckRecordRespVO> buildVOList(List<EnergyDeviceCheckRecordDO> list) {
        if (CollUtil.isEmpty(list)) {
            return Collections.emptyList();
        }
        Map<Long, EnergyDeviceDO> map = energyDeviceService.getMap(
                convertSet(list, EnergyDeviceCheckRecordDO::getDeviceId));

        return BeanUtils.toBean(list, EnergyDeviceCheckRecordRespVO.class, item -> {
            MapUtils.findAndThen(map, item.getDeviceId(),
                    deviceDO -> item.setDeviceName(deviceDO.getName()));

        });
    }
}