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.ConfigQCStandardSampleDO;
import com.zt.plat.module.qms.business.config.dal.mapper.ConfigQCStandardSampleMapper;

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 ConfigQCStandardSampleServiceImpl implements ConfigQCStandardSampleService {

    @Resource
    private ConfigQCStandardSampleMapper configQCStandardSampleMapper;

    @Override
    public ConfigQCStandardSampleRespVO createConfigQCStandardSample(ConfigQCStandardSampleSaveReqVO createReqVO) {
        // 插入
        ConfigQCStandardSampleDO configQCStandardSample = BeanUtils.toBean(createReqVO, ConfigQCStandardSampleDO.class);
        configQCStandardSampleMapper.insert(configQCStandardSample);
        // 返回
        return BeanUtils.toBean(configQCStandardSample, ConfigQCStandardSampleRespVO.class);
    }

    @Override
    public void updateConfigQCStandardSample(ConfigQCStandardSampleSaveReqVO updateReqVO) {
        // 校验存在
        validateConfigQCStandardSampleExists(updateReqVO.getId());
        // 更新
        ConfigQCStandardSampleDO updateObj = BeanUtils.toBean(updateReqVO, ConfigQCStandardSampleDO.class);
        configQCStandardSampleMapper.updateById(updateObj);
    }

    @Override
    public void deleteConfigQCStandardSample(Long id) {
        // 校验存在
        validateConfigQCStandardSampleExists(id);
        // 删除
        configQCStandardSampleMapper.deleteById(id);
    }

    @Override
        public void deleteConfigQCStandardSampleListByIds(List<Long> ids) {
        // 校验存在
        validateConfigQCStandardSampleExists(ids);
        // 删除
        configQCStandardSampleMapper.deleteByIds(ids);
        }

    private void validateConfigQCStandardSampleExists(List<Long> ids) {
        List<ConfigQCStandardSampleDO> list = configQCStandardSampleMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(CONFIG_QC_STANDARD_SAMPLE_NOT_EXISTS);
        }
    }

    private void validateConfigQCStandardSampleExists(Long id) {
        if (configQCStandardSampleMapper.selectById(id) == null) {
            throw exception(CONFIG_QC_STANDARD_SAMPLE_NOT_EXISTS);
        }
    }

    @Override
    public ConfigQCStandardSampleDO getConfigQCStandardSample(Long id) {
        return configQCStandardSampleMapper.selectById(id);
    }

    @Override
    public PageResult<ConfigQCStandardSampleDO> getConfigQCStandardSamplePage(ConfigQCStandardSamplePageReqVO pageReqVO) {
        return configQCStandardSampleMapper.selectPage(pageReqVO);
    }

}