package com.icloud.server.scala.engine.service

import com.icloud.server.redis.AmountRedis
import com.icloud.server.scala.engine.contect.Context
import org.springframework.data.redis.core.StringRedisTemplate
import org.springframework.stereotype.Service

import java.util.Random
import javax.annotation.Resource
import scala.beans.BeanProperty
import scala.collection.convert.ImplicitConversions.`seq AsJavaList`
import scala.jdk.CollectionConverters._

/** 通过权重选择支付key appid
 * 新装一个实体 new_Goodspaye
 * 参数:alipay_id wxpay_id weight opt(指定支付)
 *
 * 这个 里面回传  支付的wx_payid,  alipay_id
 *
 * 这里是 指定支付和随机支付的处理结果
 *
 * @Author : yang
 * @Date : 2022-07-15 15:43
 * @Version : 2.1.2
 */
@Service
class OptisizingSelectionService {
  @Resource
  @BeanProperty
  var stringredistemplate: StringRedisTemplate = _
  @Resource
  @BeanProperty
  var amountredis: AmountRedis = _


  def optisize(goodspaye: Goodspaye, context: Context): Goodspaye = {

    if (goodspaye == null) {
      null
    } else {
      selectByWeight(selectByPriority(goodspaye),context)
    }
  }

  //获取redis中现在的支付额度--选择支付链接
  def getredis(today: String): Unit = {
    //    stringredistemplate.opsForSet(today)
  }

  //查询封号
  def if_forceout(goodspaye: Goodspaye): Goodspaye = {
    if (!goodspaye.alipays.isEmpty) {
      goodspaye.alipays =
        goodspaye.alipays.asScala.filter(x => {
          !amountredis.if_AliForceout(x.appid)
        }).asJava
    }
    if (!goodspaye.wxpayes.isEmpty) {
      goodspaye.wxpayes =
        goodspaye.wxpayes.asScala.filter(x => {
          !amountredis.if_WxForceout(x.appid)
        }).asJava
    }
    goodspaye
  }

  //获取支付链接
  def getpay_link(studentid: String, goodsid: String, aliappid: String, wxappid: String): (String, String) =
    (amountredis.getwxpaylink(studentid, goodsid, wxappid),
      amountredis.getwxpaylink(studentid, goodsid, wxappid))

  //
  //不随机,指定支付id 判断是否是指定的
  def selectByPriority(goodspaye: Goodspaye): Goodspaye = {
    goodspaye.goodspayditches.asScala.map(x => {
      if (x.random == 0) {
        if (x.optpay == 1) { //wx
          goodspaye.alipays =
            goodspaye.alipays.asScala.filter(y => {
              y.appid != x.appointpay
            }).asJava
        } else if (x.optpay == 2) {
          goodspaye.wxpayes = goodspaye.wxpayes.asScala.filter(y => {
            y.appid != x.appointpay
          }).asJava
        }
      }
    })
    goodspaye
  }

  //通过 权重, 随机 返回 alipayid wxpayid
  def selectByWeight(goodspaye: Goodspaye, context: Context): Goodspaye = {
    if (goodspaye == null) {
      return null
    }

      var wx = new Wxpaye
      if (!goodspaye.wxpayes.isEmpty) {

        if (goodspaye.wxpayes.size() == 1) {
          wx = goodspaye.wxpayes.asScala.toList(0)
        } else {
          val wxpays = goodspaye.wxpayes.asScala.toList

          var totalWeight: Int = 0
          for (wx <- wxpays) {
            totalWeight += wx.getWeight
          }
          val random: Int = new Random().nextInt(totalWeight)
          var start: Int = 0
          var end: Int = 0
          var ni: Int = 1
          while (ni < wxpays.size) {
            if (ni == 1) {
              start = wxpays(ni - 1).getWeight
              end = wxpays(ni - 1).getWeight + wxpays(ni).getWeight
            } else {
              start = end
              end = start + wxpays.get(ni).getWeight
            }
            if (random >= start && random < end) {
              wx= wxpays.get(ni)
              ni= wxpays.size+1
            }
            else if (random < start) {
              wx= wxpays.get(ni - 1)
              ni= wxpays.size+1
            }
            ni+=1
          }
        }
      }




      var ali = new Alipaye
      if (!goodspaye.alipays.isEmpty) {
        if (goodspaye.alipays.size() == 1) {
          ali = goodspaye.alipays.asScala.toList(0)
        } else {
          val alipays = goodspaye.alipays.asScala.toList
          var totalWeight: Int = 0
          for (wx <- alipays) {
            totalWeight += wx.getWeight
          }
          val random: Int = new Random().nextInt(totalWeight)
          var start: Int = 0
          var end: Int = 0
          var ni: Int = 1
          while (ni < alipays.size) {
            if (ni == 1) {
              start = alipays(ni - 1).getWeight
              end = alipays(ni - 1).getWeight + alipays(ni).getWeight
            }
            else {
              start = end
              end = start + alipays.get(ni).getWeight
            }
            if (random >= start && random < end) {
              ali = alipays.get(ni)
              ni= alipays.size+1

            }
            else if (random < start) {
              ali = alipays.get(ni - 1)
              ni= alipays.size+1

            }
            ni+=1
          }
        }
      }


    context.aliappid = ali.appid
    context.privatekey = ali.privatekey
    context.alipaypublickey = ali.alipaypublickey
    context.wxappid = wx.appid
    context.mchkey = wx.mchkey
    context.mchid = wx.mchid
    context.goodspaye=goodspaye
    context.alipayid=ali.id
    context.wxpayid=wx.id
    context.uid=wx.userid
    if( context.request.phone!=null)
      context.phone= context.request.phone
    println("最后计算_aliappid:"+context.aliappid)
    println("最后计算_wxappid:"+context.wxappid)
    println("最后计算_goodspaye:"+context.goodspaye)

    goodspaye
  }
}
