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.
128 lines
3.5 KiB
128 lines
3.5 KiB
package com.gyf.csams
|
|
|
|
import org.jetbrains.exposed.dao.id.IntIdTable
|
|
import org.jetbrains.exposed.sql.Column
|
|
import org.jetbrains.exposed.sql.transactions.transaction
|
|
import org.jetbrains.exposed.sql.vendors.MysqlDialect
|
|
import org.jetbrains.exposed.sql.vendors.currentDialect
|
|
import java.io.File
|
|
import java.math.BigInteger
|
|
import java.security.MessageDigest
|
|
import java.time.LocalDateTime
|
|
import java.time.ZoneOffset
|
|
|
|
const val ApiPathPrefix="/api"
|
|
/**
|
|
* 从0-9生成随机字符串
|
|
*/
|
|
fun randomNum(length:Int=8):String{
|
|
return List(length) {('0' .. '9').random()}.joinToString("")
|
|
}
|
|
|
|
/**
|
|
* md5加密字符串
|
|
*/
|
|
fun String.md5(): String {
|
|
val md = MessageDigest.getInstance("MD5")
|
|
return BigInteger(1, md.digest(toByteArray())).toString(16).padStart(32, '0')
|
|
}
|
|
|
|
fun ByteArray.md5():String{
|
|
val md = MessageDigest.getInstance("MD5")
|
|
return BigInteger(1, md.digest(this)).toString(16).padStart(32, '0')
|
|
}
|
|
|
|
@Target(AnnotationTarget.CLASS,AnnotationTarget.FIELD)
|
|
annotation class TableComment(val comment:String)
|
|
|
|
/**
|
|
* 转换成时间戳
|
|
*LocalDateTime->Long
|
|
* @return
|
|
*/
|
|
fun LocalDateTime.format(): Long {
|
|
return this.toEpochSecond(
|
|
ZoneOffset.of("+8")
|
|
) * 1000
|
|
}
|
|
|
|
/**
|
|
* 根据文件头识别格式
|
|
*
|
|
* @property format
|
|
* @property head
|
|
*/
|
|
enum class FileFormat(val format:String,val head:String){
|
|
JPEG("jpg","FFD8FF")
|
|
}
|
|
|
|
/**
|
|
* 获取文件格式
|
|
*
|
|
* @return
|
|
*/
|
|
fun ByteArray.getFormat(): FileFormat = format(this) ?: throw IllegalArgumentException("无法识别数据格式")
|
|
|
|
/**
|
|
* 获取文件格式
|
|
*
|
|
* @return
|
|
*/
|
|
fun File.getFormat(): FileFormat {
|
|
val bytes=readBytes()
|
|
return format(bytes) ?: throw IllegalArgumentException("无非识别文件[${absolutePath}]文件的格式")
|
|
}
|
|
|
|
private fun format(bytes:ByteArray):FileFormat?{
|
|
FileFormat.values().forEach { it ->
|
|
if(it.head==bytes.copyOf(it.head.length / 2).joinToString(separator = "") { String.format("%02X",it) }){
|
|
return it
|
|
}
|
|
}
|
|
return null
|
|
}
|
|
|
|
/**
|
|
* 表、表字段添加注释
|
|
*
|
|
* @param T
|
|
* @param tables
|
|
* @return
|
|
*/
|
|
fun <T : IntIdTable> updateComment(vararg tables:T){
|
|
transaction {
|
|
if (currentDialect is MysqlDialect) {
|
|
tables.forEach {
|
|
val tableClass = it::class.java
|
|
|
|
var statementList: List<String> = listOf<String>()
|
|
.run {
|
|
val comment = tableClass.getAnnotation(TableComment::class.java)?.comment ?: ""
|
|
plus("ALTER TABLE ${it.tableName} COMMENT '$comment'")
|
|
}
|
|
tableClass.declaredFields.filter { it.name != "INSTANCE" }
|
|
.forEach {
|
|
it.isAccessible = true
|
|
val column = it.get(null)
|
|
if (column is Column<*>) {
|
|
val ddl = column.ddl
|
|
if (ddl.size == 1) {
|
|
val comment = it.getAnnotation(TableComment::class.java)?.comment ?: ""
|
|
statementList = statementList.plus(
|
|
ddl[0].plus(" COMMENT '$comment'")
|
|
.replace("ADD", "MODIFY")
|
|
.replace("PRIMARY KEY", "")
|
|
)
|
|
}
|
|
}
|
|
}
|
|
|
|
statementList.forEach {
|
|
exec(it)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|