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

import com.fasterxml.jackson.annotation.JsonIdentityInfo
import com.icloud.server.payutils.goods.{GoodsPayDao, GoodsRepositorye}
import com.icloud.server.scala.engine.contect.PayUrl
import com.icloud.server.scala.pay.{Alipay, Tenant, Userpay, Wxpay}
import com.icloud.server.scala.shiro.{Role, UserRole}
import io.swagger.annotations.Api
import lombok.{AllArgsConstructor, Data, NoArgsConstructor}
import org.hibernate.annotations.{CreationTimestamp, UpdateTimestamp}
import org.springframework.data.jpa.repository.{JpaRepository, JpaSpecificationExecutor}
import org.springframework.data.repository.{CrudRepository, PagingAndSortingRepository}
import org.springframework.stereotype.Repository
import org.hibernate.annotations.Fetch
import org.hibernate.annotations.FetchMode
import org.hibernate.annotations.Proxy
import org.joda.time.DateTime
import org.springframework.web.bind.annotation.{CrossOrigin, PostMapping, RequestMapping, ResponseBody, RestController}

import java.util
import javax.persistence._
import java.util.{Date, Set}
import javax.annotation.Resource
import javax.persistence.{Column, Entity, Id, Table}
import scala.beans.BeanProperty
import scala.jdk.CollectionConverters._
import org.hibernate.annotations.BatchSize


/** 支付引擎专用映射类
 * @Author : yang
 * @Date : 2022-07-13 23:22
 * @Version : 2.1.2
 */

@Api(tags = Array("商品映射"), value = "API", hidden = true)
@RestController
@RequestMapping(Array("/t/"))
@CrossOrigin
class GoodsPayController1 {
  //  @Resource
  //  @BeanProperty
  //  var gpr: GoodsRepositorye = _

  @Resource
  @BeanProperty
  var gpr: GoodspayRepositorye = _
  @PostMapping(Array("t"))
  @ResponseBody
  def getgoods(id:Long): Unit ={

    //    var goodspayes = gpr.getGoodsPaysByHour(DateTime.now.withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0).withMillisOfSecond(0))
    var goodspayes =  gpr.findByIdAndStatus(id,0)
    //    goodspayes.asScala.map(x=>{
    //      println(x)
    //      println(x)
    //    })

    println(goodspayes)
  }
}

@Repository trait GoodspayRepositorye extends CrudRepository[Goodspaye, Long]
  with JpaSpecificationExecutor[Goodspaye] with JpaRepository[Goodspaye, Long]
  with PagingAndSortingRepository[Goodspaye, Long] with GoodsPayDao{
  def findByIdAndStatus(id:Long,status:Int):Goodspaye

  //  def findBySubjectLikeAndStatusAndUseridOrderById(subject: String,status:Int,userid: Long) :util.List[Goodspaye]


}

@Proxy (`lazy` = false)
@Data
@Entity
@Table(name = "goods_pay")
@NoArgsConstructor
@AllArgsConstructor
@BatchSize (size = 2000)
//@JsonIdentityInfo (generator = classOf[Nothing] )
class Goodspaye extends Serializable {
  //  @GeneratedValue( strategy = GenerationType.IDENTITY )
  @Id
  @BeanProperty
  @Column(name = "id")
  var id: Long = _
  @transient
  @BeanProperty
  @Column
  var username:String=_
  @transient
  @BeanProperty
  @Column
  var paylinkurl:PayUrl=_
  @BeanProperty
  @Column
  var subject:String=_
  @BeanProperty
  @Column(name = "short_name")
  var shortname: String = _
    @BeanProperty
    @Column( name = "user_id" ,insertable=false ,updatable=false  )
    var userid: String = _
  //  @BeanProperty
  //  @Column(name = "goods_id")
  //  var goodsid: Long = _
  @BeanProperty
  @Column(name = "payment_model")
  var paymentmodel: Int = _
  @BeanProperty
  @Column(name = "payment")
  var payment: Long = _
  @BeanProperty
  @Column(name = "full_phone")
  var fullphone: String = _
  @BeanProperty
  @Column(name = "expire_model")
  var expiremodel: String = _
  @BeanProperty
  @Column(name = "expire_endtime")
  var expireendtime: Date = _
  @BeanProperty
  @Column
  var starttime: Date = _
  @BeanProperty
  @Column(name = "expire_millisecond")
  var expiremillisecond: Long = _
  @BeanProperty
  @Column
  var remark: String = _
  @BeanProperty
  @Column var status: Int = _
  @CreationTimestamp
  @BeanProperty
  @Column var CREATETIME: Date = _
  @UpdateTimestamp
  @BeanProperty
  @Column var UPDATETIME: Date = _

  //多对一
  @ManyToOne(fetch = FetchType.EAGER)
  @Fetch(FetchMode.JOIN)
  @JoinColumn(name = "goods_id")
  var goodse: Goodse = _

