package com.cweb.controller;


import com.alibaba.fastjson.JSONObject;
import com.hfkj.common.security.UserCenter;
import com.hfkj.common.utils.ResponseMsgUtil;
import com.hfkj.common.utils.WxUtils;
import com.hfkj.entity.BsOrderSettle;
import com.hfkj.entity.BsUserSpreadAccount;
import com.hfkj.model.ResponseData;
import com.hfkj.model.UserSessionObject;
import com.hfkj.pay.HuiPayService;
import com.hfkj.service.gas.BsGasOrderSpreadService;
import com.hfkj.service.order.BsOrderSettleService;
import com.hfkj.service.spread.BsUserSpreadAccountRecordService;
import com.hfkj.service.spread.BsUserSpreadAccountService;
import com.wechat.pay.contrib.apache.httpclient.WechatPayHttpClientBuilder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
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.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @className: BsUserSpreadAccountController
 * @author: HuRui
 * @date: 2025/3/31
 **/
@Controller
@RequestMapping(value = "/test")
@Api(value = "个人推广账户业务")
public class TestController {
    private static Logger log = LoggerFactory.getLogger(TestController.class);
    @Resource
    private UserCenter userCenter;
    @Resource
    private BsUserSpreadAccountService userSpreadAccountService;
    @Resource
    private BsGasOrderSpreadService gasOrderSpreadService;

    @RequestMapping(value = "/settleOrder", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "结算订单")
    public ResponseData settleOrder() {
        try {
            List<BsUserSpreadAccount> accountList = userSpreadAccountService.getList(new HashMap<>());
            for (BsUserSpreadAccount account : accountList) {
                try {
                    gasOrderSpreadService.settleOrder(account.getUserId());
                } catch (Exception e) {
                    System.out.println("用户ID："+account.getUserId()+"，账户结算异常");
                }
            }

            return ResponseMsgUtil.success("");

        } catch (Exception e) {
            return ResponseMsgUtil.exception(e);
        }
    }


    @RequestMapping(value = "/transferBills", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "转账交易")
    public ResponseData transferBills() {
        try {

            JSONObject obj = WxUtils.transferBills("oUGn_4unIjp90MY-oEXluY4laDrQ",""+System.currentTimeMillis(), new BigDecimal("0.1"), "推广佣金提现");

            System.out.println("响应内容："+obj);
            return ResponseMsgUtil.success(obj);

        } catch (Exception e) {
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/cancelTransferBills", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "取消转账")
    public ResponseData cancelTransferBills(@RequestParam(value = "outBillNo", required = true) String outBillNo) {
        try {

            userSpreadAccountService.cancelWithdraw(outBillNo);

            // return ResponseMsgUtil.success(WxUtils.cancelTransferBills(outBillNo));
            return ResponseMsgUtil.success("");

        } catch (Exception e) {
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/queryTransferBills", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询转账详情")
    public ResponseData queryTransferBills(@RequestParam(value = "outBillNo", required = true) String outBillNo) {
        try {

            return ResponseMsgUtil.success(WxUtils.queryTransferBills(outBillNo));

        } catch (Exception e) {
            return ResponseMsgUtil.exception(e);
        }
    }

}
