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.ConfigEntrustSourceSampleTypeDO;
import com.zt.plat.module.qms.business.config.dal.mapper.ConfigEntrustSourceSampleTypeMapper;

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 ConfigEntrustSourceSampleTypeServiceImpl implements ConfigEntrustSourceSampleTypeService {

    @Resource
    private ConfigEntrustSourceSampleTypeMapper configEntrustSourceSampleTypeMapper;

    @Override
    public ConfigEntrustSourceSampleTypeRespVO createConfigEntrustSourceSampleType(ConfigEntrustSourceSampleTypeSaveReqVO createReqVO) {
        // 插入
        ConfigEntrustSourceSampleTypeDO configEntrustSourceSampleType = BeanUtils.toBean(createReqVO, ConfigEntrustSourceSampleTypeDO.class);
        configEntrustSourceSampleTypeMapper.insert(configEntrustSourceSampleType);
        // 返回
        return BeanUtils.toBean(configEntrustSourceSampleType, ConfigEntrustSourceSampleTypeRespVO.class);
    }

    @Override
    public void updateConfigEntrustSourceSampleType(ConfigEntrustSourceSampleTypeSaveReqVO updateReqVO) {
        // 校验存在
        validateConfigEntrustSourceSampleTypeExists(updateReqVO.getId());
        // 更新
        ConfigEntrustSourceSampleTypeDO updateObj = BeanUtils.toBean(updateReqVO, ConfigEntrustSourceSampleTypeDO.class);
        configEntrustSourceSampleTypeMapper.updateById(updateObj);
    }

    @Override
    public void deleteConfigEntrustSourceSampleType(Long id) {
        // 校验存在
        validateConfigEntrustSourceSampleTypeExists(id);
        // 删除
        configEntrustSourceSampleTypeMapper.deleteById(id);
    }

    @Override
        public void deleteConfigEntrustSourceSampleTypeListByIds(List<Long> ids) {
        // 校验存在
        validateConfigEntrustSourceSampleTypeExists(ids);
        // 删除
        configEntrustSourceSampleTypeMapper.deleteByIds(ids);
        }

    private void validateConfigEntrustSourceSampleTypeExists(List<Long> ids) {
        List<ConfigEntrustSourceSampleTypeDO> list = configEntrustSourceSampleTypeMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(CONFIG_ENTRUST_SOURCE_SAMPLE_TYPE_NOT_EXISTS);
        }
    }

    private void validateConfigEntrustSourceSampleTypeExists(Long id) {
        if (configEntrustSourceSampleTypeMapper.selectById(id) == null) {
            throw exception(CONFIG_ENTRUST_SOURCE_SAMPLE_TYPE_NOT_EXISTS);
        }
    }

    @Override
    public ConfigEntrustSourceSampleTypeDO getConfigEntrustSourceSampleType(Long id) {
        return configEntrustSourceSampleTypeMapper.selectById(id);
    }

    @Override
    public PageResult<ConfigEntrustSourceSampleTypeDO> getConfigEntrustSourceSampleTypePage(ConfigEntrustSourceSampleTypePageReqVO pageReqVO) {
        return configEntrustSourceSampleTypeMapper.selectPage(pageReqVO);
    }

}