From e844ef2fdab88508e7dff4bb9e7b1632fcce15b2 Mon Sep 17 00:00:00 2001
From: feiyu02 <risaku@163.com>
Date: 星期四, 28 七月 2022 09:11:20 +0800
Subject: [PATCH] 1. 巡查统计清单调试

---
 src/main/kotlin/cn/flightfeather/supervision/business/report/DataSource.kt |  401 ++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 382 insertions(+), 19 deletions(-)

diff --git a/src/main/kotlin/cn/flightfeather/supervision/business/report/DataSource.kt b/src/main/kotlin/cn/flightfeather/supervision/business/report/DataSource.kt
index 0e23fd0..71f5ce4 100644
--- a/src/main/kotlin/cn/flightfeather/supervision/business/report/DataSource.kt
+++ b/src/main/kotlin/cn/flightfeather/supervision/business/report/DataSource.kt
@@ -1,31 +1,394 @@
 package cn.flightfeather.supervision.business.report
 
-import cn.flightfeather.supervision.domain.ds1.entity.Problemlist
-import cn.flightfeather.supervision.domain.ds1.entity.Problemtype
-import cn.flightfeather.supervision.domain.ds1.entity.Scense
-import cn.flightfeather.supervision.domain.ds1.entity.Subtask
+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.beans.factory.annotation.Autowired
+import org.springframework.stereotype.Component
+import tk.mybatis.mapper.entity.Example
+import java.time.Duration
+import java.time.LocalDateTime
+import java.time.ZoneId
+import javax.annotation.PostConstruct
 
 /**
  * 鎶ュ憡鎵�闇�婧愭暟鎹�
- * 鐢熸垚鎶ュ憡鏃讹紝鎵�闇�瑕佺殑鏁版嵁婧�
+ * 鐢熸垚鎶ュ憡鏃讹紝缁熶竴鑾峰彇鍚勫熀纭�鏁版嵁
+ * @param config 鐢熸垚鎶ュ憡鐨勫弬鏁�
  */
