You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
csamsserver/src/Service.kt

159 lines
5.0 KiB

package com.gyf.csams
import org.jetbrains.exposed.sql.deleteWhere
import org.jetbrains.exposed.sql.transactions.transaction
import org.slf4j.LoggerFactory
import java.time.ZoneOffset
object AccountService {
private val logger = LoggerFactory.getLogger(AccountService::class.java)
/**
* 检查学号是否已注册,true=已注册
*/
fun registered(selectId: String): Boolean {
return transaction {
return@transaction !User.find { Users.studentId eq selectId }.empty()
}
}
/**
* 注册
*/
fun register(userVo: UserVo): UserResDto? {
try {
return transaction {
val _pwd = randomNum(8)
User.new {
studentId=userVo.studentId
name=userVo.name
password=_pwd.md5()
}
return@transaction UserResDto(password=_pwd)
}
} catch (e: Exception) {
logger.error("注册失败,发生异常:$e")
return null
}
}
/**
* 登录
*
* @param userLoginVo 登陆表单
*/
fun login(userLoginVo: UserLoginVo,_ip:String):Token?{
return transaction {
val user=User.find { Users.studentId eq userLoginVo.studentId }.firstOrNull()
when {
user==null -> {
logger.warn("学号:${userLoginVo.studentId}不存在")
return@transaction null
}
userLoginVo.password.md5() != user.password -> {
logger.warn("密码:${userLoginVo.password}错误")
return@transaction null
}
else -> {
val token=UserToken.new{
studentId=userLoginVo.studentId
ip=_ip
device=userLoginVo.device
token=listOf(studentId,ip,device).joinToString(separator = ('a' .. 'z').random().toString()).md5()
}
token.flush()
return@transaction Token(studentId = userLoginVo.studentId,token = token.token,
createTime = token.createTime.toEpochSecond(
ZoneOffset.of("+8")),name=user.name)
}
}
}
}
fun validToken(token: Token):Boolean{
return validToken(TokenVo(token = token.token,studentId = token.studentId))
}
/**
* 令牌校验
*
* @param tokenVo
* @return
*/
fun validToken(tokenVo: TokenVo):Boolean{
return transaction {
!UserToken.find {
UserTokens.studentId eq tokenVo.studentId
UserTokens.token eq tokenVo.token
}.empty()
}
}
fun logout(studentId:String):Boolean{
return transaction {
UserTokens.deleteWhere { UserTokens.studentId eq studentId }>0
}
}
fun test(){
logger.info("开始测试")
transaction {
logger.info("查询到个${User.count()}用户")
}
logger.info("结束测试")
}
}
object MainService{
private val logger = LoggerFactory.getLogger(MainService::class.java)
var maxSize:Int=20
fun register(maxSize:Int){
this.maxSize=maxSize
}
/**
* 创建留言信息
*
* @param leaveMessageVo
* @return
*/
fun createMessage(leaveMessageVo: LeaveMessageVo):Boolean{
return if(leaveMessageVo.message.isNotEmpty()){
return transaction {
val count=LeaveMessage.count().toInt()
logger.info("系统留言数:$count,限制数:$maxSize")
if(count>= maxSize){
LeaveMessage.all().sortedBy { it.createTime }.subList(0, count-maxSize).forEach {
it.delete()
}
}
logger.info("保存留言:${leaveMessageVo.message}")
LeaveMessage.new {
studentId = leaveMessageVo.token.studentId
message = leaveMessageVo.message
}
logger.info("留言保存成功")
return@transaction true
}
}else{
logger.info("留言不能为空")
false
}
}
fun getAllLeaveMessage():List<LeaveMessageFormatVo>{
return transaction {
logger.info("获取所有留言")
return@transaction LeaveMessage.all().toList().map {
LeaveMessageFormatVo(message = "${User.find { Users.studentId eq it.studentId }.first().name}说:${it.message}",studentId = it.studentId)
}
}
}
}