feiyu02
2025-07-30 a700aeb0a07d11da1e6b2ae999983ba17a415c70
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
package cn.flightfeather.supervision.lightshare.service.impl
 
import cn.flightfeather.supervision.business.import.SceneImport
import cn.flightfeather.supervision.business.location.LocationRoadNearby
import cn.flightfeather.supervision.common.exception.BizException
import cn.flightfeather.supervision.common.utils.Constant
import cn.flightfeather.supervision.domain.ds1.entity.*
import cn.flightfeather.supervision.domain.ds1.mapper.*
import cn.flightfeather.supervision.domain.ds1.repository.SceneRep
import cn.flightfeather.supervision.domain.ds2.entity.UserMap
import cn.flightfeather.supervision.domain.ds2.repository.BaseInfoRep
import cn.flightfeather.supervision.domain.ds2.repository.UserMapRep
import cn.flightfeather.supervision.lightshare.service.*
import cn.flightfeather.supervision.lightshare.vo.*
import com.github.pagehelper.PageHelper
import com.google.gson.Gson
import org.jetbrains.kotlin.utils.addToStdlib.ifNotEmpty
import org.springframework.beans.BeanUtils
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import org.springframework.web.multipart.MultipartFile
import tk.mybatis.mapper.entity.Example
import tk.mybatis.mapper.util.StringUtil
import java.io.ByteArrayInputStream
import java.util.*
import kotlin.collections.ArrayList
 
