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.ConfigSampleFlowPageReqVO;
import com.zt.plat.module.qms.business.config.controller.vo.ConfigSampleFlowRespVO;
import com.zt.plat.module.qms.business.config.controller.vo.ConfigSampleFlowSaveReqVO;
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.ConfigSampleFlowDO;
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.ConfigSampleFlowMapper;

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

/**
 * 样品流程配置 Service 实现类
 *
 * @author 后台管理
 */
@Service
@Validated
public class ConfigSampleFlowServiceImpl implements ConfigSampleFlowService {

    @Resource
    private ConfigSampleFlowMapper configSampleFlowMapper;

    @Override
    public ConfigSampleFlowRespVO createConfigSampleFlow(ConfigSampleFlowSaveReqVO createReqVO) {
        // 插入
        ConfigSampleFlowDO configSampleFlow = BeanUtils.toBean(createReqVO, ConfigSampleFlowDO.class);
        configSampleFlowMapper.insert(configSampleFlow);
        // 返回
        return BeanUtils.toBean(configSampleFlow, ConfigSampleFlowRespVO.class);
    }

    @Override
    public void updateConfigSampleFlow(ConfigSampleFlowSaveReqVO updateReqVO) {
        // 校验存在
        validateConfigSampleFlowExists(updateReqVO.getId());
        // 更新
        ConfigSampleFlowDO updateObj = BeanUtils.toBean(updateReqVO, ConfigSampleFlowDO.class);
        configSampleFlowMapper.updateById(updateObj);
    }

    @Override
    public void deleteConfigSampleFlow(Long id) {
        // 校验存在
        validateConfigSampleFlowExists(id);
        // 删除
        configSampleFlowMapper.deleteById(id);
    }

    @Override
        public void deleteConfigSampleFlowListByIds(List<Long> ids) {
        // 校验存在
        validateConfigSampleFlowExists(ids);
        // 删除
        configSampleFlowMapper.deleteByIds(ids);
        }

    private void validateConfigSampleFlowExists(List<Long> ids) {
        List<ConfigSampleFlowDO> list = configSampleFlowMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(CONFIG_SAMPLE_FLOW_NOT_EXISTS);
        }
    }

    private void validateConfigSampleFlowExists(Long id) {
        if (configSampleFlowMapper.selectById(id) == null) {
            throw exception(CONFIG_SAMPLE_FLOW_NOT_EXISTS);
        }
    }

    @Override
    public ConfigSampleFlowDO getConfigSampleFlow(Long id) {
        return configSampleFlowMapper.selectById(id);
    }

    @Override
    public PageResult<ConfigSampleFlowDO> getConfigSampleFlowPage(ConfigSampleFlowPageReqVO pageReqVO) {
        return configSampleFlowMapper.selectPage(pageReqVO);
    }

	@Override
	public List<ConfigSampleFlowDO> getHistoryConfigSampleFlowByKey(String key) {
		return configSampleFlowMapper.selectHistoryConfigSampleByKey(key);
	}

	@Override
	public ConfigSampleFlowDO getLatestConfigSampleFlowByKey(String key) {
		return configSampleFlowMapper.selectLatestConfigSampleByKey(key);
	}

	@Override
	public List<ConfigSampleFlowDO> getLatestConfigSampleFlowListByKeys(List<String> keyList) {
		return configSampleFlowMapper.selectLatestConfigSampleFlowListByKeys(keyList);
	}

}