package com.icloud.server.scala.h5

import com.fasterxml.jackson.annotation.JsonProperty
import com.icloud.server.scala.engine.service.{GoodsPayService, GoodspayRepositorye}
import com.icloud.server.scala.goods.{GoodsRepository, GoodspayRepository}
import com.icloud.server.scala.payorder.OrderServer
import com.icloud.server.web.json.JsonResult
import io.swagger.annotations._
import org.springframework.web.bind.annotation._

import java.util.Date
import javax.annotation.Resource
import scala.annotation.meta.field
import scala.beans.BeanProperty

/**用户支付算法 --重点部份---1.0版本
 *
 * @Author : yang
 * @Date : 2022-07-07 6:51 PM
 * @Version : 2.1.2
 */
@Api( tags = Array( "获取H5页面参数" ), value = "API",hidden = true )
@RestController
@RequestMapping(Array("/unitePay/"))
@CrossOrigin
class H5PayPageCtroller {


  @Resource
  @BeanProperty
  var os: OrderServer = _
  //通过商品获取支付链接
  //获取支付链接
  @Resource
  @BeanProperty
  var gpr: GoodspayRepository = _
  @Resource
  @BeanProperty
  var gper: GoodspayRepositorye = _

  @Resource
  @BeanProperty
  var gr: GoodsRepository = _
  @Resource
  @BeanProperty
  var gps: GoodsPayService = _


  @ApiOperation(value = "获取支付页面的参数", notes = "")
  @ApiImplicitParams(Array(
    new ApiImplicitParam(name = "payreq", value = "支付页面的参数", required = true, dataType = "GetPayListReq")
  ))
  @PostMapping(Array("getPayList"))
  @ResponseBody
  def getPayList(@RequestBody payreq: GetPayListReq): JsonResult = {
    println("----获取H5页面信息----")

    val gp = gpr.findByIdAndStatus(payreq.goodsid, 0)
    val gid = gp.getGoodsid
    val goods = gr.findByGoodsidAndAndStatus(gid, 0)
    var gpp = gps.getGoodsPayById(payreq.goodsid.toString)
    if (gpp == null) {
      println("---缓存中没有这个信息,使用兜底方案------")
      gpp = gper.findByIdAndStatus(payreq.goodsid, 0)
    }
    val appId = gpp.payrealmname.appid
    val url = gpp.payrealmname.url
    val res = GetPayListRes(goods.subject, goods.shortname, goods.picurl, goods.body, gp.payment.toString, goods.markingprice.toString, gp.fullphone, gp.expireendtime, appId, url)
    new JsonResult(res)

  }
}
//  @ApiOperation( value = "获取支付宝支付链接", notes = "" )
//  @ApiImplicitParams( Array(
//    new ApiImplicitParam( name = "req", value = "支付页面的参数", required = true, dataType = "GetPayListReq" )
//  ) )
//  @PostMapping( Array( "getalipay" ) )
//  @ResponseBody
//  def getalipay( @RequestBody req:GetPayListReq,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 url = request.getHeader("Referer")
//    val o = os.getAliPayLink(req.goodsid, ip,url)
//    println(o)
//
//    new JsonResult(o)
//  }
//  @ApiOperation( value = "获取微信支付链接", notes = "" )
//  @ApiImplicitParams( Array(
//    new ApiImplicitParam( name = "req", value = "支付页面的参数", required = true, dataType = "GetPayListReq" )
//  ) )
//  @PostMapping( Array( "getwxpay" ) )
//  @ResponseBody
//  def getwxpay(@RequestBody req:GetPayListReq,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 o = os.getWxPayLink(req.goodsid, ip)
//    println(o)
//    new JsonResult(o)
//
//  }



//  @ApiOperation( value = "获取支付链接---通用版本1.0.1版本", notes = "" )
//  @ApiImplicitParams( Array(
//    new ApiImplicitParam( name = "req", value = "支付页面的参数", required = true, dataType = "GetPayListReq" )
//  ) )
//  @PostMapping( Array( "getpayLink" ) )
//  @ResponseBody
//  def getpayLink( @RequestBody req:GetPayListReq,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 url = request.getHeader("Referer")
//    val o = os.getAliPayLink(req.goodsid, ip,url)
//    println(o)
//
//    new JsonResult(o)
//  }
//
//}
@ApiModel(description = "获取支付页面的参数")
case class GetPayListReq( @(ApiModelProperty@field)(position = 1, example = "1232", value = "studentid") @JsonProperty("studentid")  studentid: String,
                          @(ApiModelProperty@field)(position = 1, example = "1232", value = "ditch") @JsonProperty("ditch")  ditch: String,
                          @(ApiModelProperty@field)(position = 1, example = "1232", value = "goodsid") @JsonProperty("goodsid")  goodsid: Long,
                          @(ApiModelProperty@field)(position = 1, example = "1232", value = "phone") @JsonProperty("phone")  phone: String)


case class GetPayListRes(  @BeanProperty subject:String,
                           @BeanProperty short_name:String,
                           @BeanProperty pic_url:String,
                          @BeanProperty body:String,
                          @BeanProperty price:String ,
                          @BeanProperty  marking_price:String,
                          @BeanProperty getphone:String,
                           @BeanProperty  endtime:Date,
                           @BeanProperty  appId:String,
                           @BeanProperty  url:String
                        )


//价格 pic 倒计时,划线价格

case class Alipay( @(ApiModelProperty@field)(position = 1, example = "1232", value = "Url") @JsonProperty("Url")  Url: String,
                   @(ApiModelProperty@field)(position = 1, example = "1232", value = "name") @JsonProperty("name")  name: String)
case class Wxpay( @(ApiModelProperty@field)(position = 1, example = "1232", value = "url") @JsonProperty("url")  url: String,
                  @(ApiModelProperty@field)(position = 1, example = "1232", value = "name") @JsonProperty("name")  name: String)


