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

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bohuikeji.delivery.backstage.entity.TCustomer;
import com.bohuikeji.delivery.backstage.entity.TOrder;
import com.bohuikeji.delivery.backstage.entity.TRewardPay;
import com.bohuikeji.delivery.backstage.service.*;
import com.bohuikeji.frame.module.admin.base.ApiController;
import com.bohuikeji.frame.module.admin.entity.back.APIEntity;
import com.bohuikeji.frame.module.admin.service.ISysConfigService;
import com.bohuikeji.frame.module.admin.wxpay.WXPayUtil;
import com.bohuikeji.frame.module.admin.wxpayv3.WxPayV3Util;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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


/**
 * @author zhouliwu
 * 订单完成后客户打赏骑手
 * 注：所以接口类必须继承 ApiController，否则无法被接口文档识别
 */
@Api(tags = {"订单完成后客户打赏骑手"})
@RestController
@Slf4j
@RequestMapping("/api/v2/rewardPay")
public class TRewardPayV2Api extends ApiController {
    @Autowired
    ITOrderService itOrderService;
    @Autowired
    ITRewardPayService rewardPayService;
    @Autowired
    ITCourierPayService courierPayService;
    @Autowired
    ITCustomerWalletService customerWalletService;
    @Autowired
    ITCustomerPayService customerPayService;
    @Autowired
    ITRefundService refundService;
    @Autowired
    ITCourierService courierService;
    @Autowired
    ISysConfigService configService;
    @Autowired
    ITCustomerService customerService;

    /**
     * 客户顶大打赏
     * @param id
     * @return
     * @throws AlipayApiException
     */
    @ApiOperation("客户顶大打赏")
    @PostMapping("/wxpay")
    public APIEntity wxpayReward(@RequestAttribute("JwtUserId") Integer id,String price,String type,Integer orderId,Integer serviceNum,Integer speedNum,HttpServletRequest request) throws Exception {
        TCustomer customer = customerService.getById(id);
        if (customer == null) {
            return APIEntity.fail("无法找到客户信息，请重新登陆！", 412);
        }
        TOrder order=itOrderService.getOne(new QueryWrapper<TOrder>().eq("order_status","已完成").eq("order_id",orderId));
        if (order == null) {
            return APIEntity.fail("打赏的订单未完成，请完成后再打赏", 412);
        }
        if("已评价".equals(order.getEvaluateStatus())){
            return APIEntity.fail("您已评价完成，请不要重复评价");
        }
        TRewardPay rewardPay = rewardPayService.getOne(new QueryWrapper<TRewardPay>().eq("pay_scenario","订单打赏").eq("pay_status","待付款").eq("reward_price",new BigDecimal(price)).eq("order_id",order.getOrderId()));
        JSONObject object = null;
        //获取预付款rewardPay对象
        if(rewardPay == null){
            rewardPay =rewardPayService.createRewardPay(order,price,"订单打赏","客户支付","微信",serviceNum,speedNum);
        }
        if("APP".equals(type)){
            log.info("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@APP@@@@@@@@@@@@@@@@@@@@@@@@@@@:{}",new BigDecimal(WXPayUtil.changeToFen(rewardPay.getRewardPrice())));
            String prepayId = WxPayV3Util.V3cusAppPay("v3/pay/transactions/app",rewardPay.getPayNum(),new BigDecimal(WXPayUtil.changeToFen(rewardPay.getRewardPrice())),"订单打赏支付",request);
            object = JSONObject.parseObject(WxPayV3Util.WxAppTuneUp(prepayId).toString());
            //return object==null?APIEntity.fail("APP调起支付错误"):APIEntity.success(object);
        }
        if("JSAPI".equals(type)){
            log.info("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@JSAPI@@@@@@@@@@@@@@@@@@@@@@@@@@@:{}",new BigDecimal(WXPayUtil.changeToFen(rewardPay.getRewardPrice())));
            String prepayId = WxPayV3Util.V3JsapiPay("v3/pay/transactions/jsapi",rewardPay.getPayNum(),new BigDecimal(WXPayUtil.changeToFen(rewardPay.getRewardPrice())),customer.getOpenId(),"订单打赏支付",request);
            object = JSONObject.parseObject(WxPayV3Util.WxTuneUp(prepayId).toString());
            //return object==null?APIEntity.fail("APP调起支付错误"):APIEntity.success(object);
        }
        return object==null?APIEntity.fail("APP调起支付错误"):APIEntity.success(object);
    }




}
