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.ConfigSubSampleParentPageReqVO;
import com.zt.plat.module.qms.business.config.controller.vo.ConfigSubSampleParentRespVO;
import com.zt.plat.module.qms.business.config.controller.vo.ConfigSubSampleParentSaveReqVO;
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.ConfigSubSampleParentDO;
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.ConfigSubSampleParentMapper;

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

/**
 * 分样配置 Service 实现类
 *
 * @author 后台管理
 */
@Service
@Validated
public class ConfigSubSampleParentServiceImpl implements ConfigSubSampleParentService {

    @Resource
    private ConfigSubSampleParentMapper configSubSampleParentMapper;

    @Override
    public ConfigSubSampleParentRespVO createConfigSubSampleParent(ConfigSubSampleParentSaveReqVO createReqVO) {
        // 插入
        ConfigSubSampleParentDO configSubSampleParent = BeanUtils.toBean(createReqVO, ConfigSubSampleParentDO.class);
        configSubSampleParentMapper.insert(configSubSampleParent);
        // 返回
        return BeanUtils.toBean(configSubSampleParent, ConfigSubSampleParentRespVO.class);
    }

    @Override
    public void updateConfigSubSampleParent(ConfigSubSampleParentSaveReqVO updateReqVO) {
        // 校验存在
        validateConfigSubSampleParentExists(updateReqVO.getId());
        // 更新
        ConfigSubSampleParentDO updateObj = BeanUtils.toBean(updateReqVO, ConfigSubSampleParentDO.class);
        configSubSampleParentMapper.updateById(updateObj);
    }

    @Override
    public void deleteConfigSubSampleParent(Long id) {
        // 校验存在
        validateConfigSubSampleParentExists(id);
        // 删除
        configSubSampleParentMapper.deleteById(id);
    }

    @Override
        public void deleteConfigSubSampleParentListByIds(List<Long> ids) {
        // 校验存在
        validateConfigSubSampleParentExists(ids);
        // 删除
        configSubSampleParentMapper.deleteByIds(ids);
        }

    private void validateConfigSubSampleParentExists(List<Long> ids) {
        List<ConfigSubSampleParentDO> list = configSubSampleParentMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(CONFIG_SUB_SAMPLE_PARENT_NOT_EXISTS);
        }
    }

    private void validateConfigSubSampleParentExists(Long id) {
        if (configSubSampleParentMapper.selectById(id) == null) {
            throw exception(CONFIG_SUB_SAMPLE_PARENT_NOT_EXISTS);
        }
    }

    @Override
    public ConfigSubSampleParentDO getConfigSubSampleParent(Long id) {
        return configSubSampleParentMapper.selectById(id);
    }

    @Override
    public PageResult<ConfigSubSampleParentDO> getConfigSubSampleParentPage(ConfigSubSampleParentPageReqVO pageReqVO) {
        return configSubSampleParentMapper.selectPage(pageReqVO);
    }

}