package com.cweb.controller;


import com.alibaba.fastjson.JSONObject;
import com.hfkj.channel.gas.newlink2.NewLink2GasService;
import com.hfkj.channel.gas.newlink2.NewLink2RequestService;
import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.security.UserCenter;
import com.hfkj.common.utils.ResponseMsgUtil;
import com.hfkj.common.utils.WxUtils;
import com.hfkj.model.ResponseData;
import com.hfkj.service.gas.BsGasOrderSpreadService;
import com.hfkj.service.spread.BsUserSpreadAccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
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.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 NewLink2GasService newLink2GasService;
    @Resource
    private BsGasOrderSpreadService gasOrderSpreadService;

    @RequestMapping(value = "/getAllGasStation", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "拉取全量油站")
    public ResponseData getAllGasStation() {
        try {

            // return ResponseMsgUtil.success(NewLink2RequestService.getAllGasStation());
            newLink2GasService.refresh();
            return ResponseMsgUtil.success("");


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

    @RequestMapping(value = "/createOrder", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "拉取全量油站")
    public ResponseData createOrder() {
        try {

            String gasId = "JE000126225";
            String orderNo = System.currentTimeMillis()+"";
            BigDecimal fuelAmount = new BigDecimal("100");
            String oilNo = "90";
            String gunNo = "3";
            JSONObject getPriceInfo = NewLink2RequestService.getPriceInfo(gasId, fuelAmount, oilNo, gunNo);
            if (!getPriceInfo.getString("code").equals("200")) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, getPriceInfo.getString("msg"));
            }
            Map<String,Object> createParam = new HashMap<>();
            createParam.put("gasId", gasId);
            createParam.put("fuelAmount", fuelAmount.toString());
            createParam.put("oilNo", oilNo);
            createParam.put("gunNo", gunNo);
            createParam.put("snapshotPriceId", getPriceInfo.getJSONObject("data").getString("snapshotPriceId"));
            createParam.put("longitude", "116.517319");
            createParam.put("latitude", "39.933826");
            createParam.put("uuid", orderNo);
            createParam.put("thirdPartyDiscountAmount", "100");
            createParam.put("phone", "17726395120");

            JSONObject createOrder = NewLink2RequestService.createOrder(createParam);
            if (!createOrder.getString("code").equals("200")) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, createOrder.getString("msg"));
            }

            JSONObject payCallBack = NewLink2RequestService.payCallBack(
                    createOrder.getJSONObject("data").getString("orderNo"),
                    3,
                    null,
                    orderNo
            );
            return ResponseMsgUtil.success(payCallBack);


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

    @RequestMapping(value = "/refundCallBack", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "拉取全量油站")
    public ResponseData refundCallBack(@RequestParam(value = "orderNo", required = true) String orderNo) {
        try {

            JSONObject object = NewLink2RequestService.refundCallBack(orderNo,
                    "加错站了",
                    "加错站了",
                    System.currentTimeMillis() + "");

            return ResponseMsgUtil.success(object);


        } 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);
        }
    }

}
