package com.zcloud.primeport.command;

import cn.hutool.json.JSONUtil;
import com.alibaba.cola.dto.SingleResponse;
import com.alibaba.fastjson.JSONObject;
import com.jjb.saas.message.client.message.facede.MessageFacade;
import com.jjb.saas.message.client.message.request.MessageSendCmd;
import com.jjb.saas.message.client.message.request.MessageTargetCmd;
import com.zcloud.gbscommon.todolistmq.TodoListEventPusherUtil;
import com.zcloud.gbscommon.todolistmq.event.TodoListAddEvent;
import com.zcloud.gbscommon.todolistmq.event.TodoListCompleteEvent;
import com.zcloud.gbscommon.todolistmq.event.TodoListDeleteEvent;
import com.zcloud.gbscommon.utils.Tools;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Map;


/**
 * web-app
 *
 * @Author txl
 * @Date 2026-07-06 16:05:38
 */
@Component
@AllArgsConstructor
@Slf4j
public class MessageNoticeExe {
    private final TodoListEventPusherUtil todoListEventPusherUtil;
    @DubboReference
    private MessageFacade messageFacade;

    public void sendMessageNotice(Long userId, String sourceCode,Map<String, Object> sendParams) {
        //消息通知
        try{

            MessageSendCmd messageSendCmd = new MessageSendCmd();
            messageSendCmd.setBusinessId(Tools.get32UUID());
            MessageTargetCmd messageTargetCmd = new MessageTargetCmd();
            messageTargetCmd.setUserId(userId);
            messageSendCmd.setTargetCmd(messageTargetCmd);
            messageSendCmd.setSourceCode(sourceCode);
            messageSendCmd.setNeedTokenEnum(false);
            messageSendCmd.setParams(sendParams);

            log.info("{},消息提醒发送消息参数:messageSendCmd: {}",sourceCode, JSONUtil.toJsonStr(messageSendCmd));
            SingleResponse<Boolean> d = messageFacade.send(messageSendCmd);
            log.info("{},消息提醒发送消息结果:d: {}", sourceCode,JSONUtil.toJsonStr(d));
        }catch (Exception e) {
            log.error("{},消息提醒发送消息异常:", sourceCode,e);
        }
    }

    /**
     *
     * @param userId 接收人ID
     * @param title 标题
     * @param content 内容
     * @param foreignKey 业务表ID
     * @param foreignSubsidiaryKey 业务附表ID
     * @param pcFlag 是否是PC端待办
     * @param appFlag 是否是APP端待办
     */
    @Async("asyncExecutor")
    public void  sendMessageTodoEvent(Long userId, String title,String content, Long foreignKey, Long foreignSubsidiaryKey, Integer pcFlag, Integer appFlag) {
        //消息通知
        try{
            // 发送待办
            TodoListAddEvent event = new TodoListAddEvent();
            event.setTitle(title);
            event.setContent(content);
            event.setForeignKey(foreignKey); // 业务表ID -申请
            event.setForeignSubsidiaryKey(foreignSubsidiaryKey); // 业务附表ID 没有附表时为foreignKey的值
            event.setReceiveUser(userId);// user表ID
            event.setPcFlag(pcFlag); // 是否PC端待办 1是 0否
            event.setAppFlag(appFlag); // 是否APP端待办 1是 0否
            event.setOtherParams(new JSONObject());
            log.info("待办,请求：{}",event);
            //为了防止先被完成
            Thread.sleep(2000L);
            boolean b = todoListEventPusherUtil.sendMessageAddEvent(event);
            log.info("待办,结果：{}",b);
        }catch (Exception e) {
            log.error("消息待办,{},发送消息异常:", title,e);
        }
    }

    public void sendMessageCompleteEvent(Long foreignKey) {
        //消息通知
        try{
            // 发送待办
            TodoListCompleteEvent event = new TodoListCompleteEvent();
            event.setForeignSubsidiaryKey(foreignKey);// 业务附表ID 没有附表时为foreignKey的值
            log.info("完成待办,请求：{}",foreignKey);
            boolean b = todoListEventPusherUtil.sendMessageCompleteEvent(event);
            log.info("完成待办,结果：{}",b);
        }catch (Exception e) {
            log.error("完成待办失败",e);

        }
    }

    public void sendMessageDeleteEvent(Long foreignKey) {
        try{
            TodoListDeleteEvent event2 = new TodoListDeleteEvent();
            event2.setForeignSubsidiaryKey(foreignKey);// 业务附表ID 没有附表时为foreignKey的值

            log.info("删除待办,请求：{}",foreignKey);
            boolean b = todoListEventPusherUtil.sendMessageDeleteEvent(event2);
            log.info("删除待办,结果：{}",b);
        }catch (Exception e) {
            log.error("完成待办失败",e);

        }
    }
}

