feiyu02
2024-11-08 d2727f231319a48019bc3b87439136ab49b97b9b
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
package cn.flightfeather.supervision.business.report
 
import cn.flightfeather.supervision.common.utils.Constant
import cn.flightfeather.supervision.common.utils.DateUtil
import cn.flightfeather.supervision.domain.ds1.entity.*
import cn.flightfeather.supervision.domain.ds1.mapper.*
import cn.flightfeather.supervision.domain.ds2.entity.LedgerRecord
import cn.flightfeather.supervision.domain.ds2.entity.LedgerSubType
import cn.flightfeather.supervision.domain.ds2.entity.UserMap
import cn.flightfeather.supervision.domain.ds2.mapper.LedgerRecordMapper
import cn.flightfeather.supervision.domain.ds2.mapper.LedgerSubTypeMapper
import cn.flightfeather.supervision.domain.ds2.mapper.UserMapMapper
import cn.flightfeather.supervision.lightshare.vo.ExcelConfigVo
import org.springframework.stereotype.Component
import tk.mybatis.mapper.entity.Example
import java.time.LocalDateTime
import java.time.ZoneId
 
/**
 * 报告所需源数据
 * 生成报告时,统一获取各基础数据
 * @param config 生成报告的参数
 */
class DataSource(val config: ExcelConfigVo, val dbMapper: DbMapper){
 
    private val sourceList = mutableListOf<Subtask>()
 
    var year = 0
 
    var month = 0
 
    var area = ""
 
    val rowData = RowData()
 
    init {
//        if (config.districtCode == null) {
//            throw IllegalStateException("参数缺少区县编码:[districtCode]")
//        }
//        if (config.sceneType == null) {
//            throw IllegalStateException("参数缺少场景类型:[sceneType]")
//        }
        getSource(config)
    }
 
    fun loop(callback: (index:Int, rowData: RowData) -> Unit) {
//        reset()
        for (i in sourceList.indices) {
//            rowData.recordLastScene()
            rowData.index = i
            rowData.subTask = sourceList[i]
            rowData.clear()
            callback(i, rowData)
        }
    }
 
    /**
     * 重置
     */
    fun reset() {
        rowData.index = 0
        rowData.subTask = if (sourceList.isEmpty()) null else sourceList.first()
        rowData.clear()
    }
 
    /**
     * 数据统计的时间和区域名称
     */
    fun areaName(): String {
        val t = dbMapper.taskMapper.selectByPrimaryKey(config.topTaskGuid)
        return "${DateUtil.DateToString(t.starttime, DateUtil.DateStyle.YYYY_MM_CN)}${t.districtname}${if (area != t.districtname) area else ""}${Constant.SceneType.getDes(config.sceneType)}"
    }
 
