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.ConfigSubSampleMethodPageReqVO;
import com.zt.plat.module.qms.business.config.controller.vo.ConfigSubSampleMethodRespVO;
import com.zt.plat.module.qms.business.config.controller.vo.ConfigSubSampleMethodSaveReqVO;
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.ConfigSubSampleMethodDO;
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.ConfigSubSampleMethodMapper;

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

/**
 * 子样与分样检测方法配置表 Service 实现类
 *
 * @author 后台管理
 */
@Service
@Validated
public class ConfigSubSampleMethodServiceImpl implements ConfigSubSampleMethodService {

    @Resource
    private ConfigSubSampleMethodMapper configSubSampleMethodMapper;

    @Override
    public ConfigSubSampleMethodRespVO createConfigSubSampleMethod(ConfigSubSampleMethodSaveReqVO createReqVO) {
        // 插入
        ConfigSubSampleMethodDO configSubSampleMethod = BeanUtils.toBean(createReqVO, ConfigSubSampleMethodDO.class);
        configSubSampleMethodMapper.insert(configSubSampleMethod);
        // 返回
        return BeanUtils.toBean(configSubSampleMethod, ConfigSubSampleMethodRespVO.class);
    }

    @Override
    public void updateConfigSubSampleMethod(ConfigSubSampleMethodSaveReqVO updateReqVO) {
        // 校验存在
        validateConfigSubSampleMethodExists(updateReqVO.getId());
        // 更新
        ConfigSubSampleMethodDO updateObj = BeanUtils.toBean(updateReqVO, ConfigSubSampleMethodDO.class);
        configSubSampleMethodMapper.updateById(updateObj);
    }

    @Override
    public void deleteConfigSubSampleMethod(Long id) {
        // 校验存在
        validateConfigSubSampleMethodExists(id);
        // 删除
        configSubSampleMethodMapper.deleteById(id);
    }

    @Override
        public void deleteConfigSubSampleMethodListByIds(List<Long> ids) {
        // 校验存在
        validateConfigSubSampleMethodExists(ids);
        // 删除
        configSubSampleMethodMapper.deleteByIds(ids);
        }

    private void validateConfigSubSampleMethodExists(List<Long> ids) {
        List<ConfigSubSampleMethodDO> list = configSubSampleMethodMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(CONFIG_SUB_SAMPLE_METHOD_NOT_EXISTS);
        }
    }

    private void validateConfigSubSampleMethodExists(Long id) {
        if (configSubSampleMethodMapper.selectById(id) == null) {
            throw exception(CONFIG_SUB_SAMPLE_METHOD_NOT_EXISTS);
        }
    }

    @Override
    public ConfigSubSampleMethodDO getConfigSubSampleMethod(Long id) {
        return configSubSampleMethodMapper.selectById(id);
    }

    @Override
    public PageResult<ConfigSubSampleMethodDO> getConfigSubSampleMethodPage(ConfigSubSampleMethodPageReqVO pageReqVO) {
        return configSubSampleMethodMapper.selectPage(pageReqVO);
    }

}