feiyu02
2024-08-15 196bb14112448857a885e32dc4149e308e00b01a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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()
                }
        })
    }
}