package com.sqx.modules.pay.controller.app;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.annotation.Login;
import com.sqx.modules.pay.dao.PayDetailsDao;
import com.sqx.modules.pay.entity.PayDetails;
import com.sqx.modules.pay.service.PayDetailsService;
import com.sqx.modules.pay.service.WxRiderService;
import com.sqx.modules.pay.service.WxService;
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.*;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.util.List;

/**
 * @author fang
 * @date 2020/2/26
 */
@RestController
@Api(value = "用户微信支付", tags = {"用户微信支付"})
@RequestMapping("/app/wxPay")
@Slf4j
public class ApiWeiXinPayController {

    @Autowired
    private WxService wxService;
   @Autowired
    private WxRiderService wxRiderService;
    @Autowired
    private PayDetailsDao payDetailsDao;
    @Autowired
    private PayDetailsService payDetailsService;

    @Login
    @ApiOperation("微信app充值")
    @PostMapping("/payAppMoney")
    public Result payAppMoney(@RequestAttribute("userId") Long userId, BigDecimal money,Long rechargeId) throws Exception {
        return wxService.payMoney(userId,money,1,rechargeId);
    }
    @Login
    @ApiOperation("微信公众号充值")
    @PostMapping("/wxPayMpMoney")
    public Result wxPayMpMoney(@RequestAttribute("userId") Long userId, BigDecimal money,Long rechargeId) throws Exception {
        return wxService.payMoney(userId,money,2, rechargeId);
    }
    @Login
    @ApiOperation("微信小程序充值")
    @PostMapping("/wxPayJsApiMoney")
    public Result wxPayJsApiMoney(@RequestAttribute("userId") Long userId, BigDecimal money,Long rechargeId) throws Exception {
        return wxService.payMoney(userId,money,3, rechargeId);
    }

    @Login
    @ApiOperation("微信小程序充值记录")
    @GetMapping("/wxPayJsApiMoneyDetails")
    public Result wxPayJsApiMoneyDetails(int page, int limit,int classify, @RequestAttribute("userId") Long userId){
        List<PayDetails> list = payDetailsService.list(new QueryWrapper<PayDetails>().eq("user_id", userId).eq("state", 1).eq("type", 1));
        return Result.success().put("data",payDetailsService.wxPayJsApiMoneyDetails(page,limit,classify,userId));
    }

    @Login
    @ApiOperation("微信支付订单")
    @PostMapping("/payOrders")
    public Result payOrders(@RequestAttribute("userId") Long userId,Long ordersId,Integer type) throws Exception {
        return wxService.payOrders(userId,ordersId,type);
    }

    @Login
    @ApiOperation("微信开通会员")
    @PostMapping("/payMember")
    public Result payMember(Long vipId,@RequestAttribute("userId") Long userId, Integer type) throws Exception {
        return wxRiderService.wxPayMember(vipId, userId, type);
    }


    @PostMapping("/notify")
    @ApiOperation("微信回调")
    public String wxPayNotify(HttpServletRequest request) {
        String resXml = "";
        try {
            InputStream inputStream = request.getInputStream();
            //将InputStream转换成xmlString
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuilder sb = new StringBuilder();
            String line = null;
            try {
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
            } catch (IOException e) {
                log.info(e.getMessage());
            } finally {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            resXml = sb.toString();
            String result = wxService.payBack(resXml,1);
            log.info("成功");
            log.info(result);

            return result;
        } catch (Exception e) {
            log.info("微信手机支付失败:" + e.getMessage());
            String result = "<xml>" + "<return_code><![CDATA[FAIL]]></return_code>" + "<return_msg><![CDATA[报文为空]]></return_msg>" + "</xml> ";
            log.info("失败");
            log.info(result);
            return result;
        }
    }

    @PostMapping("/notifyJsApi")
    @ApiOperation("微信回调")
    public String notifyJsApi(HttpServletRequest request) {
        String resXml = "";
        try {
            InputStream inputStream = request.getInputStream();
            //将InputStream转换成xmlString
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuilder sb = new StringBuilder();
            String line = null;
            try {
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
            } catch (IOException e) {
                log.info(e.getMessage());
            } finally {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            resXml = sb.toString();
            String result = wxService.payBack(resXml,3);
            log.info("成功");
            log.info(result);
            return result;
        } catch (Exception e) {
            log.info("微信手机支付失败:" + e.getMessage());
            String result = "<xml>" + "<return_code><![CDATA[FAIL]]></return_code>" + "<return_msg><![CDATA[报文为空]]></return_msg>" + "</xml> ";
            log.info("失败");
            log.info(result);
            return result;
        }
    }

    @PostMapping("/notifyMp")
    @ApiOperation("微信回调")
    public String notifyMp(HttpServletRequest request) {
        String resXml = "";
        try {
            InputStream inputStream = request.getInputStream();
            //将InputStream转换成xmlString
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuilder sb = new StringBuilder();
            String line = null;
            try {
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
            } catch (IOException e) {
                log.info(e.getMessage());
            } finally {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            resXml = sb.toString();
            String result = wxService.payBack(resXml,2);
            log.info("成功");
            log.info(result);
            return result;
        } catch (Exception e) {
            log.info("微信手机支付失败:" + e.getMessage());
            String result = "<xml>" + "<return_code><![CDATA[FAIL]]></return_code>" + "<return_msg><![CDATA[报文为空]]></return_msg>" + "</xml> ";
            log.info("失败");
            log.info(result);
            return result;
        }
    }


}
