package com.icloud.server.scala.shiro

import com.icloud.server.utils.IdWorker
import org.apache.shiro.crypto.hash.SimpleHash
import org.springframework.stereotype.Component
import sun.security.provider.MD5

import javax.annotation.Resource
import scala.beans.BeanProperty

/** 用户信息操作
  *
  * @Author : yang_y_1.0.2
  * @Date : 2020/11/24 1:53 下午 
  * @Version : 1.2.1
  */
@Component
class UserService {


  @Resource
  @BeanProperty var ur: UserRepository = _

  /**
    * 修改用户昵称
    * dfe 是否锁定昵称
    * @param nick
    * @param dfe
    */
//  def setuser(id:Int,nick:String,pwd:String ,dfe:Int): String ={
//
//    val u = ur.findById( id )
//    u.setId(id)
//    if(nick!=null && (!nick.equals("")))
//    u.setNick(nick)
//    if(pwd!=null && (!pwd.equals(""))) {
//      val hashName = "md5"
//      val result = new SimpleHash( hashName, pwd, null, 2 )
//      u.setPassword(result.toString)
//    }
//    u.setNick_dfe(dfe)
//    ur.save(u)
//   if(dfe==1){
//     nick
//   }else{
//     uylk.readom_nick()
//   }
//  }

  /**
    * 获取昵称
    * 是否锁定昵称
    * @param id
    */
//  def getnick(id:Int): NickName ={
//    val u = ur.findById( id )
//    var nick=u.getNick
//    //如果没有锁定昵称,随机获取昵称
//    if(u.nick_dfe!=1){
//      nick=uylk.readom_nick()
//    }
//
//    NickName(nick,0)
//  }
  //修改密码
  def setpwd(id:Int,pwd:String): Boolean ={
    var b=false
    val hashName = "md5"
    val result = new SimpleHash( hashName, pwd, null, 2 )
    val user = ur.findById( id )
    user.setPassword(result.toString)
    ur.save(user)
    b
  }
  //初始化密码
  def initpwd(username:String): Boolean ={
    var b=false
    val hashName = "md5"
    val pwd = "123456"
    val result = new SimpleHash( hashName, pwd, null, 2 )
    val user = ur.findByUsernameAndDfe( username,1 )
    user.setPassword(result.toString)
    ur.save(user)
    b=true
    b
  }
  //注册
  def register(username:String,pwd:String): Boolean ={
    var b=false
    val hashName = "md5"
//    val pwd = "123456"
    var salt="OCCLOUD+JWT"
    val result = new SimpleHash( hashName, pwd,null , 2 )
    val user =new User
    user.setUsername(username)
    user.setPassword(result.toString)
    user.setLock_flag("0")
    user.setMini_openid("aaa")
    user.setQq_openid("00")
    user.setRegistration_type("0")
    val str = System.currentTimeMillis().toString.substring(10)
    val worker = new IdWorker

    user.setId(worker.nextId().toInt)
    ur.save(user)

    b=true
    b
  }
  //获取id
  def getid(name:String): User ={
    ur.findByUsernameAndDfe(name,0)
  }

}
case class NickName(nick:String,dfe:Int)
