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

import cn.hutool.core.collection.CollUtil;
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.controller.vo.DeviceInfomationPageReqVO;
import com.zt.plat.module.qms.resource.device.controller.vo.DeviceInfomationRespVO;
import com.zt.plat.module.qms.resource.device.controller.vo.DeviceInfomationSaveReqVO;
import com.zt.plat.module.qms.resource.device.dal.dataobject.DeviceInfomationDO;
import com.zt.plat.module.qms.resource.device.dal.mapper.DeviceInfomationMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.List;

import static com.zt.plat.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.zt.plat.module.qms.enums.ErrorCodeConstants.DEVICE_INFOMATION_NOT_EXISTS;


/**
 * 设备-设备信息 Service 实现类
 *
 * @author 后台管理
 */
@Service
@Validated
public class DeviceInfomationServiceImpl implements DeviceInfomationService {

    @Resource
    private DeviceInfomationMapper deviceInfomationMapper;

    @Override
    public DeviceInfomationRespVO createDeviceInfomation(DeviceInfomationSaveReqVO createReqVO) {
        // 插入
        DeviceInfomationDO deviceInfomation = BeanUtils.toBean(createReqVO, DeviceInfomationDO.class);
        deviceInfomationMapper.insert(deviceInfomation);
        // 返回
        return BeanUtils.toBean(deviceInfomation, DeviceInfomationRespVO.class);
    }

    @Override
    public void updateDeviceInfomation(DeviceInfomationSaveReqVO updateReqVO) {
        // 校验存在
        validateDeviceInfomationExists(updateReqVO.getId());
        // 更新
        DeviceInfomationDO updateObj = BeanUtils.toBean(updateReqVO, DeviceInfomationDO.class);
        deviceInfomationMapper.updateById(updateObj);
    }

    @Override
    public void updateDeviceInfomation(DeviceInfomationDO entity) {
        deviceInfomationMapper.updateById(entity);
    }

    @Override
    public void deleteDeviceInfomation(Long id) {
        // 校验存在
        validateDeviceInfomationExists(id);
        // 删除
        deviceInfomationMapper.deleteById(id);
    }

    @Override
        public void deleteDeviceInfomationListByIds(List<Long> ids) {
        // 校验存在
        validateDeviceInfomationExists(ids);
        // 删除
        deviceInfomationMapper.deleteByIds(ids);
        }

    private void validateDeviceInfomationExists(List<Long> ids) {
        List<DeviceInfomationDO> list = deviceInfomationMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(DEVICE_INFOMATION_NOT_EXISTS);
        }
    }

    private void validateDeviceInfomationExists(Long id) {
        if (deviceInfomationMapper.selectById(id) == null) {
            throw exception(DEVICE_INFOMATION_NOT_EXISTS);
        }
    }

    @Override
    public DeviceInfomationDO getDeviceInfomation(Long id) {
        return deviceInfomationMapper.selectById(id);
    }

    @Override
    public PageResult<DeviceInfomationDO> getDeviceInfomationPage(DeviceInfomationPageReqVO pageReqVO) {
        return deviceInfomationMapper.selectPage(pageReqVO);
    }

}