
package com.bweb.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.domain.ForbbidenTime;
import com.alipay.api.domain.OfferObject;
import com.bweb.config.SysConst;
import com.hai.common.exception.ErrorCode;
import com.hai.common.exception.ErrorHelp;
import com.hai.common.exception.SysCode;
import com.hai.common.pay.util.sdk.WXPayConstants;
import com.hai.common.utils.*;
import com.hai.config.*;
import com.hai.entity.*;
import com.hai.model.OrderRefundModel;
import com.hai.model.ResponseData;
import com.hai.openApi.config.BlxConfig;
import com.hai.openApi.service.ApiBlxCouponService;
import com.hai.order.model.ExportOrderMobileModel;
import com.hai.order.model.OrderThirdPartyModel;
import com.hai.order.service.OrderRefundService;
import com.hai.order.service.OrderService;
import com.hai.order.type.*;
import com.hai.order.utils.OrderUtil;
import com.hai.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.math.BigDecimal;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Auther: 胡锐
 * @Description:
 * @Date: 2021/3/26 23:08
 */
@Controller
@RequestMapping(value = "/test")
@Api(value = "订单接口")
public class HighTestController {

    private static Logger log = LoggerFactory.getLogger(HighTestController.class);

    @Resource
    private OutRechargeOrderService outRechargeOrderService;

    @Autowired
    private OutRechargeChildOrderService childOrderService;

    @Resource
    private ApiBlxCouponService apiBlxCouponService;

    @Resource
    private BlxConfig blxConfig;

    @Resource
    private CommonService commonService;

    @Resource
    private OrderService orderService;


    @Resource
    private HighOrderService highOrderService;

    @RequestMapping(value="/resolveResponse",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "解析")
    public ResponseData resolveResponse(
            @RequestParam(name = "data", required = false) String data
    ) {
        try {
            JSONObject cardInfoObject = HuiLianTongUnionCardConfig.resolveResponse(data);
            return ResponseMsgUtil.success(cardInfoObject);

        } catch (Exception e) {
            log.error("getUserByTelephone",e);
            return ResponseMsgUtil.exception(e);
        }
    }



