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.ConfigEntrustSourcePageReqVO;
import com.zt.plat.module.qms.business.config.controller.vo.ConfigEntrustSourceReqVO;
import com.zt.plat.module.qms.business.config.controller.vo.ConfigEntrustSourceRespVO;
import com.zt.plat.module.qms.business.config.controller.vo.ConfigEntrustSourceSaveReqVO;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;

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.ConfigEntrustSourceDO;
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.ConfigEntrustSourceMapper;

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

/**
 * 检验委托来源配置 Service 实现类
 *
 * @author 后台管理
 */
@Service
@Validated
public class ConfigEntrustSourceServiceImpl implements ConfigEntrustSourceService {

    @Resource
    private ConfigEntrustSourceMapper configEntrustSourceMapper;

    @Override
    public ConfigEntrustSourceRespVO createConfigEntrustSource(ConfigEntrustSourceSaveReqVO createReqVO) {
        // 插入
        ConfigEntrustSourceDO configEntrustSource = BeanUtils.toBean(createReqVO, ConfigEntrustSourceDO.class);
        configEntrustSourceMapper.insert(configEntrustSource);
        // 返回
        return BeanUtils.toBean(configEntrustSource, ConfigEntrustSourceRespVO.class);
    }

    @Override
    public void updateConfigEntrustSource(ConfigEntrustSourceSaveReqVO updateReqVO) {
        // 校验存在
        validateConfigEntrustSourceExists(updateReqVO.getId());
        // 更新
        ConfigEntrustSourceDO updateObj = BeanUtils.toBean(updateReqVO, ConfigEntrustSourceDO.class);
        configEntrustSourceMapper.updateById(updateObj);
    }

    @Override
    public void deleteConfigEntrustSource(Long id) {
        // 校验存在
        validateConfigEntrustSourceExists(id);
        // 删除
        configEntrustSourceMapper.deleteById(id);
    }

    @Override
        public void deleteConfigEntrustSourceListByIds(List<Long> ids) {
        // 校验存在
        validateConfigEntrustSourceExists(ids);
        // 删除
        configEntrustSourceMapper.deleteByIds(ids);
        }

    private void validateConfigEntrustSourceExists(List<Long> ids) {
        List<ConfigEntrustSourceDO> list = configEntrustSourceMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(CONFIG_ENTRUST_SOURCE_NOT_EXISTS);
        }
    }

    private void validateConfigEntrustSourceExists(Long id) {
        if (configEntrustSourceMapper.selectById(id) == null) {
            throw exception(CONFIG_ENTRUST_SOURCE_NOT_EXISTS);
        }
    }

    @Override
    public ConfigEntrustSourceDO getConfigEntrustSource(Long id) {
        return configEntrustSourceMapper.selectById(id);
    }

    @Override
    public PageResult<ConfigEntrustSourceDO> getConfigEntrustSourcePage(ConfigEntrustSourcePageReqVO pageReqVO) {
        return configEntrustSourceMapper.selectPage(pageReqVO);
    }

	@Override
	public List<ConfigEntrustSourceDO> getConfigEntrustSourceList(@Valid ConfigEntrustSourceReqVO reqVO) {
		return configEntrustSourceMapper.selectList(reqVO);
	}

}