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

import cn.hutool.core.collection.CollUtil;
import com.zt.plat.module.qms.business.config.controller.vo.ConfigStandardSampleTypePageReqVO;
import com.zt.plat.module.qms.business.config.controller.vo.ConfigStandardSampleTypeRespVO;
import com.zt.plat.module.qms.business.config.controller.vo.ConfigStandardSampleTypeSaveReqVO;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;

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

import com.zt.plat.module.qms.business.config.dal.mapper.ConfigStandardSampleTypeMapper;

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.CONFIG_STANDARD_SAMPLE_TYPE_NOT_EXISTS;

/**
 * 标准样类型配置 Service 实现类
 *
 * @author 后台管理
 */
@Service
@Validated
public class ConfigStandardSampleTypeServiceImpl implements ConfigStandardSampleTypeService {

    @Resource
    private ConfigStandardSampleTypeMapper configStandardSampleTypeMapper;

    @Override
    public ConfigStandardSampleTypeRespVO createConfigStandardSampleType(ConfigStandardSampleTypeSaveReqVO createReqVO) {
        // 插入
        ConfigStandardSampleTypeDO configStandardSampleType = BeanUtils.toBean(createReqVO, ConfigStandardSampleTypeDO.class);
        configStandardSampleTypeMapper.insert(configStandardSampleType);
        // 返回
        return BeanUtils.toBean(configStandardSampleType, ConfigStandardSampleTypeRespVO.class);
    }

    @Override
    public void updateConfigStandardSampleType(ConfigStandardSampleTypeSaveReqVO updateReqVO) {
        // 校验存在
        validateConfigStandardSampleTypeExists(updateReqVO.getId());
        // 更新
        ConfigStandardSampleTypeDO updateObj = BeanUtils.toBean(updateReqVO, ConfigStandardSampleTypeDO.class);
        configStandardSampleTypeMapper.updateById(updateObj);
    }

    @Override
    public void deleteConfigStandardSampleType(Long id) {
        // 校验存在
        validateConfigStandardSampleTypeExists(id);
        // 删除
        configStandardSampleTypeMapper.deleteById(id);
    }

    @Override
        public void deleteConfigStandardSampleTypeListByIds(List<Long> ids) {
        // 校验存在
        validateConfigStandardSampleTypeExists(ids);
        // 删除
        configStandardSampleTypeMapper.deleteByIds(ids);
        }

    private void validateConfigStandardSampleTypeExists(List<Long> ids) {
        List<ConfigStandardSampleTypeDO> list = configStandardSampleTypeMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(CONFIG_STANDARD_SAMPLE_TYPE_NOT_EXISTS);
        }
    }

    private void validateConfigStandardSampleTypeExists(Long id) {
        if (configStandardSampleTypeMapper.selectById(id) == null) {
            throw exception(CONFIG_STANDARD_SAMPLE_TYPE_NOT_EXISTS);
        }
    }

    @Override
    public ConfigStandardSampleTypeDO getConfigStandardSampleType(Long id) {
        return configStandardSampleTypeMapper.selectById(id);
    }

    @Override
    public PageResult<ConfigStandardSampleTypeDO> getConfigStandardSampleTypePage(ConfigStandardSampleTypePageReqVO pageReqVO) {
        return configStandardSampleTypeMapper.selectPage(pageReqVO);
    }

}