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.IotMeasureDetectPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.qhse.measure.vo.IotMeasureDetectSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.qhse.measure.IotMeasureDetectDO;
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 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 IotMeasureDetectServiceImpl implements IotMeasureDetectService {

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

    @Override
    public Long createIotMeasureDetect(IotMeasureDetectSaveReqVO createReqVO) {
        // 插入
        IotMeasureDetectDO iotMeasureDetect = BeanUtils.toBean(createReqVO, IotMeasureDetectDO.class);
        iotMeasureDetect.setDeleted(false);
        iotMeasureDetectMapper.insert(iotMeasureDetect);
        // 返回
        return iotMeasureDetect.getId();
    }

    @Override
    public void updateIotMeasureDetect(IotMeasureDetectSaveReqVO updateReqVO) {
        // 校验存在
        validateIotMeasureDetectExists(updateReqVO.getId());
        // 更新
        IotMeasureDetectDO updateObj = BeanUtils.toBean(updateReqVO, IotMeasureDetectDO.class);
        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);
    }

}