From 196bb14112448857a885e32dc4149e308e00b01a Mon Sep 17 00:00:00 2001
From: feiyu02 <risaku@163.com>
Date: 星期四, 15 八月 2024 11:57:15 +0800
Subject: [PATCH] 2024.8.15 各项修正

---
 src/main/kotlin/cn/flightfeather/supervision/lightshare/service/Impl/EvaluationServiceImpl.kt |  656 +++++++++++++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 589 insertions(+), 67 deletions(-)

diff --git a/src/main/kotlin/cn/flightfeather/supervision/lightshare/service/Impl/EvaluationServiceImpl.kt b/src/main/kotlin/cn/flightfeather/supervision/lightshare/service/Impl/EvaluationServiceImpl.kt
index d527077..a2f7cb1 100644
--- a/src/main/kotlin/cn/flightfeather/supervision/lightshare/service/Impl/EvaluationServiceImpl.kt
+++ b/src/main/kotlin/cn/flightfeather/supervision/lightshare/service/Impl/EvaluationServiceImpl.kt
@@ -1,33 +1,37 @@
 package cn.flightfeather.supervision.lightshare.service.Impl
 
 import cn.flightfeather.supervision.common.score.AutoScore
-import cn.flightfeather.supervision.domain.entity.Company
-import cn.flightfeather.supervision.domain.entity.Evaluation
-import cn.flightfeather.supervision.domain.entity.Evaluationrule
-import cn.flightfeather.supervision.domain.entity.OverallEvaluation
+import cn.flightfeather.supervision.domain.entity.*
 import cn.flightfeather.supervision.domain.enumeration.AssessmentRuleType
 import cn.flightfeather.supervision.domain.enumeration.SceneType
 import cn.flightfeather.supervision.domain.mapper.*
+import cn.flightfeather.supervision.domain.repository.EvaluationRep
 import cn.flightfeather.supervision.infrastructure.utils.DateUtil
+import cn.flightfeather.supervision.infrastructure.utils.UUIDGenerator
+import cn.flightfeather.supervision.domain.repository.UserConfigRep
 import cn.flightfeather.supervision.lightshare.service.EvaluationService
-import cn.flightfeather.supervision.lightshare.vo.AssessmentGradeVo
-import cn.flightfeather.supervision.lightshare.vo.AssessmentSearchCondition
-import cn.flightfeather.supervision.lightshare.vo.CreditInfoVo
+import cn.flightfeather.supervision.lightshare.service.ItemevaluationService
+import cn.flightfeather.supervision.lightshare.vo.*
 import com.github.pagehelper.PageHelper
 import org.springframework.stereotype.Service
 import tk.mybatis.mapper.entity.Example