  //  @ManyToOne(fetch = FetchType.EAGER)
  //  @Fetch(FetchMode.JOIN)
  //  @JoinColumn(name = "user_id",insertable = false, updatable = false)
  //  var payrealmname: PayRealmName1 = _

  //  @BeanProperty
  //  @JoinColumn( name = "ditch_id" )
  //  @OneToOne( fetch = FetchType.EAGER, cascade = Array( CascadeType.ALL ) ) var goodspayditche: Goodspayditche = _


  //1-1
  //  @BeanProperty
  //  @JoinColumn( name = "ditch_id" )
  //  @OneToOne( fetch = FetchType.EAGER, cascade = Array( CascadeType.ALL ) ) var goodspayditche: Goodspayditche = _








  @JoinColumn( name = "user_id" ,insertable=false ,updatable=false ,referencedColumnName="user_id")
//  @JoinColumn( name = "user_id" )
  @OneToOne( fetch = FetchType.EAGER, cascade = Array( CascadeType.ALL ) )
  var payrealmname: PayRealmName1 = _

  @BeanProperty //  @JsonIgnore
  @OneToMany( cascade = Array( CascadeType.ALL ), fetch = FetchType.EAGER ) //targetEntity=classOf[UserRole] ,,fetch = FetchType.EAGER targetEntity=classOf[UserRole] ,
  @JoinColumn(name = "user_id",insertable = false, updatable = false,referencedColumnName="user_id")
  var alipays: Set[Alipaye] = _

  @BeanProperty //  @JsonIgnore
  @OneToMany( mappedBy = "goodspaye", cascade = Array( CascadeType.ALL ), fetch = FetchType.EAGER ) //targetEntity=classOf[UserRole] ,,fetch = FetchType.EAGER targetEntity=classOf[UserRole] ,
  var goodspayditches: Set[Goodspayditche] = _

  @BeanProperty //  @JsonIgnore
  @OneToMany( cascade = Array(CascadeType.ALL), fetch = FetchType.EAGER) //targetEntity=classOf[UserRole] ,,fetch = FetchType.EAGER targetEntity=classOf[UserRole] ,
  @JoinColumn(name = "user_id",insertable = false, updatable = false,referencedColumnName="user_id")
  var wxpayes: Set[Wxpaye] = _

  //  @BeanProperty
  //  @JoinColumn( name = "tenant_id" )
  //  @OneToOne( fetch = FetchType.EAGER, cascade = Array( CascadeType.ALL ) ) var tenant: Tenant = _





}



@Data
@Entity
@Table(name = "goods_pay_ditch")
@NoArgsConstructor
@AllArgsConstructor
class Goodspayditche extends Serializable {
  @Id
  @BeanProperty
  @Column( name = "ditch_id" )
  var id: Long = _

  @BeanProperty
  @Column(name = "goods_id")
  var goodsid: Long = _

  //  @BeanProperty
  //  @Column(name = "goods_pay_id")
  //  var goodspayid: Long = _
  @BeanProperty
  @Column(name = "pay_name")
  var payname: String = _
  @BeanProperty
  @Column(name = "opt_pay")
  var optpay: Long = _
  @BeanProperty
  @Column(name = "random")
  var random: String = _
  @BeanProperty
  @Column(name = "appoint_pay")
  var appointpay: Long = _

  @BeanProperty
  @Column var status: Int = _
  @CreationTimestamp
  @BeanProperty
  @Column var CREATETIME: Date = _
  @UpdateTimestamp
  @BeanProperty
  @Column var UPDATETIME: Date = _

  @ManyToOne (fetch=FetchType.LAZY)//(fetch=FetchType.EAGER)//fetch=FetchType.LAZY
  @JoinColumn( name = "goods_pay_id" ) var goodspaye: Goodspaye = _


}



@Data
@Entity
@Table(name = "goods")
@NoArgsConstructor
@AllArgsConstructor
class Goodse extends Serializable {
  @Id
  @BeanProperty
  @Column(name = "goods_id")
  var goodsid: Long = _
  @BeanProperty
  @Column(name = "tenant_id")
  var tenantid: Long = _
  @BeanProperty
  @Column(name = "user_id")
  var userid: Long = _
  @BeanProperty
  @Column var subject: String = _
  @BeanProperty
  @Column var free: String = _
  @BeanProperty
  @Column(name = "short_name")
  var shortname: String = _
  @BeanProperty
  @Column(name = "pic_url")
  var picurl: String = _
  @BeanProperty
  @Column
  var body: String = _
  @BeanProperty
  @Column
  var price: Long = _
  @BeanProperty
  @Column(name = "marking_price")
  var markingprice: Long = _
  @BeanProperty
  @Column(name = "selling_model")
  var sellingmodel: String = _
  @BeanProperty
  @Column var status: Int = _
  @BeanProperty
  @Column var parent: Int = _
  @CreationTimestamp
  @BeanProperty
  @Column var CREATETIME: Date = _
  @UpdateTimestamp
  @BeanProperty
  @Column var UPDATETIME: Date = _


}


