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.ConfigSampleHandoverPageReqVO;
import com.zt.plat.module.qms.business.config.controller.vo.ConfigSampleHandoverRespVO;
import com.zt.plat.module.qms.business.config.controller.vo.ConfigSampleHandoverSaveReqVO;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;

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

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 ConfigSampleHandoverServiceImpl implements ConfigSampleHandoverService {

    @Resource
    private ConfigSampleHandoverMapper configSampleHandoverMapper;

    @Override
    public ConfigSampleHandoverRespVO createConfigSampleHandover(ConfigSampleHandoverSaveReqVO createReqVO) {
        // 插入
        ConfigSampleHandoverDO configSampleHandover = BeanUtils.toBean(createReqVO, ConfigSampleHandoverDO.class);
        configSampleHandoverMapper.insert(configSampleHandover);
        // 返回
        return BeanUtils.toBean(configSampleHandover, ConfigSampleHandoverRespVO.class);
    }

    @Override
    public void updateConfigSampleHandover(ConfigSampleHandoverSaveReqVO updateReqVO) {
        // 校验存在
        validateConfigSampleHandoverExists(updateReqVO.getId());
        // 更新
        ConfigSampleHandoverDO updateObj = BeanUtils.toBean(updateReqVO, ConfigSampleHandoverDO.class);
        configSampleHandoverMapper.updateById(updateObj);
    }

    @Override
    public void deleteConfigSampleHandover(Long id) {
        // 校验存在
        validateConfigSampleHandoverExists(id);
        // 删除
        configSampleHandoverMapper.deleteById(id);
    }

    @Override
        public void deleteConfigSampleHandoverListByIds(List<Long> ids) {
        // 校验存在
        validateConfigSampleHandoverExists(ids);
        // 删除
        configSampleHandoverMapper.deleteByIds(ids);
        }

    private void validateConfigSampleHandoverExists(List<Long> ids) {
        List<ConfigSampleHandoverDO> list = configSampleHandoverMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(CONFIG_SAMPLE_HANDOVER_NOT_EXISTS);
        }
    }

    private void validateConfigSampleHandoverExists(Long id) {
        if (configSampleHandoverMapper.selectById(id) == null) {
            throw exception(CONFIG_SAMPLE_HANDOVER_NOT_EXISTS);
        }
    }

    @Override
    public ConfigSampleHandoverDO getConfigSampleHandover(Long id) {
        return configSampleHandoverMapper.selectById(id);
    }

    @Override
    public PageResult<ConfigSampleHandoverDO> getConfigSampleHandoverPage(ConfigSampleHandoverPageReqVO pageReqVO) {
        return configSampleHandoverMapper.selectPage(pageReqVO);
    }

}