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

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zt.plat.module.qms.resource.device.controller.vo.DeviceConfigBusinessItemPageReqVO;
import com.zt.plat.module.qms.resource.device.controller.vo.DeviceConfigBusinessItemRespVO;
import com.zt.plat.module.qms.resource.device.controller.vo.DeviceConfigBusinessItemSaveReqVO;
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.DeviceConfigBusinessItemDO;
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.DeviceConfigBusinessItemMapper;

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_CONFIG_BUSINESS_ITEM_NOT_EXISTS;

/**
 * 设备-检查项目配置 Service 实现类
 *
 * @author 后台管理-1
 */
@Service
@Validated
public class DeviceConfigBusinessItemServiceImpl implements DeviceConfigBusinessItemService {

    @Resource
    private DeviceConfigBusinessItemMapper deviceConfigBusinessItemMapper;

    @Override
    public List<DeviceConfigBusinessItemDO> listByRuleId(Long ruleId) {
        LambdaQueryWrapper<DeviceConfigBusinessItemDO> query = new LambdaQueryWrapper<>();
        query.eq(DeviceConfigBusinessItemDO::getRuleId, ruleId);
        return deviceConfigBusinessItemMapper.selectList(query);
    }

    @Override
    public List<DeviceConfigBusinessItemDO> getByIdList(List<Long> ruleId) {
        return deviceConfigBusinessItemMapper.selectByIds(ruleId);
    }

    @Override
    public DeviceConfigBusinessItemRespVO createDeviceConfigBusinessItem(DeviceConfigBusinessItemSaveReqVO createReqVO) {
        // 插入
        DeviceConfigBusinessItemDO deviceConfigBusinessItem = BeanUtils.toBean(createReqVO, DeviceConfigBusinessItemDO.class);
        deviceConfigBusinessItemMapper.insert(deviceConfigBusinessItem);
        // 返回
        return BeanUtils.toBean(deviceConfigBusinessItem, DeviceConfigBusinessItemRespVO.class);
    }

    @Override
    public void updateDeviceConfigBusinessItem(DeviceConfigBusinessItemSaveReqVO updateReqVO) {
        // 校验存在
        validateDeviceConfigBusinessItemExists(updateReqVO.getId());
        // 更新
        DeviceConfigBusinessItemDO updateObj = BeanUtils.toBean(updateReqVO, DeviceConfigBusinessItemDO.class);
        deviceConfigBusinessItemMapper.updateById(updateObj);
    }

    @Override
    public void deleteDeviceConfigBusinessItem(Long id) {
        // 校验存在
        validateDeviceConfigBusinessItemExists(id);
        // 删除
        deviceConfigBusinessItemMapper.deleteById(id);
    }

    @Override
        public void deleteDeviceConfigBusinessItemListByIds(List<Long> ids) {
        // 校验存在
        validateDeviceConfigBusinessItemExists(ids);
        // 删除
        deviceConfigBusinessItemMapper.deleteByIds(ids);
        }

    private void validateDeviceConfigBusinessItemExists(List<Long> ids) {
        List<DeviceConfigBusinessItemDO> list = deviceConfigBusinessItemMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(DEVICE_CONFIG_BUSINESS_ITEM_NOT_EXISTS);
        }
    }

    private void validateDeviceConfigBusinessItemExists(Long id) {
        if (deviceConfigBusinessItemMapper.selectById(id) == null) {
            throw exception(DEVICE_CONFIG_BUSINESS_ITEM_NOT_EXISTS);
        }
    }

    @Override
    public DeviceConfigBusinessItemDO getDeviceConfigBusinessItem(Long id) {
        return deviceConfigBusinessItemMapper.selectById(id);
    }

    @Override
    public PageResult<DeviceConfigBusinessItemDO> getDeviceConfigBusinessItemPage(DeviceConfigBusinessItemPageReqVO pageReqVO) {
        return deviceConfigBusinessItemMapper.selectPage(pageReqVO);
    }

}