package com.lakala.zf.laop.java.sdk.demo.utils;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.lakala.zf.laop.java.sdk.demo.BaseCommonDemo;
import com.lkl.laop.sdk.LKLSDK;
import com.lkl.laop.sdk.exception.SDKException;
import com.lkl.laop.sdk.request.V3CcssCounterOrderCreateRequest;
import com.lkl.laop.sdk.request.model.V3CcssOrderOutSplitInfo;
import com.lkl.laop.sdk.request.model.V3CcssOrderSceneFieldInfo;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Maosw
 */
public class LakalaUtils extends BaseCommonDemo {




    /**
     * 聚合收银台创建订单
     *
     * @return
     */
    public static JSONObject createOrderPayment(String orderNo, BigDecimal payPrice, List<V3CcssOrderOutSplitInfo> splitInfoList) throws Exception {
        doInit();
        //创建订单请求参数
        V3CcssCounterOrderCreateRequest request = new V3CcssCounterOrderCreateRequest();

        //商户订单号
        request.setOutOrderNo(orderNo);

        //是否支持多次发起订单 0 不支持 1 支持
        request.setSupportRepeatPay(1);

        //银联商户号
        request.setMerchantNo(KlkConstant.merchantNo);

        //订单支付金额 单位:分
        request.setTotalAmount(payPrice.multiply(new BigDecimal(100)).longValue());

        //支付有效期 一天
        request.setOrderEfficientTime(DateUtil.format(DateUtil.offsetDay(new Date(), 1), "yyyyMMddHHmmss"));

        //回调地址
        request.setNotifyUrl(KlkConstant.DDHT_ORDER);

        request.setSupportCancel(1);

        request.setSupportRefund(1);

        /*request.setSplitMark("1");
        request.setOutSplitInfo(splitInfoList);*/

        //支付类型
//        req.setCounterParam("{\"pay_mode\":\"ALIPAY\"}");

//        req.setBusiTypeParam("[{\"busi_type\":\"UPCARD\",\"params\":{\"crd_flg\":\"CRDFLG_D|CRDFLG_C|CRDFLG_OTH\"}},{\"busi_type\":\"SCPAY\",\"params\":{\"pay_mode\":\"WECHAT\",\"crd_flg\":\"CRDFLG_D\"}}]");

        //	订单标题，在使用收银台扫码支付时必输入，交易时送往账户端
        request.setOrderInfo("集材社订单支付");

        List<String> sgnInfos = new ArrayList<>();

        sgnInfos.add("1");

        request.setSgnInfo(sgnInfos);

        V3CcssOrderSceneFieldInfo.HbFqSceneInfo hbFqSceneInfo = new V3CcssOrderSceneFieldInfo.HbFqSceneInfo();

        hbFqSceneInfo.setHbFqNum("3");

        hbFqSceneInfo.setHbFqSellerPercent("0");

        //3. 发送请求
        String response = null;
        try {
            response = LKLSDK.httpPost(request);
        } catch (SDKException e) {
            throw new RuntimeException(e);
        }

        //4. 响应
        System.out.println(response);

        JSONObject jsonObject = JSONObject.parseObject(response);
        if ("000000".equals(jsonObject.get("code"))) {
            System.out.println(jsonObject.get("resp_data").toString());
        } else {
            System.out.println(jsonObject.get("msg").toString());
        }
        return jsonObject;
    }

    /**
     * 聚合收银台创建订单
     *
     * @return
     */
    public static JSONObject createOrderPay(String orderNo, BigDecimal payPrice, String notifyUrl) throws Exception {
        doInit();
        //创建订单请求参数
        V3CcssCounterOrderCreateRequest request = new V3CcssCounterOrderCreateRequest();

        //商户订单号
        request.setOutOrderNo(orderNo);

        //是否支持多次发起订单 0 不支持 1 支持
        request.setSupportRepeatPay(1);

        //银联商户号
        request.setMerchantNo(KlkConstant.merchantNo);

        //订单支付金额 单位:分
        request.setTotalAmount(payPrice.multiply(new BigDecimal(100)).longValue());

        //支付有效期 一天
        request.setOrderEfficientTime(DateUtil.format(DateUtil.offsetDay(new Date(), 1), "yyyyMMddHHmmss"));

        //回调地址
        request.setNotifyUrl(notifyUrl);

        request.setSupportCancel(1);

        request.setSupportRefund(1);

        //支付类型
//        req.setCounterParam("{\"pay_mode\":\"ALIPAY\"}");

//        req.setBusiTypeParam("[{\"busi_type\":\"UPCARD\",\"params\":{\"crd_flg\":\"CRDFLG_D|CRDFLG_C|CRDFLG_OTH\"}},{\"busi_type\":\"SCPAY\",\"params\":{\"pay_mode\":\"WECHAT\",\"crd_flg\":\"CRDFLG_D\"}}]");

        //	订单标题，在使用收银台扫码支付时必输入，交易时送往账户端
        request.setOrderInfo("集材社订单支付");

        List<String> sgnInfos = new ArrayList<>();

        sgnInfos.add("1");

        request.setSgnInfo(sgnInfos);


        V3CcssOrderSceneFieldInfo.HbFqSceneInfo hbFqSceneInfo = new V3CcssOrderSceneFieldInfo.HbFqSceneInfo();

        hbFqSceneInfo.setHbFqNum("3");

        hbFqSceneInfo.setHbFqSellerPercent("0");

        //3. 发送请求
        String response = null;
        try {
            response = LKLSDK.httpPost(request);
        } catch (SDKException e) {
            throw new RuntimeException(e);
        }

        //4. 响应
        System.out.println(response);

        JSONObject jsonObject = JSONObject.parseObject(response);
        if ("000000".equals(jsonObject.get("code"))) {
            System.out.println(jsonObject.get("resp_data").toString());
        } else {
            System.out.println(jsonObject.get("msg").toString());
        }
        return jsonObject;
    }


}
