package com.cweb.controller;

import com.alibaba.fastjson.JSONObject;
import com.cweb.config.SysConst;
import com.cweb.config.WxMsgConfig;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hai.common.exception.ErrorCode;
import com.hai.common.exception.ErrorHelp;
import com.hai.common.exception.SysCode;
import com.hai.common.pay.WechatPayUtil;
import com.hai.common.pay.entity.WeChatPayReqInfo;
import com.hai.common.pay.util.HttpReqUtil;
import com.hai.common.pay.util.MD5Util;
import com.hai.common.pay.util.XmlUtil;
import com.hai.common.pay.util.sdk.WXPayConstants;
import com.hai.common.security.AESEncodeUtil;
import com.hai.common.security.SessionObject;
import com.hai.common.security.UserCenter;
import com.hai.common.utils.*;
import com.hai.config.CommonSysConst;
import com.hai.config.UnionPayConfig;
import com.hai.dao.SecDictionaryMapper;
import com.hai.entity.*;
import com.hai.enum_type.DiscountUseScope;
import com.hai.enum_type.GoodsType;
import com.hai.enum_type.PayType;
import com.hai.model.HighUserModel;
import com.hai.model.OrderRefundModel;
import com.hai.model.ResponseData;
import com.hai.model.ResultProfitSharing;
import com.hai.service.*;
import com.hai.service.pay.impl.GoodsOrderServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;

@Controller
@RequestMapping(value = "/outRechargeOrder")
@Api(value = "充值订单接口")
public class OutRechargeOrderController {
    private static Logger log = LoggerFactory.getLogger(HighMerchantStoreController.class);

    @Autowired
    private UserCenter userCenter;

    @Resource
    private OutRechargeOrderService outRechargeOrderService;

    @Resource
    private HighOrderService highOrderService;

    @Resource
    private OutRechargePriceService outRechargePriceService;

    @Resource
    private WechatPayUtil wechatPayUtil;

    @Resource
    private GoodsOrderServiceImpl goodsOrderService;

    @Resource
    private HighProfitSharingRecordService highProfitSharingRecordService;

    @Resource
    private SecConfigService secConfigService;

    @Resource
    private HighUserCardService highUserCardService;

    @Resource
    private HighUserPayPasswordService highUserPayPasswordService;

    @Resource
    private UnionPayConfig unionPayConfig;

    @Resource
    private HighDiscountUserRelService highDiscountUserRelService;

    @RequestMapping(value="/addOrder",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "增加订单")
    public ResponseData addOrder(@RequestBody OutRechargeOrder outRechargeOrder, HttpServletRequest request) {
        try {

            // 用户
            SessionObject sessionObject = userCenter.getSessionObject(request);
            HighUserModel userInfoModel = (HighUserModel) sessionObject.getObject();
            if (StringUtils.isBlank(outRechargeOrder.getRechargeContent()) ||
                    outRechargeOrder.getPayPrice() == null ||
                    outRechargeOrder.getObjectId() == null ||
                    outRechargeOrder.getOrderPrice() == null
            ) {
                log.error("addOrder error!");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            if (!secConfigService.isConfig("RECHARGE" , "1")) {
                log.error("addOrder error!");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.RECHARGE_CLOSE, "");
            }

            String remarks = "";
            // 支付类型 1.微信支付
            outRechargeOrder.setPayType(1);
            // 判断充值模式：1 电信 2. 移动 3. 联通
            // 拼接内容
            remarks = outRechargeOrder.getRechargeContent() + "充值" + outRechargeOrder.getOrderPrice() + "元";

            HighDiscountUserRel highDiscountUserRel = null;
            if (outRechargeOrder.getMemDiscountId() != null) {
                // 卡优惠券信息
                highDiscountUserRel = highDiscountUserRelService.getRelById(outRechargeOrder.getMemDiscountId());
                if (highDiscountUserRel == null || highDiscountUserRel.getStatus() != 1) {
                    log.error("HighOrderController --> addOrder() error!", "优惠券状态错误");
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "优惠券状态错误");
                }
                if (!highDiscountUserRel.getHighDiscount().getUseScope().equals(DiscountUseScope.type1.getType())
                     && !highDiscountUserRel.getHighDiscount().getUseScope().equals(DiscountUseScope.type3.getType())) {
                    log.error("HighOrderController --> addOrder() error!", "无法使用此优惠券");
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "无法使用此优惠券");
                }
            }

