package com.icloud.server.scala.payorder

import cn.hutool.core.util.RandomUtil
import com.icloud.server.payutils.enums.PayTypeEnum
import com.icloud.server.payutils.pojo.CommonResult
import com.icloud.server.payutils.{PayEntiy, PayService}
import com.icloud.server.scala.goods.{GoodsRepository, GoodspayRepository}
import com.icloud.server.scala.pay._
import org.springframework.stereotype.Component
import org.springframework.web.bind.annotation.PostMapping

import java.util.UUID
import javax.annotation.Resource
import javax.servlet.http.HttpServletRequest
import scala.beans.BeanProperty
import scala.jdk.CollectionConverters._

/** 订单服务---1.0版本
 *
 * @Author : yang
 * @Date : 2022-07-11 14:09
 * @Version : 2.1.2
 */

@Component
class OrderServer {
  @Resource
  @BeanProperty
  var gr: GoodsRepository = _
  @Resource
  @BeanProperty
  var gpr: GoodspayRepository = _
  @Resource
  @BeanProperty
  var ps: PayService = _
  @Resource
  @BeanProperty
  var por: PayOrderRepository = _
  @Resource
  @BeanProperty
  var alipay: Alipay1Repository = _
  @Resource
  @BeanProperty
  var wxpay: Wxpay1Repository = _
  @Resource
  @BeanProperty
  var prnr: PayRealmNameRepository = _


  //redis缓存支付链接 -----todo 1.0.2 版本做 支付缓存

  /**
   * @Description:
   * @Author: yang 
   * @Date: 2022-07-12 18:33
   *
   *
   * */
  def getAliPayLink(orderid: Long, IP: String, url: String): CommonResult[_] = {
    //获取商品信息基础信息
    //校验url是否是这个域名


    val goodspay = gpr.findByIdAndStatus(orderid, 0)
    val gid = goodspay.getGoodsid
    val goods = gr.findByGoodsidAndAndStatus(gid, 0)
    val uid = goods.getUserid
    val app = getAliAppId(uid, 1L, false)
    val realmnames = prnr.findByUseridAndStatus(uid, 0)
    val realmname = if (!realmnames.isEmpty) realmnames.asScala.toList(0).url else null
    //判断包含域名
    //    if()
    val channelId = RandomUtil.randomLong
    val str = UUID.randomUUID.toString.replace("-", "")
    val OrderId = String.valueOf(System.currentTimeMillis)

    val pe = new PayEntiy()
    pe.setAmount(goodspay.payment)
    pe.setSubject(goods.subject)
    pe.setBody(goods.shortname)
    pe.setMerchantOrderId(OrderId)
    pe.setUserIp(IP)
//    pe.setUserIp("127.0.0.1")
    pe.setNotifyUrl(realmname)

    pe.setPayType(PayTypeEnum.ALIPAY.getStatus)
    pe.setAppid(app.getAppid)
    pe.setPrivateKey(app.privatekey)
    pe.setAlipayPublicKey(app.alipaypublickey)
    pe.setExpireTime(goodspay.getExpireendtime)
    pe.setChannelId(channelId)


    ps.getPayUrl(pe)


  }

  def if_contain_url(str1: String, str2: String): Boolean = {


    false
  }

  /** 取支付链接是个艺术活
   *
   * @Author : yang
   * @Date : 2022-07-13 13:49
   * @Version : 2.1.2
   */

  def getAliAppId(uid: Long, appoint_pay: Long, random: Boolean): Alipay1 = {
    val alipays = alipay.findByUseridAndStatus(uid.toString, 0)
    //排序
    //    alipays.asScala.sortBy()
    //权重
    //在redis中取数据

    // 1.0.1---取第一个
    alipays.asScala.toList(0)
  }


  /**
   * @Description:
   * @Author: yang
   * @Date: 2022-07-12 18:33
   *
   *
   * */
  def getWxPayLink(orderid: Long, IP: String): CommonResult[_] = {
    //获取商品信息基础信息
    val goodspay = gpr.findByIdAndStatus(orderid, 0)
    val gid = goodspay.getGoodsid
    val goods = gr.findByGoodsidAndAndStatus(gid, 0)
    val uid = goods.getUserid
    val app = getWxAppId(uid, 1L, false)
    val realmnames = prnr.findByUseridAndStatus(uid, 0)
    var realmname = if (!realmnames.isEmpty) realmnames.asScala.toList(0).url else null
    val channelId = RandomUtil.randomLong
    val str = UUID.randomUUID.toString.replace("-", "")
    val OrderId = String.valueOf(System.currentTimeMillis)
    //String.valueOf(System.currentTimeMillis)
    val pe = new PayEntiy()
    pe.setBody(goods.subject)
    pe.setExpireTime(goodspay.getExpireendtime)
    pe.setPayType(PayTypeEnum.WXPAY.getStatus)
    pe.setAppid(app.getAppid)
    pe.setMchId(app.getMchid)
    pe.setMchKey(app.getMchkey)
    pe.setUserIp(IP)
    pe.setNotifyUrl(realmname)
    pe.setAmount(goodspay.payment)
    pe.setMerchantOrderId(OrderId)
    pe.setChannelId(channelId)
    ps.getPayUrl(pe)

  }





  /** 取支付链接是个艺术活
   *
   * @Author : yang
   * @Date : 2022-07-13 13:49
   * @Version : 2.1.2
   */

  def getWxAppId(uid: Long, appoint_pay: Long, random: Boolean): Wxpay1 = {
    val wxpays = wxpay.findByUseridAndStatus(uid.toString, 0)
    //排序
    //    alipays.asScala.sortBy()
    //权重
    //在redis中取数据

    // 1.0.1---取第一个
    wxpays.asScala.toList(0)
  }


  /** 2.0版本
   *
   * @Author yang
   * @Description
   * @Date 3:35 下午 2021/9/16
   * @Param [pparam, request]
   * @return com.forcetv.ad.utils.JsonResults
   */
  @PostMapping(Array("getorderinfo")) def getorderinfo2(request: HttpServletRequest): Unit = {


  }

  /**
   * @Description:
   * @Author: yang
   * @Date: 2022-07-12 19:10
   *
   *
   * */
  def getWxpayLink(): Unit = {

  }


  def check_realm(): Unit = {

  }


}
