package com.bohuikeji.delivery.backstage.pay;

import com.alibaba.fastjson.JSONObject;
import com.bohuikeji.delivery.backstage.entity.bytedance.ByteConfig;
import com.bohuikeji.delivery.backstage.entity.TPay;
import com.bohuikeji.delivery.backstage.entity.TSettle;
import com.bohuikeji.delivery.backstage.service.ITOrderService;
import com.bohuikeji.frame.module.admin.utils.NumberUtils;
import com.bohuikeji.frame.module.admin.utils.douyin.byteSign;
import jodd.http.HttpRequest;
import jodd.net.MimeTypes;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class BytePay {
    private static final String Url = "https://developer.toutiao.com/api/apps/ecpay/v1/create_order";//抖音小程序有预下单地址
    private static final String refundUrl = "https://developer.toutiao.com/api/apps/ecpay/v1/create_refund";//抖音小程序退款地址
    private static final String getRecordUrl = "https://developer.toutiao.com/api/apps/bill";//账单记录查询接口
    private static final String settleUrl = "https://developer.toutiao.com/api/apps/ecpay/v1/settle";//交易分账
    private static final String byteLoginUrl = "https://developer.toutiao.com/api/apps/v2/jscode2session";//字节跳动小程序登陆请求地址
    @Autowired
    ITOrderService orderService;

    /**
     * 抖音获取openId
     * @return
     */
    public static String getDouYinOpenId(String code) {//pay.getPayExpectPrice().multiply(new BigDecimal(100)).longValue()
        JSONObject reqJson=new JSONObject();
        reqJson.put("appid", ByteConfig.App_ID);
        reqJson.put("secret",ByteConfig.APP_SECRET);
        reqJson.put("code",code);
        //reqJson.put("anonymous_code",anonymousCode);
        String resultJson = HttpRequest.post(byteLoginUrl).bodyText(reqJson.toJSONString(),MimeTypes.MIME_APPLICATION_JSON,"UTF-8").send().contentType("application/json","utf-8").bodyText();
        JSONObject res= JSONObject.parseObject(resultJson);
        log.info("登陆返回：[{}]",res);
        // 获取参数返回的
        String openId = res.getString("openid");
        return openId;
    }

    /**
     * 抖音预支付订单
     * @param payNum
     * @param payExpectPrice
     * @return
     */
    public static JSONObject getByteOrderInfo(String payNum,BigDecimal payExpectPrice) {//pay.getPayExpectPrice().multiply(new BigDecimal(100)).longValue()
        Map<String, Object> map = new HashMap<>();
        map.put("app_id",ByteConfig.App_ID);
        map.put("out_order_no",payNum);
        map.put("total_amount",payExpectPrice.multiply(new BigDecimal(100)).longValue());
        map.put("subject","订单支付");
        map.put("body","同城急送");
        map.put("valid_time",7200);
        map.put("notify_url","https://os.tcjskd.com/delivery/api/v2/byte/pay/notify");
        map.put("disable_msg",0);
        map.put("sign",byteSign.getSign(map));
        String req = JSONObject.toJSONString(map);
        String res = HttpRequest.post(Url).bodyText(req,MimeTypes.MIME_APPLICATION_JSON,"UTF-8").send()
                .contentType("application/json","utf-8").bodyText();
        JSONObject jsonObject= JSONObject.parseObject(res);
        return jsonObject.getJSONObject("data");
    }


    /**
     * 抖音小程序申请退款
     * @param pay
     * @return
     */
    public static JSONObject douyinOrderRefund(TPay pay,BigDecimal price) {
        Map<String, Object> map = new HashMap<>();
        map.put("app_id",ByteConfig.App_ID);
        map.put("out_order_no",pay.getPayNum());
        map.put("out_refund_no",NumberUtils.get("T"));
        map.put("reason","订单退款");
        map.put("refund_amount",price.multiply(new BigDecimal("100")).longValue());
        map.put("notify_url","https://os.tcjskd.com/delivery/api/v2/byte/pay/notify");
        map.put("sign",byteSign.getSign(map));
        String req = JSONObject.toJSONString(map);
        String res = HttpRequest.post(refundUrl).bodyText(req,MimeTypes.MIME_APPLICATION_JSON,"UTF-8").send()
                .contentType("application/json","utf-8").bodyText();
        JSONObject jsonObject= JSONObject.parseObject(res);
        return jsonObject;
    }


    /**
     * 支付账单查询
     * @param startDate
     * @param endDate
     * @param type
     * @return
     */
    public static String getOrderPayRecord(String startDate, String endDate,String type) {
        Map<String, Object> map = new HashMap<>();
        map.put("app_id",ByteConfig.App_ID);
        map.put("start_date",startDate);
        map.put("end_date",endDate);
        map.put("seller",ByteConfig.Merchant_Uid);
        map.put("bill_type",type);
        map.put("sign",byteSign.getSign(map));
        Map<String, String> pmap = new HashMap<>();
        pmap.put("app_id",map.get("app_id").toString());
        pmap.put("start_date",map.get("start_date").toString());
        pmap.put("end_date",map.get("end_date").toString());
        pmap.put("seller",map.get("seller").toString());
        pmap.put("bill_type",map.get("bill_type").toString());
        pmap.put("sign",map.get("sign").toString());
        String res = HttpRequest.get(getRecordUrl).query(pmap).send().bodyText();
        //JSONObject jsonObject= JSONObject.parseObject(res);
        return res;
    }

    /**
     * 抖音订单交易分账
     * @param settle
     * @return
     */
    public static JSONObject getOrderSettle(TSettle settle) {
        Map<String, Object> map = new HashMap<>();
        map.put("app_id",ByteConfig.App_ID);
        map.put("out_settle_no",settle.getSettleNum());
        map.put("out_order_no",settle.getPayNum());
        map.put("settle_desc","订单支付分账");
        map.put("notify_url","https://os.tcjskd.com/delivery/api/v2/byte/get/pay/settle");
        map.put("sign",byteSign.getSign(map));
        String req = JSONObject.toJSONString(map);
        log.info("@@@@@@@@@@@@@@@@@@@@@@@@@map:{}",map);
        String res = HttpRequest.post(settleUrl).bodyText(req,MimeTypes.MIME_APPLICATION_JSON,"UTF-8").send().bodyText();
        JSONObject jsonObject= JSONObject.parseObject(res);
        return jsonObject;
    }

}
