feiyu02
2025-08-14 f373bbf83d9d2a7e5f96118d7dcd658c9fea8bc8
src/main/kotlin/cn/flightfeather/supervision/business/autooutput/datasource/AopDataSource.kt
@@ -7,6 +7,9 @@
import cn.flightfeather.supervision.domain.ds2.entity.LedgerSubType
import cn.flightfeather.supervision.domain.ds2.entity.UserMap
import cn.flightfeather.supervision.domain.ds2.entity.UserinfoTZ
import cn.flightfeather.supervision.lightshare.vo.AreaVo
import cn.flightfeather.supervision.lightshare.vo.EvaluationSubRuleVo
import org.springframework.beans.BeanUtils
import tk.mybatis.mapper.entity.Example
import java.time.LocalDateTime
import java.time.ZoneId
@@ -26,8 +29,10 @@
     * 设置数据源
     */
    fun setResource(topTaskGuid: String?, sceneType: Int, districtName: String?, townCode: String?) {
        val config = AopDataConfig(topTaskGuid = topTaskGuid, sceneType = sceneType, districtName = districtName,
            townCode = townCode)
        val config = AopDataConfig(
            topTaskGuid = topTaskGuid, sceneType = sceneType, districtName = districtName,
            townCode = townCode
        )
        evaluationScene.config = config
        val check = aopSceneTypeCheck.checkSceneType(sceneType)
        if (check.first) {
@@ -36,6 +41,12 @@
            initUserSource(config)
        }
    }
