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

import com.sqx.common.utils.Result;
import com.sqx.modules.app.annotation.Login;
import com.sqx.modules.app.dao.UserDao;
import com.sqx.modules.app.entity.UserEntity;
import com.sqx.modules.pay.service.WxErrRiderService;
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;

/**
 * @author fang
 * @date 2020/2/26
 */
@RestController
@Api(value = "跑腿师傅微信支付", tags = {"跑腿师傅微信支付"})
@RequestMapping("/app/wxPayErrRider")
@Slf4j
public class ApiWeiXinErrRiderPayController {

    @Autowired
    private WxErrRiderService wxService;
    @Autowired
    private UserDao userDao;


    @Login
    @ApiOperation("微信app充值")
    @PostMapping("/payAppMoney")
    public Result payAppMoney(@RequestAttribute("userId") Long userId, BigDecimal money) throws Exception {
        Integer userType = 2;
        return wxService.payMoney(userId,money,1, userType);
    }

    @Login
    @ApiOperation("微信app支付订单")
    @PostMapping("/payAppOrder")
    public Result payAppOrder(@RequestAttribute("userId") Long userId, String indentNumber) throws Exception {
        Integer userType = 2;
        return wxService.payOrder(userId,indentNumber,1, userType);
    }


    @Login
    @ApiOperation("微信小程序充值")
    @PostMapping("/wxPayJsApiMoney")
    public Result wxPayJsApiMoney(@RequestAttribute("userId") Long userId, BigDecimal money) throws Exception {
        Integer userType = 2;
        return wxService.payMoney(userId,money,3, userType);
    }

    @Login
    @ApiOperation("微信小程序支付订单")
    @PostMapping("/wxPayJsApiOrder")
    public Result wxPayJsApiOrder(@RequestAttribute("userId") Long userId, String indentNumber) throws Exception {
        Integer userType = 2;
        return wxService.payOrder(userId,indentNumber,3, userType);
    }

    @Login
    @ApiOperation("小程序跑腿师傅实名认证")
    @PostMapping("/wxPayJsApiRiderCertification/{type}")
    public Result wxPayJsApiRiderCertification(@RequestAttribute("userId") Long userId, @PathVariable("type") Integer type, @RequestBody UserEntity userEntity) throws Exception{
        Integer userType = 2;
        return wxService.wxPayJsApiRiderCertification(userId,type,userEntity, userType);
    }

    /*@Login
    @ApiOperation("公众号跑腿师傅实名认证")
    @PostMapping("/wxPayMpRiderCertification")
    public Result wxPayMpRiderCertification(@RequestAttribute("userId") Long userId, @RequestBody UserEntity userEntity) throws Exception{
        Integer userType = 2;
        return wxService.wxPayJsApiRiderCertification(userId,2,userEntity, userType);
    }

    @Login
    @ApiOperation("app跑腿师傅实名认证")
    @PostMapping("/appRiderCertification")
    public Result appRiderCertification(@RequestAttribute("userId") Long userId, @RequestBody UserEntity userEntity) throws Exception{
        Integer userType = 2;
        return wxService.wxPayJsApiRiderCertification(userId,1,userEntity, userType);
    }*/

    @Login
    @ApiOperation("判断用户是否已实名认证成功")
    @GetMapping(value = "selectCertificationFlag")
    public Result selectCertificationFlag(@RequestAttribute Long userId){
        UserEntity userEntity = userDao.selectById(userId);
        if(!"1".equals(userEntity.getCheckCertification())){
            return Result.error("实名认证审核成功后才能交保证金");
        }
        return Result.success();
    }

    @Login
    @ApiOperation("骑手交保证金（可以自己填写缴纳金额）")
    @PostMapping(value = "wxPayCashDeposit")
    public Result wxPayCashDeposit(@RequestAttribute Long userId, Double money, Integer type) throws Exception {
        Integer userType = 2;
        return wxService.wxPayCashDeposit(userId, money, type, userType);
    }


    @Login
    @ApiOperation("微信公众号充值")
    @PostMapping("/wxPayMpMoney")
    public Result wxPayMpMoney(@RequestAttribute("userId") Long userId, BigDecimal money) throws Exception {
        Integer userType = 2;
        return wxService.payMoney(userId,money,2, userType);
    }

    @Login
    @ApiOperation("微信公众号支付订单")
    @PostMapping("/wxPayMpOrder")
    public Result wxPayMpOrder(@RequestAttribute("userId") Long userId, String indentNumber) throws Exception {
        Integer userType = 2;
        return wxService.payOrder(userId,indentNumber,2, userType);
    }


    @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;
        }
    }

}
