package com.icloud.server.scala.shiro

//import com.icloud.server.scala.UserRole
import org.springframework.data.jpa.repository.{JpaRepository, JpaSpecificationExecutor}
import org.springframework.data.repository.{CrudRepository, PagingAndSortingRepository}
import org.springframework.stereotype.Repository

import java.util.{Date, Optional, Set}
import javax.persistence._
import scala.beans.BeanProperty
//asJava
/**
  * @Author : yang_y_1.0.2
  * @Date : 2020/8/24 12:01 下午 
  * @Version : 1.2.1  with PagingAndSortingRepository[User, Integer]
  */
@Repository trait UserRepository extends CrudRepository[User, Integer] with JpaSpecificationExecutor[User] with JpaRepository[User, Integer]  {
//  def findById (id: Int): Optional[User]
  def findById (id: Int): User

  def findByUsernameAndDfe (username: java.lang.String,dfe:Int): User
}

@Entity
@Table( name = "sys_user" ) class User extends Serializable {
  @Id
//  @GeneratedValue( strategy = GenerationType.IDENTITY )
  @BeanProperty
  @Column(name="user_id")
  var id: Int = _
  @BeanProperty
  @Column var username: String = _
  @BeanProperty
  @Column var password: String = _
  @BeanProperty
  @Column var phone: String = _
  @BeanProperty
  @Column var avatar: String = _
  @BeanProperty
  @Column var client_id: String = _
  @BeanProperty
  @Column var lock_flag: String = _
  @BeanProperty
  @Column var registration_type: String = _
  @BeanProperty
  @Column var wx_openid: String = _
  @BeanProperty
  @Column var mini_openid: String = _
  @BeanProperty
  @Column var qq_openid: String = _
  @BeanProperty
  @Column (name = "delete_flg")
  var dfe: Int = _
  @BeanProperty
//  @Column (name = "lock_flag")
//  @JoinColumn(name = "lock_flag",insertable = false, updatable = false)
//  var lock: Int = _
  @BeanProperty
  @Column var salt: String = _
  @BeanProperty //  @JsonIgnore
  @OneToMany( mappedBy = "user", cascade = Array( CascadeType.ALL ), fetch = FetchType.EAGER ) //targetEntity=classOf[UserRole] ,,fetch = FetchType.EAGER targetEntity=classOf[UserRole] ,
  var userrole: Set[UserRole] = _
}

@Entity
@Table( name = "user_role" ) class UserRole extends Serializable {
  @Id
  @GeneratedValue( strategy = GenerationType.IDENTITY )
  @BeanProperty
  @Column var id: String = _
  @BeanProperty
  @JoinColumn( name = "role_id" )
  @OneToOne( fetch = FetchType.EAGER, cascade = Array( CascadeType.ALL ) ) var role: Role = _

  //  @JsonBackReference
  @ManyToOne //(fetch=FetchType.EAGER)//fetch=FetchType.LAZY
  @JoinColumn( name = "user_id" ) var user: User = _
  @BeanProperty
  @Column var remarks: String = _
}

/**
  * 角色
  */
@Entity
@Table( name = "role" ) class Role extends Serializable {
  @Id
  @GeneratedValue( strategy = GenerationType.IDENTITY )
  @BeanProperty
  @Column( name = "role_id" ) var id: String = _
  @BeanProperty
  @Column var name: String = _
  @BeanProperty
  @Column var description: String = _
  @BeanProperty //  @JsonIgnore
  @OneToMany( targetEntity = classOf[RolePermission], mappedBy = "role", cascade = Array( CascadeType.ALL ), fetch = FetchType.EAGER ) //targetEntity=classOf[UserRole] ,
  var rolepermission: Set[RolePermission] = _
}

@Entity
@Table( name = "role_permission" ) class RolePermission extends Serializable {
  @Id
  @GeneratedValue( strategy = GenerationType.IDENTITY )
  @BeanProperty
  @Column var id: String = _

  //  @JsonBackReference
  @ManyToOne //(fetch=FetchType.EAGER)
  @JoinColumn( name = "role_id" ) var role: Role = _
  @BeanProperty
  @JoinColumn( name = "permission_id" )
  @OneToOne( fetch = FetchType.EAGER, cascade = Array( CascadeType.ALL ) ) var permission: Permission = _
}


/**
  * 权限
  */
@Entity
@Table( name = "permission" ) //@SerialVersionUID(42)
class Permission extends Serializable {
  @Id
  @GeneratedValue( strategy = GenerationType.IDENTITY )
  @BeanProperty
  @Column var permission_id: String = _
  @BeanProperty
  @Column var name: String = _
  @BeanProperty
  @Column var url: String = _


}

