package com.heyu.api.controller.vv;

import com.alibaba.fastjson.JSON;
import com.heyu.api.alibaba.request.vv.AppOrderRequest;
import com.heyu.api.common.annotation.Describe;
import com.heyu.api.data.dao.vv.VvAllDataDao;
import com.heyu.api.data.dao.vv.VvChartConfigDao;
import com.heyu.api.data.dao.vv.VvCreateDataConfigDao;
import com.heyu.api.data.dto.PackageDelivedDelayDTO;
import com.heyu.api.data.dto.VvCreateDataConfigDTO;
import com.heyu.api.data.entity.vv.VvChartConfigEntity;
import com.heyu.api.data.entity.vv.VvCreateDataConfigEntity;
import com.heyu.api.utils.ChartUtil;
import com.heyu.api.data.utils.DateUtils;
import com.heyu.api.data.utils.R;
import com.heyu.api.oss.OssFileUploadService;
import com.heyu.api.schedule.CronTriggerUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;


@Slf4j
@RestController
@RequestMapping("/app/test")
public class AppTestController {


    @Autowired
    private VvAllDataDao vvAllDataDao;


    @Autowired
    private ChartUtil chartUtil;

    @Autowired
    private OssFileUploadService ossFileUploadService;

    @Autowired
    private VvCreateDataConfigDao vvCreateDataConfigDao;


    @Autowired
    private VvChartConfigDao vvChartConfigDao;


    @Value("${eb.config.rabbitQueue.createDataQueue}")
    private String createDataQueue;


    @Value("${eb.config.rabbitQueue.sendDingDingQueue}")
    private String sendDingDingQueue;

    @Autowired
    private RabbitTemplate rabbitTemplate;


    @Value("${eb.config.rabbitQueue.delayQueue}")
    private String delayQueue;


    @Value("${eb.config.rabbitQueue.delayExchangeName}")
    private String delayExchangeName;


    // /app/test/insert
    @Describe("测试数据插入")
    @RequestMapping("/insert")
    public R list(@RequestBody AppOrderRequest vvOrderRequest) {
        List<VvCreateDataConfigEntity> vvCreateDataConfigEntityList = vvCreateDataConfigDao.selectVvCreateDataConfigByAll();
        Date now = new Date();
        for (VvCreateDataConfigEntity vvCreateDataConfigEntity : vvCreateDataConfigEntityList) {
            VvCreateDataConfigDTO dto = new VvCreateDataConfigDTO();
            BeanUtils.copyProperties(vvCreateDataConfigEntity, dto);
            dto.setRunDate(CronTriggerUtils.cronCurrentDate(now, vvCreateDataConfigEntity.getCronExp()));
            rabbitTemplate.convertAndSend(createDataQueue, JSON.toJSONString(dto));
        }


        return R.ok();

    }

    // /app/test/chart
    @Describe("测试图表")
    @RequestMapping("/chart")
    public R chart(@RequestBody AppOrderRequest vvOrderRequest) throws Exception {
        List<VvChartConfigEntity> vvChartConfigEntities = vvChartConfigDao.selectVvChartConfigByAll();
        for (VvChartConfigEntity vvChartConfigEntity : vvChartConfigEntities) {
            rabbitTemplate.convertAndSend(sendDingDingQueue, JSON.toJSONString(vvChartConfigEntity));
        }
        return R.ok();

    }


    // /app/test/delay
    @Describe("测试延迟队列")
    @RequestMapping("/delay")
    public R delay(@RequestBody AppOrderRequest vvOrderRequest) throws Exception {
        PackageDelivedDelayDTO dto = new PackageDelivedDelayDTO();
        dto.setPackageId(10L);
        dto.setRole("test");
        Date time = new Date();
        Date timeAfter15 = DateUtils.addSeconds(time, 10);
        dto.setSendTime(DateUtils.formatDate(new Date(), com.heyu.api.data.utils.DateUtils.YYYY_MM_DD_HH_MM_SS));
        dto.setDelayTime(DateUtils.formatDate(timeAfter15, com.heyu.api.data.utils.DateUtils.YYYY_MM_DD_HH_MM_SS));

        int second = DateUtils.betweenSecond(time, timeAfter15);
        log.info("xxxxxxxxxxxxx:{} ,second:{}",JSON.toJSONString(dto),second);
        // 通过广播模式发布延时消息 延时30分钟 持久化消息 消费后销毁 这里无需指定路由，会广播至每个绑定此交换机的队列
        rabbitTemplate.convertAndSend(delayExchangeName, "", JSON.toJSONString(dto), message -> {
            message.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
            message.getMessageProperties().setDelay((second) * 1000);   // 毫秒为单位，指定此消息的延时时长 ,+ 1 尽量保证机器人跑完了，再发送消息
            return message;
        });
        return R.ok();

    }



    // /app/test/qrcode
    @Describe("测试延迟队列")
    @RequestMapping("/qrcode")
    public R qrcode(@RequestBody AppOrderRequest vvOrderRequest) throws Exception {

        String url = chartUtil.generateQRCodeUp2Oss("9983989832983");
        log.info("url:",url);




        return R.ok();

    }








    public static void main(String[] args) {
        System.out.println(System.currentTimeMillis());
    }
}