@Service
class ScenseServiceImpl(
    val scenseMapper: ScenseMapper,
    val sceneConstructionSiteMapper: SceneConstructionSiteMapper,
    val sceneDeviceMapper: SceneDeviceMapper,
    val sceneMixingPlantMapper: SceneMixingPlantMapper,
    val sceneStorageYardMapper: SceneStorageYardMapper,
    val sceneWharfMapper: SceneWharfMapper,
    val userinfoService: UserinfoService,
    private val locationRoadNearby: LocationRoadNearby,
    private val sceneImport: SceneImport,
    private val sceneRep: SceneRep,
    private val baseInfoRep: BaseInfoRep,
    private val userMapRep: UserMapRep,
) : ScenseService {
 
    @Autowired
    lateinit var taskService: TaskService
 
    @Autowired
    lateinit var domainitemService: DomainitemService
 
    @Autowired
    lateinit var monitorobjectversionService: MonitorobjectversionService
 
    //根据顶层任务ID及已经建立的任务ID列表,查询可用的任务
    override fun getUserByTaskId(list: List<String>, taskId: String): List<ScenseVo> {
        val scenseVos = mutableListOf<ScenseVo>()
        val example = Example(Scense::class.java)
        val criteria = example.createCriteria()
        val taskvo = taskService.findByID(taskId)
        //根据地址获取对应场景
        criteria.andEqualTo("districtcode", taskvo.districtcode)
        criteria.andEqualTo("provincecode", taskvo.provincecode)
        criteria.andEqualTo("citycode", taskvo.citycode)
        criteria.andEqualTo("towncode", taskvo.towncode)
        if (list.isNotEmpty()) {
            //过滤已经选择的ID
            criteria.andNotIn("guid", list)
        }
        val re = scenseMapper.selectByExample(example)
        if (re.isNotEmpty()) {
            re.forEach {
                val scenseVo = ScenseVo()
                BeanUtils.copyProperties(it, scenseVo)
                scenseVos.add(scenseVo)
            }
        }
        return scenseVos
    }
 
    //根据scense条件查询场景
    override fun search(scense: Scense): MutableList<ScenseVo> {
        val scenseVos = mutableListOf<ScenseVo>()
        val name = scense.name ?: ""
        val example = Example(Scense::class.java)
        val criteria = example.createCriteria()
        criteria.andEqualTo("typeid", scense.typeid)
        //如果有type信息,就构建type查询
        if (StringUtil.isNotEmpty(scense.type)) {
            criteria.andEqualTo("type", scense.type)
        }
        if (StringUtil.isNotEmpty(scense.provincecode)) {
            criteria.andEqualTo("provincecode", scense.provincecode)
        }
        if (StringUtil.isNotEmpty(scense.citycode)) {
            criteria.andEqualTo("citycode", scense.citycode)
        }
        if (StringUtil.isNotEmpty(scense.districtcode)) {
            criteria.andEqualTo("districtcode", scense.districtcode)
        }
        if (StringUtil.isNotEmpty(scense.towncode)) {
            criteria.andEqualTo("towncode", scense.towncode)
        }
        if (StringUtil.isNotEmpty(scense.extension1)) {
            criteria.andEqualTo("extension1", scense.extension1)
        }
        criteria.andEqualTo("index", scense.index)
        //name查询
        criteria.andLike("name", "%$name%")
        val re = scenseMapper.selectByExample(example)
        if (re.isNotEmpty()) {
            re.forEach {
                val scenseVo = ScenseVo()
                BeanUtils.copyProperties(it, scenseVo)
                scenseVos.add(scenseVo)
            }
        }
        return scenseVos
    }
 
    //根据name查询
    override fun findByName(name: String): ScenseVo {
        val scenseVo = ScenseVo()
        val scense = Scense()
        scense.name = name
        val re = scenseMapper.select(scense)
        if (re.isNotEmpty()) {
            BeanUtils.copyProperties(re[0], scenseVo)
        }
        return scenseVo
    }
 
    //根据ID查询
    override fun findOne(id: String): ScenseVo {
        val scenseVo = ScenseVo()
        val scense = scenseMapper.selectByPrimaryKey(id)
        if (scense != null) {
            BeanUtils.copyProperties(scense, scenseVo)
        }
        return scenseVo
    }
 
    //获取全部
    override fun findAll(): MutableList<ScenseVo> {
        val scenseVoList = mutableListOf<ScenseVo>()
        val scenseList = scenseMapper.selectAll()
        scenseList.forEach {
            val scenseVo = ScenseVo()
            BeanUtils.copyProperties(it, scenseVo)
            scenseVoList.add(scenseVo)
        }
        return scenseVoList
    }
 
    override fun save(scense: Scense): Int {
        return try {
            createScene(scense)
            1
        } catch (e: BizException) {
            println(e.message)
            0
        }
    }
 
    override fun update(scense: Scense): Int = scenseMapper.updateByPrimaryKeySelective(scense)
 
    @Transactional
    override fun updateList(sceneList: MutableList<Scense>): Int {
        var result = 0
        sceneList.forEach {
            result += scenseMapper.updateByPrimaryKeySelective(it)
        }
        return result
    }
 
    override fun delete(id: String): Int = scenseMapper.deleteByPrimaryKey(id)
 
    override fun getSceneType(): List<SceneTypeVo> {
        val d = domainitemService.findByLogName("场景类型")
        val res = mutableListOf<SceneTypeVo>()
        d.forEach {
            val typeId = it.value?.toInt() ?: 0
            res.add(SceneTypeVo(typeId, it.text))
        }
        return res
    }
 
    /**
     *
     * @param task 总任务
     * @param mode 0:只会获取总任务对应的监管版本中存在的场景;1:除了监管版本中存在的场景,还会获取剩余的可用场景
     * @return 场景列表
     */
    override fun getByTaskId(task: Task, mode: Int): ArrayList<ScenseVo> {
        val movList = monitorobjectversionService.findByTaskId(task.tguid ?: "")
        val sceneList = scenseMapper.selectByExample(
                Example(Scense::class.java).apply {
                    createCriteria().andEqualTo("provincecode", task.provincecode)
                            .andEqualTo("citycode", task.citycode)
                            .andEqualTo("districtcode", task.districtcode)
 
                    orderBy("index").asc()
                }
        )
 
        val sceneVoList = mutableListOf<ScenseVo>()
        sceneList.forEach {
            val vo = ScenseVo()
            BeanUtils.copyProperties(it, vo)
            sceneVoList.add(vo)
        }
 
        val result = ArrayList<ScenseVo>()
 
        when (mode) {
            0 -> {
                movList.forEach m@{ m ->
                    sceneVoList.forEach {s ->
                        if (s.guid == m.sguid) {
                            s.monitorNum = m.monitornum ?: 0
                            s.inspectedNum = m.extension1?.toInt() ?: 0
                            result.add(s)
                            return@m
                        }
                    }
                }
            }
            1 -> {
                movList.forEach m@{ m ->
                    sceneVoList.forEach {s ->
                        if (s.guid == m.sguid) {
                            s.monitorNum = 1
                            s.inspectedNum = 0
                            result.add(s)
                            return@m
                        }
                    }
                }
                sceneVoList.forEach {
                    //获取新的场景。extension1:是否完工;monitorNum==0:表示不在上次监管版本中,即新场景
                    if (it.extension1 != "0" && it.monitorNum == 0) {
                        result.add(it)
                    }
                }
            }
        }
 
        return result
    }
 
    override fun getSceneByToken(token: String, page: Int?, perPage: Int?): BaseResponse<BaseSearchResultVo> {
        if (token != "jinshan") {
            return BaseResponse(false, "请求token错误")
        }
 
        val p = PageHelper.startPage<Scense>(page ?: 1, perPage ?: 30)
 
        val sceneList = scenseMapper.selectByExample(Example(Scense::class.java).apply {
            createCriteria().andEqualTo("districtcode", "310116")
                    .andNotEqualTo("extension1", "0")
        })
 
        val result = BaseSearchResultVo()
 
        result.head = DataHead().apply {
            this.page = p.pageNum
            this.totalPage = p.pages
        }
 
        sceneList.forEach {
            it.typeid = null
            it.contactswx = null
            it.createdate = null
            it.updatedate = null
            it.extension1 = null
            it.index = null
        }
 
        result.data.addAll(sceneList)
 
        return BaseResponse(true, "请求成功", data = result)
    }
 
    override fun getSceneDetail(sceneId: String): BaseResponse<SceneDetail> {
        val sceneDetail = SceneDetail()
 
        val scene = sceneRep.findScene(sceneId = sceneId)
        sceneDetail.scense = scene
 
        val mapper = when (scene?.typeid.toString()) {
            Constant.SceneType.TYPE1.value -> sceneConstructionSiteMapper
            Constant.SceneType.TYPE2.value -> sceneWharfMapper
            Constant.SceneType.TYPE3.value -> sceneMixingPlantMapper
            Constant.SceneType.TYPE14.value -> sceneStorageYardMapper
            else -> null
        }
 
        mapper?.run {
            val subScene = selectByPrimaryKey(sceneId)
            sceneDetail.subScene = subScene
        }
 
        val sceneDevice = sceneDeviceMapper.selectByPrimaryKey(sceneId)
        sceneDetail.sceneDevice = sceneDevice
 
        return BaseResponse(true, data = sceneDetail)
    }
 
    override fun updateSceneDetail(typeId: Int, sceneDetailStr: SceneDetailStr): BaseResponse<String> {
        val result = StringBuilder()
 
        if (sceneDetailStr.scense?.guid != null) {
            val r = scenseMapper.updateByPrimaryKeySelective(sceneDetailStr.scense)
            result.append("场景基本信息更新: $r; ")
        }
        if (sceneDetailStr.sceneDevice?.getsGuid() != null) {
            val record = sceneDeviceMapper.selectByPrimaryKey(sceneDetailStr.sceneDevice?.getsGuid())
            if (record == null) {
                val r = sceneDeviceMapper.insert(sceneDetailStr.sceneDevice)
                result.append("场景设备信息新增: $r; ")
            } else {
                val r = sceneDeviceMapper.updateByPrimaryKeySelective(sceneDetailStr.sceneDevice)
                result.append("场景设备信息更新: $r; ")
            }
        }
 
        if (sceneDetailStr.subScene != null) {
            var r = 0
            var isUpdate = true
            when (typeId.toString()) {
                Constant.SceneType.TYPE1.value -> {
                    val subScene = Gson().fromJson(sceneDetailStr.subScene, SceneConstructionSite::class.java)
                    if (subScene.getsGuid() != null) {
                        val record = sceneConstructionSiteMapper.selectByPrimaryKey(subScene.getsGuid())
                        isUpdate = record != null
                        r = if (record == null) {
                            sceneConstructionSiteMapper.insert(subScene)
                        } else {
                            sceneConstructionSiteMapper.updateByPrimaryKeySelective(subScene)
                        }
                    }
                }
                Constant.SceneType.TYPE2.value -> {
                    val subScene = Gson().fromJson(sceneDetailStr.subScene, SceneWharf::class.java)
                    if (subScene.getsGuid() != null) {
                        val record = sceneWharfMapper.selectByPrimaryKey(subScene.getsGuid())
                        isUpdate = record != null
                        r = if (record == null) {
                            sceneWharfMapper.insert(subScene)
                        } else {
                            sceneWharfMapper.updateByPrimaryKeySelective(subScene)
                        }
                    }
                }
                Constant.SceneType.TYPE3.value -> {
                    val subScene = Gson().fromJson(sceneDetailStr.subScene, SceneMixingPlant::class.java)
                    if (subScene.getsGuid() != null) {
                        val record = sceneMixingPlantMapper.selectByPrimaryKey(subScene.getsGuid())
                        isUpdate = record != null
                        r = if (record == null) {
                            sceneMixingPlantMapper.insert(subScene)
                        } else {
                            sceneMixingPlantMapper.updateByPrimaryKeySelective(subScene)
                        }
                    }
                }
                Constant.SceneType.TYPE14.value -> {
                    val subScene = Gson().fromJson(sceneDetailStr.subScene, SceneStorageYard::class.java)
                    if (subScene.getsGuid() != null) {
                        val record = sceneStorageYardMapper.selectByPrimaryKey(subScene.getsGuid())
                        isUpdate = record != null
                        r = if (record == null) {
                            sceneStorageYardMapper.insert(subScene)
                        } else {
                            sceneStorageYardMapper.updateByPrimaryKeySelective(subScene)
                        }
                    }
                }
            }
            result.append("场景特有信息${if (isUpdate) "更新" else "新增"}: $r; ")
        }
 
        return BaseResponse(true, data = result.toString())
    }
 
    override fun searchScene(areaVo: AreaVo, page: Int?, perPage: Int?): BaseResponse<List<Scense>> {
        val p = PageHelper.startPage<Scense>(page ?: 1, perPage ?: 30)
        val list = scenseMapper.selectByExample(Example(Scense::class.java).apply {
            createCriteria().apply {
                areaVo.provincecode?.let { andEqualTo("provincecode", it) }
                areaVo.citycode?.let { andEqualTo("citycode", it) }
                areaVo.districtcode?.let { andEqualTo("districtcode", it) }
                areaVo.towncode?.let { andEqualTo("towncode", it) }
                areaVo.scensetypeid?.let { andEqualTo("typeid", it) }
                areaVo.sceneName?.let { andLike("name", "%${it}%") }
            }
            areaVo.online?.let {
                and(createCriteria().apply {
                    if (it) {
                        orNotEqualTo("extension1", "0").orIsNull("extension1")
                    } else {
                        andEqualTo("extension1", "0")
                    }
                })
            }
            orderBy("typeid").orderBy("index")
        })
        return BaseResponse(true, head = DataHead(p.pageNum, p.pages, p.total), data = list)
    }
 
    override fun searchByCoordinate(lng: Double, lat: Double, radius: Double): List<Scense> {
        return locationRoadNearby.searchByRadius(Pair(lng, lat), radius)
    }
 
    override fun importSceneInfo(file: MultipartFile): Boolean {
        val f = ByteArrayInputStream(file.bytes)
        val scenes = sceneImport.readFromFile(f)
        // 查找场景名称是否重复
        val names = scenes.map { it.name }
        sceneRep.findSceneList(names).map { it?.name }.ifNotEmpty {
            val str = this.joinToString(",")
            throw BizException("存在重复场景,如下:${str}")
        }
        scenes.forEach {
            createOneScene(it)
        }
        return true
    }
 
    override fun createScene(scense: Scense): Scense {
        val names = listOf(scense.name)
        sceneRep.findSceneList(names).map { it?.name }.ifNotEmpty {
            val str = this.joinToString(",")
            throw BizException("存在重复场景,如下:${str}")
        }
        createOneScene(scense)
        return scense
    }
 
 
    override fun createOneScene(scense: Scense) {
        //1. 插入场景表
        scense.townname = scense.townname?.trim()
        sceneRep.insert(scense)
        //2. 生成对应账户信息
        val userInfo = userinfoService.createAccount(scense)
        createTZUserInfo(userInfo, scense)
    }
 
    override fun createTZUserInfo(userInfo: Userinfo, scense: Scense) {
        //1. 生成飞羽环境系统对应账户和基础信息
        val userInfoTZ = userinfoService.createAccountTZ(userInfo, scense)
        val baseInfo = baseInfoRep.create(userInfoTZ, scense)
        //2. 生成用户匹配信息
        userMapRep.insert(UserMap().apply {
            tzUserId = userInfoTZ.guid
            tzUserName = userInfoTZ.realname
            svUserId = userInfo.guid
            svUserName = userInfo.realname
            umCreateTime = Date()
        })
    }
 
}