package org.jeecg.modules.pay.routing;

import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.pay.config.WechatPayV3Utils;
import org.jeecg.modules.pay.config.WechatUrlConstants;
import org.jeecg.modules.pay.entity.ProfitSharingRequest;
import org.jeecg.modules.pay.entity.Receiver;
import org.jeecg.modules.pay.paytest.ServiceProvider;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 添加分账接收方
 *
 * @author libaibai
 * @version 1.0 2020年10月19日
 */
@Slf4j
public class ProfitsharingAddReceiverBo {

    @Resource
     private WechatPayV3Utils wechatPayV3Utils;

    /**
     * @func :微信单次分账
     * @data: 2019-11-11
     * @auther :lijinquan
     */
    public JSONObject wechatProfitSharing() {
        //1.参数封装
        /**
         * 分账接收方列表，不超过50个json对象，不能设置出资子商户作为分账接受方
         点击行前的+展开字段详情
         */
        List<Receiver> receivers = new ArrayList<>();
        //查询当前订单的分账接收方列表
        for (int i = 0; i < 1; i++) {
            Receiver receiver = new Receiver();
            receiver.setType("MERCHANT_ID")
                    .setAccount(ServiceProvider.sp_mchid)
                    .setAmount(1)
                    .setDescription("微信文档值得吐槽");
            receivers.add(receiver);
        }
        ProfitSharingRequest profitSharingRequest = new ProfitSharingRequest();
        profitSharingRequest
//                .setNonce_str(RandomUtil.randomString(32))
//                .setSign_type("HMAC-SHA256")
                .setAppid(ServiceProvider.sp_appid)
                .setSub_mchid(ServiceProvider.sub_mchid)
                .setTransaction_id("4200001042202104228993583353")
                .setOut_order_no("P001001001")
                .setReceivers(receivers)
                .setUnfreeze_unsplit(true);
        try {
            JSONObject res = wechatPayV3Utils.sendPost(WechatUrlConstants.PAY_V3_JSAPI, JSONObject.from(profitSharingRequest));
            log.info("wechatPay res:{}", res.toString());
            return res;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

}