package com.zt.plat.module.qms.resource.device.service;

import cn.hutool.core.collection.CollUtil;
import com.zt.plat.module.qms.resource.device.controller.vo.DeviceCalibrationPageReqVO;
import com.zt.plat.module.qms.resource.device.controller.vo.DeviceCalibrationRespVO;
import com.zt.plat.module.qms.resource.device.controller.vo.DeviceCalibrationSaveReqVO;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;

import com.zt.plat.module.qms.resource.device.dal.dataobject.DeviceCalibrationDO;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.util.object.BeanUtils;

import com.zt.plat.module.qms.resource.device.dal.mapper.DeviceCalibrationMapper;

import static com.zt.plat.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.zt.plat.framework.common.util.collection.CollectionUtils.convertList;
import static com.zt.plat.module.qms.enums.ErrorCodeConstants.DEVICE_CALIBRATION_NOT_EXISTS;

/**
 * 设备-检定校准 Service 实现类
 *
 * @author 后台管理-1
 */
@Service
@Validated
public class DeviceCalibrationServiceImpl implements DeviceCalibrationService {

    @Resource
    private DeviceCalibrationMapper deviceCalibrationMapper;

    @Override
    public DeviceCalibrationRespVO createDeviceCalibration(DeviceCalibrationSaveReqVO createReqVO) {
        // 插入
        DeviceCalibrationDO deviceCalibration = BeanUtils.toBean(createReqVO, DeviceCalibrationDO.class);
        deviceCalibrationMapper.insert(deviceCalibration);
        // 返回
        return BeanUtils.toBean(deviceCalibration, DeviceCalibrationRespVO.class);
    }

    @Override
    public void updateDeviceCalibration(DeviceCalibrationSaveReqVO updateReqVO) {
        // 校验存在
        validateDeviceCalibrationExists(updateReqVO.getId());
        // 更新
        DeviceCalibrationDO updateObj = BeanUtils.toBean(updateReqVO, DeviceCalibrationDO.class);
        deviceCalibrationMapper.updateById(updateObj);
    }

    @Override
    public void deleteDeviceCalibration(Long id) {
        // 校验存在
        validateDeviceCalibrationExists(id);
        // 删除
        deviceCalibrationMapper.deleteById(id);
    }

    @Override
        public void deleteDeviceCalibrationListByIds(List<Long> ids) {
        // 校验存在
        validateDeviceCalibrationExists(ids);
        // 删除
        deviceCalibrationMapper.deleteByIds(ids);
        }

    private void validateDeviceCalibrationExists(List<Long> ids) {
        List<DeviceCalibrationDO> list = deviceCalibrationMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(DEVICE_CALIBRATION_NOT_EXISTS);
        }
    }

    private void validateDeviceCalibrationExists(Long id) {
        if (deviceCalibrationMapper.selectById(id) == null) {
            throw exception(DEVICE_CALIBRATION_NOT_EXISTS);
        }
    }

    @Override
    public DeviceCalibrationDO getDeviceCalibration(Long id) {
        return deviceCalibrationMapper.selectById(id);
    }

    @Override
    public PageResult<DeviceCalibrationDO> getDeviceCalibrationPage(DeviceCalibrationPageReqVO pageReqVO) {
        return deviceCalibrationMapper.selectPage(pageReqVO);
    }

}