package com.dxhy.admin.sys.rabbitmq;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dxhy.admin.sys.service.SysTabulateDeptService;

import lombok.extern.slf4j.Slf4j;

/**
 * @author jiaohongyang（焦红阳）
 * @date 2020-09-25
 */
@Component
@Slf4j
public class AdminRabbitMq {

    @Resource
    private SysTabulateDeptService sysTabulateDeptService;

    /**
     * 菜单保存显示列表信息保存消费者
     *
     * @param menuJson
     *            显示列表信息
     */
    @RabbitHandler
    @RabbitListener(queues = "${TO_MENU_TABULATE:TO_MENU_TABULATE}")
    public void handleMessage(String menuJson) {
        try {
            log.info("菜单显示列表信息消费者开始：{}", menuJson);
            JSONObject jsonObject = JSON.parseObject(menuJson, JSONObject.class);
            JSONArray menuId = jsonObject.getJSONArray("menuId");
            String company = jsonObject.getString("company");
            List<Long> ids = menuId.toJavaList(Long.class);
            sysTabulateDeptService.saveListTablate(ids, company);
            log.info("菜单显示列表信息消费者结束：{}", menuJson);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
