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

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

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

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.framework.common.util.collection.CollectionUtils.diffList;
import static com.zt.plat.module.qms.enums.ErrorCodeConstants.*;

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

    @Resource
    private ConfigSubSampleParentMethodMapper configSubSampleParentMethodMapper;

    @Override
    public ConfigSubSampleParentMethodRespVO createConfigSubSampleParentMethod(ConfigSubSampleParentMethodSaveReqVO createReqVO) {
        // 插入
        ConfigSubSampleParentMethodDO configSubSampleParentMethod = BeanUtils.toBean(createReqVO, ConfigSubSampleParentMethodDO.class);
        configSubSampleParentMethodMapper.insert(configSubSampleParentMethod);
        // 返回
        return BeanUtils.toBean(configSubSampleParentMethod, ConfigSubSampleParentMethodRespVO.class);
    }

    @Override
    public void updateConfigSubSampleParentMethod(ConfigSubSampleParentMethodSaveReqVO updateReqVO) {
        // 校验存在
        validateConfigSubSampleParentMethodExists(updateReqVO.getId());
        // 更新
        ConfigSubSampleParentMethodDO updateObj = BeanUtils.toBean(updateReqVO, ConfigSubSampleParentMethodDO.class);
        configSubSampleParentMethodMapper.updateById(updateObj);
    }

    @Override
    public void deleteConfigSubSampleParentMethod(Long id) {
        // 校验存在
        validateConfigSubSampleParentMethodExists(id);
        // 删除
        configSubSampleParentMethodMapper.deleteById(id);
    }

    @Override
        public void deleteConfigSubSampleParentMethodListByIds(List<Long> ids) {
        // 校验存在
        validateConfigSubSampleParentMethodExists(ids);
        // 删除
        configSubSampleParentMethodMapper.deleteByIds(ids);
        }

    private void validateConfigSubSampleParentMethodExists(List<Long> ids) {
        List<ConfigSubSampleParentMethodDO> list = configSubSampleParentMethodMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(CONFIG_SUB_SAMPLE_PARENT_METHOD_NOT_EXISTS);
        }
    }

    private void validateConfigSubSampleParentMethodExists(Long id) {
        if (configSubSampleParentMethodMapper.selectById(id) == null) {
            throw exception(CONFIG_SUB_SAMPLE_PARENT_METHOD_NOT_EXISTS);
        }
    }

    @Override
    public ConfigSubSampleParentMethodDO getConfigSubSampleParentMethod(Long id) {
        return configSubSampleParentMethodMapper.selectById(id);
    }

    @Override
    public PageResult<ConfigSubSampleParentMethodDO> getConfigSubSampleParentMethodPage(ConfigSubSampleParentMethodPageReqVO pageReqVO) {
        return configSubSampleParentMethodMapper.selectPage(pageReqVO);
    }

}