package me.zhengjie.modules.quartz.task;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.modules.test.TestController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.ParseException;

@Slf4j
@RequiredArgsConstructor
@Component
public class DayPcDataTask {
    @Autowired
    public TestController controller;

    public void run() throws ParseException {
        log.info("run 执行开始");
        send();
        log.info("run 执行结束");
    }

    /**
     * 排菜生产者
     */
    public void send() throws ParseException {
        controller.testSend();
        //获取连接
//        Connection connection = ConnectionUtil.getConnection();
//        try {
//            List<DmDayPc> dmDayPc = dmDayPcService.foodRepository();
//            String userString = JSON.toJSONString(dmDayPc);
//            Channel channel = connection.createChannel();
//            String exchangeName = "schedule_consume";//交换机
//            channel.exchangeDeclare(exchangeName, BuiltinExchangeType.DIRECT, true,false, null);
//            String queue1Name = "schedule_consume";//队列名称
//            channel.queueDeclare(queue1Name, true, false, false, null);
//            channel.queueBind(queue1Name, exchangeName, "schedule_consume");// 队列绑定hello路由
//            channel.basicPublish(exchangeName, "schedule_consume", null, userString.getBytes());
//            System.out.println("sned排菜:"+dmDayPc);
//            channel.close();
//            connection.close();
//        } catch (IOException | TimeoutException e) {
//            e.printStackTrace();
//        }
    }

//    /**
//     * 消费者
//     */
//    public void receive(){
//        Connection connection = ConnectionUtil.getConnection();
//        try {
//            Channel channel = connection.createChannel();
//            //通过consumer来处理数据
//            Consumer consumer = new DefaultConsumer(channel){
//                @SneakyThrows
//                @Override
//                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
//                    //body就是从队列中获取的数据
//                    String str = new String(body);
//                    if (str==null){
//                        System.out.println("erropc"+str);
//                        System.out.println("erropc"+body);
//                    }else {
//                        System.out.println("sucesspc"+str);
//                        System.out.println("sucesspc"+body);
//                    }
//                    System.out.println("dingshipc"+str);
//                    List<DmDayPc>  list = JSONArray.parseArray(str,DmDayPc.class);
//                    for (DmDayPc dmDayPc : list) {
//                        dmDayPc.setPcdate(new Timestamp(System.currentTimeMillis()));
//                        dmDayPcService.create(dmDayPc);
//                        System.out.println("新增food==="+dmDayPc.getFood());
//                        dmFoodService.create(dmDayPc.getFood());
//                    }
//                }
//            };
//            //参数1：接收哪个队列的数据
//            //参数2：消息确认 是否应答，收到消息是否回复
//            //参数3：
//            channel.basicConsume("schedule_produce",true,consumer);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }
}
