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 com.baomidou.mybatisplus.core.toolkit.IdWorker;

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.framework.mq.redis.core.RedisMQTemplate;
import com.zt.plat.module.qms.business.config.controller.vo.ConfigSimpleFlowCodePageReqVO;
import com.zt.plat.module.qms.business.config.controller.vo.ConfigSimpleFlowCodeReqVO;
import com.zt.plat.module.qms.business.config.controller.vo.ConfigSimpleFlowCodeRespVO;
import com.zt.plat.module.qms.business.config.controller.vo.ConfigSimpleFlowCodeSaveReqVO;
import com.zt.plat.module.qms.business.config.dal.dataobject.ConfigSimpleFlowCodeDO;
import com.zt.plat.module.qms.business.config.dal.mapper.ConfigSimpleFlowCodeMapper;
import com.zt.plat.module.qms.core.liteflow.handler.RedisLiteFlowMessage;

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.*;

/**
 * LiteFlow脚本配置 Service 实现类
 *
 * @author 后台管理
 */
@Service
@Validated
public class ConfigSimpleFlowCodeServiceImpl implements ConfigSimpleFlowCodeService {
	
	@Resource
	private RedisMQTemplate redisMQTemplate;

    @Resource
    private ConfigSimpleFlowCodeMapper configSimpleFlowCodeMapper;

    @Override
    public ConfigSimpleFlowCodeRespVO createConfigSimpleFlowCode(ConfigSimpleFlowCodeSaveReqVO createReqVO) {
        // 插入
        ConfigSimpleFlowCodeDO configSimpleFlowCode = BeanUtils.toBean(createReqVO, ConfigSimpleFlowCodeDO.class);
        configSimpleFlowCodeMapper.insert(configSimpleFlowCode);
        redisMQTemplate.send(new RedisLiteFlowMessage().setId(IdWorker.getId()));
        // 返回
        return BeanUtils.toBean(configSimpleFlowCode, ConfigSimpleFlowCodeRespVO.class);
    }

    @Override
    public void updateConfigSimpleFlowCode(ConfigSimpleFlowCodeSaveReqVO updateReqVO) {
        // 校验存在
        validateConfigSimpleFlowCodeExists(updateReqVO.getId());
        // 更新
        ConfigSimpleFlowCodeDO updateObj = BeanUtils.toBean(updateReqVO, ConfigSimpleFlowCodeDO.class);
        configSimpleFlowCodeMapper.updateById(updateObj);
        redisMQTemplate.send(new RedisLiteFlowMessage().setId(IdWorker.getId()));
    }

    @Override
    public void deleteConfigSimpleFlowCode(Long id) {
        // 校验存在
        validateConfigSimpleFlowCodeExists(id);
        // 删除
        configSimpleFlowCodeMapper.deleteById(id);
        redisMQTemplate.send(new RedisLiteFlowMessage().setId(IdWorker.getId()));
    }

    @Override
        public void deleteConfigSimpleFlowCodeListByIds(List<Long> ids) {
        // 校验存在
        validateConfigSimpleFlowCodeExists(ids);
        // 删除
        configSimpleFlowCodeMapper.deleteByIds(ids);
        redisMQTemplate.send(new RedisLiteFlowMessage().setId(IdWorker.getId()));
        }

    private void validateConfigSimpleFlowCodeExists(List<Long> ids) {
        List<ConfigSimpleFlowCodeDO> list = configSimpleFlowCodeMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(CONFIG_SIMPLE_FLOW_CODE_NOT_EXISTS);
        }
    }

    private void validateConfigSimpleFlowCodeExists(Long id) {
        if (configSimpleFlowCodeMapper.selectById(id) == null) {
            throw exception(CONFIG_SIMPLE_FLOW_CODE_NOT_EXISTS);
        }
    }

    @Override
    public ConfigSimpleFlowCodeDO getConfigSimpleFlowCode(Long id) {
        return configSimpleFlowCodeMapper.selectById(id);
    }

    @Override
    public PageResult<ConfigSimpleFlowCodeDO> getConfigSimpleFlowCodePage(ConfigSimpleFlowCodePageReqVO pageReqVO) {
        return configSimpleFlowCodeMapper.selectPage(pageReqVO);
    }

	@Override
	public List<ConfigSimpleFlowCodeDO> getConfigSimpleFlowCodeList() {
		return configSimpleFlowCodeMapper.selectList();
	}

	@Override
	public List<ConfigSimpleFlowCodeDO> getConfigSimpleFlowCodeList(ConfigSimpleFlowCodeReqVO reqVO) {
		return configSimpleFlowCodeMapper.selectList(reqVO);
	}

}