feiyu02
2025-09-12 dc4f12f66685260ac357997680e5f3fe723c3c4a
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
package cn.flightfeather.supervision.business.autooutput.dataanalysis
 
import cn.flightfeather.supervision.common.exception.BizException
import cn.flightfeather.supervision.common.utils.Constant
import cn.flightfeather.supervision.common.utils.ExcelUtil
import cn.flightfeather.supervision.domain.ds1.entity.Scense
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.repository.JADustSiteRep
import cn.flightfeather.supervision.domain.ds3.repository.JSDustSiteRep
import cn.flightfeather.supervision.domain.ds3.repository.XHFumeSiteMapRep
import cn.flightfeather.supervision.lightshare.vo.*
import com.github.pagehelper.PageHelper
import org.apache.poi.hssf.usermodel.HSSFWorkbook
import org.springframework.stereotype.Component
import java.io.FileOutputStream
import java.util.*
 
/**
 * 监测数据和系统用户的映射管理
 */
@Component
class AopDataDeviceMap(
    private val jaDustSiteRep: JADustSiteRep,
    private val jsDustSiteRep: JSDustSiteRep,
    private val xhFumeSiteMapRep: XHFumeSiteMapRep,
    private val taskRep: TaskRep,
    private val sceneRep: SceneRep,
    private val userInfoSVRep: UserInfoSVRep,
) {
 
    /**
     * 获取区域条件下的设备和用户的映射关系
     */
    fun findMapSet(areaVo: AreaVo, userIdList: List<String?>): List<DeviceMapVo> {
        return when (areaVo.sourceType) {
            //以飞羽环境系统中的用户为主体
            1 -> find1(areaVo, userIdList)
            //以飞羽监管系统中的用户为主体
            2 -> find2(areaVo, userIdList)
            else -> emptyList()
        }
    }
 
    private fun find1(areaVo: AreaVo, userIdList: List<String?>): List<DeviceMapVo> {
        return when (areaVo.districtcode) {
            //徐汇区
            "310104" -> {
                when (areaVo.scensetypeid) {
                    //汽修
                    Constant.SceneTypeTZ.VehicleRepair.value.toString() -> emptyList()
                    else -> emptyList()
                }
            }
            else -> emptyList()
        }
    }
 
    private fun find2(areaVo: AreaVo, userIdList: List<String?>): List<DeviceMapVo> {
        return when (areaVo.districtcode) {
            //静安区
            "310106" -> when (areaVo.scensetypeid) {
                //工地
                Constant.SceneType.TYPE1.value -> jaDustSiteRep.findMapBySVUserId(userIdList)
                else -> emptyList()
            }
            //金山区
            "310116" -> when (areaVo.scensetypeid) {
                //工地,码头水泥搅拌站
                Constant.SceneType.TYPE1.value,
                Constant.SceneType.TYPE2.value,
                Constant.SceneType.TYPE3.value,
                -> jsDustSiteRep.findMapBySVUserId(userIdList)
                else -> emptyList()
            }
            //徐汇区
            "310104" -> when (areaVo.scensetypeid) {
                //餐饮
                Constant.SceneType.TYPE5.value -> xhFumeSiteMapRep.findBySVUserId(userIdList)
                else -> emptyList()
            }
            else -> emptyList()
 
        }
    }
 
    fun insertOrUpdate(areaVo: AreaVo, mapSet: List<DeviceMapVo>):Int {
        return when (areaVo.sourceType) {
            //以飞羽环境系统中的用户为主体
            1 -> {
                insertOrUpdate1(areaVo,mapSet)
            }
            //以飞羽监管系统中的用户为主体
            2 -> {
                insertOrUpdate2(areaVo,mapSet)
            }
            else -> 0
        }
    }
 
    private fun insertOrUpdate1(areaVo: AreaVo, mapSet: List<DeviceMapVo>):Int {
        return when (areaVo.districtcode) {
            //徐汇区
            "310104" -> {
                when (areaVo.scensetypeid) {
                    //汽修
                    Constant.SceneTypeTZ.VehicleRepair.value.toString() -> 0
                    else -> 0
                }
            }
            else -> 0
        }
    }
 
    private fun insertOrUpdate2(areaVo: AreaVo, mapSet: List<DeviceMapVo>):Int {
        return when (areaVo.districtcode) {
            //静安区
            "310106" -> {
                val mapSet1 = mapSet.map { DeviceMapVo.toJADustSiteMap(it) }
                when (areaVo.scensetypeid) {
                    //工地
                    Constant.SceneType.TYPE1.value -> {
                        var rc = 0
                        mapSet1.forEach {
                            rc += if(jaDustSiteRep.findByPrimaryKey(it.id) == null) {
                                jaDustSiteRep.insert(it)
                            } else {
                                jaDustSiteRep.update(it)
                            }
                        }
                        rc
                    }
                    else -> 0
                }
            }
            //金山区
            "310116" -> {
                val mapSet1 = mapSet.map { DeviceMapVo.toJSDustSiteMap(it) }
                when (areaVo.scensetypeid) {
                    //工地,码头水泥搅拌站
                    Constant.SceneType.TYPE1.value,
                    Constant.SceneType.TYPE2.value,
                    Constant.SceneType.TYPE3.value,
                        -> {
                            var rc = 0
                            mapSet1.forEach {
                                rc += if(jsDustSiteRep.findByPrimaryKey(it.id) == null) {
                                    jsDustSiteRep.insert(it)
                                } else {
                                    jsDustSiteRep.update(it)
                                }
                            }
                            rc
                        }
 
                    else -> 0
                }
            }
            //徐汇区
            "310104" -> {
                val mapSet1 = mapSet.map { DeviceMapVo.toXHFumeSiteMap(it) }
                when (areaVo.scensetypeid) {
                    //餐饮
                    Constant.SceneType.TYPE5.value -> {
                        var rc = 0
                        mapSet1.forEach {
                            rc += if(xhFumeSiteMapRep.findByPrimaryKey(it.id) == null) {
                                xhFumeSiteMapRep.insert(it)
                            } else {
                                xhFumeSiteMapRep.update(it)
                            }
                        }
                        rc
                    }
                    else -> 0
                }
            }
            else -> 0
 
        }
    }
 
    /**
     * 搜索设备
     * @param areaVo 区域条件
     * @param keyword 搜索关键词
     * @param page 页码
     * @param perPage 每页数量
     * @return 设备列表
     */
    fun searchDevice(areaVo: AreaVo, keyword: String, page: Int, perPage: Int): Pair<DataHead, List<DeviceSiteVo>> {
        return when (areaVo.districtcode) {
            //静安区
            "310106" -> when (areaVo.scensetypeid) {
                //工地
                Constant.SceneType.TYPE1.value -> {
                    val pageInfo = PageHelper.startPage<DeviceSiteVo>(page, perPage)
                    val list = jaDustSiteRep.searchSiteInfo(keyword)
                    DataHead(pageInfo.pageNum, pageInfo.pages, pageInfo.total) to list
                }
 
                else -> DataHead() to emptyList()
            }
            //金山区
            "310116" -> when (areaVo.scensetypeid) {
                //工地,码头水泥搅拌站
                Constant.SceneType.TYPE1.value,
                Constant.SceneType.TYPE2.value,
                Constant.SceneType.TYPE3.value,
                    -> {
                        val pageInfo = PageHelper.startPage<DeviceSiteVo>(page, perPage)
                        val list = jsDustSiteRep.searchSiteInfo(keyword)
                        DataHead(pageInfo.pageNum, pageInfo.pages, pageInfo.total) to list
                    }
 
                else -> DataHead() to emptyList()
            }
            //徐汇区
            "310104" -> when (areaVo.scensetypeid) {
                //餐饮
                Constant.SceneType.TYPE5.value -> {
//                    val pageInfo = PageHelper.startPage<DeviceSiteVo>(page, perPage)
//                    val list = xhFumeSiteRep.searchSiteInfo(keyword)
//                    DataHead(pageInfo.pageNum, pageInfo.pages, pageInfo.total) to list
                    DataHead() to emptyList()
                }
                else -> DataHead() to emptyList()
            }
 
            else -> DataHead() to emptyList()
        }
    }
 
    /**
     * 输出监测点位和监测设备的对应匹配关系
     * @param areaVo 区域条件
     */
    fun outputMapSet(areaVo: AreaVo) {
        // 监管总任务
        val task = taskRep.findOneTask(areaVo) ?: throw BizException("当前查询条件下未找到对应顶层任务")
        // 总任务包含对应类型的场景
        val scenes = sceneRep.findSceneList(task.tguid!!, areaVo.scensetypeid?.toInt(), areaVo.towncode)
        val sceneIds = scenes.map { it?.guid }
        val userIdList = userInfoSVRep.findUser(sceneIds).map { it?.guid }
        // 场景和监测点的匹配关系
        val mapSet = this.findMapSet(areaVo, userIdList)
        // 监测设备信息
        val deviceSiteList = when (areaVo.districtcode) {
            //静安区
            "310106" -> when (areaVo.scensetypeid) {
                //工地
                Constant.SceneType.TYPE1.value -> jaDustSiteRep.findSiteInfo()
                else -> emptyList()
            }
            //金山区
            "310116" -> when (areaVo.scensetypeid) {
                //工地,码头水泥搅拌站
                Constant.SceneType.TYPE1.value,
                Constant.SceneType.TYPE2.value,
                Constant.SceneType.TYPE3.value,
                -> jsDustSiteRep.findSiteInfo()
                else -> emptyList()
            }
            //徐汇区
            "310104" -> when (areaVo.scensetypeid) {
                //餐饮
                Constant.SceneType.TYPE5.value -> emptyList()
                else -> emptyList()
            }
            else -> emptyList()
        }
 
        outputMapSetToFile(areaVo, scenes, mapSet, deviceSiteList)
    }
 
    /**
     * 输出监测点位和监测设备的对应匹配关系到文件中
     * @param scenes 场景信息列表
     * @param mapSet 场景和监测点对应关系
     * @param siteList 监测点信息列表
     */
    private fun outputMapSetToFile(areaVo: AreaVo, scenes: List<Scense?>, mapSet: List<DeviceMapVo>, siteList:
    List<DeviceSiteVo>) {
        val _scenes = mutableListOf<Scense?>().also { it.addAll(scenes) }
        val _siteList = mutableListOf<DeviceSiteVo>().also { it.addAll(siteList) }
 
        val sceneDeviceList = mutableListOf<SceneDeviceVo>()
        //表头
        val head = sceneDeviceList.tableHead()
        scenes.forEach {
            val sceneDeviceVo = SceneDeviceVo()
            sceneDeviceVo.scene = it
            val user = userInfoSVRep.findUser(it?.guid)
            mapSet.findBySVUserId(user?.guid).forEach { map ->
                val site = siteList.findByMNCode(map.deviceCode)
                sceneDeviceVo.deviceList.add(site)
                _siteList.remove(site)
            }
            sceneDeviceList.add(sceneDeviceVo)
        }
        //内容
        val contents = sceneDeviceList.tableContent()
 
 
        val workbook = HSSFWorkbook()
        ExcelUtil.write(head, contents, workbook, sheetName = "场景设备匹配")
 
        val head2 = sceneDeviceList.tableHeadDevice()
        val contents2 = mutableListOf<Array<Any>>()
        _siteList.forEach {
            contents2.add(arrayOf(it.id ?: "", it.mnCode ?: "", it.name ?: "", it.address ?: "", it.beginDate ?: "", it
                .dutyCompany ?: "", it.groupName ?: ""))
        }
        ExcelUtil.write(head2, contents2, workbook, sheetName = "未匹配设备")
 
        val path = "target/"
        val fileName = "${areaVo.districtname}-场景设备信息-${Date().time}.xls"
        val out = FileOutputStream(path + fileName)
        workbook.write(out)
        workbook.close()
        out.flush()
        out.close()
    }
 
}