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.DeviceAffiliationRelativityPageReqVO;
import com.zt.plat.module.qms.resource.device.controller.vo.DeviceAffiliationRelativityRespVO;
import com.zt.plat.module.qms.resource.device.controller.vo.DeviceAffiliationRelativitySaveReqVO;
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.DeviceAffiliationRelativityDO;
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.DeviceAffiliationRelativityMapper;

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

/**
 * 附属设备关系 Service 实现类
 *
 * @author 后台管理-1
 */
@Service
@Validated
public class DeviceAffiliationRelativityServiceImpl implements DeviceAffiliationRelativityService {

    @Resource
    private DeviceAffiliationRelativityMapper deviceAffiliationRelativityMapper;

    @Override
    public DeviceAffiliationRelativityRespVO createDeviceAffiliationRelativity(DeviceAffiliationRelativitySaveReqVO createReqVO) {
        // 插入
        DeviceAffiliationRelativityDO deviceAffiliationRelativity = BeanUtils.toBean(createReqVO, DeviceAffiliationRelativityDO.class);
        deviceAffiliationRelativityMapper.insert(deviceAffiliationRelativity);
        // 返回
        return BeanUtils.toBean(deviceAffiliationRelativity, DeviceAffiliationRelativityRespVO.class);
    }

    @Override
    public void updateDeviceAffiliationRelativity(DeviceAffiliationRelativitySaveReqVO updateReqVO) {
        // 校验存在
        validateDeviceAffiliationRelativityExists(updateReqVO.getId());
        // 更新
        DeviceAffiliationRelativityDO updateObj = BeanUtils.toBean(updateReqVO, DeviceAffiliationRelativityDO.class);
        deviceAffiliationRelativityMapper.updateById(updateObj);
    }

    @Override
    public void deleteDeviceAffiliationRelativity(Long id) {
        // 校验存在
        validateDeviceAffiliationRelativityExists(id);
        // 删除
        deviceAffiliationRelativityMapper.deleteById(id);
    }

    @Override
        public void deleteDeviceAffiliationRelativityListByIds(List<Long> ids) {
        // 校验存在
        validateDeviceAffiliationRelativityExists(ids);
        // 删除
        deviceAffiliationRelativityMapper.deleteByIds(ids);
        }

    private void validateDeviceAffiliationRelativityExists(List<Long> ids) {
        List<DeviceAffiliationRelativityDO> list = deviceAffiliationRelativityMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(DEVICE_AFFILIATION_RELATIVITY_NOT_EXISTS);
        }
    }

    private void validateDeviceAffiliationRelativityExists(Long id) {
        if (deviceAffiliationRelativityMapper.selectById(id) == null) {
            throw exception(DEVICE_AFFILIATION_RELATIVITY_NOT_EXISTS);
        }
    }

    @Override
    public DeviceAffiliationRelativityDO getDeviceAffiliationRelativity(Long id) {
        return deviceAffiliationRelativityMapper.selectById(id);
    }

    @Override
    public PageResult<DeviceAffiliationRelativityDO> getDeviceAffiliationRelativityPage(DeviceAffiliationRelativityPageReqVO pageReqVO) {
        return deviceAffiliationRelativityMapper.selectPage(pageReqVO);
    }

}