package com.icloud.server.scala.engine.web

import com.icloud.server.payutils.enums.PayTypeEnum
import com.icloud.server.redis.AmountRedis
import com.icloud.server.scala.engine.contect.{Context, PayRequest}
import com.icloud.server.scala.engine.service.{GoodsPayService, OnloadPayLink, PayOrderServer}
import com.icloud.server.web.json.JsonResult
import io.swagger.annotations.{Api, ApiImplicitParam, ApiImplicitParams, ApiOperation}
import org.springframework.web.bind.annotation._

import javax.annotation.Resource
import javax.servlet.http.HttpServletRequest
import scala.beans.BeanProperty
import scala.util.Random
import java.util.regex.{Matcher, Pattern}



/**1.0.2支付链接 ---加风控
 *
 * @Author : yang
 * @Date : 2022-07-15 01:45
 * @Version : 2.1.2
 */
@Api( tags = Array( "H5统一支付--1.0.2" ), value = "API",hidden = true )
@RestController
@RequestMapping(Array("/unitePay/"))
@CrossOrigin
class PayControllerPlus {
  @Resource
  @BeanProperty var goodspayservice: GoodsPayService = _
  @Resource
  @BeanProperty var onloadpaylink: OnloadPayLink = _
  @Resource
  @BeanProperty var amountredis: AmountRedis = _
  @Resource
  @BeanProperty
  var payorderserver: PayOrderServer = _
  @PostMapping( Array( "loding" ) )
  def Loding(): Unit ={
    goodspayservice.lading()
  }

  @ApiOperation( value = "获取支付链接---通用版本1.0.2版本--风控版本", notes = "" )
  @ApiImplicitParams( Array(
    new ApiImplicitParam( name = "req", value = "支付页面的参数", required = true, dataType = "PayRequest" )
  ) )
  @PostMapping( Array( "getLink" ) )
  @ResponseBody
  def getpayLink( @RequestBody req:PayRequest,request: HttpServletRequest): JsonResult ={
    var ip = if (request.getRemoteAddr != null) request.getRemoteAddr else if (request.getHeader( "x-forwarded-for" ) != null) request.getHeader( "x-forwarded-for" )
    else if (request.getHeader( "Proxy-Client-IP" ) != null) request.getHeader( "Proxy-Client-IP" ) else if (request.getHeader( "WL-Proxy-Client-IP" ) != null) request.getHeader( "WL-Proxy-Client-IP" )
    else if (request.getHeader( "HTTP_CLIENT_IP" ) != null) request.getHeader( "HTTP_CLIENT_IP" ) else if (request.getHeader( "HTTP_X_FORWARDED_FOR" ) != null) request.getHeader( "HTTP_X_FORWARDED_FOR" )
    else if (request.getRemoteAddr() != null) request.getRemoteAddr() else "0.0.0.0"
    val new_req = req.toRequest(ip)
    val cache = goodspayservice.getGoodsPayById(new_req.goodsid.toString)
    if(cache==null) return null
    val url2 = cache.payrealmname.url
    val wxappid2 = cache.payrealmname.appid
//    val url2 = ""
    val url = request.getHeader("Referer")
    println(s"请求url = ${url}")
    println("数据库域名:"+url2)
//    val bool1 = patterns(url, url2)
//    println("域名校验结果:"+bool1)
    val bool2 = patterns( url2,url)
    println("域名校验结果:"+bool2)
    //域名过滤
    if(!patterns(url2,url)){
      println("====支付域名不对=====")
      return null
    }
    val context = new Context( goodspayservice, new_req,null)
    onloadpaylink.process(context)
    if(wxappid2!=null) context.setWxappid2(wxappid2)
    var result =
    context. request.paytypeenum match {
      case PayTypeEnum.WXPAY => GetWxReq(context)
      case PayTypeEnum.ALIPAY =>GetAliReq(context)
      case PayTypeEnum.WXJSPAY =>GetWxJsReq(context)
      case _ =>  null
    }

      new JsonResult(result)
//    new JsonResult("ok")
  }
  def GetAliReq(context: Context): Object ={
    if(context.payurl!=null){
      if(context.payurl.aliurl!=null)  return context. payurl.aliurl
    }
    payorderserver.getAliPayLink(context)

  }
  def GetWxReq(context: Context): Object ={  //
    if(context.payurl!=null){
      if( context.payurl.wxurl!=null)return context.payurl.wxurl
    }
    payorderserver.getWxPayLink(context)

  }
  def GetWxJsReq(context: Context): Object ={  //
    if(context.payurl!=null){
      if( context.payurl.wxurl!=null)return context.payurl.wxurl
    }
    payorderserver.getWxJSPayLink(context)

  }
  def patterns(param1:String,param2:String): Boolean ={
    var boolean=false
    try {
      val pattern = Pattern.compile(param1)
      val matcher: Matcher = pattern.matcher(param2)
      boolean = matcher.find
    } catch {
      case e =>boolean=false
    }
    boolean
  }
//
//  @GetMapping(Array("mini"))
//  @ResponseBody
//  def getminitets(): JsonResult ={
//    val unit = amountredis.WxgetminAmount("1", "2")
//    new JsonResult(unit)
//  }
//  @GetMapping(Array("all"))
//  @ResponseBody
//  def getminitets1(): JsonResult ={
//    new JsonResult(amountredis.WxgetAllPay("1","2"))
//  }
//  @GetMapping(Array("push"))
//  def pustest(payid:String,L:Long): Unit ={
////    val l = new Random(100).
////    println(l)
//    amountredis.WxpushAmount("1","2",payid, L)
//  }
//  @GetMapping(Array("push"))
//  def pustest(payid:String,L:Long): Unit ={
//    //    val l = new Random(100).
//    //    println(l)
//    amountredis.WxpushAmount("1","2",payid, L)
//  }
//  @GetMapping(Array("push"))
//  def pustest(payid:String,L:Long): Unit ={
//    //    val l = new Random(100).
//    //    println(l)
//    amountredis.WxpushAmount("1","2",payid, L)
//  }


}
