package com.zt.plat.module.qms.business.config.service;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.pojo.PageParam;
import com.zt.plat.framework.common.util.object.BeanUtils;
import com.zt.plat.module.qms.business.config.controller.vo.*;
import com.zt.plat.module.qms.business.config.dal.dataobject.ConfigXRFConversionRateDO;
import com.zt.plat.module.qms.business.config.dal.mapper.ConfigXRFConversionRateMapper;

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.framework.common.util.collection.CollectionUtils.diffList;
import static com.zt.plat.module.qms.enums.ErrorCodeConstants.*;

/**
 * 荧光数据采集特殊检测项目转换率配置 Service 实现类
 *
 * @author 后台管理
 */
@Service
@Validated
public class ConfigXRFConversionRateServiceImpl implements ConfigXRFConversionRateService {

    @Resource
    private ConfigXRFConversionRateMapper configXRFConversionRateMapper;

    @Override
    public ConfigXRFConversionRateRespVO createConfigXRFConversionRate(ConfigXRFConversionRateSaveReqVO createReqVO) {
        // 插入
        ConfigXRFConversionRateDO configXRFConversionRate = BeanUtils.toBean(createReqVO, ConfigXRFConversionRateDO.class);
        configXRFConversionRateMapper.insert(configXRFConversionRate);
        // 返回
        return BeanUtils.toBean(configXRFConversionRate, ConfigXRFConversionRateRespVO.class);
    }

    @Override
    public void updateConfigXRFConversionRate(ConfigXRFConversionRateSaveReqVO updateReqVO) {
        // 校验存在
        validateConfigXRFConversionRateExists(updateReqVO.getId());
        // 更新
        ConfigXRFConversionRateDO updateObj = BeanUtils.toBean(updateReqVO, ConfigXRFConversionRateDO.class);
        configXRFConversionRateMapper.updateById(updateObj);
    }

    @Override
    public void deleteConfigXRFConversionRate(Long id) {
        // 校验存在
        validateConfigXRFConversionRateExists(id);
        // 删除
        configXRFConversionRateMapper.deleteById(id);
    }

    @Override
        public void deleteConfigXRFConversionRateListByIds(List<Long> ids) {
        // 校验存在
        validateConfigXRFConversionRateExists(ids);
        // 删除
        configXRFConversionRateMapper.deleteByIds(ids);
        }

    private void validateConfigXRFConversionRateExists(List<Long> ids) {
        List<ConfigXRFConversionRateDO> list = configXRFConversionRateMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(CONFIG_XRF_CONVERSION_RATE_NOT_EXISTS);
        }
    }

    private void validateConfigXRFConversionRateExists(Long id) {
        if (configXRFConversionRateMapper.selectById(id) == null) {
            throw exception(CONFIG_XRF_CONVERSION_RATE_NOT_EXISTS);
        }
    }

    @Override
    public ConfigXRFConversionRateDO getConfigXRFConversionRate(Long id) {
        return configXRFConversionRateMapper.selectById(id);
    }

    @Override
    public PageResult<ConfigXRFConversionRateDO> getConfigXRFConversionRatePage(ConfigXRFConversionRatePageReqVO pageReqVO) {
        return configXRFConversionRateMapper.selectPage(pageReqVO);
    }

}