package com.unis.crk.rabbitmq;

import cn.hutool.core.util.StrUtil;
import com.rabbitmq.client.Channel;
import com.unis.crk.basic.entity.OrgInfo;
import com.unis.crk.basic.service.IOrgInfoService;
import com.unis.crk.common.service.IDataCenterDownloadService;
import com.unis.crk.common.service.IDataCenterUploadService;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Component;

import java.util.Iterator;
import java.util.List;
import java.util.Map;




/**
 * @description: 【监听MQ】
 */
//@Component
public class RabbitListener  {

    @Autowired
    IDataCenterDownloadService dataCenterDownloadService;
    @Autowired
    IDataCenterUploadService dataCenterUploadService;
    @Autowired
    private IOrgInfoService orgInfoService;

    @org.springframework.amqp.rabbit.annotation.RabbitListener(queues = RabbitConfig.QUEUE_NAME)
    public void testListener( String object, Channel channel,
                              @Headers Map<String, Object> headers,
                              @Header(value = AmqpHeaders.RECEIVED_ROUTING_KEY) String routingKey) {
        try {
/*
            String routerKey_ = "lcapPatrol.PurchaseOrder,lcapPatrol.SalesOrder,lcapPatrol.SalesChangeOrder,lcapPatrol.PurchaseChangeOrder,lcapPatrol.PurchaseExchangeOrder,lcapPatrol.SalesExchangeOrder";
            if (StringUtils.indexOf(routerKey_,routingKey) >= 0 ){
                dataCenterDownloadService.savetTzd(object,routingKey);
                return;
            }
            //原粮通知单
            routerKey_ = "lcapPatrol.WarehousingNotice,lcapPatrol.OutboundNotice";
            if (StringUtils.indexOf(routerKey_,routingKey) >= 0 ){
                dataCenterDownloadService.savetYlTzd(object,routingKey);
                return;
            }
            //车辆信息数据
            if (StringUtils.indexOf("lcapPatrol.vehicle",routingKey) >= 0 ){
                dataCenterDownloadService.saveClxx(object,routingKey);
                return;
            }
            //物流人员信息数据
            if (StringUtils.indexOf("lcapPatrol.logisticsPersonnelInformation",routingKey) >= 0 ){
                dataCenterDownloadService.savetRyxx(object,routingKey);
                return;
            }
            //计量单位信息数据
            if (StringUtils.indexOf("lcapPatrol.MeasurementUnits",routingKey) >= 0 ){
                dataCenterDownloadService.savetjldw(object,routingKey);
                return;
            }
            //品种信息数据
            if (StringUtils.indexOf("lcapPatrol.CultivatedVarieties",routingKey) >= 0 ){
                dataCenterDownloadService.savetpz(object,routingKey);
                return;
            }
            //品牌信息
            if(StringUtils.indexOf("lcapPatrol.BrandZpt",routingKey) >= 0 ){
                dataCenterDownloadService.savetpp(object,routingKey);
                return;
            }
            //市平台单位信息
            if( StringUtils.indexOf("lcapPatrol.SupplyStationSpt",routingKey) >= 0 ){
                dataCenterDownloadService.savetsdwxx(object,routingKey);
                return;
            }
            //站平台单位信息
            if( StringUtils.indexOf("lcapPatrol.SupplyStationZpt",routingKey) >= 0 ){
                dataCenterDownloadService.savetzdwxx(object,routingKey);
                return;
            }
            //商品信息
           if( StringUtils.indexOf("lcapPatrol.Materiel",routingKey) >= 0 ){
            dataCenterDownloadService.savesp(object,routingKey);
             return;
            }
            //仓房信息
            if( StringUtils.indexOf("lcapPatrol.WareHouse",routingKey) >= 0 ){
                dataCenterDownloadService.saveCf(object,routingKey);
                return;
            }

            //货位信息
            if( StringUtils.indexOf("lcapPatrol.goodsAllocation",routingKey) >= 0 ){
                dataCenterDownloadService.saveHw(object,routingKey);
                return;
            }
            //生产加工结果
            if( StringUtils.indexOf("lcapPatrol.ProductionResult",routingKey) >= 0 ){
                dataCenterDownloadService.saveScjg(object,routingKey);
                return;
            }
            //性质转换入库——出入库移仓
            if( StringUtils.indexOf("lcapPatrol.TransformInGoodsDetail",routingKey) >= 0){
                dataCenterDownloadService.saveYcr(object,routingKey);
                return;
            }
            //性质转换出库——出入库移仓
            if(StringUtils.indexOf("lcapPatrol.TransformOutGoodsDetail",routingKey) >= 0){
                dataCenterDownloadService.saveYcc(object,routingKey);
                return;
            }
            //供应商
            if(StringUtils.indexOf("lcapPatrol.Supplier",routingKey) >= 0){
                dataCenterDownloadService.saveGys(object,routingKey);
                return;
            }
            //质检机构
            if(StringUtils.indexOf("lcapPatrol.QualityInspectionOrganization",routingKey) >= 0){
                dataCenterDownloadService.saveZjjg(object,routingKey);
                return;
            }
            //粮质量标准
            if(StringUtils.indexOf("lcapPatrol.QualityStandard",routingKey) >= 0){
                dataCenterDownloadService.saveZlbz(object,routingKey);
                return;
            }*/

        }catch (Exception e){
            e.printStackTrace();
        }

    }
    //接收出入库这边指定的rq,进行数据上传
    @org.springframework.amqp.rabbit.annotation.RabbitListener(queues = RabbitConfig.CRK_QUEUE_NAME)
    public void crkListener( String object, Channel channel,
                              @Headers Map<String, Object> headers,
                              @Header(value = AmqpHeaders.RECEIVED_ROUTING_KEY) String routingKey) {
        System.out.println("自我监听数据上传");
      /*  String[] strs = object.split("\\|");
        String topic = strs[0];
        String methods = strs[1];
        List<OrgInfo> allChildren = orgInfoService.getAllChildren("00", "");
        Iterator<OrgInfo> iterator = allChildren.iterator();
        while (iterator.hasNext()){
            OrgInfo next = iterator.next();
            dataCenterUploadService.uploadSjzxDataByTopic(next.getOrgId(),topic);
            //删除内容单独上传，只在删除的时候上传
            if (StrUtil.equals("删除",methods) || methods.contains("库存")){
                dataCenterUploadService.deleteSjzxData(next.getOrgId(),topic);
            }
        }*/
    }

/*    @org.springframework.amqp.rabbit.annotation.RabbitListener(queues = RabbitConfig.DS_QUEUE_NAME)
    public void ListenerDs( String object, Channel channel,
                              @Headers Map<String, Object> headers,
                              @Header(value = AmqpHeaders.RECEIVED_ROUTING_KEY) String routingKey) {
        try {
            String routerKey_ = "dsInOutLib.storeConfirm,dsInOutLib.delivery";
            //售后退货订单
            if(StringUtils.indexOf("dsInOutLib.storeConfirm",routingKey) >= 0){
               // dataCenterDownloadService.storeConfirm(object,routingKey);
                return;
                //发货订单
            }if(StringUtils.indexOf("dsInOutLib.delivery",routingKey) >= 0){
               // dataCenterDownloadService.delivery(object,routingKey);
                return;
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }*/
}