//  @BeanProperty
//  @Column(name = "user_id" )
//  var userid: Long = _
//  @BeanProperty
//  @Column(name = "user_id" )
//  var userid: Long = _


@Data
@Entity
@Table( name = "alipay" )
@NoArgsConstructor
@AllArgsConstructor
class Alipaye{
  @Id
  @BeanProperty
  @Column
  var id: Long = _
  @BeanProperty
  @Column var name: String = _
  @BeanProperty
  @Column(name = "user_id")
  var userid: Long = _
  @BeanProperty
  @Column(name = "app_id")
  var appid: String = _
  @BeanProperty
  @Column(name = "private_key")
  var privatekey: String = _
  @BeanProperty
  @Column(name = "alipay_publickey")
  var alipaypublickey: String = _
  @BeanProperty
  @Column(name = "paystatus")
  var paystatus: Int = _
  @BeanProperty
  @Column var status: Int = _
  @BeanProperty
  @Column
  var weight: Int = _
  @BeanProperty
  @Column
  var amount: Long = _
  @BeanProperty
  @Column(name="pay_type")
  var paytype: Int = _

//  @ManyToOne (fetch=FetchType.LAZY)//(fetch=FetchType.EAGER)//fetch=FetchType.LAZY
//  @JoinColumn( name = "user_id" ) var goodspaye: Goodspaye = _
  @ManyToOne (fetch=FetchType.LAZY)//(fetch=FetchType.EAGER)//fetch=FetchType.LAZY
  @JoinColumn( name = "user_id" , referencedColumnName = "user_id",insertable=false,updatable=false)
  var goodspaye: Goodspaye = _

}

@Data
@Entity
@Table( name = "wxpay" )
@NoArgsConstructor
@AllArgsConstructor
class Wxpaye{
  @Id
  @BeanProperty
  @Column
  var id: Long = _
  @BeanProperty
  @Column var name: String = _
  @BeanProperty
  @Column(name = "user_id")
  var userid: Long = _
  @BeanProperty
  @Column(name = "app_id")
  var appid: String = _
  @BeanProperty
  @Column(name = "mch_id")
  var mchid: String = _
  @BeanProperty
  @Column(name = "mch_key")
  var mchkey: String = _
  @BeanProperty
  @Column(name = "paystatus")
  var paystatus: Int = _
  @BeanProperty
  @Column
  var weight: Int = _
  @BeanProperty
  @Column
  var amount: Long = _
  @BeanProperty
  @Column var status: Int = _
  @BeanProperty
  @Column(name="pay_type")
  var paytype: Int = _
//  @ManyToOne (fetch=FetchType.LAZY)//(fetch=FetchType.EAGER)//fetch=FetchType.LAZY
//  @JoinColumn( name = "user_id" , referencedColumnName = "user_id",insertable=false,updatable=false)
//  var goodspaye: Goodspaye = _


  @ManyToOne (fetch=FetchType.LAZY)//(fetch=FetchType.EAGER)//fetch=FetchType.LAZY
  @JoinColumn( name = "user_id" , referencedColumnName = "user_id",insertable=false,updatable=false)
  var goodspaye: Goodspaye = _


}
@Data
@Entity
@Table( name = "pay_realm_name" )
//@NoArgsConstructor
//@AllArgsConstructor
class PayRealmName1  extends Serializable {
  @Id
  @BeanProperty
  @Column
  var id: Long = _
  @BeanProperty
  @Column(name="user_id")
  var userid: Long = _
  @BeanProperty
  @Column(name = "tenant_id")
  var tenantid: String = _
  @BeanProperty
  @Column(name = "url")
  var url: String = _
  @BeanProperty
  @Column
  var remarks: String = _
  @BeanProperty
  @Column
  var appid: String = _
  @BeanProperty
  @Column
  var secret: String = _


  @BeanProperty
  @Column var status: Int = _
  @CreationTimestamp
  @BeanProperty
  @Column var CREATETIME: Date = _
  @UpdateTimestamp
  @BeanProperty
  @Column var UPDATETIME: Date = _

  //  @OneToMany( mappedBy = "payrealmname1", cascade = Array( CascadeType.ALL ), fetch = FetchType.EAGER ) //targetEntity=classOf[UserRole] ,,fetch = FetchType.EAGER targetEntity=classOf[UserRole] ,
  //  @JoinColumn( name = "user_id",insertable = false, updatable = false )
  //  @BeanProperty //  @JsonIgnore
  //  @OneToMany( mappedBy = "payrealmname1", cascade = Array( CascadeType.ALL ), fetch = FetchType.EAGER )
  //  var goodspaye: Set[Goodspaye] = _

}