package com.chaozhanggui.admin.system.util;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.*;

/**
 * 打印机
 */
public class PrinterUtils {
    //请求地址
    private static final String URL_STR = "https://ioe.car900.com/v1/openApi/dev/customPrint.json";
    //APPID
    private static final String APP_ID = "ZF544";
    //USERCODE
    private static final String USER_CODE = "ZF544";
    //APPSECRET
    private static final String APP_SECRET = "2022bsjZF544GAH";

    /**
     * 获取TOKEN值
     * @param timestamp 时间戳，13位
     * @param requestId 请求ID，自定义
     * @return
     */
    private static Map<String, String> getToken(String timestamp, String requestId) {
        String token = "";
        String encode = "";
        SortedMap<String, Object> map = new TreeMap();
        map.put("appId", APP_ID);
        map.put("timestamp", timestamp);
        map.put("requestId", requestId);
        map.put("userCode", USER_CODE);
        Iterator<Map.Entry<String, Object>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Object> next = iterator.next();
            String key = next.getKey();
            Object value = next.getValue();
            token += key + value;
            encode += key + "=" + value + "&";
        }
        System.out.println("token"+token);
        Map<String, String> finalMap = new HashMap<>();
        finalMap.put("ENCODE",encode);
        finalMap.put("TOKEN", MD5Util.md5(token + APP_SECRET).toUpperCase());
        return finalMap;
    }

    private static String getPrintData(OilTicketsDTO oilTicketsDTO) {
        StringBuilder builder = new StringBuilder();
        builder.append("<F><C>贩卖刘一帆</C></F><BR>");
        builder.append("<F><C>买卖凭票</C></F><BR>");
        builder.append("               <BR>");
        builder.append("<RS:18><CS:30>人口出售地点:");
        builder.append(oilTicketsDTO.getOilName()).append("<BR>");
        builder.append("<RS:18><CS:30>出售订单号:");
        builder.append(oilTicketsDTO.getOrderId()).append("<BR>");
        builder.append("<RS:18>单价:");
        builder.append(oilTicketsDTO.getPrice()).append("/L").append("<BR>");
        builder.append("<RS:18><BOLD>人口序号:");
        builder.append(oilTicketsDTO.getOilNo());
        builder.append("   人口序列号:");
        builder.append(oilTicketsDTO.getOilGun()).append("号").append("</BOLD><BR>");
        builder.append("<RS:18><CS:30>付款时间:");
        builder.append(oilTicketsDTO.getPaymentTime()).append("<BR>");
        builder.append("<RS:18><CS:30>下单时间:");
        builder.append(oilTicketsDTO.getOrderTime()).append("<BR>");
        builder.append("<RS:18><BOLD>人数:");
        builder.append(oilTicketsDTO.getOilAmount()).append("/L").append("<BR>");
        builder.append("<RS:18><BOLD>金额:");
        builder.append("￥").append(oilTicketsDTO.getMoney()).append("<BR>");
     //   builder.append("               <BR>");
//        builder.append("<OUT:113>");
        builder.append("<CUT>");
        return builder.toString();
    }


    /**
     * 打印票据
     * @param oilTicketsDTO
     * @throws Exception
     */
    public static void printTickets(OilTicketsDTO oilTicketsDTO) {
        //设备名称
        String devName = "ZF544PG0100001";
        //行为方式 1:只打印数据 2:只播放信息 3:打印数据并播放信息
        Integer actWay = 1;
        //打印联数
        int cn = 1;
        //打印内容
        String oilName = oilTicketsDTO.getOilName();
        String data = getPrintData(oilTicketsDTO);
        //播报语音数据体,字段参考文档IOT_XY_API11001
        String voiceJson = "{\"bizType\":\"1\",\"broadCastType\":\"1\",\"money\":\""+oilTicketsDTO.getPrice()+"\"}";
        String time = String.valueOf(System.currentTimeMillis());
        String uuid = UUID.randomUUID().toString();
        Map<String, String> param = getToken(time, uuid);
        //参数
        MultiValueMap<String, Object> multiValueMap = new LinkedMultiValueMap<>();
        multiValueMap.add("token",param.get("TOKEN"));
        multiValueMap.add("devName",devName);
        multiValueMap.add("actWay",actWay);
        multiValueMap.add("cn",cn);
        multiValueMap.add("data",data);
        multiValueMap.add("voiceJson",voiceJson);
        multiValueMap.add("appId",APP_ID);
        multiValueMap.add("timestamp",time);
        multiValueMap.add("requestId",uuid);
        multiValueMap.add("userCode",USER_CODE);
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders header = new HttpHeaders();
        header.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(multiValueMap,header);
        String httpResponse = restTemplate.postForObject(URL_STR,
                httpEntity, String.class);

        System.out.println("map"+multiValueMap);
    }



   static class OilTicketsDTO{
        private String oilAmount;

        private String oilGun;

        private String oilNo;

        private String oilName;

        private String orderTime;

        private String paymentTime;

        private String money;

        private String orderId;

        private String price;

       public String getOilAmount() {
           return oilAmount;
       }

       public void setOilAmount(String oilAmount) {
           this.oilAmount = oilAmount;
       }

       public String getOilGun() {
           return oilGun;
       }

       public void setOilGun(String oilGun) {
           this.oilGun = oilGun;
       }

       public String getOilNo() {
           return oilNo;
       }

       public void setOilNo(String oilNo) {
           this.oilNo = oilNo;
       }

       public String getOilName() {
           return oilName;
       }

       public void setOilName(String oilName) {
           this.oilName = oilName;
       }

       public String getOrderTime() {
           return orderTime;
       }

       public void setOrderTime(String orderTime) {
           this.orderTime = orderTime;
       }

       public String getPaymentTime() {
           return paymentTime;
       }

       public void setPaymentTime(String paymentTime) {
           this.paymentTime = paymentTime;
       }

       public String getMoney() {
           return money;
       }

       public void setMoney(String money) {
           this.money = money;
       }

       public String getOrderId() {
           return orderId;
       }

       public void setOrderId(String orderId) {
           this.orderId = orderId;
       }

       public String getPrice() {
           return price;
       }

       public void setPrice(String price) {
           this.price = price;
       }
   }

    public static void main(String[] args)throws Exception {
        OilTicketsDTO oilTicketsDTO = new OilTicketsDTO();
        oilTicketsDTO.setOilAmount("300000");
        oilTicketsDTO.setOilGun("2014");
        oilTicketsDTO.setOilNo("201408");
        oilTicketsDTO.setOilName("人口买卖");
        oilTicketsDTO.setOrderTime("2023-7-1 10:33:21");
        oilTicketsDTO.setPaymentTime("2023-7-1 10:33:21");
        oilTicketsDTO.setMoney("211");
        oilTicketsDTO.setOrderId("1231212");
        oilTicketsDTO.setPrice("2000");
        printTickets(oilTicketsDTO);
    }
}
