package com.bohuikeji.delivery.backstage.controller.api.customer;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bohuikeji.delivery.backstage.entity.TCustomerWallet;
import com.bohuikeji.delivery.backstage.service.ITCustomerWalletService;
import com.bohuikeji.delivery.backstage.service.ITOrderService;
import com.bohuikeji.delivery.backstage.service.ITPayService;
import com.bohuikeji.frame.module.admin.base.ApiController;
import com.bohuikeji.frame.module.admin.entity.back.APIEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author zhouliwu
 * 钱包相关接口
 * 注：所以接口类必须继承 ApiController，否则无法被接口文档识别
 */
@Api(tags = {"客户钱包充值相关接口"})
@RestController
@Slf4j
@RequestMapping("/api/v2/Wallet")
public class CustomerWalletV2Api extends ApiController {

    @Autowired
    ITCustomerWalletService customerWalletService;
    @Autowired
    ITOrderService orderService;
    @Autowired
    ITPayService payService;

    /**
     * 根据用户登陆获取用户钱包金额数量
     * @return
     */
    @ApiOperation("根据用户登陆获取用户钱包金额数量")
    @GetMapping("/get/customer/amount")
    public APIEntity<TCustomerWallet> getAmount(@RequestAttribute("JwtUserId") Integer id){
        TCustomerWallet tCustomerWallet = customerWalletService.getOne(new QueryWrapper<TCustomerWallet>().eq("customer_id",id));
        return APIEntity.success(tCustomerWallet);
    }

    /**
     *  钱包支付
     * @param orderId 订单id
     * @param request
     * @return
     * @throws Exception
     */
   /* @ApiOperation("钱包支付（业务支付）")
    @PostMapping("/walletPay")
    @ApiImplicitParams({
            @ApiImplicitParam(name="orderId",value="订单id",dataType="Integer", paramType = "query",example="15",required=true)})
    public APIEntity wxAliPay(Integer orderId, HttpServletRequest request) throws Exception {
        TOrder tOrder = orderService.getById(orderId);
        TCustomerWallet tCustomerWallet = customerWalletService.getOne(new QueryWrapper<TCustomerWallet>().eq("customer_id",tOrder.getPlayerId()));
        if(tOrder==null){
            log.error("未找到该订单信息");
            return APIEntity.fail("未找到该订单信息！");
        }
        if(tCustomerWallet==null){
            log.error("未找到该订单信息");
            return APIEntity.fail("尊敬的客户您好，您的钱包金额为'0',请及时充值");
        }
        if(!"待支付".equals(tOrder.getOrderStatus())){
            log.error("您好由于在规定的时间内未支付，系统以为你自动取消，如需支付请重新下单");
            return APIEntity.fail("您好由于在规定的时间内未支付，系统以为你自动取消，如需支付请重新下单");
        }
        if(new BigDecimal(0.00).compareTo(tCustomerWallet.getWalletPrice())==0){
            log.error("未找到该订单信息");
            return APIEntity.fail("尊敬的客户您好，您的钱包金额为'0',请及时充值");
        }
        if(tOrder.getOrderExpectPrice().compareTo(tCustomerWallet.getWalletPrice())==1){
            log.error("未找到该订单信息");
            return APIEntity.fail("尊敬的客户您好，您的钱包余额不足,请及时充值");
        }
        if(payService.list(new QueryWrapper<TPay>().eq("order_id",tOrder.getOrderId()).eq("pay_status","已支付")).size() > 0){//判断该订单是否已有支付记录，有则请勿重复支付
            return APIEntity.fail("当前订单已支付,请勿重复支付");
        }
        TPay pay = payService.getOne(new QueryWrapper<TPay>().eq("order_id",tOrder.getOrderId()).eq("pay_channel","微信"));
        if (pay!=null){
            if("已支付".equals(pay.getPayStatus())){
                //如果有已支付的订单则提示用户刷新客户端
                return APIEntity.fail("您已经支付过了，请重新打开APP再试！");
            }
            if("待付款".equals(pay.getPayStatus())){
                return APIEntity.success(payService.walletPay(pay,tCustomerWallet,tOrder));
            }
        }
        pay =payService.createBeforePay(tOrder,"业务支付","客户支付","钱包");
        return APIEntity.success( payService.walletPay(pay,tCustomerWallet,tOrder));
    }*/
}
