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.SysDept;
//import com.lutao.cloud.admin.client.CasstHttpClient;
//import lombok.AllArgsConstructor;
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.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

@AllArgsConstructor
@Slf4j
@Service
public class DeptMqMessage {

//    @Value("${sync.url}")
//    private String sysnUrl;

    private final SysnDataProperties sysnDataProperties;

    private final CasstHttpClient casstHttpClient;

    private final static String DEPT_TOP = "/accident/dept/sync";

    public final static String DEPT_ADD = "DEPT_ADD";
    public final static String DEPT_UPDATE = "DEPT_UPDATE";
    public final static String DEPT_DELETE = "DEPT_DELETE";

//    private RocketMQTemplate rocketMQTemplate;

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

        JSONObject userJson = new JSONObject();
        userJson.putOnce("name",sysDept.getName());
        userJson.putOnce("deptId",sysDept.getDeptId());
        userJson.putOnce("sort",sysDept.getSort());
        userJson.putOnce("parentId",sysDept.getParentId());

        jsonObject.putOnce("deptInfo",userJson);
        try {
            if (StrUtil.isNotBlank(sysnDataProperties.getSysnUrl())) {
                JSONObject retJson = casstHttpClient.sendSynData(sysnDataProperties.getSysnUrl(), JSONUtil.toJsonStr(jsonObject));
                log.info("sendMessage dept:{} retJson:{}",jsonObject,retJson);
            }
        } catch (Exception e) {
            log.error("同步部门失败:{}",jsonObject);
        }
//        rocketMQTemplate.convertAndSend(DEPT_TOP,jsonObject);
    }

    public void sendMessage(List<Integer> idList, Integer parentId, String command) {
        idList.forEach(id -> {
            JSONObject jsonObject = new JSONObject();
            jsonObject.putOnce("command",command);

            JSONObject userJson = new JSONObject();
            userJson.putOnce("name",null);
            userJson.putOnce("deptId",id);
            userJson.putOnce("sort",null);
            userJson.putOnce("parentId",parentId);

            jsonObject.putOnce("deptInfo",userJson);
            try {
                if (StrUtil.isNotBlank(sysnDataProperties.getSysnUrl())) {
                    JSONObject retJson = casstHttpClient.sendSynData(sysnDataProperties.getSysnUrl(),JSONUtil.toJsonStr(jsonObject));
                    log.info(" sendMessage dept:{} retJson:{}",jsonObject,retJson);
                }
            } catch (Exception e) {
                log.error("同步部门失败:{}",jsonObject);
            }
//            rocketMQTemplate.convertAndSend(DEPT_TOP,jsonObject);
        });

    }
}