    /**
     * 获取报告数据源(巡查任务)
     */
    private fun getSource(config: ExcelConfigVo) {
//        if (config.sceneType == null) return
 
        val result = mutableListOf<Subtask>()
 
        if (config.townCode != null) {
            dbMapper.townMapper.selectByExample(Example(Town::class.java).apply {
                createCriteria().andEqualTo("towncode", config.townCode)
            })?.takeIf { it.isNotEmpty() }?.get(0)?.let { area = it.townname ?: "" }
        }
 
        dbMapper.taskMapper.selectByPrimaryKey(config.topTaskGuid)?.let {
            val time = LocalDateTime.ofInstant(it.starttime?.toInstant(), ZoneId.systemDefault())
            this.year = time.year
            this.month = time.monthValue
            if (area.isBlank()) area = it.districtname ?: ""
        }
 
        //1. 查找特定的巡查任务或者所有的计划巡查任务
        val taskSceneIdList = dbMapper.scenseMapper.getSceneByType(config.topTaskGuid, config.sceneType,
            config.townCode).map { it.guid ?: "" }
 
        // 统计总任务下所有场景
        if (config.allScene) {
            val subTaskList = dbMapper.subtaskMapper.selectByExample(Example(Subtask::class.java).apply {
                createCriteria().apply {
                    if (taskSceneIdList.isNotEmpty()) andIn("scenseid", taskSceneIdList)
                }.andEqualTo("tguid", config.topTaskGuid)
            })
            // 给还未巡查的场景生成空的子任务对象
            if (taskSceneIdList.isNotEmpty()) {
                taskSceneIdList.forEach {
                    var subtask: Subtask? = null
                    for (s in subTaskList) {
                        if (s.scenseid == it) {
                            subtask = s
                            result.add(s)
                        }
                    }
                    if (subtask == null) {
                        result.add(Subtask().apply { scenseid = it })
                    }
                }
            } else {
                result.addAll(subTaskList)
            }
        }
        else {
            val subTaskList = dbMapper.subtaskMapper.selectByExample(Example(Subtask::class.java).apply {
                    createCriteria().apply {
                        if (config.subTaskIdList?.isNotEmpty() == true) andIn("stguid", config.subTaskIdList)
                        config.startTime?.let { andGreaterThanOrEqualTo("planstarttime", it) }
                        config.endTime?.let { andLessThanOrEqualTo("planendtime", it) }
                        config.districtCode?.let { andEqualTo("districtcode", it) }
                        andEqualTo("tguid", config.topTaskGuid)
                    }
                })
            result.addAll(subTaskList)
        }
 
        sourceList.addAll(result)
    }
 
 
    inner class RowData(){
        var index = 0
 
        var subTask: Subtask? = null
 
        private var lastScene: Scense? = null
 
        //场景基本信息
        val scene: Scense?
            get() {
                if (_scene == null) {
                    _scene = dbMapper.scenseMapper.selectByPrimaryKey(subTask?.scenseid)
                }
                return _scene
            }
        private var _scene: Scense? = null
 
        //各场景特有的基本信息
        val baseScene: BaseScene?
            get() {
                if (_baseScene == null) {
                    _baseScene = when (this.scene?.typeid.toString()) {
                        Constant.SceneType.TYPE1.value -> {
                            dbMapper.sceneConstructionSiteMapper.selectByPrimaryKey(scene?.guid)
                        }
                        Constant.SceneType.TYPE2.value -> {
                            dbMapper.sceneWharfMapper.selectByPrimaryKey(scene?.guid)
                        }
                        Constant.SceneType.TYPE3.value -> {
                            dbMapper.sceneMixingPlantMapper.selectByPrimaryKey(scene?.guid)
                        }
                        Constant.SceneType.TYPE14.value -> {
                            dbMapper.sceneStorageYardMapper.selectByPrimaryKey(scene?.guid)
                        }
                        else -> null
                    }
                }
                return _baseScene
            }
        private var _baseScene: BaseScene? = null
 
        //具体的问题
        val problems: List<Problemlist>
            get() {
                if (noRecord()) return mutableListOf()
 
                if (_problems == null) {
                    val r = dbMapper.problemlistMapper.selectByExample(Example(Problemlist::class.java).apply {
                        createCriteria().andEqualTo("stguid", subTask?.stguid)
                    })
                    _problems = r
                }
                return _problems ?: emptyList()
            }
        private var _problems: List<Problemlist>? = null
 
        //评估总分
        val evaluation: Evaluation?
            get() {
                if (_evaluation == null) {
                    if (noRecord()) return null
 
                    dbMapper.evaluationMapper.selectByExample(Example(Evaluation::class.java).apply {
                        createCriteria().andEqualTo("stguid", subTask?.stguid)
                    })?.takeIf { it.isNotEmpty() }?.let { _evaluation = it[0] }
                }
                return _evaluation
            }
        private var _evaluation: Evaluation? = null
 
        //评估细则得分
        val itemevaluationList: List<Itemevaluation>
            get() {
                if (_itemevaluationList == null) {
                    val r = dbMapper.itemevaluationMapper.selectByExample(Example(Itemevaluation::class.java).apply {
                        createCriteria().andEqualTo("stguid", subTask?.stguid)
                    })
                    _itemevaluationList = r
                    println("-------------------itemevaluationList--------------------------------")
                }
                return _itemevaluationList ?: emptyList()
            }
        private var _itemevaluationList: List<Itemevaluation>? = null
 
        //问题类型
        val problemTypes: List<Problemtype>
            get() {
                if (_problemTypes == null) {
                    val r = dbMapper.problemtypeMapper.selectByExample(Example(Problemtype::class.java).apply {
                        createCriteria().andEqualTo("scensetypeid", scene?.typeid)
                            .andEqualTo("districtcode", scene?.districtcode)
                        orderBy("extension1")
                    })
                    _problemTypes = r
                }
                return _problemTypes ?: emptyList()
            }
        private var _problemTypes: List<Problemtype>? = null
 
        //自动评分规则一级分类
        val topItems: List<Evaluationsubrule2>
            get() {
                if (_topItems == null) {
                    val tempTopItems = mutableListOf<Evaluationsubrule2>()
                    val secRules = mutableListOf<Pair<Evaluationsubrule2, MutableList<Evaluationsubrule2>>>()
                    val rule = dbMapper.evaluationruleMapper.selectByExample(Example(Evaluationrule::class.java).apply {
                        createCriteria()
                            .andEqualTo("tasktypeid", 99)
                            .andEqualTo("scensetypeid", scene?.typeid)
                    })
                    if (rule.isNotEmpty()) {
                        val ruleId = rule[0].guid
                        val rules = dbMapper.evaluationsubruleMapper.selectByExample(Example(Evaluationsubrule2::class.java).apply {
                            createCriteria().andEqualTo("erguid", ruleId)
                        })
                        rules.forEach {
                            if (it.ertype == 2) {
                                tempTopItems.add(it)
                            }
                        }
                        tempTopItems.sortBy { it.displayid }
 
                        var t = 0
                        tempTopItems.forEach {
                            t += it.maxscore ?: 0
                            val tempRules = mutableListOf<Evaluationsubrule2>()
                            for (i in rules) {
                                if (i.fatherid == it.guid && i.ertype == 3) {
                                    tempRules.add(i)
                                }
                            }
                            //评分大项如果没有找到评分小项,则说明其直接对应最小评分项,其本身变成评分项
                            if (tempRules.isEmpty()) {
                                tempRules.add(it)
                            }
                            tempRules.sortBy { t -> t.displayid }
                            tempRules.forEach { temp ->
                                val tempSubRules = mutableListOf<Evaluationsubrule2>()
                                for (i in rules) {
                                    if (i.fatherid == temp.guid && i.ertype == 4) {
                                        tempSubRules.add(i)
                                    }
                                }
                                tempSubRules.sortBy { ts -> ts.displayid }
                                secRules.add(Pair(temp, tempSubRules))
                            }
                        }
                    }
                    this._rules = secRules
                    _topItems = tempTopItems
                }
                return _topItems ?: emptyList()
            }
        private var _topItems: List<Evaluationsubrule2>? = null
 
        //自动评分规则二级和三级分类
        val rules: List<Pair<Evaluationsubrule2, MutableList<Evaluationsubrule2>>>
            get() {
                if (_rules == null) {
                    this.topItems
                }
                return _rules ?: emptyList()
            }
        private var _rules: List<Pair<Evaluationsubrule2, MutableList<Evaluationsubrule2>>>? = null
 
        //必填台账数量
        val ledgerCount: Int
            get() {
                if (_ledgerCount == -1) {
                    val tzSceneType = Constant.SceneType.typeMap(scene?.typeid)
                    _ledgerCount = dbMapper.ledgerSubTypeMapper.selectCountByExample(Example(LedgerSubType::class.java).apply {
                        createCriteria().andEqualTo("lScenetype", tzSceneType).andEqualTo("lNeedupdate", true)
                    })
                }
                return _ledgerCount
            }
        private var _ledgerCount = -1
 
        //用户实际提交台账数量
        val ledgerRecords: List<LedgerRecord>
            get() {
                if (_ledgerRecordNum == null) {
                    val userInfo = dbMapper.userinfoMapper.selectByExample(Example(Userinfo::class.java).apply {
                        createCriteria().andEqualTo("dGuid", subTask?.scenseid)
                    })?.takeIf { l -> l.isNotEmpty() }?.get(0)
 
                    val tzUserId = dbMapper.userMapMapper.selectByExample(Example(UserMap::class.java).apply {
                        createCriteria().andEqualTo("svUserId", userInfo?.guid)
                    })?.takeIf { m-> m.isNotEmpty() }?.get(0)?.tzUserId
 
                    if (tzUserId != null) {
                        _ledgerRecordNum = dbMapper.ledgerRecordMapper.selectByExample(Example(LedgerRecord::class.java).apply {
                            createCriteria().andEqualTo("lrYear", year)
                                    .andEqualTo("lrMonth", month)
                                    .andEqualTo("lrSubmitid", tzUserId)
                        })
                    }
                }
                return _ledgerRecordNum ?: emptyList()
            }
        private var _ledgerRecordNum: List<LedgerRecord>? = null
 
        /**
         * 获取当前巡查任务的上期巡查记录
         */
        fun lastOne(): RowData {
            val last = RowData()
            val r = dbMapper.subtaskMapper.selectByExample(Example(Subtask::class.java).apply {
                createCriteria().andEqualTo("scenseid", subTask?.scenseid)
                    .andLessThan("planstarttime", subTask?.planstarttime)
                orderBy("planstarttime").desc()
            })
            if (r.isNotEmpty()) {
                val thisMonth = LocalDateTime.ofInstant(subTask?.planstarttime?.toInstant(), ZoneId.systemDefault()).withDayOfMonth(1).toLocalDate()
                val lastMonth = LocalDateTime.ofInstant(r[0]?.planstarttime?.toInstant(), ZoneId.systemDefault()).withDayOfMonth(1).toLocalDate()
                if (lastMonth.plusMonths(1).isEqual(thisMonth)) {
                    last.subTask = r[0]
                }
            }
            return last
        }
 
        /**
         * 记录上一个场景
         */
        fun recordLastScene() {
            lastScene = scene
        }
 
        /**
         * 清空当前处理的对象的相关数据源
         */
        fun clear() {
            _scene = null
            _baseScene = null
            _problems = null
            _evaluation = null
            _itemevaluationList = null
            _ledgerRecordNum = null
            if (lastScene != null && lastScene?.typeid != scene?.typeid) {
                _problemTypes = null
                _topItems = null
                _rules = null
                _ledgerCount = -1
            }
            recordLastScene()
        }
 
        /**
         * 判断场景是否巡查监管,有无巡查记录
         */
        fun noRecord() = subTask?.stguid == null
    }
}
 
@Component
data class DbMapper(
    val scenseMapper: ScenseMapper,
    val problemlistMapper: ProblemlistMapper,
    val problemtypeMapper: ProblemtypeMapper,
    val subtaskMapper: SubtaskMapper,
    val monitorobjectversionMapper: MonitorobjectversionMapper,
    val sceneConstructionSiteMapper: SceneConstructionSiteMapper,
    val sceneMixingPlantMapper: SceneMixingPlantMapper,
    val sceneStorageYardMapper: SceneStorageYardMapper,
    val sceneWharfMapper: SceneWharfMapper,
    val taskMapper: TaskMapper,
    val evaluationruleMapper: EvaluationruleMapper,
    val evaluationsubruleMapper: EvaluationsubruleMapper2,
    val evaluationMapper: EvaluationMapper,
    val itemevaluationMapper: ItemevaluationMapper,
    val ledgerSubTypeMapper: LedgerSubTypeMapper,
    val ledgerRecordMapper: LedgerRecordMapper,
    val userinfoMapper: UserinfoMapper,
    val userMapMapper: UserMapMapper,
    val townMapper: TownMapper,
)