            // 优惠券抵扣
            if (highDiscountUserRel != null) {
                outRechargeOrder.setMemDiscountName(highDiscountUserRel.getHighDiscount().getDiscountName());

                // 卡卷类型  1：满减  2：抵扣   3：折扣
                if (highDiscountUserRel.getHighDiscount().getDiscountType() == 1) {
                    // 如果订单总额 小于 满减价格
                    if (outRechargeOrder.getOrderPrice().compareTo(highDiscountUserRel.getHighDiscount().getDiscountCondition()) > 1) {
                        log.error("HighOrderController --> addOrder() error!", "订单未达到满减额度");
                        throw ErrorHelp.genException(SysCode.System, ErrorCode.UN_MEMBER_ERROR, "订单未达到"+highDiscountUserRel.getHighDiscount().getDiscountCondition()+"元,无法使用此优惠券");
                    }
                    // 订单总额 - 满减额度
                    BigDecimal payPrice = outRechargeOrder.getOrderPrice().subtract(highDiscountUserRel.getHighDiscount().getDiscountPrice());

                    outRechargeOrder.setDeductionPrice(highDiscountUserRel.getHighDiscount().getDiscountPrice());
                    // 如果总额小于0
                    if (payPrice.compareTo(new BigDecimal("0")) == -1) {
                        outRechargeOrder.setPayPrice(new BigDecimal("0"));
                    } else {
                        outRechargeOrder.setPayPrice(payPrice);
                    }
                }

                // 卡卷类型  1：满减  2：抵扣   3：折扣
                if (highDiscountUserRel.getHighDiscount().getDiscountType() == 2) {
                    // 订单总额 - 满减额度
                    BigDecimal payPrice = outRechargeOrder.getOrderPrice().subtract(highDiscountUserRel.getHighDiscount().getDiscountPrice());
                    outRechargeOrder.setDeductionPrice(highDiscountUserRel.getHighDiscount().getDiscountPrice());

                    // 如果总额小于0
                    if (payPrice.compareTo(new BigDecimal("0")) == -1) {
                        outRechargeOrder.setPayPrice(new BigDecimal("0"));
                    } else {
                        outRechargeOrder.setPayPrice(payPrice);
                    }
                }

                // 卡卷类型  1：满减  2：抵扣   3：折扣
                if (highDiscountUserRel.getHighDiscount().getDiscountType() == 3) {
                    BigDecimal discountPrice = highDiscountUserRel.getHighDiscount().getDiscountPrice();
                    // 订单总额 * 折扣
                    BigDecimal payPrice = outRechargeOrder.getOrderPrice().multiply(discountPrice);
                    outRechargeOrder.setDeductionPrice(outRechargeOrder.getOrderPrice().subtract(payPrice));
                    outRechargeOrder.setPayPrice(payPrice);
                }
            }

            OutRechargePrice outRechargePrice = outRechargePriceService.findById(outRechargeOrder.getObjectId().longValue());

            HighUserCard highUserCard = highUserCardService.getUserHtlCardNo(userInfoModel.getHighUser().getId());
            if (highUserCard != null) {
                outRechargeOrder.setIdCard(highUserCard.getCardNo());
            }
            outRechargeOrder.setRemarks(remarks);
            outRechargeOrder.setRechargeName(remarks);
            outRechargeOrder.setOrderNo("CZ" + DateUtil.date2String(new Date(),"yyyyMMddHHmmss") + IDGenerator.nextId(5));
            outRechargeOrder.setUserId(userInfoModel.getHighUser().getId());
            outRechargeOrder.setUserName(userInfoModel.getHighUser().getName());
            outRechargeOrder.setRechargeType(outRechargePrice.getRechargeType());
            outRechargeOrder.setUserPhone(userInfoModel.getHighUser().getPhone());

            outRechargeOrder.setCreateTimed(new Date());
            outRechargeOrder.setStatus(1);
            outRechargeOrderService.insertOrder(outRechargeOrder);
            return ResponseMsgUtil.success(outRechargeOrder);

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