    @RequestMapping(value = "/getHuiLianTongCardConsume", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询汇联通工会卡消费记录")
    public ResponseData getHuiLianTongCardConsume(@RequestParam(name = "businessType", required = true) String businessType,
                                                  @RequestParam(name = "cardNo", required = true) String cardNo,
                                                  @RequestParam(name = "sdate", required = true) Long sdate,
                                                  @RequestParam(name = "edate", required = true) Long edate,
                                                  @RequestParam(name = "pageNum", required = true) Integer pageNum,
                                                  @RequestParam(name = "pageSize", required = true) Integer pageSize,
                                                  HttpServletRequest request) {
        try {


            JSONObject consumptionRecord = HuiLianTongUnionCardConfig.queryConsumptionRecordByBusiness(businessType, cardNo, sdate, edate, pageNum, pageSize);
            if (StringUtils.isBlank(consumptionRecord.getString("data"))) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQUEST_ERROR, "");
            }

            JSONArray array = HuiLianTongUnionCardConfig.resolveResponse(consumptionRecord.getString("data")).getJSONArray("records");

            return ResponseMsgUtil.success(array);

        } catch (Exception e) {
            log.error("HighUserCardController --> getHuiLianTongCardConsume() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }


    @RequestMapping(value = "/getStationStores", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取油站")
    public ResponseData getStationStores() {
        try {

            Map<String , Object> mapPost = new HashMap<>();
            mapPost.put("apiKey" , "4B1DF80A2D64376D2807ACA6CFEDE8AA");
            mapPost.put("merchId" , "20220896102");

            Map<String , Object> map = new HashMap<>();
            map.put("apiKey" , "4B1DF80A2D64376D2807ACA6CFEDE8AA");
            map.put("merchId" , "20220896102");

            System.out.println(map);

            mapPost.put("sign" , WxUtils.generateSignApi(map, MapUtils.getString(map, "apiKey"), WXPayConstants.SignType.MD5));
            return ResponseMsgUtil.success(HttpsUtils.doPost("https://hsg.dctpay.com/v1/refuelOnline/getStationStore" , mapPost , new HashMap<>()));
        } catch (Exception e) {
            log.error("HighOrderController --> getBackendToken() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/getRechargeProduct", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取充值产品")
    public ResponseData getRechargeProduct() {
        try {

            Map<String , Object> mapPost = new HashMap<>();
            mapPost.put("apiKey" , "0C7897EA3BD96156A5881618FF2FF645");
            mapPost.put("merchId" , "20230258302");

            Map<String , Object> map = new HashMap<>();
            map.put("apiKey" , "0C7897EA3BD96156A5881618FF2FF645");
            map.put("merchId" , "20230258302");

            System.out.println(map);

            mapPost.put("sign" , WxUtils.generateSignApi(map, MapUtils.getString(map, "apiKey"), WXPayConstants.SignType.MD5));
            return ResponseMsgUtil.success(HttpsUtils.doPost("http://localhost:9902/v1/mobileRecharge/getMobileProduct" , mapPost , new HashMap<>()));
        } catch (Exception e) {
            log.error("HighOrderController --> getBackendToken() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/getAllCities", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询可用城市")
    public ResponseData getAllCities() {
        try {

            Map<String , Object> mapPost = new HashMap<>();
            mapPost.put("apiKey" , "0C7897EA3BD96156A5881618FF2FF645");
            mapPost.put("merchId" , "20230258302");

            Map<String , Object> map = new HashMap<>();
            map.put("apiKey" , "0C7897EA3BD96156A5881618FF2FF645");
            map.put("merchId" , "20230258302");

            System.out.println(map);

            mapPost.put("sign" , WxUtils.generateSignApi(map, MapUtils.getString(map, "apiKey"), WXPayConstants.SignType.MD5));
            return ResponseMsgUtil.success(HttpsUtils.doPost("http://localhost:9902/v1/apiMerchants/getAllCities" , mapPost , new HashMap<>()));
        } catch (Exception e) {
            log.error("HighOrderController --> getBackendToken() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/starbucksStore", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询星巴克门店")
    public ResponseData starbucksStore() {
        try {

            Map<String , Object> mapPost = new HashMap<>();
            mapPost.put("apiKey" , "0C7897EA3BD96156A5881618FF2FF645");
            mapPost.put("merchId" , "20230258302");
            mapPost.put("lat" , "29.56900");
            mapPost.put("lon" , "106.55843");

            Map<String , Object> map = new HashMap<>();
            map.put("apiKey" , "0C7897EA3BD96156A5881618FF2FF645");
            map.put("merchId" , "20230258302");
            map.put("lat" , "29.56900");
            map.put("lon" , "106.55843");

            System.out.println(map);

            mapPost.put("sign" , WxUtils.generateSignApi(map, MapUtils.getString(map, "apiKey"), WXPayConstants.SignType.MD5));
            return ResponseMsgUtil.success(HttpsUtils.doPost("http://localhost:9902/v1/starbucks/starbucksStore" , mapPost , new HashMap<>()));
        } catch (Exception e) {
            log.error("HighOrderController --> getBackendToken() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/starbucksProducts", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询星巴克商品")
    public ResponseData starbucksProducts() {
        try {

            Map<String , Object> mapPost = new HashMap<>();
            mapPost.put("apiKey" , "0C7897EA3BD96156A5881618FF2FF645");
            mapPost.put("merchId" , "20230258302");


            Map<String , Object> map = new HashMap<>();
            map.put("apiKey" , "0C7897EA3BD96156A5881618FF2FF645");
            map.put("merchId" , "20230258302");

            System.out.println(map);

            mapPost.put("sign" , WxUtils.generateSignApi(map, MapUtils.getString(map, "apiKey"), WXPayConstants.SignType.MD5));
            return ResponseMsgUtil.success(HttpsUtils.doPost("http://localhost:9902/v1/starbucks/starbucksProducts" , mapPost , new HashMap<>()));
        } catch (Exception e) {
            log.error("HighOrderController --> getBackendToken() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/kfcStoresList", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询肯德基门店")
    public ResponseData kfcStoresList() {
        try {

            Map<String , Object> mapPost = new HashMap<>();
            mapPost.put("apiKey" , "0C7897EA3BD96156A5881618FF2FF645");
            mapPost.put("merchId" , "20230258302");


            Map<String , Object> map = new HashMap<>();
            map.put("apiKey" , "0C7897EA3BD96156A5881618FF2FF645");
            map.put("merchId" , "20230258302");

            System.out.println(map);

            mapPost.put("sign" , WxUtils.generateSignApi(map, MapUtils.getString(map, "apiKey"), WXPayConstants.SignType.MD5));
            return ResponseMsgUtil.success(HttpsUtils.doPost("http://localhost:9902/v1/kfc/kfcStoresList" , mapPost , new HashMap<>()));
        } catch (Exception e) {
            log.error("HighOrderController --> getBackendToken() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/kfcProductByStoreCode", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "根据门店编码查询KFC产品")
    public ResponseData kfcProductByStoreCode(@RequestParam(name = "storeCode", required = true) String storeCode) {
        try {

            Map<String , Object> mapPost = new HashMap<>();
            mapPost.put("apiKey" , "0C7897EA3BD96156A5881618FF2FF645");
            mapPost.put("merchId" , "20230258302");
            mapPost.put("storeCode" , storeCode);


            Map<String , Object> map = new HashMap<>();
            map.put("apiKey" , "0C7897EA3BD96156A5881618FF2FF645");
            map.put("merchId" , "20230258302");
            map.put("storeCode" , storeCode);

            System.out.println(map);

            mapPost.put("sign" , WxUtils.generateSignApi(map, MapUtils.getString(map, "apiKey"), WXPayConstants.SignType.MD5));
            return ResponseMsgUtil.success(HttpsUtils.doPost("http://localhost:9902/v1/kfc/kfcProductByStoreCode" , mapPost , new HashMap<>()));
        } catch (Exception e) {
            log.error("HighOrderController --> getBackendToken() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/getRegional", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询区域信息")
    public ResponseData getRegional() {
        try {

            return ResponseMsgUtil.success(commonService.getRegional("code" , "name" , "childs"));
        } catch (Exception e) {
            log.error("HighOrderController --> getBackendToken() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/memberProducts", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询会员产品")
    public ResponseData memberProducts() {
        try {

            Map<String , Object> mapPost = new HashMap<>();
            mapPost.put("apiKey" , "0C7897EA3BD96156A5881618FF2FF645");
            mapPost.put("merchId" , "20230258302");


            Map<String , Object> map = new HashMap<>();
            map.put("apiKey" , "0C7897EA3BD96156A5881618FF2FF645");
            map.put("merchId" , "20230258302");

            System.out.println(map);

            mapPost.put("sign" , WxUtils.generateSignApi(map, MapUtils.getString(map, "apiKey"), WXPayConstants.SignType.MD5));
            return ResponseMsgUtil.success(HttpsUtils.doPost("http://localhost:9902/v1/memberRecharge/memberProducts" , mapPost , new HashMap<>()));
        } catch (Exception e) {
            log.error("HighOrderController --> getBackendToken() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }


    @RequestMapping(value = "/createOrder", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "创建订单")
    public ResponseData createOrder(@RequestBody JSONObject object) {
        try {

            Map<String , Object> mapPost = new HashMap<>();
            mapPost.put("apiKey" , "0C7897EA3BD96156A5881618FF2FF645");
            mapPost.put("merchId" , "20230258302");
            mapPost.put("timetable" , new Date().getTime());
            mapPost.put("orderNo" , "TEST" +  OrderUtil.generateOrderNo());
            mapPost.put("productType" , object.getString("productType"));
            mapPost.put("notifyUrl" , "test");

            mapPost.put("content" , object.toString());
            System.out.println(mapPost);

            Map<String , Object> map = new HashMap<>();
            map.put("apiKey" , "0C7897EA3BD96156A5881618FF2FF645");
            map.put("merchId" , "20230258302");
            map.put("timetable" , mapPost.get("timetable").toString());
            map.put("orderNo" , mapPost.get("orderNo"));
            map.put("productType" , object.getString("productType"));
            map.put("content" , object.toString());

            System.out.println(map);

            mapPost.put("sign" , WxUtils.generateSignApi(map, MapUtils.getString(map, "apiKey"), WXPayConstants.SignType.MD5));
            return ResponseMsgUtil.success(HttpsUtils.doPost("http://localhost:9902/v1/order/createOrder" , mapPost , new HashMap<>()));
        } catch (Exception e) {
            log.error("HighOrderController --> getBackendToken() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/payOrder", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "支付订单")
    public ResponseData payOrder(@RequestParam(name = "mchOrderNo", required = true) String mchOrderNo) {
        try {

            Map<String , Object> mapPost = new HashMap<>();
            mapPost.put("apiKey" , "0C7897EA3BD96156A5881618FF2FF645");
            mapPost.put("merchId" , "20230258302");
            mapPost.put("mchOrderNo" , mchOrderNo);


            Map<String , Object> map = new HashMap<>();
            map.put("apiKey" , "0C7897EA3BD96156A5881618FF2FF645");
            map.put("merchId" , "20230258302");
            map.put("mchOrderNo" , mchOrderNo);

            System.out.println(map);

            mapPost.put("sign" , WxUtils.generateSignApi(map, MapUtils.getString(map, "apiKey"), WXPayConstants.SignType.MD5));
            return ResponseMsgUtil.success(HttpsUtils.doPost("http://localhost:9902/v1/order/orderPay" , mapPost , new HashMap<>()));
        } catch (Exception e) {
            log.error("HighOrderController --> getBackendToken() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }


    @RequestMapping(value = "/queryOrderDetail", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询订单详情")
    public ResponseData queryOrderDetail(@RequestParam(name = "mchOrderNo", required = true) String mchOrderNo) {
        try {

            Map<String , Object> mapPost = new HashMap<>();
            mapPost.put("apiKey" , "0C7897EA3BD96156A5881618FF2FF645");
            mapPost.put("merchId" , "20230258302");
            mapPost.put("mchOrderNo" , mchOrderNo);


            Map<String , Object> map = new HashMap<>();
            map.put("apiKey" , "0C7897EA3BD96156A5881618FF2FF645");
            map.put("merchId" , "20230258302");
            map.put("mchOrderNo" , mchOrderNo);

            System.out.println(map);

            mapPost.put("sign" , WxUtils.generateSignApi(map, MapUtils.getString(map, "apiKey"), WXPayConstants.SignType.MD5));
            return ResponseMsgUtil.success(HttpsUtils.doPost("http://localhost:9902/v1/order/queryOrderDetail" , mapPost , new HashMap<>()));
        } catch (Exception e) {
            log.error("HighOrderController --> getBackendToken() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }


    @RequestMapping(value = "/deposit", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "工会卡充值")
    public ResponseData deposit(@RequestParam(name = "orderNo", required = true) String orderNo) {
        try {

            HighOrder order = highOrderService.getOrderByOrderNo(orderNo);
            HighChildOrder highChildOrder = order.getHighChildOrderList().get(0);

            if (order.getOrderStatus() != 2) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "订单状态错误");
            }

            // 汇联通充值
            String goodsDesc = "汇联通充值"+order.getPayPrice()+"元";
            String tranDesc = "";
            String businessType = "ghk_deposit";

            String orderNoAgain = OrderUtil.generateOrderNo();

            order.setOrderStatus(3);
            order.setRemarks("工会卡再次充值订单号" + orderNoAgain);

            highOrderService.updateOrder(order);

            // 汇联通卡充值
            JSONObject deposit = HuiLianTongUnionCardConfig.deposit(orderNoAgain, highChildOrder.getGoodsSpecName(), order.getPayPrice(), businessType, order.getPaySerialNo(), goodsDesc);

            return ResponseMsgUtil.success( HuiLianTongUnionCardConfig.resolveResponse(deposit.getString("data")));
        } catch (Exception e) {
            log.error("HighOrderController --> getBackendToken() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }


    @RequestMapping(value = "/getHuiLianTongCardByCardNo", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "根据卡号查询汇联通工会卡详情")
    public ResponseData getHuiLianTongCardByCardNo(@RequestParam(name = "cardNo", required = true) String cardNo) {
        try {
            // 查询工会卡
            JSONObject cardInfo = HuiLianTongUnionCardConfig.queryBalance(cardNo);
            JSONObject resolveResponse = HuiLianTongUnionCardConfig.resolveResponse(cardInfo.getString("data"));
            return ResponseMsgUtil.success(resolveResponse);

        } catch (Exception e) {
            log.error("HighUserCardController --> getHuiLianTongCardByCardNo() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/consumption", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "工会卡支付")
    public ResponseData consumption(@RequestParam(name = "payRealPrice", required = true) BigDecimal payRealPrice,
                                    @RequestParam(name = "cardNo", required = true) String cardNo) {
        try {


            String goodsDesc = "";
            String tranDesc = "";
            String instCode = "11101527";
            String businessType = "hisen_consume";


            // 工会卡支付
            JSONObject consumption = HuiLianTongUnionCardConfig.consumption(OrderUtil.generateOrderNo(), cardNo, payRealPrice, businessType, instCode, goodsDesc, tranDesc);
            if (!consumption.getString("respCode").equals("0000")) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, consumption.getString("respMessage"));
            }
            // 响应参数
            JSONObject consumptionResult = HuiLianTongUnionCardConfig.resolveResponse(consumption.getString("data"));
            if (consumptionResult.getBoolean("success") != true) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, consumptionResult.getString("message"));
            }
            return ResponseMsgUtil.success(consumptionResult);

        } catch (Exception e) {
            log.error("HighUserCardController --> getHuiLianTongCardByCardNo() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }


    @RequestMapping(value = "/refundOrder", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "工会卡退款")
    public ResponseData refundOrder(@RequestParam(name = "orderNo", required = true) String orderNo) {
        try {

            HighOrder order = highOrderService.getOrderByOrderNo(orderNo);

            if (order.getOrderStatus() != 2) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "订单状态错误");
            }

            orderService.refundOrder(order, "工会卡充值退款",OrderRefundOpUserType.TYPE1,
                    null,
                    "系统自动退款");

            return ResponseMsgUtil.success("退款成功");
        } catch (Exception e) {
            log.error("HighOrderController --> getBackendToken() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }


    @RequestMapping(value = "/mobileDw", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "话费订单下载")
    public ResponseData mobileDw(@RequestParam(name = "createTimeS", required = false) String createTimeS,
                                 @RequestParam(name = "createTimeE", required = false) String createTimeE,
                                 @RequestParam(name = "refundTimeS", required = false) String refundTimeS,
                                 @RequestParam(name = "refundTimeE", required = false) String refundTimeE
    ) {
        try {

            Map<String , Object> map = new HashMap<>();
            map.put("productType" , 9);
            map.put("createTimeS" , createTimeS);
            map.put("createTimeE" , createTimeE);
            map.put("refundTimeS" , refundTimeS);
            map.put("refundTimeE" , refundTimeE);

            List<HighOrder> order = highOrderService.getOrderListNotChild(map);

            List<ExportOrderMobileModel> list = new ArrayList<>();

            ExportOrderMobileModel model;
            for (HighOrder highOrder : order) {
                model = new ExportOrderMobileModel();

                OutRechargeOrder rechargeOrder = outRechargeOrderService.findByOrderNoNotChild(highOrder.getOrderNo());

                if (rechargeOrder != null) {

                    OutRechargeChildOrder childOrder = mobileChildOrderHandle(highOrder,rechargeOrder);

                    model.setRechargeOrderNo(rechargeOrder.getOrderNo());

                    if (childOrder != null) {
                        model.setChildOrderNo(childOrder.getOrderNo());
                        if (childOrder.getRechargePlatform() != null) {
                            model.setRechargePlatform(RechargePlatformType.getDataByType(childOrder.getRechargePlatform()).getName());
                        }
                        model.setRemark(childOrder.getExt1());
                    }

                }
                model.setFinishTime(highOrder.getFinishTime());
                model.setOrderStatus(OrderStatus.getDataByType(highOrder.getOrderStatus()).getName());
                model.setMemPhone(highOrder.getMemPhone());
                model.setCreateTime(highOrder.getCreateTime());
                model.setTitle(highOrder.getTitle());
                model.setPayablePrice(highOrder.getPayablePrice());
                model.setRefundTime(highOrder.getRefundTime());
                model.setPayPrice(highOrder.getPayPrice());
                model.setOrderNo(highOrder.getOrderNo());
                model.setMemCardNo(highOrder.getMemCardNo());
                if (highOrder.getPayType() != null) {
                    model.setPayType(OrderPayType.getDataByType(highOrder.getPayType()).getName());
                }
                list.add(model);
            }

            String fileUrl = SysConst.getSysConfig().getFileUrl() + "temporary/";
            String pathName = System.currentTimeMillis()+".xlsx";
            File file = new File(fileUrl);
            if (!file.exists()) {
                file.mkdirs();
            }

            EasyExcel.write(fileUrl+pathName)
                    .head(ExportOrderMobileModel.class)
                    .sheet("话费交易订单")
                    .doWrite(list);
            return ResponseMsgUtil.success(pathName);

        } catch (Exception e) {
            log.error("HighUserCardController --> oilCardRefund() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }



    public OutRechargeChildOrder mobileChildOrderHandle(HighOrder  highOrder , OutRechargeOrder outRechargeOrder) {

        // 组装查询子订单数据
        Map<String , Object> childOrderMap101 = new HashMap<>();
        childOrderMap101.put("parent_order_id", outRechargeOrder.getId());
        childOrderMap101.put("status", "101,102");
        // 查询充值订单
        List<OutRechargeChildOrder> childOrders = childOrderService.getListRechargeChildOrder(childOrderMap101);

        OutRechargeChildOrder childOrder;

        // 判读是否存在多个充值中的订单
        if (childOrders.size() > 0) {
            childOrder = childOrders.get(0);
            if (childOrders.size() > 1) {
                childOrder.setExt1("当前订单存在多个充值订单");
            }

        } else {
            childOrder = childOrderService.findByOrderId(outRechargeOrder.getId());
            if (childOrder != null && highOrder != null && childOrder.getStatus() != null && highOrder.getOrderStatus() != null) {
                if (highOrder.getOrderStatus() == 2 && childOrder.getStatus() != 102) {
                    childOrder.setExt1("主订单充值中，但是充值订单状态不为充值中");
                }
                if (highOrder.getOrderStatus() == 3 && childOrder.getStatus() != 101) {
                    childOrder.setExt1("主订单充值成功，但是充值订单状态不为充值成功");
                }
                if (highOrder.getOrderStatus() == 4 && !(childOrder.getStatus() == 103 || childOrder.getStatus() == 104)) {
                    childOrder.setExt1("主订单退款，但是充值订单状态不为退款");
                }
            }

        }

        return childOrder;

    }


    @RequestMapping(value = "/externalOrderRefund", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "对外订单退款")
    public ResponseData externalOrderRefund(@RequestParam(name = "paySerialNo", required = true) String paySerialNo,
                                            @RequestParam(name = "WxMchId", required = true) String WxMchId,
                                            @RequestParam(name = "payRealPrice", required = true) String payRealPrice) {
        try {


            Map<String , Object> mapPost = new HashMap<>();
            mapPost.put("apiKey" , "B94B5E62807FA56A7260AB581D8A1B48");
            mapPost.put("merchId" , "20230394901");
            mapPost.put("paySerialNo" , paySerialNo);
            mapPost.put("wxMchId" , WxMchId);
            mapPost.put("payRealPrice" , payRealPrice);
            mapPost.put("refundPrice" , payRealPrice);

            Map<String , Object> map = new HashMap<>();
            map.put("apiKey" , "B94B5E62807FA56A7260AB581D8A1B48");
            map.put("merchId" , "20230394901");
            map.put("paySerialNo" , paySerialNo);
            map.put("wxMchId" , WxMchId);
            map.put("payRealPrice" , payRealPrice);
            map.put("refundPrice" , payRealPrice);

            System.out.println(map);

            mapPost.put("sign" , WxUtils.generateSignApi(map, MapUtils.getString(map, "apiKey"), WXPayConstants.SignType.MD5));
            return ResponseMsgUtil.success(HttpsUtils.doPost("https://hsg.dctpay.com/v1/order/externalOrderRefund" , mapPost , new HashMap<>()));
        } catch (Exception e) {
            log.error("HighOrderController --> getBackendToken() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/test", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "测试")
    public ResponseData test() {

        try {

            Map<String , Object > map = new HashMap<>();
            map.put("organizeCode" , "dhy");

            JSONObject object = HttpsUtils.doGet("https://vertical-formats-gateway.blx.bodata.cn:18443/admin/api/generateJwt" , map);

            return ResponseMsgUtil.success(object);
        } catch (Exception e) {
            log.error("HighOrderController --> getBackendToken() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "qureyCouponPage", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "测试")
    public ResponseData qureyCouponPage() {

        try {

            JSONObject object = blxConfig.queryCouponPage();

            if (object.getInteger("code") == 1) {

                JSONArray jsonArray = object.getJSONObject("data").getJSONArray("records");

                for (int i = 0; i < jsonArray.size(); i++) {
                    if (jsonArray.getJSONObject(i).getString("couponCode") != null) {
                        ApiBlxCoupon apiBlxCoupon = apiBlxCouponService.findBlxCoupon(jsonArray.getJSONObject(i).getString("couponCode"));
                        if (apiBlxCoupon == null) {
                            apiBlxCoupon = new ApiBlxCoupon();
                            apiBlxCoupon.setCouponCode(jsonArray.getJSONObject(i).getString("couponCode"));
                            apiBlxCoupon.setCouponStatus(jsonArray.getJSONObject(i).getInteger("couponStatus"));
                            apiBlxCoupon.setCreateTime(new Date());
                            apiBlxCoupon.setCouponName(jsonArray.getJSONObject(i).getString("couponName"));
                            apiBlxCoupon.setDenomination(jsonArray.getJSONObject(i).getInteger("denomination"));
                            apiBlxCoupon.setUpdateTime(new Date());
                            apiBlxCoupon.setMinPrice(jsonArray.getJSONObject(i).getInteger("minPrice"));
                            apiBlxCoupon.setUseStartTime(jsonArray.getJSONObject(i).getDate("useStartTime"));
                            apiBlxCoupon.setUseEndTime(jsonArray.getJSONObject(i).getDate("useEndTime"));
                            apiBlxCoupon.setCouponStatus(jsonArray.getJSONObject(i).getInteger("couponStatus"));
                            apiBlxCoupon.setReceiveDays(jsonArray.getJSONObject(i).getInteger("receiveDays"));
                            apiBlxCoupon.setReceiveLimit(jsonArray.getJSONObject(i).getInteger("receiveLimit"));
                            apiBlxCouponService.insertBlxCoupon(apiBlxCoupon);

                        } else {
                            apiBlxCoupon.setCouponStatus(jsonArray.getJSONObject(i).getInteger("couponStatus"));
                            apiBlxCoupon.setCouponName(jsonArray.getJSONObject(i).getString("couponName"));
                            apiBlxCoupon.setDenomination(jsonArray.getJSONObject(i).getInteger("denomination"));
                            apiBlxCoupon.setUpdateTime(new Date());
                            apiBlxCoupon.setMinPrice(jsonArray.getJSONObject(i).getInteger("minPrice"));
                            apiBlxCoupon.setUseStartTime(jsonArray.getJSONObject(i).getDate("useStartTime"));
                            apiBlxCoupon.setUseEndTime(jsonArray.getJSONObject(i).getDate("useEndTime"));
                            apiBlxCoupon.setCouponStatus(jsonArray.getJSONObject(i).getInteger("couponStatus"));
                            apiBlxCoupon.setReceiveDays(jsonArray.getJSONObject(i).getInteger("receiveDays"));
                            apiBlxCoupon.setReceiveLimit(jsonArray.getJSONObject(i).getInteger("receiveLimit"));
                            apiBlxCouponService.editBlxCoupon(apiBlxCoupon);
                        }
                    }
                }
            }

            return ResponseMsgUtil.success(apiBlxCouponService.getBlxCouponList(new HashMap<>()));
        } catch (Exception e) {
            log.error("HighOrderController --> getBackendToken() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "sendCoupon", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "停车券发券")
    public ResponseData sendCoupon() {

        try {

            JSONObject jsonObject = new JSONObject();
            jsonObject.put("couponCode" , "16881066428954950");
            jsonObject.put("receiverPhone" , "18090580471");
            jsonObject.put("receiverCount" , "1");

            JSONObject object = blxConfig.sendCoupon(jsonObject);

            return ResponseMsgUtil.success(object);
        } catch (Exception e) {
            log.error("HighOrderController --> getBackendToken() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "getUserCouponPage", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取用户卡券")
    public ResponseData getUserCouponPage(@RequestParam(name = "phone", required = true) String phone) {

        try {

            JSONObject jsonObject = new JSONObject();
            jsonObject.put("userMobile" , phone);

            JSONObject object = blxConfig.getUserCouponPage(jsonObject);

            int num = 0;

            if (object.getInteger("code") == 1) {
                num = object.getJSONObject("data").getInteger("total");
            }

            return ResponseMsgUtil.success(num);
        } catch (Exception e) {
            log.error("HighOrderController --> getBackendToken() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }
}


