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

import cn.iocoder.yudao.framework.common.exception.ErrorCode;
import cn.iocoder.yudao.module.pms.controller.admin.qhse.measure.vo.IotMeasureRecordPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.qhse.measure.vo.IotMeasureRecordSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.qhse.measure.IotMeasureRecordDO;
import cn.iocoder.yudao.module.pms.dal.mysql.qhse.measure.IotMeasureRecordMapper;
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 org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;


import javax.annotation.Resource;

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

/**
 * 计量器具-使用记录 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class IotMeasureRecordServiceImpl implements IotMeasureRecordService {

    @Resource
    private IotMeasureRecordMapper iotMeasureRecordMapper;
    @Autowired
    private DeptService deptService;

    @Override
    public Long createIotMeasureRecord(IotMeasureRecordSaveReqVO createReqVO) {
        // 插入
        IotMeasureRecordDO iotMeasureRecord = BeanUtils.toBean(createReqVO, IotMeasureRecordDO.class);
        iotMeasureRecord.setDeleted(false);
        iotMeasureRecordMapper.insert(iotMeasureRecord);
        // 返回
        return iotMeasureRecord.getId();
    }

    @Override
    public void updateIotMeasureRecord(IotMeasureRecordSaveReqVO updateReqVO) {
        // 校验存在
        validateIotMeasureRecordExists(updateReqVO.getId());
        // 更新
        IotMeasureRecordDO updateObj = BeanUtils.toBean(updateReqVO, IotMeasureRecordDO.class);
        iotMeasureRecordMapper.updateById(updateObj);
    }

    @Override
    public void deleteIotMeasureRecord(Long id) {
        // 校验存在
        validateIotMeasureRecordExists(id);
        // 删除
        iotMeasureRecordMapper.deleteById(id);
    }

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

    @Override
    public IotMeasureRecordDO getIotMeasureRecord(Long id) {
        return iotMeasureRecordMapper.selectById(id);
    }

    @Override
    public PageResult<IotMeasureRecordDO> getIotMeasureRecordPage(IotMeasureRecordPageReqVO pageReqVO) {
        Set<Long> ids = new HashSet<>();
        if (Objects.nonNull(pageReqVO.getDeptId())) {
            ids = deptService.getChildDeptIdListFromCache(pageReqVO.getDeptId());
            ids.add(pageReqVO.getDeptId());
        }
        return iotMeasureRecordMapper.selectPage(pageReqVO, ids);
    }

}