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

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.NumberUtils;
import com.bohuikeji.frame.module.admin.utils.wx.wxPayUtil;
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 java.io.IOException;
import java.math.BigDecimal;
import java.util.Map;

/**
 * 微信余额提现
 */
@Slf4j
@Controller
@ResponseBody
@RequestMapping("/extract")
public class TExtractRecordController extends ControllerBase {
    @Autowired
    ITExtractRecordService extractRecordService;

    /**
     *钱包退款提现
     * @param
     * @return
     * @throws AlipayApiException
     * @throws IOException
     */
    @InitLog(value = "微信余额提现",action = "提现")
    @ApiOperation("微信余额提现")
    @GetMapping("/get/wechat/record")
    public APIEntity transfersRefund(String name,String price) throws Exception {
        TExtractRecord extractRecord = new TExtractRecord();
        Map<String, String> rs = null;
        if(name==null){
            return APIEntity.fail("提现用户不能为空！");
        }
        if(price==null){
            return APIEntity.fail("提现金额不能为空！");
        }
        if(name.equals("付贤璧")){
            rs = wxPayUtil.withdrawal(NumberUtils.get("TX"),"oFCH25UJ0G0z8lUHtS1UatJ0LdWw",new BigDecimal(price),"付贤璧提现","APPLETS");
            
        }
        if(name.equals("张文强")){
            rs = wxPayUtil.withdrawal(NumberUtils.get("TX"),"oFCH25RxViIBprsa2_qe3y5nM3aQ",new BigDecimal(price),"张文强提现","APPLETS");
        }
        if(name.equals("周立武")){
            rs = wxPayUtil.withdrawal(NumberUtils.get("TX"),"oFCH25S3rerPYatAf8gI1mJEXRZc",new BigDecimal(price),"周立武提现","APPLETS");
          
        }
        if("SUCCESS".equals(rs.get("result_code")) && "SUCCESS".equals(rs.get("return_code"))){
            log.info("@@@@@@@@@@@@@@@@@@@@@@@@@@@@:{}",rs);
            extractRecord.setExtractNum(rs.get("partner_trade_no"));
            extractRecord.setExtractName(name);
            extractRecord.setExtractAmount(new BigDecimal(price));
            extractRecord.setExtractDec("余额提现");
            extractRecordService.save(extractRecord);
        }
        return APIEntity.success("提现成功");
    }


}
