package com.bluefire.top.controller;


import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bluefire.top.entity.FlowOrderInfo;
import com.bluefire.top.service.FlowOrderService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @classname: FlowOrderController
 * @description TODO
 * @author: liu liu
 * @create: 2020-07-15 14:23
 */
@RestController
@RequestMapping("/flow/order")
@AllArgsConstructor
@Slf4j
public class FlowOrderController {

    private FlowOrderService flowOrderService;


    /**
     * @Description: 获取指定时间查询数据对象  算的是回调数据的数量
     * @Param:
     * @return:
     * @Author: liu liu
     * @date: 2020/7/15 17:14
     */
    @GetMapping("/info")
    public List<String> getFlowOrderInfoList() {
        List<String> flowOrderInfo = flowOrderService.getFlowOrderInfo("2020-07-30 00:00:00", "2020-07-30 23:59:59");
        System.out.println("flowOrderInfo.size() = " + flowOrderInfo.size());
        return flowOrderInfo;
    }


    @GetMapping("/getlist")
    public String getFlowLostList(String startDate, String endDate, String redPath) {
        startDate = "2020-10-9 00:00:00";
        endDate = "2020-10-9 23:59:59";
        redPath = "D:\\WorkSpace\\Accounts\\10-9.xlsx";

        flowOrderService.queryLostOrder(startDate, endDate, redPath);
        return "SUCCESS";
    }


    /**
     * @Description: 查询对方有我们没有的数据
     * @Param:
     * @return:
     * @Author: liu liu
     * @date: 2020/7/15 20:03
     */
    @GetMapping("/toher")
    public List<FlowOrderInfo> getFlowOrderToHer(String startDate, String endDate, String redPath) {
        //通过的选中和对方发送的文件找出对方数据中有的订单号我方没有的订单号
        startDate = "2020-09-29 00:00:00";
        endDate = "2020-09-29 23:59:59";
        redPath = "D:\\WorkSpace\\Accounts\\9-29.xlsx";


        List<String> orderIds = flowOrderService.queryHerOrderIds(startDate, endDate, redPath);
        if (orderIds.size() > 0) {
            log.info("对方多余的订单编号:{}", orderIds.toString());
            return flowOrderService.queryOrderInfoByList(orderIds);
        }

        //通过获取的订单号从数据库中查询
        return null;

    }


    /**
     * @Description: 获取我方有对方没有的数据
     * @Param:
     * @return:
     * @Author: liu liu
     * @date: 2020/7/15 20:11
     */
    @GetMapping("/tome")
    public List<FlowOrderInfo> getFlowOrderToMe(String startDate, String endDate, String redPath) {
        //通过的选中和对方发送的文件找出对方数据中有的订单号我方没有的订单号
        startDate = "2020-09-29 00:00:00";
        endDate = "2020-09-29 23:59:59";
        redPath = "D:\\WorkSpace\\Accounts\\9-29.xlsx";


        List<String> orderIds = flowOrderService.queryMyOrderIds(startDate, endDate, redPath);
        if (orderIds.size() > 0) {
            log.info("对方多余的订单编号:{}", orderIds.toString());
            return flowOrderService.queryOrderInfoByList(orderIds);
        }

        log.info("我方和对方的数据一致");

        return null;

    }


    public static void main(String[] args) {
       String xml="<xml><ToUserName><![CDATA[ounWJwhZ0kQxVWxGiaVTkQjT9k8s]]></ToUserName><FromUserName><![CDATA[gh_1da1d8058601]]></FromUserName><CreateTime>1595577784</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[收到信息内容：{\"allFieldsMap\": {\"Content\": \"1\",\"CreateTime\": \"1595577784\",\"ToUserName\": \"gh_1da1d8058601\",\"FromUserName\": \"ounWJwhZ0kQxVWxGiaVTkQjT9k8s\",\"MsgType\": \"text\", \"MsgId\": \"22843179102984220\"},\"toUser\": \"gh_1da1d8058601\",\"fromUser\": \"ounWJwhZ0kQxVWxGiaVTkQjT9k8s\",\"createTime\": 1595577784,\"msgType\": \"text\",\"content\": \"1\",\"msgId\": 22843179102984220,\"scanCodeInfo\": {},\"sendPicsInfo\": {\"picList\": []},\"sendLocationInfo\": {},\"hardWare\": {}}]]></Content></xml>\n".replace("\\\\","");

        System.out.println("xml = " + xml);

       /* JSONObject jsonObject = JSONUtil.parseFromXml(xml);


        Object content = jsonObject.get("Content");

        System.out.println("jsonObject = " + jsonObject);*/



        Document doc = null;
        try {


            doc = DocumentHelper.parseText(xml); // 将字符串转为XML

            Element rootElt = doc.getRootElement(); // 获取根节点

            Element content1 = rootElt.element("Content");
            String stringValue = content1.getStringValue();

            JSONArray objects = JSONObject.parseArray(stringValue);
            System.out.println("parse = " + objects);

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

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

        }


    }



}