    @RequestMapping(value = "/getOrderById", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "根据id查询订单详情")
    public ResponseData getOrderById(@RequestParam(name = "orderId", required = true) Long orderId) {
        try {

            return ResponseMsgUtil.success(outRechargeOrderService.findByOrderId(orderId));

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

    @RequestMapping(value = "/cancelOrder", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "取消订单")
    public ResponseData cancelOrder(@RequestParam(name = "orderId", required = true) Long orderId) {
        try {

            OutRechargeOrder outRechargeOrder  = outRechargeOrderService.findByOrderId(orderId);

            if (outRechargeOrder.getStatus() == 1) {
                outRechargeOrderService.cancelOrder(orderId);
            } else {
                log.error("orderToPay error!");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.STATUS_ERROR, "");
            }

            return ResponseMsgUtil.success("操作成功");

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

    @RequestMapping(value = "/getUserOrderList", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取用户订单")
    public ResponseData getUserOrderList(
            @RequestParam(name = "status", required = false) Integer status,
            @RequestParam(name = "orderNo", required = false) String orderNo,
            @RequestParam(name = "rechargeModel", required = false) String rechargeModel,
            @RequestParam(name = "pageNum", required = true) Integer pageNum,
            @RequestParam(name = "pageSize", required = true) Integer pageSize,
            HttpServletRequest request) {
        try {

            // 用户
            SessionObject sessionObject = userCenter.getSessionObject(request);
            HighUserModel userInfoModel = (HighUserModel) sessionObject.getObject();

            Map<String,String> map = new HashMap<>();
            map.put("userId", userInfoModel.getHighUser().getId().toString());
            if (status != null) {
                map.put("status", status.toString());
            }
            if (StringUtils.isNotBlank(orderNo)) {
                map.put("orderNo", orderNo);
            }
            if (StringUtils.isNotBlank(rechargeModel)) {
                map.put("rechargeModel", rechargeModel);
            }

            PageHelper.startPage(pageNum,pageSize);
            return ResponseMsgUtil.success(new PageInfo<>(outRechargeOrderService.getListRechargeOrder(map)));

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


    @RequestMapping(value="/orderToPay",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "订单支付发起支付")
    public ResponseData orderToPay(@RequestBody String reqBodyStr) {
        try {
            if (StringUtils.isBlank(reqBodyStr)) {
                log.error("orderToPay error!", "参数错误");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }
            JSONObject jsonObject = JSONObject.parseObject(reqBodyStr);
            Long orderId = jsonObject.getLong("orderId");
            Integer openIdType = jsonObject.getInteger("openIdType"); // openId类型 1：小程序  2：公众号
            String openId = jsonObject.getString("openId");       // openId
            if (orderId == null || StringUtils.isBlank(openId)) {
                log.error("orderToPay error!", "参数错误");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            // 订单
            OutRechargeOrder order = outRechargeOrderService.findByOrderId(orderId);
            if (order == null) {
                log.error("orderToPay error!", "未找到订单信息");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到订单信息");
            }

            // 订单状态 ： 1.待支付 2.已支付 3.已完成 4.已退款 5.已取消
            if (order.getStatus() != 1) {
                log.error("orderToPay error!", "无法支付，订单不处于待支付状态");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "无法支付，订单不处于待支付状态");
            }

            Map<String,Object> map = new HashMap<>();
            map.put("orderNo", order.getOrderNo());
            map.put("payPrice", order.getPayPrice());
            map.put("orderScene", "RECHARGE_ORDER");
            map.put("body","充值话费");
            map.put("subject","充值话费");

            //微信支付
            String nonce_str = MD5Util.MD5Encode(String.valueOf(ThreadLocalRandom.current().nextInt(10000)), "UTF-8");
            int total_fee = MathUtils.objectConvertBigDecimal(map.get("payPrice")).multiply(new BigDecimal("100")).intValue();
            WeChatPayReqInfo weChatPayReqInfo = new WeChatPayReqInfo();
            weChatPayReqInfo.setAppid(SysConst.getSysConfig().getWxMchAppId());                                //公众号id
            weChatPayReqInfo.setMch_id(SysConst.getSysConfig().getWxMchId());                               //商户号
            if (openIdType != null && openIdType.equals(2)) {
                weChatPayReqInfo.setSub_appid("wxa075e8509802f826"); //商户号公众号
            } else {
                weChatPayReqInfo.setSub_appid(SysConst.getSysConfig().getWxSubAppId());//小程序
            }
            weChatPayReqInfo.setSub_mch_id("1614670195");
            weChatPayReqInfo.setSub_openid(openId);
            weChatPayReqInfo.setNonce_str(nonce_str);                                                   //随机字符串
            weChatPayReqInfo.setBody(map.get("body").toString());                                           	//商品描述
            weChatPayReqInfo.setOut_trade_no(map.get("orderNo").toString());                			//商户订单号
            weChatPayReqInfo.setTotal_fee(total_fee);                                           //总金额
            weChatPayReqInfo.setSpbill_create_ip("139.159.177.244");                              //终端ip
            weChatPayReqInfo.setNotify_url(SysConst.getSysConfig().getNotifyUrl());                       //通知url
            weChatPayReqInfo.setTrade_type("JSAPI");                                              //交易类型
            weChatPayReqInfo.setAttach(map.get("orderScene").toString());
            weChatPayReqInfo.setProfit_sharing("N");
            //附加数据，区分订单类型
            Map<String,String> payMap = new HashMap<>();

            payMap.put("app_id", weChatPayReqInfo.getSub_appid());
            payMap.put("api_key",SysConst.getSysConfig().getWxApiKey());
            payMap.put("unified_order_url",SysConst.getSysConfig().getWxUnifiedOrderUrl());
            SortedMap<Object, Object> sortedMap = wechatPayUtil.goWechatPay(weChatPayReqInfo,payMap);
            return ResponseMsgUtil.success(sortedMap);
        } catch (Exception e) {
            log.error("orderToPay error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/hltUnionCardPay",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "工会卡支付")
    public ResponseData hltUnionCardPay(@RequestBody JSONObject body,HttpServletRequest request) {
        try {
            // 用户
            SessionObject sessionObject = userCenter.getSessionObject(request);
            HighUserModel userInfoModel = (HighUserModel) sessionObject.getObject();

            if (body == null
                    || body.getLong("orderId") == null
                    || StringUtils.isBlank(body.getString("cardNo"))
            ) {
                log.error("orderToPay error!", "参数错误");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            // 查询用户支付密码
            HighUserPayPassword userPayPassword = highUserPayPasswordService.getDetailByUser(userInfoModel.getHighUser().getId());
            if (userPayPassword == null) {
                log.error("orderToPay error!", "未设置支付密码");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.NOT_SET_USER_PAY_PWD, "");
            }
            if (StringUtils.isBlank(body.getString("password"))) {
                log.error("hltUnionCardPay error!", "未输入支付密码");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.NOT_ENTER_USER_PAY_PWD, "");
            }
            // 校验支付密码是否一直
            if (!AESEncodeUtil.aesEncrypt(body.getString("password")).equals(userPayPassword.getPassword())) {
                log.error("hltUnionCardPay error!", "");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.USER_PAY_PWD_ERROR, "");
            }

            // 查询用户与卡号的关系
            HighUserCard userCard = highUserCardService.getDetailByUserCardNo(userInfoModel.getHighUser().getId(), body.getString("cardNo"));
            if (userCard == null) {
                log.error("hltUnionCardPay() ERROR", "未绑定卡号");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未绑定卡号");
            }

            // 订单
            OutRechargeOrder order = outRechargeOrderService.findByOrderId(body.getLong("orderId"));
            if (order == null) {
                log.error("hltUnionCardPay error!", "未找到订单信息");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到订单信息");
            }

            // 订单状态 ： 1.待支付 2.已支付 3.已完成 4.已退款 5.已取消
            if (order.getStatus() != 1) {
                log.error("hltUnionCardPay error!", "无法支付，订单不处于待支付状态");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "无法支付，订单不处于待支付状态");
            }

            outRechargeOrderService.hltUnionCardPay(userCard.getId(), order.getId());

            return ResponseMsgUtil.success(outRechargeOrderService.findByOrderId(body.getLong("orderId")));
        } catch (Exception e) {
            log.error("hltUnionCardPay error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/orderToGoldPay",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "订单支付发起金币支付")
    public ResponseData orderToGoldPay(@RequestBody String reqBodyStr,HttpServletRequest request) {
        try {
            // 用户
            SessionObject sessionObject = userCenter.getSessionObject(request);
            HighUserModel userInfoModel = (HighUserModel) sessionObject.getObject();

            if (StringUtils.isBlank(reqBodyStr)) {
                log.error("orderToPay error!", "参数错误");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }
            JSONObject jsonObject = JSONObject.parseObject(reqBodyStr);
            Long orderId = jsonObject.getLong("orderId");
            String password = jsonObject.getString("password");

            if (orderId == null) {
                log.error("orderToPay error!", "参数错误");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            // 查询用户支付密码
            HighUserPayPassword userPayPassword = highUserPayPasswordService.getDetailByUser(userInfoModel.getHighUser().getId());
            if (userPayPassword == null) {
                log.error("orderToPay error!", "未设置支付密码");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.NOT_SET_USER_PAY_PWD, "");
            }
            if (StringUtils.isBlank(password)) {
                log.error("orderToPay error!", "未输入支付密码");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.NOT_ENTER_USER_PAY_PWD, "");
            }
            // 校验支付密码是否一直
            if (!AESEncodeUtil.aesEncrypt(password).equals(userPayPassword.getPassword())) {
                log.error("orderToPay error!", "");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.USER_PAY_PWD_ERROR, "");
            }

            // 订单
            OutRechargeOrder order = outRechargeOrderService.findByOrderId(orderId);
            if (order == null) {
                log.error("hltUnionCardPay error!", "未找到订单信息");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到订单信息");
            }

            // 订单状态 ： 1.待支付 2.已支付 3.已完成 4.已退款 5.已取消
            if (order.getStatus() != 1) {
                log.error("hltUnionCardPay error!", "无法支付，订单不处于待支付状态");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "无法支付，订单不处于待支付状态");
            }

            outRechargeOrderService.goldPayOrder(userInfoModel.getHighUser().getId(), order.getId());

            if (order.getRechargeType() == 1) {
               JSONObject object =  outRechargeOrderService.getMobile(order.getRechargeContent() , order.getOrderPrice().intValue() , order.getOrderNo() , order.getRechargeType());
               if (object.getInteger("code") != 200) {
                   order.setRechargeStatus(1);
                   order.setStatus(2);
                   order.setPayType(3);
                   order.setPayTime(new Date()); // 支付时间
                   order.setAbnormalMsg(object.getString("message"));
                   outRechargeOrderService.updateOrder(order);
               }
            }

            return ResponseMsgUtil.success("充值成功");
        } catch (Exception e) {
            log.error("orderToPay error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }




    @RequestMapping(value="/orderToUnionPay",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "订单支付发起银联支付")
    public ResponseData orderToUnionPay(@RequestBody String reqBodyStr,HttpServletRequest request) {
        try {
            // 用户
            SessionObject sessionObject = userCenter.getSessionObject(request);
            HighUserModel userInfoModel = (HighUserModel) sessionObject.getObject();

            if (StringUtils.isBlank(reqBodyStr)) {
                log.error("orderToPay error!", "参数错误");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }
            JSONObject jsonObject = JSONObject.parseObject(reqBodyStr);
            Long orderId = jsonObject.getLong("orderId");

            if (orderId == null) {
                log.error("orderToPay error!", "参数错误");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }
            // 订单
            OutRechargeOrder order = outRechargeOrderService.findByOrderId(orderId);
            if (order == null) {
                log.error("hltUnionCardPay error!", "未找到订单信息");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到订单信息");
            }

            // 订单状态 ： 1.待支付 2.已支付 3.已完成 4.已退款 5.已取消
            if (order.getStatus() != 1) {
                log.error("hltUnionCardPay error!", "无法支付，订单不处于待支付状态");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "无法支付，订单不处于待支付状态");
            }

            return ResponseMsgUtil.success(unionPayConfig.upPreOrder(UnionPayConfig.MER_ID2, UnionPayConfig.TERM_ID2, order.getOrderNo(), order.getPayPrice(), "话费充值", CommonSysConst.getSysConfig().getUnionPayTelNotifyUrl(), request));

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


    @RequestMapping(value = "/wxProfitsharing", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "分账")
    public ResponseData wxProfitsharing() {
        try {
            HighOrder order = highOrderService.getOrderByOrderNo("20211019173044475879505");
            BigDecimal rake = new BigDecimal("0.01");
            // 计算微信收取的手续费     支付金额 * 0.002      注：如果与两个相邻数字的距离相等，则为上舍入的舍入模式。
            BigDecimal wxHandlingFee = order.getPayPrice().multiply(new BigDecimal("0.002")).setScale(2,BigDecimal.ROUND_HALF_DOWN);
            BigDecimal price = order.getPayPrice().subtract(wxHandlingFee);

            Map<String,String> param = new LinkedHashMap<>();
            param.put("appid", "wx637bd6f7314daa46");
            param.put("mch_id", "1289663601");
            param.put("sub_mch_id" , "1609882817"); // 个体户黎杨珍
            param.put("transaction_id" , order.getPaySerialNo());
            param.put("out_order_no" , order.getOrderNo());
            param.put("nonce_str" , WxUtils.makeNonStr());

            // 计算分账金额    手续费后的价格 * 0.01  注：如果与两个相邻数字的距离相等，则为上舍入的舍入模式。
            BigDecimal porofitSharingAmount = price.multiply(rake).setScale(2,BigDecimal.ROUND_DOWN);

            List<Map<String,Object>> receiversList = new ArrayList<>();
            Map<String,Object> receiversMap = new LinkedHashMap<>();
            receiversMap.put("type", "MERCHANT_ID");
            receiversMap.put("account", "1603942866");
            receiversMap.put("amount", porofitSharingAmount.multiply(new BigDecimal("100")).intValue());
            receiversMap.put("description", "分给商户【惠昕石化】");
            receiversList.add(receiversMap);
            param.put("receivers" , JSONObject.toJSONString(receiversList));
            String signStr = WxUtils.generateSignature(param, "Skufk5oi85wDFGl888i6wsRSTkdd5df5" , WXPayConstants.SignType.HMACSHA256);
            param.put("sign" , signStr);
            String resultXmL = this.doRefundRequest(param.get("mch_id"),null, WxUtils.mapToXml(param));

            // 请求分账返回的结果
            ResultProfitSharing resultProfitSharing = XmlUtil.getObjectFromXML(resultXmL, ResultProfitSharing.class);

            HighProfitSharingRecord sharingRecord = new HighProfitSharingRecord();
            sharingRecord.setOutOrderNo(resultProfitSharing.getOut_order_no());
            sharingRecord.setTransactionId(resultProfitSharing.getTransaction_id());
            sharingRecord.setOrderId(resultProfitSharing.getOrder_id());
            sharingRecord.setStatus(resultProfitSharing.getResult_code());
            sharingRecord.setPrice(porofitSharingAmount);
            sharingRecord.setCreateTime(new Date());
            sharingRecord.setContent(resultXmL);
            highProfitSharingRecordService.insert(sharingRecord);

                return ResponseMsgUtil.success("操作成功");

            } catch (Exception e) {
                log.error("CmsContentController --> getCorporateAdvertising() error!", e);
                return ResponseMsgUtil.success(e);
            }
    }

    @RequestMapping(value = "/wxEndProfitsharing", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "完结分账")
    public ResponseData wxEndProfitsharing(String transaction_id,String out_order_no, BigDecimal amount) {
        try {

            HighOrder orderNo = highOrderService.getOrderByOrderNo("HF2021101812025050304");
            Map<String,String> param = new LinkedHashMap<>();
            param.put("appid", "wx637bd6f7314daa46");
            param.put("mch_id", "1289663601");
            param.put("sub_mch_id" , "1609882817"); // 个体户黎杨珍
            param.put("nonce_str" , WxUtils.makeNonStr());
            param.put("transaction_id" , orderNo.getPaySerialNo());
            param.put("out_order_no" , orderNo.getOrderNo());
            param.put("description" , "系统自动完结分账");
            String signStr = WxUtils.generateSignature(param, "Skufk5oi85wDFGl888i6wsRSTkdd5df5" , WXPayConstants.SignType.HMACSHA256);
            param.put("sign" , signStr);

            String resultXmL = this.doRefundRequest2(param.get("mch_id"),"https://api.mch.weixin.qq.com/secapi/pay/profitsharingfinish", WxUtils.mapToXml(param));
            // 请求分账返回的结果
            ResultProfitSharing resultProfitSharing = XmlUtil.getObjectFromXML(resultXmL, ResultProfitSharing.class);
            return ResponseMsgUtil.success(resultXmL);

        } catch (Exception e) {
            log.error("CmsContentController --> getCorporateAdvertising() error!", e);
            return ResponseMsgUtil.success(e);
        }
    }

    @RequestMapping(value = "/wxSelectProfitsharing", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询剩余分账金额")
    public ResponseData wxSelectProfitsharing(String transaction_id,String out_order_no, BigDecimal amount) {
        try {
            Map<String,String> param = new LinkedHashMap<>();
            param.put("mch_id", "1289663601");
            //param.put("sub_mch_id" , "1609882817"); // 个体户黎杨珍
            param.put("transaction_id" , "4200001175202108102870717911");
            ///param.put("out_order_no" , String.valueOf(new Date().getTime()));
            param.put("nonce_str" , WxUtils.makeNonStr());
            String signStr = WxUtils.generateSignature(param, "Skufk5oi85wDFGl888i6wsRSTkdd5df5" , WXPayConstants.SignType.HMACSHA256);
            param.put("sign", signStr);
            String unifiedXmL = WxUtils.mapToXml(param);
            return ResponseMsgUtil.success(HttpReqUtil.HttpsDefaultExecute("POST", "https://api.mch.weixin.qq.com/pay/profitsharingorderamountquery", null, unifiedXmL, null));

        } catch (Exception e) {
            log.error("CmsContentController --> getCorporateAdvertising() error!", e);
            return ResponseMsgUtil.success(e);
        }
    }

    public String doRefundRequest(String mchId, String url, String data) throws Exception {

        //小程序退款需要调用双向证书的认证
        CloseableHttpClient httpClient = goodsOrderService.readCertificate(mchId);
        try {
            HttpPost httpost = new HttpPost("https://api.mch.weixin.qq.com/secapi/pay/profitsharing"); // 设置响应头信息
            httpost.addHeader("Connection", "keep-alive");
            httpost.addHeader("Accept", "*/*");
            httpost.addHeader("Content-Type", "text/xml");
            httpost.addHeader("Host", "api.mch.weixin.qq.com");
            httpost.addHeader("X-Requested-With", "XMLHttpRequest");
            httpost.addHeader("Cache-Control", "max-age=0");
            httpost.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0) ");
            httpost.setEntity(new StringEntity(data, "UTF-8"));
            CloseableHttpResponse response = httpClient.execute(httpost);
            try {
                HttpEntity entity = response.getEntity();
                String jsonStr = EntityUtils.toString(response.getEntity(), "UTF-8");
                EntityUtils.consume(entity);
                return jsonStr;
            } finally {
                response.close();
            }
        } catch (Exception e){
            throw new RuntimeException(e);
        } finally {
            httpClient.close();
        }
    }

    public String doRefundRequest2(String mchId, String url, String data) throws Exception {

        //小程序退款需要调用双向证书的认证
        CloseableHttpClient httpClient = goodsOrderService.readCertificate(mchId);
        try {
            HttpPost httpost = new HttpPost(url); // 设置响应头信息
            httpost.addHeader("Connection", "keep-alive");
            httpost.addHeader("Accept", "*/*");
            httpost.addHeader("Content-Type", "text/xml");
            httpost.addHeader("Host", "api.mch.weixin.qq.com");
            httpost.addHeader("X-Requested-With", "XMLHttpRequest");
            httpost.addHeader("Cache-Control", "max-age=0");
            httpost.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0) ");
            httpost.setEntity(new StringEntity(data, "UTF-8"));
            CloseableHttpResponse response = httpClient.execute(httpost);
            try {
                HttpEntity entity = response.getEntity();
                String jsonStr = EntityUtils.toString(response.getEntity(), "UTF-8");
                EntityUtils.consume(entity);
                return jsonStr;
            } finally {
                response.close();
            }
        } catch (Exception e){
            throw new RuntimeException(e);
        } finally {
            httpClient.close();
        }
    }
}
