feiyu02
2025-09-30 a3cc1d220f8a1de11874bebceba0130d32157ff1
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
package cn.flightfeather.supervision.lightshare.service.impl
 
import cn.flightfeather.supervision.business.autooutput.dataanalysis.AopDataDeviceMap
import cn.flightfeather.supervision.common.exception.BizException
import cn.flightfeather.supervision.common.utils.UUIDGenerator
import cn.flightfeather.supervision.domain.ds1.entity.Scense
import cn.flightfeather.supervision.domain.ds1.entity.Userinfo
import cn.flightfeather.supervision.domain.ds1.mapper.UserinfoMapper
import cn.flightfeather.supervision.domain.ds1.repository.SceneRep
import cn.flightfeather.supervision.domain.ds1.repository.TaskRep
import cn.flightfeather.supervision.domain.ds1.repository.UserInfoSVRep
import cn.flightfeather.supervision.domain.ds2.entity.UserMap
import cn.flightfeather.supervision.domain.ds2.entity.UserinfoTZ
import cn.flightfeather.supervision.domain.ds2.mapper.UserMapMapper
import cn.flightfeather.supervision.domain.ds2.mapper.UserinfoTZMapper
import cn.flightfeather.supervision.domain.ds2.repository.UserInfoTZRep
import cn.flightfeather.supervision.domain.ds2.repository.UserMapRep
import cn.flightfeather.supervision.lightshare.service.UserMapService
import cn.flightfeather.supervision.lightshare.vo.AreaVo
import cn.flightfeather.supervision.lightshare.vo.DataHead
import cn.flightfeather.supervision.lightshare.vo.DeviceMapVo
import cn.flightfeather.supervision.lightshare.vo.DeviceSiteVo
import org.springframework.stereotype.Service
import tk.mybatis.mapper.entity.Example
import java.util.*
 
@Service
class UserMapServiceImpl(
    private val userMapMapper: UserMapMapper,
    private val userinfoMapper: UserinfoMapper,
    private val userinfoTZMapper: UserinfoTZMapper,
    private val sceneRep: SceneRep,
    private val taskRep: TaskRep,
    private val userInfoSVRep: UserInfoSVRep,
    private val userInfoTZRep: UserInfoTZRep,
    private val userMapRep: UserMapRep,
    private val aopDataDeviceMap: AopDataDeviceMap,
) : UserMapService {
 
    override fun getTZIdBySceneId(sceneId: String): UserMap {
        val user = userInfoSVRep.findUser(sceneId)
        val userId = user?.guid
        val result = userMapMapper.selectByExample(Example(UserMap::class.java).apply {
            createCriteria().andEqualTo("svUserId", userId)
        })
        return if (result.isNotEmpty()) result[0] else UserMap()
    }
 
    override fun getSceneByTZId(tzUserId: String): Scense? {
        return userMapRep.findFromSupervision(tzUserId)
    }
 
    override fun autoCreateMap(userList: List<Userinfo?>) {
        // 选择需要处理的账户
//        val userList = userinfoMapper.selectByExample(Example(Userinfo::class.java).apply {
//            createCriteria().andEqualTo("remark", "徐汇区")
//        })
        userList.forEach {
            it ?: return@forEach
            // 查找是否已经有账户匹配记录
            val records = userMapMapper.selectByExample(Example(UserMap::class.java).apply {
                createCriteria().andEqualTo("svUserId", it.guid)
            })
            // 没有记录时,创建匹配记录
            if (records.isEmpty()) {
                // 1、 根据两个系统中的用户名称进行精确匹配
                val tzUsers = userinfoTZMapper.selectByExample(Example(UserinfoTZ::class.java).apply {
                    createCriteria().andEqualTo("realname", it.realname)
                })
                if (tzUsers.isNotEmpty()) {
                    val tzU = tzUsers[0]
                    if (userMapMapper.selectByPrimaryKey(tzU.guid) == null) {
                        userMapMapper.insert(UserMap().apply {
                            tzUserId = tzU.guid
                            tzUserName = tzU.realname
                            svUserId = it.guid
                            svUserName = it.realname
                            umCreateTime = Date()
                        })
                    }
                }
                // 2、 无法精确匹配的,采用默认的编号,插入记录。后续人工修改(暂定)
                else {
                    if (userMapMapper.selectByExample(Example(UserMap::class.java).apply {
                            createCriteria().andEqualTo("svUserId", it.guid)
                        }).isEmpty()) {
                        val id = "temp" + UUIDGenerator.generateShortUUID()
                        userMapMapper.insert(UserMap().apply {
                            tzUserId = id
                            tzUserName = id
                            svUserId = it.guid
                            svUserName = it.realname
                            umCreateTime = Date()
                        })
                    }
                }
            }
        }
    }
 
    override fun fetchDeviceMap(page: Int?, perPage: Int?, areaVo: AreaVo): List<DeviceMapVo?> {
        // 1.通过区域条件获取主体用户
        val userIdList = when (areaVo.sourceType) {
            //以飞羽环境系统中的用户为主体
            1 -> {
                val u = userInfoTZRep.findEnterpriseUser(areaVo.districtname, areaVo.scensetypeid?.toInt())
                u.map { it?.guid to it?.realname }
            }
            //以飞羽监管系统中的用户为主体
            2 -> {
                val task = taskRep.findOneTask(areaVo) ?: throw BizException("当前查询条件下未找到对应顶层任务")
                val scenes = sceneRep.findSceneList(task.tguid!!, areaVo.scensetypeid?.toInt(), areaVo.towncode)
                    .map { it?.guid }
                userInfoSVRep.findUser(scenes).map { it?.guid to it?.realname }
            }
            else -> emptyList()
        }
        // 2.通过区域条件决定匹配的数据来源
        val mapSet = aopDataDeviceMap.findMapSet(areaVo, userIdList.map { it.first })
        val result = mutableListOf<DeviceMapVo>()
        // 将不在mapSet中的用户,添加到结果集中
        userIdList.forEach {u ->
            val ddm = mapSet.find { m->
                when (areaVo.sourceType) {
                    //以飞羽环境系统中的用户为主体
                    1 -> m.tzUserId == u.first
                    //以飞羽监管系统中的用户为主体
                    2 -> m.svUserId == u.first
                    else -> false
                }
            }
            if (ddm == null) {
                result.add(DeviceMapVo().apply {
                    when (areaVo.sourceType) {
                        //以飞羽环境系统中的用户为主体
                        1 -> {
                            tzUserId = u.first
                            tzUserName = u.second
                        }
                        //以飞羽监管系统中的用户为主体
                        2 -> {
                            svUserId = u.first
                            svUserName = u.second
 
                        }
                    }
                })
            }
        }
        // 将mapSet中的数据添加到结果集中
        result.addAll(mapSet)
        return result.sortedBy { it.svUserId }
    }
 
    override fun insertOrUpdate(param: Pair<AreaVo, DeviceMapVo>): Int {
        return aopDataDeviceMap.insertOrUpdate(param.first, listOf(param.second))
    }
 
    override fun searchThirdPartyDevice(
        areaVo: AreaVo,
        keyword: String,
        page: Int?,
        perPage: Int?,
    ): Pair<DataHead, List<DeviceSiteVo>> {
        return aopDataDeviceMap.searchDevice(areaVo, keyword, page ?: 1, perPage ?: 30)
    }
}