//    fun setResource(areaVo: AreaVo) {
//        val task = taskRep.findOneTask(areaVo)
//        val taskId = task?.tguid
//        val sceneType = areaVo.scensetypeid?.toInt() ?: throw BizException("场景类型未设置,无法评估")
//    }
    fun setResource(config: AopDataConfig) {
        config.sceneType ?: return
@@ -51,8 +62,10 @@
    fun setResource(subtask: Subtask) {
        val scene = aopDbMapper.scenseMapper.selectByPrimaryKey(subtask.scenseid)
        val sceneType = scene.typeid?.toInt()
        val config = AopDataConfig(topTaskGuid = subtask.tguid, sceneType = sceneType,
            districtName = subtask.districtname, townCode = subtask.towncode)
        val config = AopDataConfig(
            topTaskGuid = subtask.tguid, sceneType = sceneType,
            districtName = subtask.districtname, townCode = subtask.towncode
        )
        evaluationScene.config = config
//        val check = aopSceneTypeCheck.checkSceneType(sceneType)
        this.mode = 0
@@ -63,7 +76,35 @@
    /**
     * 轮询数据源
     */
    fun loop(callback: (index:Int, evaluationScene: EvaluationScene) -> Unit) {
    fun loop(callback: (index: Int, evaluationScene: EvaluationScene) -> Unit) {
        when (mode) {
            0 -> {
                sceneSourceList.forEachIndexed { i, scene ->
                    evaluationScene.clear()
                    evaluationScene.index = i
                    evaluationScene.setDefaultScene(scene)
                    do {
                        callback(i, evaluationScene)
                    } while (evaluationScene.gotoNextSubTask())
                }
            }
            1 -> {
                userSourceList.forEachIndexed { i, user ->
                    evaluationScene.clear()
                    evaluationScene.index = i
                    evaluationScene.setDefaultUserTZ(user)
                    callback(i, evaluationScene)
                }
            }
        }
    }
    /**
     * 根据子任务只轮询一次
     * 当数据源是通过[setResource]传入[Subtask]时决定时
     */
    fun runBySubTask(callback: (index: Int, evaluationScene: EvaluationScene) -> Unit) {
        when (mode) {
            0 -> {
                sceneSourceList.forEachIndexed { i, scene ->
@@ -73,6 +114,7 @@
                    callback(i, evaluationScene)
                }
            }
            1 -> {
                userSourceList.forEachIndexed { i, user ->
                    evaluationScene.clear()
@@ -92,6 +134,9 @@
        sceneSourceList.clear()
        val taskSceneIdList =
            aopDbMapper.scenseMapper.getSceneByType(config.topTaskGuid, config.sceneType, config.townCode)
        taskSceneIdList.forEach {
            println(it.name)
        }
        sceneSourceList.addAll(taskSceneIdList)
    }
@@ -110,7 +155,7 @@
    }
    // 单个评估对象
    inner class EvaluationScene(){
    inner class EvaluationScene() {
        var index = 0
        var config: AopDataConfig? = null
@@ -121,7 +166,7 @@
        fun setDefaultScene(s: Scense?) {
            scene.value = s
            userInfoTZ.fetch = {_,_ ->
            userInfoTZ.fetch = { _, _ ->
                val svUserId = aopDbMapper.userinfoMapper.selectByExample(Example(Userinfo::class.java).apply {
                    createCriteria().andEqualTo("dGuid", scene.value?.guid)
                })?.takeIf { m -> m.isNotEmpty() }?.get(0)?.guid
@@ -134,7 +179,7 @@
        fun setDefaultUserTZ(u: UserinfoTZ?) {
            userInfoTZ.value = u
            scene.fetch = {_,_ ->
            scene.fetch = { _, _ ->
                val svUserId = aopDbMapper.userMapMapper.selectByExample(Example(UserMap::class.java).apply {
                    createCriteria().andEqualTo("tzUserId", userInfoTZ.value?.guid)
                })?.takeIf { m -> m.isNotEmpty() }?.get(0)?.svUserId
@@ -143,41 +188,54 @@
            }
        }
        val userInfo = InfoProxy<Userinfo>{_,_ ->
        val userInfo = InfoProxy<Userinfo> { _, _ ->
            scene.value?.guid ?: return@InfoProxy null
            return@InfoProxy aopDbMapper.userinfoMapper.selectOne(Userinfo().apply {
                dGuid = scene.value?.guid
            })
        }
        //巡查任务信息
        val subTask = InfoProxy<Subtask> {_,_ ->
        var subTaskIndex = 0
        val subTask = InfoProxy<Subtask> { _, _ ->
            return@InfoProxy if (subTaskList.value?.isNotEmpty() == true) {
                subTaskList.value?.get(subTaskIndex)
            } else {
                null
            }
        }
        val subTaskList = InfoProxy<List<Subtask>> { _, _ ->
            return@InfoProxy aopDbMapper.subtaskMapper.selectByExample(Example(Subtask::class.java).apply {
                createCriteria().andEqualTo("scenseid", scene.value?.guid)
//                    .andBetween("planstarttime", config?.startTime, config?.endTime)
                    .andEqualTo("tguid", config?.topTaskGuid)
            })?.takeIf { it.isNotEmpty() }?.get(0)
            })
        }
        //现场巡查信息
        val inspection = InfoProxy<Inspection>{_,_ ->
        val inspection = InfoProxy<Inspection> { _, _ ->
            val inspection = Inspection()
            inspection.stguid = subTask.value?.stguid
            return@InfoProxy aopDbMapper.inspectionMapper.selectOne(inspection)
        }
        //各场景特有的基本信息
        val baseScene = InfoProxy<BaseScene> {_,_ ->
        val baseScene = InfoProxy<BaseScene> { _, _ ->
            return@InfoProxy when (config?.sceneType.toString()) {
                // 监管系统
                Constant.SceneType.TYPE1.value -> {
                    aopDbMapper.sceneConstructionSiteMapper.selectByPrimaryKey(scene.value?.guid)
                }
                Constant.SceneType.TYPE2.value -> {
                    aopDbMapper.sceneWharfMapper.selectByPrimaryKey(scene.value?.guid)
                }
                Constant.SceneType.TYPE3.value -> {
                    aopDbMapper.sceneMixingPlantMapper.selectByPrimaryKey(scene.value?.guid)
                }
                Constant.SceneType.TYPE14.value -> {
                    aopDbMapper.sceneStorageYardMapper.selectByPrimaryKey(scene.value?.guid)
                }
@@ -185,15 +243,17 @@
                Constant.SceneType.TYPE5.value -> {
                    aopDbMapper.restaurantBaseInfoMapper.selectByPrimaryKey(userInfoTZ.value?.guid)
                }
                Constant.SceneType.TYPE6.value -> {
                    aopDbMapper.vehicleBaseInfoMapper.selectByPrimaryKey(userInfoTZ.value?.guid)
                }
                else -> null
            }
        }
        //具体的问题
        val problems = InfoProxy<MutableList<Problemlist>> {_,_ ->
        val problems = InfoProxy<MutableList<Problemlist>> { _, _ ->
            if (noRecord()) return@InfoProxy mutableListOf()
            return@InfoProxy aopDbMapper.problemlistMapper.selectByExample(Example(Problemlist::class.java).apply {
                createCriteria().andEqualTo("stguid", subTask.value?.stguid)
@@ -201,7 +261,7 @@
        }
        //评估总分
        val evaluation = InfoProxy<Evaluation> {_,_ ->
        val evaluation = InfoProxy<Evaluation> { _, _ ->
            if (noRecord()) return@InfoProxy null
            return@InfoProxy aopDbMapper.evaluationMapper.selectByExample(Example(Evaluation::class.java).apply {
@@ -210,7 +270,7 @@
        }
        //评估细则得分
        val itemevaluationList = InfoProxy<MutableList<Itemevaluation>> {_,_ ->
        val itemevaluationList = InfoProxy<MutableList<Itemevaluation>> { _, _ ->
            return@InfoProxy aopDbMapper.itemevaluationMapper.selectByExample(Example(Itemevaluation::class.java).apply {
                createCriteria().andEqualTo("stguid", subTask.value?.stguid)
            })
@@ -234,13 +294,21 @@
        val totalScore = InfoProxy<Int>()
        //自动评分规则一级分类
        val topItems = InfoProxy<MutableList<Evaluationsubrule2>>(config?.sceneType.toString()) { _, cacheMap ->
            val _topItems = mutableListOf<Evaluationsubrule2>()
            val _rules = mutableListOf<Pair<Evaluationsubrule2, MutableList<Evaluationsubrule2>>>()
        val topRules = InfoProxy<MutableList<EvaluationSubRuleVo>>(config?.sceneType.toString()) { _, cacheMap ->
            val _topItems = mutableListOf<EvaluationSubRuleVo>()
            val _rules = mutableListOf<Pair<EvaluationSubRuleVo, MutableList<EvaluationSubRuleVo>>>()
            val rule = aopDbMapper.evaluationruleMapper.selectByExample(Example(Evaluationrule::class.java).apply {
                createCriteria()
                    .andEqualTo("tasktypeid", 99)
                    .andEqualTo("scensetypeid", scene.value?.typeid)
                and(createCriteria().orEqualTo("provincecode", scene.value?.provincecode).orIsNull("provincecode"))
                and(createCriteria().orEqualTo("citycode", scene.value?.citycode).orIsNull("citycode"))
                and(createCriteria().orEqualTo("districtcode", scene.value?.districtcode).orIsNull("districtcode"))
                and(createCriteria().orEqualTo("towncode", scene.value?.towncode).orIsNull("towncode"))
                orderBy("towncode").desc()
                    .orderBy("districtcode").desc()
                    .orderBy("citycode").desc()
                    .orderBy("provincecode").desc()
            })
            if (rule.isNotEmpty()) {
                this@EvaluationScene.baseRule.value = rule[0]
@@ -250,7 +318,11 @@
                val rules =
                    aopDbMapper.evaluationsubruleMapper.selectByExample(Example(Evaluationsubrule2::class.java).apply {
                        createCriteria().andEqualTo("erguid", ruleId)
                    })
                    }).map {
                        val vo = EvaluationSubRuleVo()
                        BeanUtils.copyProperties(it, vo)
                        vo
                    }
                rules.forEach {
                    if (it.ertype == 2) {
                        _topItems.add(it)
@@ -261,7 +333,7 @@
                var t = 0
                _topItems.forEach {
                    t += it.maxscore ?: 0
                    val tempRules = mutableListOf<Evaluationsubrule2>()
                    val tempRules = mutableListOf<EvaluationSubRuleVo>()
                    for (i in rules) {
                        if (i.fatherid == it.guid && i.ertype == 3) {
                            tempRules.add(i)
@@ -273,7 +345,7 @@
                    }
                    tempRules.sortBy { t -> t.displayid }
                    tempRules.forEach { temp ->
                        val tempSubRules = mutableListOf<Evaluationsubrule2>()
                        val tempSubRules = mutableListOf<EvaluationSubRuleVo>()
                        for (i in rules) {
                            if (i.fatherid == temp.guid && i.ertype == 4) {
                                tempSubRules.add(i)
@@ -294,10 +366,10 @@
        // FIXME: 2023/8/9 子规则的获取必须在[topItems]调用之后,否则为空
        //自动评分规则二级和三级分类
        val rules = InfoProxy<MutableList<Pair<Evaluationsubrule2, MutableList<Evaluationsubrule2>>>>()
        val rules = InfoProxy<MutableList<Pair<EvaluationSubRuleVo, MutableList<EvaluationSubRuleVo>>>>()
        //必填台账数量
        val ledgerCount = InfoProxy<Int>(config?.sceneType.toString()) {_, cacheMap ->
        val ledgerCount = InfoProxy<Int>(config?.sceneType.toString()) { _, cacheMap ->
            val tzSceneType = Constant.SceneType.typeMap(scene.value?.typeid)
            val list = aopDbMapper.ledgerSubTypeMapper.selectCountByExample(Example(LedgerSubType::class.java).apply {
                createCriteria().andEqualTo("lScenetype", tzSceneType).andEqualTo("lNeedupdate", true)
@@ -307,7 +379,7 @@
        }
        //用户实际提交台账数量
        val ledgerRecords = InfoProxy<List<LedgerRecord>> {_,_ ->
        val ledgerRecords = InfoProxy<List<LedgerRecord>> { _, _ ->
            return@InfoProxy aopDbMapper.ledgerRecordMapper.selectByExample(Example(LedgerRecord::class.java).apply {
                createCriteria().andEqualTo("lrYear", config?.year)
                    .andEqualTo("lrMonth", config?.month)
@@ -326,9 +398,12 @@
                orderBy("planstarttime").desc()
            })
            if (r.isNotEmpty()) {
                val thisMonth = LocalDateTime.ofInstant(subTask.value?.planstarttime?.toInstant(), ZoneId.systemDefault())
                    .withDayOfMonth(1).toLocalDate()
                val lastMonth = LocalDateTime.ofInstant(r[0]?.planstarttime?.toInstant(), ZoneId.systemDefault()).withDayOfMonth(1).toLocalDate()
                val thisMonth =
                    LocalDateTime.ofInstant(subTask.value?.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.value = r[0]
                }
@@ -341,21 +416,43 @@
         */
        fun clear() {
            scene.clear()
            subTaskIndex = 0
            subTaskList.clear()
            userInfo.clear()
            userInfoTZ.clear()
            baseScene.clear()
            problemTypes.clear()
            ledgerCount.clear()
            ledgerRecords.clear()
            clearSubtask()
        }
        fun clearSubtask() {
            subTask.clear()
            inspection.clear()
            baseScene.clear()
            problems.clear()
            evaluation.clear()
            itemevaluationList.clear()
            problemTypes.clear()
            baseRule.clear()
            totalScore.clear()
            topItems.clear()
            topRules.clear()
            baseRule.clear()
            rules.clear()
            ledgerCount.clear()
            ledgerRecords.clear()
        }
        /**
         * 切换至下一个巡查任务
         * 一个场景可能同时存在多个巡查任务[subTaskList],需要对每个巡查任务分别进行评分
         * @return 是否有下一个巡查任务
         */
        fun gotoNextSubTask(): Boolean {
            return if (subTaskIndex + 1 < (subTaskList.value?.size ?: 0)) {
                clearSubtask()
                subTaskIndex++
                true
            } else {
                false
            }
        }
        /**
@@ -384,12 +481,13 @@
                if (defaultValue != null) {
                    return defaultValue
                } else if (!fetched && _value == null) {
                    _value = if (cacheMap.containsKey(key) && LocalDateTime.now().minusDays(1).isBefore(cacheTimeStamp)) {
                        cacheMap[key]
                    } else {
                        cacheTimeStamp = LocalDateTime.now()
                        fetch(defaultValue, cacheMap)
                    }
                    _value =
                        if (cacheMap.containsKey(key) && LocalDateTime.now().minusDays(1).isBefore(cacheTimeStamp)) {
                            cacheMap[key]
                        } else {
                            cacheTimeStamp = LocalDateTime.now()
                            fetch(defaultValue, cacheMap)
                        }
                    fetched = true
                }
                return _value