package com.lutao.cloud.admin.mq;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.lutao.cloud.admin.api.entity.SysUser;
import com.lutao.cloud.admin.client.CasstHttpClient;
import com.lutao.cloud.admin.config.SysnDataProperties;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@AllArgsConstructor
@Slf4j
@Service
public class UserMqMessage {

    private final SysnDataProperties sysnDataProperties;

    private final CasstHttpClient casstHttpClient;

    private final static String USER_TOP = "/accident/user/sync";

    public final static String USER_ADD = "USER_ADD";
    public final static String USER_UPDATE = "USER_UPDATE";
    public final static String USER_DELETE = "USER_DELETE";

//    private RocketMQTemplate rocketMQTemplate;

    public void sendMessage(SysUser sysUser,String command){
        JSONObject jsonObject = new JSONObject();
        jsonObject.putOnce("command",command);

        JSONObject userJson = new JSONObject();
        userJson.putOnce("username",sysUser.getUsername());
        userJson.putOnce("deptId",sysUser.getDeptId());
        userJson.putOnce("nickname",sysUser.getNickname());
        userJson.putOnce("id",sysUser.getUserId());

        jsonObject.putOnce("userInfo",userJson);
        try {
            if (StrUtil.isNotBlank(sysnDataProperties.getSysnUrl())) {
                JSONObject retJson = casstHttpClient.sendSynData(sysnDataProperties.getSysnUrl(), JSONUtil.toJsonStr(jsonObject));
                log.info("sendMessage user:{} retJson:{}",jsonObject,retJson);
            }
        }catch (Exception e) {
            log.error("同步用户数据失败:{}",jsonObject);
        }

//        rocketMQTemplate.convertAndSend(USER_TOP,jsonObject);
    }
}
