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.ConfigSimpleFlowRulePageReqVO;
import com.zt.plat.module.qms.business.config.controller.vo.ConfigSimpleFlowRuleReqVO;
import com.zt.plat.module.qms.business.config.controller.vo.ConfigSimpleFlowRuleRespVO;
import com.zt.plat.module.qms.business.config.controller.vo.ConfigSimpleFlowRuleSaveReqVO;
import com.zt.plat.module.qms.business.config.dal.dataobject.ConfigSimpleFlowRuleDO;
import com.zt.plat.module.qms.business.config.dal.mapper.ConfigSimpleFlowRuleMapper;
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 ConfigSimpleFlowRuleServiceImpl implements ConfigSimpleFlowRuleService {
	
	@Resource
	private RedisMQTemplate redisMQTemplate;

    @Resource
    private ConfigSimpleFlowRuleMapper configSimpleFlowRuleMapper;

    @Override
    public ConfigSimpleFlowRuleRespVO createConfigSimpleFlowRule(ConfigSimpleFlowRuleSaveReqVO createReqVO) {
        // 插入
        ConfigSimpleFlowRuleDO configSimpleFlowRule = BeanUtils.toBean(createReqVO, ConfigSimpleFlowRuleDO.class);
        configSimpleFlowRuleMapper.insert(configSimpleFlowRule);
        redisMQTemplate.send(new RedisLiteFlowMessage().setId(IdWorker.getId()));
        // 返回
        return BeanUtils.toBean(configSimpleFlowRule, ConfigSimpleFlowRuleRespVO.class);
    }

    @Override
    public void updateConfigSimpleFlowRule(ConfigSimpleFlowRuleSaveReqVO updateReqVO) {
        // 校验存在
        validateConfigSimpleFlowRuleExists(updateReqVO.getId());
        // 更新
        ConfigSimpleFlowRuleDO updateObj = BeanUtils.toBean(updateReqVO, ConfigSimpleFlowRuleDO.class);
        configSimpleFlowRuleMapper.updateById(updateObj);
        redisMQTemplate.send(new RedisLiteFlowMessage().setId(IdWorker.getId()));
    }

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

    @Override
    public void deleteConfigSimpleFlowRuleListByIds(List<Long> ids) {
        // 校验存在
        validateConfigSimpleFlowRuleExists(ids);
        // 删除
        configSimpleFlowRuleMapper.deleteByIds(ids);

        redisMQTemplate.send(new RedisLiteFlowMessage().setId(IdWorker.getId()));
    }

    private void validateConfigSimpleFlowRuleExists(List<Long> ids) {
        List<ConfigSimpleFlowRuleDO> list = configSimpleFlowRuleMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(CONFIG_SIMPLE_FLOW_RULE_NOT_EXISTS);
        }
    }

    private void validateConfigSimpleFlowRuleExists(Long id) {
        if (configSimpleFlowRuleMapper.selectById(id) == null) {
            throw exception(CONFIG_SIMPLE_FLOW_RULE_NOT_EXISTS);
        }
    }

    @Override
    public ConfigSimpleFlowRuleDO getConfigSimpleFlowRule(Long id) {
        return configSimpleFlowRuleMapper.selectById(id);
    }

    @Override
    public PageResult<ConfigSimpleFlowRuleDO> getConfigSimpleFlowRulePage(ConfigSimpleFlowRulePageReqVO pageReqVO) {
        return configSimpleFlowRuleMapper.selectPage(pageReqVO);
    }

	@Override
	public List<ConfigSimpleFlowRuleDO> getConfigSimpleFlowRuleList() {
		return configSimpleFlowRuleMapper.selectList();
	}

	@Override
	public List<ConfigSimpleFlowRuleDO> getConfigSimpleFlowRuleList(ConfigSimpleFlowRuleReqVO reqVO) {
		return configSimpleFlowRuleMapper.selectList(reqVO);
	}

}