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


import com.sqx.modules.shopping.service.SelfWXService;
import com.sqx.modules.shopping.utils.Result;
import com.sqx.modules.shopping.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Map;

@Api(value="自营商城微信支付",tags={"自营商城微信支付"})
@RestController
@RequestMapping("/pay")
public class AppSelfWXPayController {
    private final Logger log = LoggerFactory.getLogger(AppSelfWXPayController.class);
    @Autowired
    private SelfWXService service;


    @ApiOperation("app支付")
    @PostMapping("/wxPayApp")
    public Map wxPayApp(Long ordersId,  HttpServletRequest request) throws Exception {
        return service.doUnifiedOrder(ordersId,1, request);
    }


    @ApiOperation("公众号JSAPI支付")
    @PostMapping("/wxPayWeb")
    public Map wxPayWeb(Long ordersId,  HttpServletRequest request) throws Exception {
        return service.doUnifiedOrder(ordersId,2, request);
    }

    @ApiOperation("小程序JSAPI支付")
    @PostMapping("/wxPayJS")
    public Map wxPayJS(Long ordersId,  HttpServletRequest request) throws Exception {
        return service.doUnifiedOrder(ordersId,3, request);
    }

    @ApiOperation("H5支付")
    @PostMapping("/wxPayH5")
    public Map wxPayH5(Long ordersId,  HttpServletRequest request) throws Exception {
        return service.doUnifiedOrder(ordersId,4, request);
    }


    @PostMapping("/notify")
    public String notify(HttpServletRequest request) {
        return notify(request,1);
    }

    @PostMapping("/notifyjs")
    public String notifyjs(HttpServletRequest request) {
        return notify(request,2);
    }

    @PostMapping("/notifys")
    public String notifys(HttpServletRequest request) {
        return notify(request,3);
    }

    public String notify(HttpServletRequest request,Integer type) {
        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 = service.payBack(resXml,type);
            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;
        }
    }


    @ApiOperation("微信退款")
    @PostMapping("/refund")
    public Result refund(Long ordersId){
        return ResultUtil.success(service.refund(ordersId));
    }

}