+import java.time.LocalDateTime
 import java.util.*
 import javax.servlet.http.HttpServletResponse
 
 @Service
 class EvaluationServiceImpl(
-        val evaluationMapper: EvaluationMapper,
-        val evaluationruleMapper: EvaluationruleMapper,
-        val userinfoMapper: UserinfoMapper,
-        val baseInfoMapper: BaseInfoMapper,
-        val companyMapper: CompanyMapper,
-        val overallEvaluationMapper: OverallEvaluationMapper,
-        val autoScore: AutoScore
+    val evaluationMapper: EvaluationMapper,
+    val evaluationruleMapper: EvaluationruleMapper,
+    val evaluationsubruleMapper: EvaluationsubruleMapper,
+    val userinfoMapper: UserinfoMapper,
+    val baseInfoMapper: BaseInfoMapper,
+    val companyMapper: CompanyMapper,
+    val overallEvaluationMapper: OverallEvaluationMapper,
+    val autoScore: AutoScore,
+    val itemevaluationService: ItemevaluationService,
+    private val userConfigRep: UserConfigRep,
+    private val evaluationRep: EvaluationRep,
 ) : EvaluationService {
 
     override fun findOne(id: String): Evaluation = evaluationMapper.selectByPrimaryKey(id)
@@ -36,7 +40,7 @@
 
     override fun save(evaluation: Evaluation): Int {
         updateRank(evaluation, true)
-        return  evaluationMapper.insert(evaluation)
+        return evaluationMapper.insert(evaluation)
     }
 
     override fun update(evaluation: Evaluation): Int {
@@ -46,17 +50,25 @@
 
     override fun delete(id: String): Int = evaluationMapper.deleteByPrimaryKey(id)
 
-    override fun getTotalPoints(userId: String, evaluatorType: Int, startTime: String, endTime: String, sceneTypeId: Int?, erGuid: String?, eId: String?): List<Evaluation> {
+    override fun getTotalPoints(
+        userId: String,
+        evaluatorType: Int,
+        startTime: String,
+        endTime: String,
+        sceneTypeId: Int?,
+        erGuid: String?,
+        eId: String?,
+    ): List<Evaluation> {
         val example = Example(Evaluation::class.java)
         val criteria = example.createCriteria()
-        val startDate = DateUtil().StringToDate(startTime)
-        val endDate = DateUtil().StringToDate(endTime)
+        val startDate = DateUtil.StringToDate(startTime)
+        val endDate = DateUtil.StringToDate(endTime)
         criteria.andEqualTo("iguid", userId)
-                .andBetween("createdate", startDate, endDate)
+            .andBetween("createdate", startDate, endDate)
         example.and(example.createCriteria().apply {
             if (evaluatorType == 0) {
                 orEqualTo("evaluatorrealname", evaluatorType.toString())
-                        .orIsNull("evaluatorrealname")
+                    .orIsNull("evaluatorrealname")
             } else {
                 andEqualTo("evaluatorrealname", evaluatorType.toString())
             }
@@ -65,23 +77,65 @@
         eId?.let { criteria.andEqualTo("guid", eId) }
         sceneTypeId?.let {
             example.and(example.createCriteria()
-                    .orEqualTo("scensetypeid", sceneTypeId.toByte())
-                    .orIsNull("scensetypeid")
+                .orEqualTo("scensetypeid", sceneTypeId.toByte())
+                .orIsNull("scensetypeid")
             )
         }
 
         return evaluationMapper.selectByExample(example)
     }
 
-    override fun getHistoryPoint(userId: String, page: Int, per_page: Int, response: HttpServletResponse): List<AssessmentGradeVo> {
+    override fun getHistoryPoint(
+        userId: String,
+        page: Int,
+        per_page: Int,
+        platform: String?,
+        period: String?,
+        response: HttpServletResponse,
+    ): List<AssessmentGradeVo> {
         val userInfo = userinfoMapper.selectByPrimaryKey(userId) ?: return emptyList()
+        // FIXME: 2022/11/8 涓存椂娣诲姞杩囧害鍔熻兘锛屾彁渚涘井淇″皬绋嬪簭鍓嶇姹戒慨绫诲瀷鐨勫満鏅崟鐙殑璇勪及娓呭崟 锛屽満鏅被鍨嬩负 -7
+        var sceneType = userInfo.extension2
+        if (platform == "weixin" && sceneType == SceneType.VehicleRepair.value.toString()) {
+            sceneType = "-7"
+        }
+        //璇勫垎瑙勫垯涓嬬殑鍒嗙骇銆佺瓑绾ч鑹插強绛夌骇璇勮
+        val pointLevel = mutableListOf<Pair<Int, Int>>()
+        val evaluateLevel = mutableListOf<String>()
+        val creditTexts = mutableListOf<String>()
+        val levelColors = mutableListOf<String>()
+        val rule = evaluationruleMapper.selectByExample(Example(Evaluationrule::class.java).apply {
+            createCriteria().andEqualTo("scensetypeid", sceneType)
+                .andEqualTo("ruletype", "0")
+        })
+        if (rule.isNotEmpty()) {
+            val r = rule[0]
+            r.extension1?.split("#")?.forEach {
+                val pStr = it.split(",")
+                pointLevel.add(Pair(pStr[0].toInt(), pStr[1].toInt()))
+            }
+            r.extension2?.split("#")?.forEach {
+                evaluateLevel.add(it)
+            }
+            r.extension3?.split("#")?.forEach {
+                creditTexts.add(it)
+            }
+            r.remark?.split(";")?.forEach {
+                levelColors.add(it)
+            }
+        }
         val example = Example(Evaluation::class.java).apply {
             createCriteria().andEqualTo("iguid", userId)
-                    .andEqualTo("ertype", AssessmentRuleType.Total.value.toByte())
+                .andEqualTo("ertype", AssessmentRuleType.Total.value.toByte())
+                .apply {
+                    period?.let { andEqualTo("scensename", it) }
+                }
             //鏍规嵁璇勪及浜虹殑绫诲瀷杩涜绛涢�夛紝鑷瘎鍜屽畼鏂硅瘎鍒嗗垎寮�鎺掑悕
-            and(createCriteria().orIsNull("evaluatorrealname")
-                    .orEqualTo("evaluatorrealname", 0))
-            orderBy("updatedate").desc()
+            and(
+                createCriteria().orIsNull("evaluatorrealname")
+                    .orEqualTo("evaluatorrealname", 0)
+            )
+            orderBy("createdate").desc()
         }
         val counts = evaluationMapper.selectCountByExample(example)
         val totalPage = Math.ceil(counts.toDouble() / per_page.toDouble()).toInt()
@@ -96,53 +150,62 @@
             resultList.add(AssessmentGradeVo().apply {
                 this.userId = userId
                 userRealName = userInfo.realname
-                sceneType = userInfo.extension2?.toIntOrNull() ?: SceneType.NoType.value
+                this.sceneType = userInfo.extension2?.toIntOrNull() ?: SceneType.NoType.value
                 tPGuid = it.guid
                 tPRuleGuid = it.stguid
                 totalPoint = it.resultscorebef?.toInt() ?: 0
-                level = getEvaluationLevel(tPRuleGuid, totalPoint)
+                val l = getEvaluationLevel(totalPoint, pointLevel, evaluateLevel, creditTexts, levelColors)
+                level = l["evaluateLevel"]
+                color = l["color"]
+                creditText = l["creditText"]
                 rank = it.promissednum ?: 1
-                updateDate = it.updatedate
+                updateDate = it.createdate
+                this.period = it.scensename
             })
         }
 
         return resultList
     }
 
-    override fun getCreditInfo(userId: String): CreditInfoVo {
-        val userinfo = userinfoMapper.selectByPrimaryKey(userId)
+    override fun getCreditInfo(userId: String, period: String?): CreditInfoVo {
+        val userinfo = userinfoMapper.selectByPrimaryKey(userId) ?: return CreditInfoVo()
         val baseInfo = baseInfoMapper.selectByPrimaryKey(userId)
-        val company = companyMapper.selectByPrimaryKey(baseInfo.ciGuid)
-
+        val company = baseInfo?.let { companyMapper.selectByPrimaryKey(it.ciGuid) }
 
         val result = CreditInfoVo(
-                userId,
-                baseInfo.biName,
-                userinfo.extension2?.toIntOrNull() ?: SceneType.NoType.value,
-                baseInfo.ciName,
-                baseInfo.biManagementCompany,
-                baseInfo.biContact,
-                baseInfo.biTelephone,
-                baseInfo.biAddress,
-                district = company.ciDistrictName,
-                town = company.ciTownName
+            userId,
+            baseInfo?.biName,
+            userinfo?.extension2?.toIntOrNull() ?: SceneType.NoType.value,
+            baseInfo?.ciName,
+            baseInfo?.biManagementCompany,
+            baseInfo?.biContact,
+            baseInfo?.biTelephone,
+            baseInfo?.biAddress,
+            district = company?.ciDistrictName,
+            town = company?.ciTownName
         )
 
         val rule = evaluationruleMapper.selectByExample(Example(Evaluationrule::class.java).apply {
             val c = createCriteria()
-            userinfo.extension2?.toByteOrNull()?.let {
+            userinfo?.extension2?.toByteOrNull()?.let {
                 c.andEqualTo("scensetypeid", it)
-                        .andEqualTo("ruletype", "0")
+                    .andEqualTo("ruletype", "0")
             }
         }).takeIf { it.isNotEmpty() }?.get(0) ?: return result
 
+        val periodList = DateUtil.getSuitablePeriod(period)
         val overallEvaluation = overallEvaluationMapper.selectByExample(Example(OverallEvaluation::class.java).apply {
-            createCriteria().andEqualTo("biGuid", baseInfo.biGuid)
+            createCriteria().andEqualTo("biGuid", userinfo?.guid)
+                .apply {
+                    if (periodList.isNotEmpty()) {
+                        andIn("oePeriod", periodList)
+                    }
+                }
             orderBy("oePublishTime").desc()
         }).takeIf { it.isNotEmpty() }?.get(0) ?: return result
 
         //璇勫垎璁板綍瀵瑰簲 鐨勬墦鍒嗗懆鏈� 锛屾牸寮忎负 YYYY/M-M锛屾煇骞存煇鏈堣嚦鏌愭湀
-        val period = overallEvaluation.oePeriod
+//        val period = overallEvaluation.oePeriod
         //璇勫垎绛夌骇鍒嗘暟鐣岄檺锛屾牸寮忎负  0,59#60,89#90,100锛岃〃绀烘湁涓変釜鍒嗘暟娈�
         val levels = rule.extension1?.split("#")?.asReversed()
         //璇勫垎绛夌骇瀵瑰簲鐨勬弿杩帮紝鏍煎紡涓� 杈冨樊#涓�鑸�#浼樼锛屽搴斾笁涓垎鏁版鐨勬弿杩�
@@ -174,8 +237,8 @@
                 else -> null
             }
 
-        result.publishTime = DateUtil().DateToString(overallEvaluation.oePublishTime, "YYYY骞碝M鏈�")
-        result.updateTime = DateUtil().DateToString(overallEvaluation.oeUpdateTime, "YYYY骞碝M鏈�")
+        result.publishTime = DateUtil.DateToString(overallEvaluation.oePublishTime, "YYYY骞碝M鏈�")
+        result.updateTime = DateUtil.DateToString(overallEvaluation.oeUpdateTime, "YYYY骞碝M鏈�")
         result.creditLevel = creditLevel
         result.creditLevelDes = creditLevelDes
         result.codeLevel = codeLevel
@@ -184,15 +247,51 @@
         return result
     }
 
+    override fun getCreditCount(userId: String, condition: UserSearchCondition): BaseResponse<CountVo> {
+        if (condition.sceneTypes.size != 1) return BaseResponse(false, "鍙敮鎸佸崟涓満鏅被鍨嬬殑鏌ヨ")
 
-    override fun getAssessments(userId: String, condition: AssessmentSearchCondition, page: Int, perPage: Int, response: HttpServletResponse): List<AssessmentGradeVo> {
+        val config = userConfigRep.getUserConfigBySubType(userId)
+        val condition2 = UserSearchCondition.fromUserConfig(config, condition)
+
+        // 缁跨爜
+        var level0 = 0
+        // 榛勭爜
+        var level1 = 0
+        // 绾㈢爜
+        var level2 = 0
+        condition2.period = condition2.period
+            ?: overallEvaluationMapper.getLatestPeriod(condition2)
+                    ?: return BaseResponse(false, "鏃犺褰�")
+
+        overallEvaluationMapper.getCreditCount(condition2).forEach {
+            when (it.oeCodeLevel?.toInt()) {
+                0 -> level0++
+                1 -> level1++
+                2 -> level2++
+            }
+        }
+        val result = CountVo().apply {
+            tag = condition2.period
+            countList.addAll(listOf(level0, level1, level2))
+        }
+
+        return BaseResponse(true, data = result)
+    }
+
+    override fun getAssessments(
+        userId: String,
+        condition: AssessmentSearchCondition,
+        page: Int,
+        perPage: Int,
+        response: HttpServletResponse,
+    ): List<AssessmentGradeVo> {
         val user = userinfoMapper.selectByPrimaryKey(userId)
-        val districtName = user.extension1
+        val districtName = user?.extension1
 
         val p = PageHelper.startPage<Evaluation>(page, perPage)
 
         val result = mutableListOf<AssessmentGradeVo>()
-        evaluationMapper.getAssessments(districtName?:"", condition.searchText, "", condition.sceneTypes).forEach {
+        evaluationMapper.getAssessments(districtName ?: "", condition.searchText, "", condition.sceneTypes).forEach {
             //姝ゅ鐢变簬 PageHelper 鐨勫垎椤垫搷浣滐紝浼氬鑷寸粨鏋滅殑鏁伴噺寮哄埗涓� perPage鐨勫�硷紝鍖呮嫭null
             if (it != null) {
                 result.add(AssessmentGradeVo().apply {
@@ -202,7 +301,7 @@
                     tPGuid = it["tPGuid"] as String?
                     tPRuleGuid = it["tPRuleGuid"] as String?
                     totalPoint = (it["totalPoint"] as String?)?.toIntOrNull() ?: 0
-                    level = getEvaluationLevel(tPRuleGuid, totalPoint)
+                    level = getEvaluationLevel(totalPoint)["evaluateLevel"]
                     rank = (it["rank"] as Int?) ?: -1
                     updateDate = it["updateDate"] as Date?
                 })
@@ -220,29 +319,453 @@
         return response
     }
 
+    override fun uploadScore(
+        userId: String,
+        period: String,
+        ruleId: String?,
+        itemList: List<Pair<String, String>>,
+    ): Boolean {
+        val userinfo = userinfoMapper.selectByPrimaryKey(userId) ?: return false
+        var sceneType = userinfo.extension2
+
+        // FIXME: 2022/11/8 涓存椂娣诲姞杩囧害鍔熻兘锛屾彁渚涘井淇″皬绋嬪簭鍓嶇姹戒慨绫诲瀷鐨勫満鏅崟鐙殑璇勪及娓呭崟 锛屽満鏅被鍨嬩负 -7
+        if (sceneType == SceneType.VehicleRepair.value.toString()) {
+            sceneType = "-7"
+        }
+
+        //璇ュ満鏅被鍨嬩笅鐨勬�诲緱鍒嗚鍒�
+        val tRule = evaluationruleMapper.selectByExample(Example(Evaluationrule::class.java).apply {
+            createCriteria().andEqualTo("scensetypeid", sceneType)
+                .andEqualTo("ruletype", AssessmentRuleType.Total.value)
+                .andIsNull("tasktypeid")
+        }).takeIf { it.isNotEmpty() }?.get(0)
+
+        var totalPoint = 0
+        var tEvaluation = Evaluation()
+
+        evaluationruleMapper.selectByExample(Example(Evaluationrule::class.java).apply {
+            createCriteria().andEqualTo("scensetypeid", sceneType)
+                .andNotEqualTo("ruletype", AssessmentRuleType.Total.value)
+                .andIsNull("tasktypeid")
+        }).forEach { rule ->
+            val subRules = evaluationsubruleMapper.selectByExample(Example(Evaluationsubrule::class.java).apply {
+                createCriteria().andEqualTo("erguid", rule.guid)
+            })
+            if (subRules.isEmpty()) return@forEach
+            //鎬诲垎璁板綍
+            val evaluation = Evaluation().apply { guid = UUIDGenerator.generate16ShortUUID() }
+            //鍏蜂綋椤瑰緱鍒嗚褰�
+            val itemEvaluationList = mutableListOf<Itemevaluation>()
+            //鎬诲緱鍒�
+            var totalScore = 0
+
+            //鏍规嵁涓婁紶鐨勯�夋嫨鐨勬潯鐩紝鐢熸垚瀵瑰簲鐨勮瘎鍒嗚褰�
+            itemList.forEach {
+                for (s in subRules) {
+                    if (s.guid == it.first) {
+                        var fatherId: String?
+                        var subRule = s
+                        var score = it.second
+                        // 鏍规嵁涓婁紶鐨勬潯鐩敓鎴愬搴旂殑璇ユ潯鐩緱鍒嗚褰曚互鍙婂叾鎵�鏈夌殑鐖堕」鏉$洰鐨勫緱鍒嗚褰�
+                        do {
+                            val result =
+                                calculateScore(rule, evaluation.guid!!, userId, subRule, score, itemEvaluationList)
+                            fatherId = result.first
+                            for (s1 in subRules) {
+                                if (s1.guid == fatherId) {
+                                    subRule = s1
+                                    score = result.second ?: "0"
+                                    break
+                                }
+                            }
+                        } while (fatherId?.isNotBlank() == true)
+                        break
+                    }
+                }
+            }
+
+            //瑙勫垯璁惧畾鐨勬渶澶у垎鍊�
+            var maxScore = 0
+            subRules.forEach subRules@{ s ->
+                //璁$畻搴斿緱鎬诲垎锛宐asic_score锛氳〃绀哄熀纭�鍒嗭紝璁$畻鍦ㄥ簲寰楁�诲垎鍐咃紱addition_score琛ㄧず鍔犲垎锛屼笉璁$畻鍦ㄥ唴
+                if (s.ertype == 2 && s.extension1 != "addition_score") {
+                    maxScore += s.maxscore ?: 0
+                }
+                //瑙勫垯宸茬粡鎵撳垎瀹屾瘯锛屽垯璺宠繃
+                itemEvaluationList.forEach { i ->
+                    if (i.esrguid == s.guid) {
+                        //濡傛灉鏄渶澶х殑2绾ц瘎浼拌鍒欙紝璁$畻宸茶幏寰楃殑鎬诲垎
+                        if (s.ertype == 2) {
+                            totalScore += i.value?.toInt() ?: 0
+                        }
+                        return@subRules
+                    }
+                }
+                //鍚﹀垯濡傛灉鍓嶇鍏佽涓嶇敤鍏ㄩ儴璇勫垎锛屾柊澧炰竴鏉℃湭璇勫垎鐨勮褰�
+                itemEvaluationList.add(Itemevaluation().apply {
+                    ieguid = UUIDGenerator.generate16ShortUUID()
+                    iguid = userId
+                    sguid = evaluation.guid
+                    erguid = rule.guid
+                    rulename = rule.rulename
+                    ruletype = rule.ruletype?.toInt()
+                    ertype = s.ertype
+                    esrguid = s.guid
+                    name = s.itemname
+                    value = "0"
+                    extension1 = "false"
+                })
+            }
+            //璁$畻鎬诲垎
+            evaluation.apply {
+                iguid = userId
+                stguid = rule.guid
+                scensetypeid = userinfo.extension2?.toByte()
+                ertype = rule.ruletype?.toByte()
+                scensename = period
+                evaluatorguid = userId
+                evaluatorusername = userinfo.acountname
+                evaluatorrealname = "0"
+                resultscorebef = (maxScore + totalScore).toString()
+                createdate = Date()
+                updatedate = Date()
+            }
+            save(evaluation)
+            itemevaluationService.savelist(itemEvaluationList)
+            tEvaluation = evaluation
+
+            totalPoint += evaluation.resultscorebef?.toInt() ?: 0
+        }
+
+        //璁$畻鎵�鏈夎〃鐨勬�诲垎
+        tRule?.let {
+            tEvaluation.apply {
+                guid = UUIDGenerator.generate16ShortUUID()
+                stguid = it.guid
+                ertype = it.ruletype?.toByte()
+                resultscorebef = totalPoint.toString()
+            }
+            save(tEvaluation)
+        }
+        return true
+    }
+
+    override fun updateScore(
+        userId: String,
+        period: String,
+        ruleId: String?,
+        itemList: List<Pair<String, String>>,
+    ): Boolean {
+        TODO("Not yet implemented")
+    }
+
+    override fun getDetail(userId: String, period: String): GradeDetailVo {
+        val list = period.split("-")
+        var endM = list[1].toInt()
+        val list2 = list[0].split("/")
+        val year = list2[0].toInt()
+        var startM = list2[1].toInt()
+        var st = LocalDateTime.of(year, startM, 1, 0, 0, 0, 0)
+        var et = LocalDateTime.of(year, endM, 1, 0, 0, 0, 0).plusMonths(1)
+
+        val result = GradeDetailVo()
+
+        val userinfo = userinfoMapper.selectByPrimaryKey(userId) ?: return result
+        var sceneType = SceneType.getByValue(userinfo.extension2?.toIntOrNull())
+        // FIXME: 2022/11/8 涓存椂娣诲姞杩囧害鍔熻兘锛屾彁渚涘井淇″皬绋嬪簭鍓嶇姹戒慨绫诲瀷鐨勫満鏅崟鐙殑璇勪及娓呭崟 锛屽満鏅被鍨嬩负 -7
+        if (sceneType == SceneType.VehicleRepair) {
+            sceneType = SceneType.VehicleRepair
+        }
+
+        //璇ュ満鏅被鍨嬩笅鐨勬墍鏈夊緱鍒嗚鍒�
+        val rule0 = evaluationRep.findRule(sceneType) ?: return result
+        //鑾峰緱寰楀垎瑙勫垯涓畾涔夌殑璇勪及鍛ㄦ湡锛堝崟浣嶏細鏈堬級
+        val rulePeriod = rule0.scensesubtypeid?.toInt() ?: 1
+        DateUtil.getStartMonthByPeriod(startM, rulePeriod)?.let {
+            startM = it
+            endM = startM + rulePeriod - 1
+            st = LocalDateTime.of(year, startM, 1, 0, 0, 0, 0)
+            et = LocalDateTime.of(year, endM, 1, 0, 0, 0, 0).plusMonths(1)
+        }
+        //鑾峰彇鏈�鏂颁竴鏈熺殑鎬诲垎
+        val latestEva = evaluationRep.findLatest(rule0.guid, userId, period, st, et)
+        //鏍规嵁鎬诲垎鍛ㄦ湡鑾峰彇鐩稿悓鍛ㄦ湡鍐呯殑瀛愯瘎鍒�
+        val latestSubEva = evaluationMapper.selectByExample(Example(Evaluation::class.java).apply {
+            createCriteria().andEqualTo("iguid", userId)
+                .andEqualTo("scensename", latestEva?.scensename)
+                .andNotEqualTo("ertype", AssessmentRuleType.Total.value.toByte())
+        })
+        latestEva?.let {
+            latestSubEva.add(it)
+        }
+        latestSubEva.forEach { e ->
+            val rule = evaluationruleMapper.selectByPrimaryKey(e.stguid)
+            //鎬诲垎瑙勫垯锛岀敓鎴愭�讳綋璇勪环
+            if (rule.ruletype == AssessmentRuleType.Total.value.toString()) {
+                rule?.apply {
+                    val pointLevel = mutableListOf<Pair<Int, Int>>()
+                    val evaluateLevel = mutableListOf<String>()
+                    val creditTexts = mutableListOf<String>()
+                    val levelColors = mutableListOf<String>()
+                    extension1?.split("#")?.forEach {
+                        val pStr = it.split(",")
+                        pointLevel.add(Pair(pStr[0].toInt(), pStr[1].toInt()))
+                    }
+                    extension2?.split("#")?.forEach {
+                        evaluateLevel.add(it)
+                    }
+                    extension3?.split("#")?.forEach {
+                        creditTexts.add(it)
+                    }
+                    remark?.split(";")?.forEach {
+                        levelColors.add(it)
+                    }
+
+                    val l = getEvaluationLevel(e.resultscorebef?.toInt()
+                        ?: 0, pointLevel, evaluateLevel, creditTexts, levelColors)
+                    result.apply {
+                        creditText = l["creditText"]
+                        score = e.resultscorebef?.toInt() ?: 0
+                        rank = e.promissednum
+                        level = l["evaluateLevel"]
+                        this.period = latestEva?.scensename
+                        time = e.createdate
+//                    color = l["color"]
+                    }
+                }
+            }
+            //鍏蜂綋璇勪及琛�
+            else {
+                val subRules = evaluationsubruleMapper.selectByExample(Example(Evaluationsubrule::class.java).apply {
+                    createCriteria().andEqualTo("erguid", rule.guid)
+                })
+                val itemEvaluations = itemevaluationService.getItemEvaluationList(e.guid!!)
+                val ruleMap = result.loseScore
+                val ruleMap2 = result.scoring
+                //鍒嗙被鍒殑寰楀垎(姣忎釜璇勫垎琛ㄧ殑璇勫垎澶ч」鐨勫緱鍒�)鍜屽け鍒嗘潯鐩�
+                itemEvaluations.sortedBy { it.ertype }.forEach { item ->
+                    val value = item.value?.toInt() ?: 0
+                    when (item.ertype) {
+                        //鍒嗙被鍒殑寰楀垎
+                        2 -> {
+                            ruleMap[item.name] = mutableMapOf()
+                            ruleMap2[item.name] = mutableMapOf()
+                            for (s in subRules) {
+                                if (s.guid == item.esrguid) {
+                                    val score = if (value <= 0) {
+                                        s.maxscore?.plus(value)
+                                    } else {
+                                        value
+                                    }
+                                    result.classScore.add(Triple(item.name ?: "",
+                                        s.maxscore?.toString() ?: "0",
+                                        score.toString()))
+                                    break
+                                }
+                            }
+                        }
+                        3 -> {
+                            if (value != 0 && item.extension1 == "true") {
+                                val map = if (value > 0) ruleMap2 else ruleMap
+                                for (s in subRules) {
+                                    if (s.guid == item.esrguid) {
+                                        if (!map.containsKey(s.fathername)) {
+                                            map[s.fathername] = mutableMapOf()
+                                        }
+                                        map[s.fathername]?.put(item.name, mutableListOf())
+                                        break
+                                    }
+                                }
+                            }
+                        }
+                        //澶卞垎鎴栧緱鍒嗘潯鐩�
+                        4 -> {
+                            if (value != 0 && item.extension1 == "true") {
+                                val map = if (value > 0) ruleMap2 else ruleMap
+                                for (s in subRules) {
+                                    if (s.guid == item.esrguid) {
+                                        val d =
+                                            Triple(
+                                                item.name ?: "",
+//                                                if ((item.value?.toInt() ?: 0) < 0) {
+//                                                    item.value ?: "0"
+//                                                } else {
+//                                                    (item.value?.toInt() ?: 0).minus(s.maxscore ?: 0).toString()
+//                                                },
+                                                item.value ?: "0",
+                                                s.remark ?: ""
+                                            )
+                                        val rule3Name = s.fathername
+                                        for (s1 in subRules) {
+                                            if (s1.itemname == rule3Name) {
+                                                val rule2Name =
+                                                    //鐖秈d涓虹┖锛岃鏄庢4绾ц瘎浼伴」鐩存帴闄勫睘浜�2绾ц瘎浼伴」
+                                                    if (s1.fathername.isNullOrBlank()) {
+                                                        map[rule3Name]?.put(rule3Name, mutableListOf())
+                                                        rule3Name
+                                                    } else {
+                                                        s1.fathername
+                                                    }
+                                                map[rule2Name]?.get(rule3Name)?.add(d)
+                                                break
+                                            }
+                                        }
+
+                                        break
+                                    }
+                                }
+                            }
+                        }
+                    }
+                }
+            }
+
+        }
+
+
+        return result
+    }
+
+    override fun searchGradeList(
+        userId: String,
+        condition: UserSearchCondition,
+        page: Int,
+        perPage: Int,
+    ): BaseResponse<List<CreditInfoVo>> {
+        if (condition.period == null) return BaseResponse(false, "蹇呴』閫夋嫨鍛ㄦ湡")
+        if (condition.sceneTypes.size > 2) return BaseResponse(false, "鍦烘櫙绫诲瀷鍙敮鎸佷竴绉�")
+
+        val config = userConfigRep.getUserConfigBySubType(userId)
+        val condition2 = UserSearchCondition.fromUserConfig(config, condition)
+
+//        val period = condition.period!!
+//        val sceneType = condition.sceneTypes[0]
+//        var district = condition.districtName
+//        val sort = condition.sorts
+//
+//        if (district == null) {
+//            val userInfo = userinfoMapper.selectByPrimaryKey(userId)
+//            district = userInfo?.extension1
+//        }
+        val p = PageHelper.startPage<Evaluation>(page, perPage)
+        val result = evaluationMapper.searchGradeList(condition2).onEach {
+                if (it.score == null) it.score = "/"
+            }
+        return BaseResponse(true, head = DataHead(p.pageNum, p.pages, p.total), data = result)
+    }
+
+    /**
+     * 鏍规嵁涓婁紶鐨勫瓙瑙勫垯鎵e垎鎯呭喌锛岀敓鎴愬瓙椤瑰緱鍒嗚褰�
+     * @param rule 鎬昏鍒�
+     * @param eGuid 鎬诲垎璁板綍id
+     * @param userId 鐢ㄦ埛id
+     * @param subRule 鎵e垎鐨勮鍒�
+     * @param score 鎵e垎
+     * @param itemEvaluationList 寰楀垎璁板綍琛�
+     * @return 璇勫垎椤圭殑鐖秈d鍜屽緱鍒�
+     */
+    private fun calculateScore(
+        rule: Evaluationrule,
+        eGuid: String,
+        userId: String,
+        subRule: Evaluationsubrule,
+        score: String,
+        itemEvaluationList: MutableList<Itemevaluation>,
+    ): Pair<String?, String?> {
+        var result: Pair<String?, String?> = Pair(null, null)
+        var exist = false
+        for (i in itemEvaluationList) {
+            //璁板綍宸插瓨鍦紝璇存槑鏄埗椤圭殑璇勫垎瑙勫垯涓嬬殑鏌愪釜瀛愰」鐨勪竴閮ㄥ垎鎵e垎锛屾墸鍒嗙疮鍔�,骞朵笖涓嶈兘瓒呰繃鐖堕」鐨勪笂闄�
+            if (subRule.guid == i.esrguid) {
+                val s = i.value?.toInt()?.plus(score.toInt()) ?: 0
+                //鍔犲垎妯″紡
+                if (subRule.extension2 == "add_mode") {
+                    if (s > (subRule.maxscore ?: 0)) {
+                        i.value = subRule.maxscore?.toString() ?: "0"
+                    } else {
+                        i.value = s.toString()
+                    }
+                }
+                //榛樿鎯呭喌涓哄噺鍒嗘ā寮�
+                else {
+                    //鍏佽鎵i櫎鐨勫垎鏁版渶灏忓�硷紙璐熸暟锛�
+                    val min = subRule.minscore?.minus(subRule.maxscore ?: 0) ?: 0
+                    if (s < min) {
+                        i.value = min.toString()
+                    } else {
+                        i.value = s.toString()
+                    }
+                }
+                result = Pair(subRule.fatherid, i.value)
+                exist = true
+                break
+            }
+        }
+        //璁板綍涓嶅瓨鍦ㄦ椂锛岃鏄庢槸瀛愰」绗竴娆℃墸鍒嗭紝鏂板涓�鏉¤褰�
+        if (!exist) {
+            itemEvaluationList.add(Itemevaluation().apply {
+                ieguid = UUIDGenerator.generate16ShortUUID()
+                iguid = userId
+                sguid = eGuid
+                erguid = rule.guid
+                rulename = rule.rulename
+                ruletype = rule.ruletype?.toInt()
+                ertype = subRule.ertype
+                esrguid = subRule.guid
+                name = subRule.itemname
+                value = score
+                extension1 = "true"
+            })
+            result = Pair(subRule.fatherid, score)
+        }
+        return result
+    }
+
     /**
      * 鏍规嵁鍒嗘暟鑾峰彇绛夌骇
      */
-    private fun getEvaluationLevel(ruleId:String?, score: Int): String {
-        return when (score) {
-            in 0..40 -> "鏋佸樊"
-            in 41..64 -> "杈冨樊"
-            in 65..79 -> "涓�鑸�"
-            in 80..94 -> "鑹ソ"
-            in 95..Int.MAX_VALUE -> "浼樼"
-            else -> "鏋佸樊"
+    private fun getEvaluationLevel(
+        score: Int,
+        pointLevel: MutableList<Pair<Int, Int>> = mutableListOf(),
+        evaluateLevel: MutableList<String> = mutableListOf(),
+        creditTexts: MutableList<String> = mutableListOf(),
+        levelColors: MutableList<String> = mutableListOf(),
+    ): Map<String, String> {
+        val result = mutableMapOf<String, String>()
+        if (pointLevel.isEmpty() || evaluateLevel.isEmpty() || creditTexts.isEmpty() || levelColors.isEmpty()) {
+            result["evaluateLevel"] = when (score) {
+                in 0..40 -> "鏋佸樊"
+                in 41..64 -> "杈冨樊"
+                in 65..79 -> "涓�鑸�"
+                in 80..94 -> "鑹ソ"
+                in 95..Int.MAX_VALUE -> "浼樼"
+                else -> "鏋佸樊"
+            }
+        } else {
+            for (i in pointLevel.indices) {
+                if (score in pointLevel[i].first..pointLevel[i].second ||
+                    (i == pointLevel.size - 1 && score > pointLevel[i].second)
+                ) {
+                    result["color"] = levelColors[i % levelColors.size]
+                    result["creditText"] = creditTexts[i % creditTexts.size]
+                    result["evaluateLevel"] = evaluateLevel[i % evaluateLevel.size]
+                }
+            }
         }
+        return result
+
     }
 
     private fun updateRank(evaluation: Evaluation, isInsert: Boolean) {
         if (evaluation.ertype == AssessmentRuleType.Total.value.toByte()) {
             val eList = evaluationMapper.selectByExample(Example(Evaluation::class.java).apply {
                 createCriteria().andEqualTo("scensename", evaluation.scensename)
-                        .andIsNotNull("scensename")
+                    .andIsNotNull("scensename")
+                    .andEqualTo("ertype", AssessmentRuleType.Total.value)
                 //鏍规嵁璇勪及浜虹殑绫诲瀷杩涜绛涢�夛紝鑷瘎鍜屽畼鏂硅瘎鍒嗗皝闈㈠紑鎺掑悕
                 if (evaluation.evaluatorrealname == null || evaluation.evaluatorrealname == "0") {
                     and(createCriteria().orIsNull("evaluatorrealname")
-                            .orEqualTo("evaluatorrealname", evaluation.evaluatorrealname))
+                        .orEqualTo("evaluatorrealname", evaluation.evaluatorrealname))
                 } else {
                     and(createCriteria().andEqualTo("evaluatorrealname", evaluation.evaluatorrealname))
                 }
@@ -262,10 +785,9 @@
             }
             val updateList = mutableListOf<Evaluation>()
             for (i in eList.indices) {
-                if (eList[i].guid == evaluation.guid){
+                if (eList[i].guid == evaluation.guid) {
                     evaluation.promissednum = i + 1
-                }
-                else if (eList[i].promissednum != i + 1) {
+                } else if (eList[i].promissednum != i + 1) {
                     eList[i].promissednum = i + 1
                     updateList.add(eList[i])
                 }

--
Gitblit v1.9.3