package com.icloud.server.scala.pay

import com.icloud.server.scala.shiro.{Permission, RolePermission, User, UserRepository, UserRole}
import com.icloud.server.web.json.JsonResult
import io.swagger.annotations.Api
import lombok.{AllArgsConstructor, Data, NoArgsConstructor}
import org.springframework.data.jpa.repository.{JpaRepository, JpaSpecificationExecutor}
import org.springframework.data.repository.{CrudRepository, PagingAndSortingRepository}
import org.springframework.stereotype.Repository
import org.springframework.web.bind.annotation.{CrossOrigin, PostMapping, RequestMapping, ResponseBody, RestController}
import springfox.documentation.annotations.ApiIgnore

import java.util
import java.util.Set
import javax.annotation.Resource
import javax.persistence.{CascadeType, Column, Entity, FetchType, Id, JoinColumn, ManyToOne, OneToMany, OneToOne, Table}
import scala.beans.BeanProperty

/**商户版本
 *
 * @Author : yang
 * @Date : 2022-07-06 9:57 AM
 * @Version : 2.1.2
 */
@Api( tags = Array( "商户版本---2.0" ), value = "API",hidden = true )
@RestController
@RequestMapping(Array("/tenant/"))
@CrossOrigin
class TenantController {
  @Resource
  @BeanProperty var tr: TenantRepository = _
  @Resource
  @BeanProperty var tur: TenantuserRepository = _




  @PostMapping( Array( "create" ) )
  @ResponseBody
  def create(nick: String, title: String, content: String, p_type: Array[String], txturl: String,subtitle:String): JsonResult ={

    val t = new Tenant
    t.setCreator("")
    t.setName("")
    t.setContactmobile("135***")


    new JsonResult()
  }
  @PostMapping( Array( "selecttenant" ) )
  @ResponseBody
  def select(tid:Long): JsonResult ={
    println(tid)
//    tr.findByIdAndStatus(tid,0)
    new JsonResult(tr.findByIdAndStatus(tid,0))
  }
  @PostMapping( Array( "selectuserid" ) )
  @ResponseBody
  def selectuserid(uid:Long): JsonResult ={
    println(uid)
    //    tr.findByIdAndStatus(tid,0)
    new JsonResult(tur.findByUseridAndStatus(uid,0))
  }
  @PostMapping( Array( "update" ) )
  @ResponseBody
  def update(nick: String, title: String, content: String, p_type: Array[String], txturl: String,subtitle:String): JsonResult ={
    new JsonResult()
  }
}




@Repository trait TenantRepository extends CrudRepository[Tenant, Long] with JpaSpecificationExecutor[Tenant] with JpaRepository[Tenant, Long] with PagingAndSortingRepository[Tenant, Long] {
  //  def findByUidOrderByFidDesc( uid:Int,p:Pageable):util.List[Menuinit]
  //  def countByUid( uid:Int):Long
//  def findByStatus(status:Int):util.List[Tenant]
//  def findByTypesAndStatus(types:Int,status:Int):util.List[Tenant]
//  def findByParentidAndStatus(parentid:Int,status:Int):util.List[Tenant]

    def findByIdAndStatus(id:Long,status:Int):util.List[Tenant]


}

@Repository trait TenantuserRepository extends CrudRepository[Tenantuser1, Long] with JpaSpecificationExecutor[Tenantuser1] with JpaRepository[Tenantuser1, Long] with PagingAndSortingRepository[Tenantuser1, Long] {
  //  def findByUidOrderByFidDesc( uid:Int,p:Pageable):util.List[Menuinit]
  //  def countByUid( uid:Int):Long
  //  def findByStatus(status:Int):util.List[Tenant]
  //  def findByTypesAndStatus(types:Int,status:Int):util.List[Tenant]
  //  def findByParentidAndStatus(parentid:Int,status:Int):util.List[Tenant]

//  def findByIdAndStatus(id:Long,status:Int):util.List[Tenantuser]
    def findByUseridAndStatus(userid:Long,status:Int):util.List[Tenantuser1]

}
@Data
@Entity
@Table( name = "system_tenant" )
@NoArgsConstructor
@AllArgsConstructor
class Tenant{
  @Id
  @BeanProperty
  @Column(name="tenant_id")
  var id: Long = _
  @BeanProperty
  @Column var name: String = _
  @BeanProperty
  @Column(name = "contact_user_id")
  var contactuserid: String = _
  @BeanProperty
  @Column(name = "contact_mobile")
  var contactmobile: String = _
  @BeanProperty
  @Column(name = "account_count")
  var accountcount: Int = _
  @BeanProperty
  @Column(name = "creator")
  var creator: String = _
  @BeanProperty
  @Column var status: Int = _
  @BeanProperty
  @Column var deleted: Int = _
  @BeanProperty //  @JsonIgnore
  @OneToMany( mappedBy = "tenant", cascade = Array( CascadeType.ALL ), fetch = FetchType.EAGER ) //targetEntity=classOf[UserRole] ,,fetch = FetchType.EAGER targetEntity=classOf[UserRole] ,
  var tenantuser: Set[Tenantuser] = _


}
@Data
@Entity
@Table( name = "tenant_user" )
class Tenantuser{
  @Id
  @BeanProperty
  @Column var id: Long = _
//  @BeanProperty
//  @Column var tenant_id: Long = _
  @BeanProperty
  @Column(name="user_id")
  var userid: Long = _
  @BeanProperty
  @Column var status: Int = _


  @ManyToOne //(fetch=FetchType.EAGER)//fetch=FetchType.LAZY
  @JoinColumn( name = "tenant_id" ) var tenant: Tenant = _
}


@Data
@Entity
@Table( name = "tenant_user" )
class Tenantuser1{
  @Id
  @BeanProperty
  @Column var id: Long = _
  //  @BeanProperty
  //  @Column var tenant_id: Long = _
  @BeanProperty
  @Column(name="user_id")
  var userid: Long = _
  @BeanProperty
  @Column var status: Int = _
  @BeanProperty
  @JoinColumn( name = "tenant_id" )
  @OneToOne( fetch = FetchType.EAGER, cascade = Array( CascadeType.ALL ) ) var tenant: Tenant = _
}


