package com.icloud.server.scala.engine.cache

import com.icloud.server.scala.engine.service.{GoodspayRepositorye, Goodspaye}
import com.icloud.server.web.err.ServicerunException
import org.joda.time.DateTime
import org.springframework.stereotype.Component

import java.text.SimpleDateFormat
import java.util
import java.util.Date
import javax.annotation.Resource
import scala.jdk.CollectionConverters._
@Component
class GoodsCache extends AbstractCache[Goodspaye]{

  @volatile var goodsPayeList: util.List[Goodspaye] = null
//  @volatile var goodsPayeList: util.List[Goodspaye] = null

  @Resource private[cache] var gpr: GoodspayRepositorye = _
  override def buildCache: util.Map[String, Goodspaye] = {
    val now = DateTime.now
    val day = now.withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0).withMillisOfSecond(0);
    val pays = gpr.getGoodsPaysByHour(now)
    //过滤禁用的支付渠道
    val goodspayes = new util.ArrayList[Goodspaye]()
    pays.asScala.map(x=>{
      var goodspaye = gpr.findByIdAndStatus(x.id, 0)
      if(goodspaye!=null) {
        goodspaye.alipays= goodspaye.alipays.asScala.filter(x=>{
          x.paystatus==0
        }).toSet.asJava
        goodspaye.wxpayes=goodspaye.wxpayes.asScala.filter(x=>{
          x.paystatus==0
        }).toSet.asJava
        goodspayes.add(goodspaye)
      }


    })
    goodsPayeList=goodspayes


    val ret = new util.HashMap[String, Goodspaye]()
    if(!goodsPayeList.isEmpty){
      goodsPayeList.asScala.map(x=>{
        ret.put( x.getId.toString,  x)
      })
    }
    ret
  }

  def getgoodsPayeList :util.List[Goodspaye] = {
    goodsPayeList
  }



  /**
   * @Description:判断当前结束日期否过期
   * @Author: yang
   * @Date: 2022-07-13 23:57
   *
   * @param startstime
   * @param endtime
   *
   **/
  def aftertime(startstime:String,endtime:String): Boolean ={
    var date=new Date();
    val start = dates(startstime)
    val end= dates(endtime)
    var b=false
    //判断过期,只加载有用的日期
    if(date.after(end)||date.before(start)){
     b=true
    }
    b
  }
  def dates(time:String): Date = {
    val format = new SimpleDateFormat( "yyyy/MM/dd" )
    var d = new Date()
    try {
      d=format.parse( time )
    } catch {
      case e =>throw new ServicerunException("日期不合法,无法加载商品支付链接缓存")
    }
    d
  }

}