-class DataSource {
+class DataSource(val config: ExcelConfigVo, val dbMapper: DbMapper){
 
-    private var subTask: Subtask? = null
+    private val dateUtil = DateUtil()
 
-    /**
-     * 鎸囧畾褰撳墠杩涜缁熻鐨勪富浣撳璞�
-     */
-    fun mainObj(subtask: Subtask) {
-        this.subTask = subTask
+    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)
     }
 
-    //鍦烘櫙鍩烘湰淇℃伅
-    var scene: Scense? = null
+    fun loop(callback: (index:Int, rowData: RowData) -> Unit) {
+        reset()
+        for (i in sourceList.indices) {
+            rowData.index = i
+            rowData.clear()
+            rowData.subTask = sourceList[i]
+            callback(i, rowData)
+        }
+    }
 
-    //闂绫诲瀷
-    var problemTypes = mutableListOf<Problemtype>()
+    /**
+     * 閲嶇疆
+     */
+    fun reset() {
+        rowData.index = 0
+        rowData.subTask = sourceList.first()
+        rowData.clear()
+    }
 
-    //鍏蜂綋鐨勯棶棰�
-    var problems = mutableListOf<Problemlist>()
-}
\ No newline at end of file
+    /**
+     * 鏁版嵁缁熻鐨勬椂闂村拰鍖哄煙鍚嶇О
+     */
+    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.ScenseType.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. 鏌ユ壘鐗瑰畾鐨勫贰鏌ヤ换鍔℃垨鑰呮墍鏈夌殑璁″垝宸℃煡浠诲姟
+        var taskSceneIdList = listOf<String>()
+        val subTaskList = if (config.subTaskIdList?.isNotEmpty() == true ||
+            (config.startTime != null || config.endTime != null)
+        ) {
+            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) }
+                }
+            })
+        } else {
+            taskSceneIdList = dbMapper.monitorobjectversionMapper.getSceneByType(config.topTaskGuid, config.sceneType, config.townCode)
+            dbMapper.subtaskMapper.selectByExample(Example(Subtask::class.java).apply {
+                createCriteria().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
+                        break
+                    }
+                }
+                if (subtask == null) subtask = Subtask().apply { scenseid = it }
+                result.add(subtask)
+            }
+        } else {
+            result.addAll(subTaskList)
+        }
+
+        sourceList.addAll(result)
+    }
+
+
+    inner class RowData(){
+        var index = 0
+
+        var subTask: Subtask? = 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.ScenseType.TYPE1.value -> {
+                            dbMapper.sceneConstructionSiteMapper.selectByPrimaryKey(scene?.guid)
+                        }
+                        Constant.ScenseType.TYPE2.value -> {
+                            dbMapper.sceneWharfMapper.selectByPrimaryKey(scene?.guid)
+                        }
+                        Constant.ScenseType.TYPE3.value -> {
+                            dbMapper.sceneMixingPlantMapper.selectByPrimaryKey(scene?.guid)
+                        }
+                        Constant.ScenseType.TYPE14.value -> {
+                            dbMapper.sceneStorageYardMapper.selectByPrimaryKey(scene?.guid)
+                        }
+                        else -> null
+                    }
+                }
+                return _baseScene
+            }
+        private var _baseScene: BaseScene? = null
+
+        //鍏蜂綋鐨勯棶棰�
+        val problems: MutableList<Problemlist>
+            get() {
+                if (noRecord()) return mutableListOf()
+
+                if (_problems.isEmpty()) {
+                    val r = dbMapper.problemlistMapper.selectByExample(Example(Problemlist::class.java).apply {
+                        createCriteria().andEqualTo("stguid", subTask?.stguid)
+                    })
+                    _problems.addAll(r)
+                }
+                return _problems
+            }
+        private var _problems = mutableListOf<Problemlist>()
+
+        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: MutableList<Itemevaluation>
+            get() {
+                if (_itemevaluationList.isEmpty()) {
+                    val r = dbMapper.itemevaluationMapper.selectByExample(Example(Itemevaluation::class.java).apply {
+                        createCriteria().andEqualTo("stguid", subTask?.stguid)
+                    })
+                    _itemevaluationList.addAll(r)
+                    println("-------------------itemevaluationList--------------------------------")
+                }
+                return _itemevaluationList
+            }
+        private var _itemevaluationList = mutableListOf<Itemevaluation>()
+
+        //闂绫诲瀷
+        val problemTypes: MutableList<Problemtype>
+            get() {
+                if (_problemTypes.isEmpty()) {
+                    val r = dbMapper.problemtypeMapper.selectByExample(Example(Problemtype::class.java).apply {
+                        createCriteria().andEqualTo("scensetypeid", config.sceneType)
+                            .andEqualTo("districtcode", config.districtCode)
+                        orderBy("extension1")
+                    })
+                    _problemTypes.addAll(r)
+                }
+                return _problemTypes
+            }
+        private var _problemTypes = mutableListOf<Problemtype>()
+
+        //鑷姩璇勫垎瑙勫垯涓�绾у垎绫�
+        val topItems: MutableList<Evaluationsubrule2>
+            get() {
+                if (_topItems.isEmpty()) {
+                    val rule = dbMapper.evaluationruleMapper.selectByExample(Example(Evaluationrule::class.java).apply {
+                        createCriteria()
+                            .andEqualTo("tasktypeid", 99)
+                            .andEqualTo("scensetypeid", config.sceneType)
+                    })
+                    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) {
+                                _topItems.add(it)
+                            }
+                        }
+                        _topItems.sortBy { it.displayid }
+
+                        var t = 0
+                        _topItems.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 }
+                                this._rules.add(Pair(temp, tempSubRules))
+                            }
+                        }
+                    }
+                }
+                return _topItems
+            }
+        private var _topItems = mutableListOf<Evaluationsubrule2>()
+
+        //鑷姩璇勫垎瑙勫垯浜岀骇鍜屼笁绾у垎绫�
+        val rules: MutableList<Pair<Evaluationsubrule2, MutableList<Evaluationsubrule2>>>
+            get() {
+                if (_rules.isEmpty()) {
+                    this.topItems
+                }
+                return _rules
+            }
+        private var _rules = mutableListOf<Pair<Evaluationsubrule2, MutableList<Evaluationsubrule2>>>()
+
+        //蹇呭~鍙拌处鏁伴噺
+        val ledgerCount: Int
+            get() {
+                if (_ledgerCount == -1) {
+                    val tzSceneType = Constant.ScenseType.typeMap(config.sceneType?.toByte())
+                    _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 clear() {
+            _scene = null
+            _baseScene = null
+            _problems.clear()
+            _evaluation = null
+            _itemevaluationList.clear()
+            _ledgerRecordNum = null
+        }
+
+        /**
+         * 鍒ゆ柇鍦烘櫙鏄惁宸℃煡鐩戠锛屾湁鏃犲贰鏌ヨ褰�
+         */
+        fun noRecord() = subTask?.stguid == null
+    }
+}
+
+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,
+)

--
Gitblit v1.9.3