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.ConfigSubSamplePageReqVO;
import com.zt.plat.module.qms.business.config.controller.vo.ConfigSubSampleRespVO;
import com.zt.plat.module.qms.business.config.controller.vo.ConfigSubSampleSaveReqVO;
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.ConfigSubSampleDO;
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.ConfigSubSampleMapper;

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

/**
 * 子样配置 Service 实现类
 *
 * @author 后台管理
 */
@Service
@Validated
public class ConfigSubSampleServiceImpl implements ConfigSubSampleService {

    @Resource
    private ConfigSubSampleMapper configSubSampleMapper;

    @Override
    public ConfigSubSampleRespVO createConfigSubSample(ConfigSubSampleSaveReqVO createReqVO) {
        // 插入
        ConfigSubSampleDO configSubSample = BeanUtils.toBean(createReqVO, ConfigSubSampleDO.class);
        configSubSampleMapper.insert(configSubSample);
        // 返回
        return BeanUtils.toBean(configSubSample, ConfigSubSampleRespVO.class);
    }

    @Override
    public void updateConfigSubSample(ConfigSubSampleSaveReqVO updateReqVO) {
        // 校验存在
        validateConfigSubSampleExists(updateReqVO.getId());
        // 更新
        ConfigSubSampleDO updateObj = BeanUtils.toBean(updateReqVO, ConfigSubSampleDO.class);
        configSubSampleMapper.updateById(updateObj);
    }

    @Override
    public void deleteConfigSubSample(Long id) {
        // 校验存在
        validateConfigSubSampleExists(id);
        // 删除
        configSubSampleMapper.deleteById(id);
    }

    @Override
        public void deleteConfigSubSampleListByIds(List<Long> ids) {
        // 校验存在
        validateConfigSubSampleExists(ids);
        // 删除
        configSubSampleMapper.deleteByIds(ids);
        }

    private void validateConfigSubSampleExists(List<Long> ids) {
        List<ConfigSubSampleDO> list = configSubSampleMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(CONFIG_SUB_SAMPLE_NOT_EXISTS);
        }
    }

    private void validateConfigSubSampleExists(Long id) {
        if (configSubSampleMapper.selectById(id) == null) {
            throw exception(CONFIG_SUB_SAMPLE_NOT_EXISTS);
        }
    }

    @Override
    public ConfigSubSampleDO getConfigSubSample(Long id) {
        return configSubSampleMapper.selectById(id);
    }

    @Override
    public PageResult<ConfigSubSampleDO> getConfigSubSamplePage(ConfigSubSamplePageReqVO pageReqVO) {
        return configSubSampleMapper.selectPage(pageReqVO);
    }

}