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

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.bohuikeji.delivery.backstage.entity.*;
import com.bohuikeji.delivery.backstage.service.*;
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.utils.StringUtils;
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.HttpSession;
import java.io.IOException;

/**
 * 支付、退款业务
 */
@Slf4j
@Controller
@ResponseBody
@RequestMapping("/transfers")
public class TWxtransfersController extends ControllerBase {

    @Autowired
    ITOrderService itOrderService;
    @Autowired
    ITPayService payService;
    @Autowired
    ITCourierPayService courierPayService;
    @Autowired
    ITRefundService refundService;
    @Autowired
    ITNewsService newsService;
    @Autowired
    ITOrderExceptionService orderExceptionService;
    @Autowired
    ITCourierService courierService;
    @Autowired
    ITCustomerService customerService;
    @Autowired
    ITCustomerWalletService customerWalletService;
    @Autowired
    ITwxtransfersService twxtransfersService;
    @Autowired
    ITCustomerPayService customerPayService;
    @Autowired
    ITGiveDetailService giveDetailService;
    @Autowired
    IWxUserService wxUserService;
    @Autowired
    protected HttpSession session;

    /**
     *钱包退款提现
     * @param
     * @return
     * @throws AlipayApiException
     * @throws IOException
     */
    @InitLog(value = "钱包提现",action = "提现")
    @ApiOperation("钱包提现")
    @GetMapping("/wallet/transfers")
    public APIEntity transfersRefund(Integer walletId,String type) throws Exception {
        String msg=null;
        TCustomerWallet tCustomerWallet = customerWalletService.getById(walletId);
        TCustomer tCustomer = customerService.getById(tCustomerWallet.getCustomerId());
        if(tCustomer==null){
            return APIEntity.fail("未找到提现用户");
        }
        if("提现".equals(tCustomerWallet.getWalletState())){
            return APIEntity.fail("该用户没有提交余额提现申请");
        }
        if(StringUtils.isBlank(tCustomer.getOpenId()) && StringUtils.isBlank(tCustomer.getCustAppOpenId())){
            return APIEntity.fail("提现用户没有绑定微信，请绑定微信或者小程序登陆提现");
        }
        JSONObject jsonObject = twxtransfersService.wXAndAliWithdrawal(type,tCustomerWallet,tCustomer);
        if("SUCCESS".equals(jsonObject.getString("success"))){
            return APIEntity.success("提现成功");
        }
        return APIEntity.fail(jsonObject.getString("fail"));
    }

}
