package com.bohuikeji.delivery.backstage.controller.admin;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bohuikeji.delivery.backstage.constants.payConst;
import com.bohuikeji.delivery.backstage.entity.*;
import com.bohuikeji.delivery.backstage.service.ITCustomerPayService;
import com.bohuikeji.delivery.backstage.service.ITCustomerService;
import com.bohuikeji.delivery.backstage.service.ITCustomerWalletService;
import com.bohuikeji.delivery.backstage.service.ITStoreWalletPayService;
import com.bohuikeji.frame.module.admin.annotation.InitLog;
import com.bohuikeji.frame.module.admin.base.ControllerBase;
import com.bohuikeji.frame.module.admin.entity.back.APIEntity;
import com.bohuikeji.frame.module.admin.entity.sys.SysUser;
import com.bohuikeji.frame.module.admin.service.ISysUserService;
import com.bohuikeji.frame.module.admin.utils.NumberUtils;
import com.bohuikeji.frame.module.admin.utils.ZxingUtils;
import com.bohuikeji.frame.module.admin.wxpay.WXPayUtil;
import com.bohuikeji.frame.module.admin.wxpayv3.WxPayV3Util;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.math.BigDecimal;

@Slf4j
@Controller
@ResponseBody
@RequestMapping("/store/wallet")
public class TStoreWalletController  extends ControllerBase {
    @Autowired
    ITStoreWalletPayService storeWalletPayService;
    @Autowired
    ITCustomerWalletService customerWalletService;
    @Autowired
    ITCustomerService customerService;
    @Autowired
    ITCustomerPayService customerPayService;
    @Autowired
    ISysUserService sysUserService;

    /**
     *门店钱包充值
     * @param
     * @return /storewallet/image
     * @throws AlipayApiException
     * @throws IOException
     */
    @InitLog(value = "钱包提现",action = "退款")
    @ApiOperation("钱包提现")
    @GetMapping("/native/sys")
    public APIEntity transfersRefund(HttpServletRequest request,String price, Integer franId) throws Exception {
        TStoreWalletPay storeWalletPay = storeWalletPayService.getOne(new QueryWrapper<TStoreWalletPay>().eq("fran_id",franId).eq("pay_status","待付款"));
        if (storeWalletPay!=null){
            if("待付款".equals(storeWalletPay.getPayStatus())){
                storeWalletPay.setPayNum(NumberUtils.get("MD"));
                storeWalletPay.setPayExpectPrice(new BigDecimal(price.trim()));
                storeWalletPayService.updateById(storeWalletPay.setPayExpectPrice(new BigDecimal(price.trim())));
                String codeurl = WxPayV3Util.V3Native(request,"v3/pay/transactions/native",storeWalletPay.getPayNum(),new BigDecimal(WXPayUtil.changeToFen(storeWalletPay.getPayExpectPrice())),"业务订单支付");
                log.info("返回native的请求地址 [{}]",codeurl);
                return APIEntity.success(codeurl);
            }
        }
        log.info("付款码支付金额:[{}]",price);
        log.info("付款码支付加盟商:[{}]",franId);
        storeWalletPay =storeWalletPayService.storeWalletCreatePay(franId,price.trim(),"微信","门店支付");
        String codeurl = WxPayV3Util.V3Native(request,"v3/pay/transactions/native",storeWalletPay.getPayNum(),new BigDecimal(WXPayUtil.changeToFen(new BigDecimal(price))),"业务订单支付");
        log.info("返回native的请求地址 [{}]",codeurl);
        return APIEntity.success(codeurl);
    }

    /**
     *开放平台商户钱包充值
     * @param
     * @return /storewallet/image
     * @throws AlipayApiException
     * @throws IOException
     */
    @InitLog(value = "钱包提现",action = "退款")
    @ApiOperation("钱包提现")
    @GetMapping("/open/merchant/rec")
    public APIEntity openMerchantRecharge(HttpServletRequest request,String price,String type) throws Exception {
        SysUser loginUser=getLoginUser();
        TCustomer customer = customerService.getById(loginUser.getCustomerId());
        if (customer == null) {
            return APIEntity.fail("无法找到客户信息，请重新登陆！", 412);
        }
        TCustomerWallet tCustomerWallet = customerWalletService.getOne(new QueryWrapper<TCustomerWallet>().eq("customer_id",customer.getCustomerId()).eq("wallet_state","提现中"));
        if(tCustomerWallet != null){
            return APIEntity.fail("您的钱包余额正在提现中，请稍候充值。");
        }
        JSONObject jsonObject = customerPayService.getCustomer(customer,price,type,"待付款",request);
        return APIEntity.success(jsonObject);
    }
}
