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.ConfigBaseSamplePageReqVO;
import com.zt.plat.module.qms.business.config.controller.vo.ConfigBaseSampleRespVO;
import com.zt.plat.module.qms.business.config.controller.vo.ConfigBaseSampleSaveReqVO;
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.ConfigBaseSampleDO;
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.ConfigBaseSampleMapper;

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

/**
 * 主样配置 Service 实现类
 *
 * @author 后台管理
 */
@Service
@Validated
public class ConfigBaseSampleServiceImpl implements ConfigBaseSampleService {

    @Resource
    private ConfigBaseSampleMapper configBaseSampleMapper;

    @Override
    public ConfigBaseSampleRespVO createConfigBaseSample(ConfigBaseSampleSaveReqVO createReqVO) {
        // 插入
        ConfigBaseSampleDO configBaseSample = BeanUtils.toBean(createReqVO, ConfigBaseSampleDO.class);
        configBaseSampleMapper.insert(configBaseSample);
        // 返回
        return BeanUtils.toBean(configBaseSample, ConfigBaseSampleRespVO.class);
    }

    @Override
    public void updateConfigBaseSample(ConfigBaseSampleSaveReqVO updateReqVO) {
        // 校验存在
        validateConfigBaseSampleExists(updateReqVO.getId());
        // 更新
        ConfigBaseSampleDO updateObj = BeanUtils.toBean(updateReqVO, ConfigBaseSampleDO.class);
        configBaseSampleMapper.updateById(updateObj);
    }

    @Override
    public void deleteConfigBaseSample(Long id) {
        // 校验存在
        validateConfigBaseSampleExists(id);
        // 删除
        configBaseSampleMapper.deleteById(id);
    }

    @Override
        public void deleteConfigBaseSampleListByIds(List<Long> ids) {
        // 校验存在
        validateConfigBaseSampleExists(ids);
        // 删除
        configBaseSampleMapper.deleteByIds(ids);
        }

    private void validateConfigBaseSampleExists(List<Long> ids) {
        List<ConfigBaseSampleDO> list = configBaseSampleMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(CONFIG_BASE_SAMPLE_NOT_EXISTS);
        }
    }

    private void validateConfigBaseSampleExists(Long id) {
        if (configBaseSampleMapper.selectById(id) == null) {
            throw exception(CONFIG_BASE_SAMPLE_NOT_EXISTS);
        }
    }

    @Override
    public ConfigBaseSampleDO getConfigBaseSample(Long id) {
        return configBaseSampleMapper.selectById(id);
    }

    @Override
    public PageResult<ConfigBaseSampleDO> getConfigBaseSamplePage(ConfigBaseSamplePageReqVO pageReqVO) {
        return configBaseSampleMapper.selectPage(pageReqVO);
    }

}