feiyu02
2025-08-14 f373bbf83d9d2a7e5f96118d7dcd658c9fea8bc8
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package cn.flightfeather.supervision.domain.ds1.repository
 
import cn.flightfeather.supervision.common.exception.BizException
import cn.flightfeather.supervision.common.utils.Constant
import cn.flightfeather.supervision.common.utils.UUIDGenerator
import cn.flightfeather.supervision.domain.ds1.entity.*
import cn.flightfeather.supervision.domain.ds1.mapper.*
import com.google.gson.Gson
import org.springframework.stereotype.Repository
import tk.mybatis.mapper.entity.Example
import java.util.*
 
/**
 * 场景信息数据库相关操作
 */
@Repository
class SceneRep(
    private val scenseMapper: ScenseMapper,
    private val userinfoMapper: UserinfoMapper,
    private val subTaskRep: SubTaskRep,
    val sceneConstructionSiteMapper: SceneConstructionSiteMapper,
    val sceneDeviceMapper: SceneDeviceMapper,
    val sceneMixingPlantMapper: SceneMixingPlantMapper,
    val sceneStorageYardMapper: SceneStorageYardMapper,
    val sceneWharfMapper: SceneWharfMapper,
) {
 
    fun insert(scene: Scense?):Int {
        scene ?: return 0
        if (scene.guid == null) scene.guid = UUIDGenerator.generate16ShortUUID()
        scene.createdate = Date()
        return scenseMapper.insertSelective(scene)
    }
 
    fun insertOrUpdateSubScene(typeId: Int?, guid: String?, subScene: BaseScene?): Pair<Boolean, Int> {
        var r = 0
        var isUpdate = false
        subScene ?: return false to 0
 
        when (typeId.toString()) {
            Constant.SceneType.TYPE1.value -> {
                if ((subScene as SceneConstructionSite).getsGuid() == null) {
                    subScene.setsGuid(guid)
                }
                subScene.csUpdateTime = Date()
                val record = sceneConstructionSiteMapper.selectByPrimaryKey(subScene.getsGuid())
                r = if (record == null) {
                    sceneConstructionSiteMapper.insert(subScene)
                } else {
                    sceneConstructionSiteMapper.updateByPrimaryKeySelective(subScene)
                }
            }
 
            Constant.SceneType.TYPE2.value -> {
                if ((subScene as SceneWharf).getsGuid() == null) {
                    subScene.setsGuid(guid)
                }
                val record = sceneWharfMapper.selectByPrimaryKey(subScene.getsGuid())
                r = if (record == null) {
                    sceneWharfMapper.insert(subScene)
                } else {
                    sceneWharfMapper.updateByPrimaryKeySelective(subScene)
                }
            }
 
            Constant.SceneType.TYPE3.value -> {
                if ((subScene as SceneMixingPlant).getsGuid() == null) {
                    subScene.setsGuid(guid)
                }
                val record = sceneMixingPlantMapper.selectByPrimaryKey(subScene.getsGuid())
                r = if (record == null) {
                    sceneMixingPlantMapper.insert(subScene)
                } else {
                    sceneMixingPlantMapper.updateByPrimaryKeySelective(subScene)
                }
            }
 
            Constant.SceneType.TYPE14.value -> {
                if ((subScene as SceneStorageYard).getsGuid() == null) {
                    subScene.setsGuid(guid)
                }
                val record = sceneStorageYardMapper.selectByPrimaryKey(subScene.getsGuid())
                isUpdate = record != null
                r = if (record == null) {
                    sceneStorageYardMapper.insert(subScene)
                } else {
                    sceneStorageYardMapper.updateByPrimaryKeySelective(subScene)
                }
            }
        }
        return isUpdate to r
    }
 
    fun findScene(userId: String?): Scense? {
        val user = userinfoMapper.selectByPrimaryKey(userId) ?: throw BizException("用户id不存在")
        return scenseMapper.selectByPrimaryKey(user.dGuid)
    }
 
    fun findScene(sceneId: String? = null, sceneName: String? = null): Scense? {
        return scenseMapper.selectOne(Scense().apply {
            guid = sceneId
            name = sceneName
        })
    }
 
    fun findSceneList(scene: Scense): List<Scense?> {
        return scenseMapper.select(scene)
    }
 
    fun findSceneList(nameList: List<String?>): List<Scense?> {
        return scenseMapper.selectByExample(Example(Scense::class.java).apply {
            createCriteria().andIn("name", nameList)
        })
    }
 
    /**
     * 查找场景
     */
    fun findSceneList(topTaskId: String, sceneTypeId: Int? = null, townCode: String? = null): List<Scense?> {
        return scenseMapper.getSceneByType(topTaskId, sceneTypeId, townCode)
    }
 
    fun findBySubTask(subTaskId: String): Scense? {
        val subtask = subTaskRep.findOne(subTaskId)
        return scenseMapper.selectByPrimaryKey(subtask?.scenseid)
    }
}