package cn.flightfeather.supervision.domain.repository
|
|
import cn.flightfeather.supervision.common.exception.ResponseErrorException
|
import cn.flightfeather.supervision.domain.entity.PracticalOperation
|
import cn.flightfeather.supervision.domain.entity.PracticalOperationRecord
|
import cn.flightfeather.supervision.domain.mapper.PracticalOperationMapper
|
import cn.flightfeather.supervision.domain.mapper.PracticalOperationRecordMapper
|
import com.github.pagehelper.PageHelper
|
import org.springframework.stereotype.Repository
|
import tk.mybatis.mapper.entity.Example
|
import java.time.LocalDateTime
|
import java.util.*
|
|
@Repository
|
class PracticalOperationRep(
|
private val practicalOperationMapper: PracticalOperationMapper,
|
private val practicalOperationRecordMapper: PracticalOperationRecordMapper,
|
private val userInfoRep: UserInfoRep,
|
) {
|
|
/**
|
* 根据用户配置id获取实操事务
|
* @param configIds 用户配置的主键id
|
*/
|
fun getOperation(configIds: List<Int>): List<PracticalOperation?> {
|
return practicalOperationMapper.selectByExample(Example(PracticalOperation::class.java).apply {
|
createCriteria().andIn("poUserConfigId", configIds)
|
})
|
}
|
|
/**
|
* 根据用户id获取实操事务
|
* @param userId 用户id
|
*/
|
fun getOperation(userId: String): List<PracticalOperation?> {
|
return practicalOperationMapper.selectByExample(Example(PracticalOperation::class.java).apply {
|
createCriteria().andEqualTo("poUserId", userId)
|
})
|
}
|
|
/**
|
* 执行一件实操事务,记录执行状态结果
|
* @param userId 执行人id
|
* @param operationId 事务id
|
* @param stateId 事务的结果状态id(根据每个事务的定义决定)
|
*/
|
fun executeOperation(userId: String, operationId: Int, stateId: String): PracticalOperationRecord? {
|
val userInfo = userInfoRep.getUser(userId) ?: throw ResponseErrorException("用户不存在,无法操作")
|
val operation =
|
practicalOperationMapper.selectByPrimaryKey(operationId) ?: throw ResponseErrorException("该实操事务不存在")
|
val stateRangeIds = operation.poStateRangeId.split(";")
|
val stateRangeNames = operation.poStateRange.split(";")
|
if (stateRangeIds.size != stateRangeNames.size) throw ResponseErrorException("事务执行结果的可选状态配置错误!")
|
val index = stateRangeIds.indexOf(stateId)
|
if (index == -1) throw ResponseErrorException("事务执行的状态无效")
|
val record = PracticalOperationRecord().apply {
|
poId = operationId
|
poTitle = operation.poTitle
|
prUserId = userId
|
prUserName = userInfo.realname
|
prUserSceneType = userInfo.extension2?.toIntOrNull()
|
prTime = Date()
|
prStateId = stateId
|
prStateName = stateRangeNames[index]
|
}
|
practicalOperationRecordMapper.insert(record)
|
return record
|
}
|
|
/**
|
* 获取用户实操事务操作记录
|
* @param userId
|
* @param operationId
|
* @param page
|
* @param perPage
|
*/
|
fun getRecords(userId: String, operationId: Int, page: Int = 1, perPage: Int = 30):
|
List<PracticalOperationRecord?> {
|
PageHelper.startPage<PracticalOperationRecord>(page, perPage)
|
return getRecords(userId, listOf(operationId))
|
}
|
|
/**
|
* 获取用户实操事务操作记录
|
* @param userId
|
* @param operationIds
|
* @param sT
|
* @param eT
|
* @param isAsc 是否按上传时间正序排列,默认false
|
*/
|
fun getRecords(
|
userId: String?, operationIds: List<Int?>, sT: LocalDateTime? = null, eT: LocalDateTime? = null,
|
isAsc: Boolean = false,
|
)
|
: List<PracticalOperationRecord?> {
|
return practicalOperationRecordMapper.selectByExample(Example(PracticalOperationRecord::class.java).apply {
|
createCriteria().andIn("poId", operationIds)
|
.andEqualTo("prUserId", userId)
|
.andGreaterThanOrEqualTo("prTime", sT)
|
.andLessThanOrEqualTo("prTime", eT)
|
orderBy("poId").asc()
|
.orderBy("prTime").apply {
|
if (isAsc) asc() else desc()
|
}
|
})
|
}
|
}
|