package com.sptg.repeater.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.sptg.repeater.vo.CommonValues;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SendRabbitmqController {

    // 设备编号
    public static final String serialNumber = "AEAC082E";
    public static final String serialNumber_2G = "CD329D84";

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Autowired
    private CommonValues cv;

    @GetMapping("/isOnline")
    public String sendTransientDH(){
        String id = "1000000001";
        String code = cv.getDhOnline();
        JSONObject data = JSONUtil.createObj()
                .put("serialNumber", serialNumber);

        JSONObject object = JSONUtil.createObj()
                .put("id", id)
                .put("code", code)
                .put("data", data);
        rabbitTemplate.convertAndSend("TRANSIENT_EXCHANGE","dh." + code, object.toString());

        return "isOnline";
    }

    @GetMapping("/openDoor")
    public String openRemoteDeviceLock(){
        String id = "1000000002";
        String code = cv.getOpenDoor();
        JSONObject data = JSONUtil.createObj()
                .put("serialNumber", serialNumber)
                .put("serialPsw", "12345678");

        JSONObject object = JSONUtil.createObj()
                .put("id", id)
                .put("code", code)
                .put("data", data);

        rabbitTemplate.convertAndSend("TRANSIENT_EXCHANGE","dh." + code, object.toString());

        return "open_door";
    }

    @GetMapping("/openDoor2G")
    public String openDoor2G(){
        String id = "1000000003";
        String code = cv.getOpenDoor();
        JSONObject data = JSONUtil.createObj()
                .put("serialNumber", serialNumber_2G)
                .put("serialPsw", "12345678");

        JSONObject object = JSONUtil.createObj()
                .put("id", id)
                .put("code", code)
                .put("data", data);

        rabbitTemplate.convertAndSend("TRANSIENT_EXCHANGE","dh." + code, object.toString());

        return "open_door_2G";
    }


    //-------------------PERSISTENT_DH------------------------
    @GetMapping("/addCard")
    public String addCard(String card){
        if(StrUtil.isBlank(card)){
            return "请输入卡号";
        }
        String id = "1163664108063813634";
        String code = cv.getAddOpenDoorCard();
        JSONObject data = JSONUtil.createObj()
                .put("id", 10)
                .put("serialNumber", serialNumber_2G)
                .put("serialPsw", "12345678")
                .put("cardNo", card)
                .put("beginDate", "2020-04-04 00:00:00")
                .put("endDate", "2021-04-24 14:40:00");

        JSONObject object = JSONUtil.createObj()
                .put("id", id)
                .put("code", code)
                .put("data", data);

        rabbitTemplate.convertAndSend("PERSISTENT_EXCHANGE","dh." + code, object.toString());

        return "add_open_door_card_单张";
    }

    @GetMapping("/addCardMore")
    public String addCardOne(String card){
        if(StrUtil.isBlank(card)){
            return "请输入卡号多张";
        }
        String id = "265656565";
        String code = cv.getAddOpenDoorCardBatch();
        JSONObject data = JSONUtil.createObj()
                .put("id", 10)
                .put("serialNumber", serialNumber)
                .put("serialPsw", "12345678")
                .put("cardNo", card)
                .put("beginDate", "2020-04-24 00:00:00")
                .put("endDate", "2020-04-24 14:40:00");

        JSONObject object = JSONUtil.createObj()
                .put("id", id)
                .put("code", code)
                .put("data", data);

        rabbitTemplate.convertAndSend("PERSISTENT_EXCHANGE","dh." + code, object.toString());

        return "add_open_door_card_多张";
    }

    @GetMapping("/deleteAdvert")
    public String deleteAdvert(String card){
        if(StrUtil.isBlank(card)){
            return "请输入卡号";
        }

        String id = "1163664108063813634";
        String code = cv.getDelOpenDoorCard();
        JSONObject data = JSONUtil.createObj()
                .put("id", 10)
                .put("serialNumber", serialNumber)
                .put("serialPsw", "12345678")
                .put("cardNo", card);

        JSONObject object = JSONUtil.createObj()
                .put("id", id)
                .put("code", code)
                .put("data", data);

        rabbitTemplate.convertAndSend("PERSISTENT_EXCHANGE","dh." + code, object.toString());

        return "del_open_door_card";
    }

}
