feiyu02
2025-09-18 baf2cc2ce3dfd1235c012a3750132769fcd9ad2f
src/main/kotlin/cn/flightfeather/supervision/lightshare/service/impl/TaskServiceImpl.kt
@@ -1,5 +1,6 @@
package cn.flightfeather.supervision.lightshare.service.impl
import cn.flightfeather.supervision.common.exception.BizException
import cn.flightfeather.supervision.domain.ds1.entity.Problemlist
import cn.flightfeather.supervision.domain.ds1.entity.Subtask
import cn.flightfeather.supervision.domain.ds1.entity.Task
@@ -8,10 +9,11 @@
import cn.flightfeather.supervision.domain.ds1.mapper.TaskMapper
import cn.flightfeather.supervision.common.utils.Constant
import cn.flightfeather.supervision.common.utils.DateUtil
import cn.flightfeather.supervision.lightshare.service.EvaluationService
import cn.flightfeather.supervision.lightshare.service.SubtaskService
import cn.flightfeather.supervision.lightshare.service.TaskService
import cn.flightfeather.supervision.lightshare.service.UserinfoService
import cn.flightfeather.supervision.common.utils.UUIDGenerator
import cn.flightfeather.supervision.domain.ds1.repository.SceneRep
import cn.flightfeather.supervision.domain.ds1.repository.SubTaskRep
import cn.flightfeather.supervision.domain.ds1.repository.TaskRep
import cn.flightfeather.supervision.lightshare.service.*
import cn.flightfeather.supervision.lightshare.vo.*
import org.springframework.beans.BeanUtils
import org.springframework.beans.factory.annotation.Autowired
@@ -21,7 +23,12 @@
import kotlin.collections.ArrayList
@Service
class TaskServiceImpl(val taskMapper: TaskMapper) : TaskService {
class TaskServiceImpl(
    val taskMapper: TaskMapper,
    private val taskRep: TaskRep,
    private val subTaskRep: SubTaskRep,
    private val sceneRep: SceneRep,
) : TaskService {
    @Autowired
    lateinit var subtaskService: SubtaskService
@@ -101,7 +108,7 @@
                    townRank.standardRate = it.get("standardRate").toString().toFloat()
                } catch (e: Exception) {
                }
                val s =it.get("hasScoredNum").toString()
                val s = it.get("hasScoredNum").toString()
                try {
                    townRank.hasScoredNum = it.get("hasScoredNum").toString().toInt()
                } catch (e: Exception) {
@@ -161,7 +168,7 @@
        if (type == 0)
            return getAllTop()
        //获取未完成顶层任务及对应日任务列表
        else if (type == 1){
        else if (type == 1) {
            val tmp = getAllTop()
            tmp.forEach {
                if (it.runingstatus != Constant.TaskProgress.RUNINGSTATUS3.text) {
@@ -175,13 +182,13 @@
    }
    //根据日期获取日任务
    override fun findByDate(date: String): TaskVo {
    override fun findByDate(topTaskId: String, date: String): TaskVo {
        val taskVo = TaskVo()
        val example = Example(Task::class.java)
        val criteria = example.createCriteria()
        //gsguid不为空为日任务,日任务开始时间和结束时间在同一天,过滤开始时间就ok
        criteria.andEqualTo("starttime", dateUtil.StringToDate(date.substring(0, 10)))
        criteria.andIsNotNull("tsguid")
        criteria.andEqualTo("starttime", DateUtil.StringToDate(date.substring(0, 10)))
        criteria.andEqualTo("tsguid", topTaskId)
        val tasklist = taskMapper.selectByExample(example)
        if (tasklist.isNotEmpty()) {
            BeanUtils.copyProperties(tasklist[0], taskVo)
@@ -220,10 +227,10 @@
//                .andEqualTo("districtcode", districtCode)
//                .andLessThanOrEqualTo("endtime", date)
//        example.or(
                example.createCriteria().andIsNull("tsguid")
                        .andEqualTo("districtcode", districtCode)
                        .andLessThan("starttime", date)
                        .andGreaterThanOrEqualTo("endtime", date)
        example.createCriteria().andIsNull("tsguid")
            .andEqualTo("districtcode", districtCode)
            .andLessThanOrEqualTo("starttime", date)
            .andGreaterThanOrEqualTo("endtime", date)
//        )
        example.orderBy("starttime").desc()
        val tasklist = taskMapper.selectByExample(example)
@@ -286,8 +293,6 @@
        return if (tasks.isEmpty()) Task() else tasks[0]
    }
    val dateUtil = DateUtil()
    //根据顶层列表,日期,和执行人员ID,用户类型获取日任务
    override fun getDayTaskList(taskVoList: List<TaskVo>, date: String, guid: String, userType: String): List<TaskVo> {
        if (taskVoList.isEmpty()) return taskVoList
@@ -296,9 +301,9 @@
            val criteria = example.createCriteria()
            //构造查询条件
            criteria.andEqualTo("tsguid", it.tguid)
            val dateTemo = dateUtil.StringToDate(date)
            val dateTemo = DateUtil.StringToDate(date)
            //条件过滤
            criteria.andBetween("starttime", dateTemo, dateUtil.addSecond(dateUtil.addMonth(dateTemo!!, 1)!!, 1))
            criteria.andBetween("starttime", dateTemo, DateUtil.addSecond(DateUtil.addMonth(dateTemo!!, 1)!!, 1))
            if (userType == "1") {
                criteria.andLike("executorguids", "%$guid%")
            }
@@ -315,55 +320,64 @@
        return taskVoList
    }
    override fun getDayTask(taskId: String, userId: String, userType: String): List<DayTaskProgressVo> {
        val example = Example(Task::class.java)
        val criteria = example.createCriteria()
        //构造查询条件
        criteria.andEqualTo("tsguid", taskId)
        if (userType == "1") {
            criteria.andLike("executorguids", "%$userId%")
        }
    override fun getDayTask(taskId: String, userId: String?, userType: String): List<DayTaskProgressVo> {
        val resultList = ArrayList<DayTaskProgressVo>()
        // 获取总任务下所有日任务
        val dayTasks = if (userType == "1") {
            taskRep.findDayTasks(taskId, userId)
        } else {
            taskRep.findDayTasks(taskId)
        }
        // 获取总任务下所有的子任务
        val subTasks = subTaskRep.findAll(Subtask().apply { tguid = taskId })
        //根据sql条件查询
        taskMapper.selectByExample(example).forEach {
            val exampleTotal = Example(Subtask::class.java).apply {
                createCriteria().andEqualTo("tsguid", it.tguid)
        dayTasks.forEach {t->
            // 筛选当前日任务下的子任务
            val filterSubTasks = subTasks.filter {s->
                s?.tsguid == t?.tguid
            }
            // 子任务总数
            val total = filterSubTasks.size
            // 子任务完成数
            val complete = filterSubTasks.count {fs->
                fs?.status == Constant.TaskProgress.RUNINGSTATUS3.text
            }
            val total = subtaskMapper.selectCountByExample(exampleTotal)
            val exampleComplete = exampleTotal.apply {
                and().andEqualTo("status", Constant.TaskProgress.RUNINGSTATUS3.text)
            // 获取当日所有的问题
            val subTaskIds = filterSubTasks.map { fs-> fs?.stguid }
            val problemList = if (subTaskIds.isNotEmpty()) {
                problemListMapper.selectByExample(Example(Problemlist::class.java).apply {
                    createCriteria().andIn("stguid", subTaskIds)
                })
            } else{
                emptyList()
            }
            val complete = subtaskMapper.selectCountByExample(exampleComplete)
            var changed = 0
            problemListMapper.findUnchangedCount(it.tguid ?: "").forEach {i ->
                //结果表示该子任务未整改问题数
                if (i == 0) {
                    changed++
                }
            }
            //审核是否完成
            var check = false
            with(subtaskMapper.selectByExample(exampleTotal)) breaking@{
                forEach {
                    problemListMapper.selectByExample(Example(Problemlist::class.java).apply {
                        createCriteria().andEqualTo("stguid", it.stguid)
                    }).forEach {problem ->
                        if (problem.extension3 == Constant.PROBLEM_UNCHECKED) {
                            check = true
                            return@breaking
                        }
            var check = true
            filterSubTasks.forEach {fs ->
                // 筛选每个子任务下的问题未整改数
                problemList.filter { p-> p?.stguid == fs?.stguid }.onEach { pro ->
                    // 当存在至少一个问题没有审核时,当日审核状态为未审核
                    if (pro.extension3 == Constant.PROBLEM_UNCHECKED || pro.extension3 == Constant.CHANGE_UNCHECKED) {
                        check = false
                    }
                }.count { i -> i?.ischanged != true }.let { c ->
                    // 没有未整改问题时,则表示该子任务已经整改完成
                    if (c == 0) {
                        changed++
                    }
                }
            }
           resultList.add(DayTaskProgressVo(
                    it.tguid, it.starttime, taskId, complete, changed, total, check
            resultList.add(DayTaskProgressVo(
                t?.tguid, t?.starttime, taskId, complete, changed, total, check
            ))
        }
@@ -378,9 +392,9 @@
            val criteria = example.createCriteria()
            //构造查询条件
            criteria.andEqualTo("tsguid", it.tguid)
            val dateTemo = dateUtil.StringToDate(date)
            val dateTemo = DateUtil.StringToDate(date)
            //条件过滤
            criteria.andBetween("starttime", dateTemo, dateUtil.addSecond(dateUtil.addMonth(dateTemo!!, 1)!!, 1))
            criteria.andBetween("starttime", dateTemo, DateUtil.addSecond(DateUtil.addMonth(dateTemo!!, 1)!!, 1))
            criteria.andLike("executorguids", "%$guid%")
            //根据sql条件查询
            val taskList = taskMapper.selectByExample(example)
@@ -401,10 +415,10 @@
        if (date.length < 7) {
            return taskVoList
        }
        val startDate = DateUtil().StringToDate(date.substring(0, 7)) ?: return taskVoList
        val startDate = DateUtil.StringToDate(date.substring(0, 7)) ?: return taskVoList
        //查询一个月任务
        var endDate = DateUtil().addMonth(startDate, 1) ?: return taskVoList
        endDate = DateUtil().addSecond(endDate, -1)!!
        var endDate = DateUtil.addMonth(startDate, 1) ?: return taskVoList
        endDate = DateUtil.addSecond(endDate, -1)!!
        //构造查询条件
        val example = Example(Task::class.java)
        val criteria = example.createCriteria()
@@ -453,22 +467,22 @@
//            return taskVoList
//        }
//        //时间转换
//        val d1 = DateUtil().StringToDate(date.substring(0, 7)) ?: return taskVoList
//        val d1 = DateUtil.StringToDate(date.substring(0, 7)) ?: return taskVoList
//        var startDate: Date? = null
//        var endDate: Date? = null
//        //根据type类型,得到要查询的范围
//        when (type) {
//            "Middle" -> {
//                startDate = DateUtil().addMonth(d1, -1);endDate = DateUtil().addMonth(d1, 2)
//                startDate = DateUtil.addMonth(d1, -1);endDate = DateUtil.addMonth(d1, 2)
//            }
//            "Left" -> {
//                startDate = DateUtil().addMonth(d1, -3);endDate = d1
//                startDate = DateUtil.addMonth(d1, -3);endDate = d1
//            }
//            "Right" -> {
//                startDate = DateUtil().addMonth(d1, 1);endDate = DateUtil().addMonth(d1, 4)
//                startDate = DateUtil.addMonth(d1, 1);endDate = DateUtil.addMonth(d1, 4)
//            }
//            else -> {
//                startDate = d1;endDate = DateUtil().addMonth(d1, 1)
//                startDate = d1;endDate = DateUtil.addMonth(d1, 1)
//            }
//        }
//
@@ -497,16 +511,16 @@
        if (date.length < 7) {
            return taskVoList
        }
        val startDate = DateUtil().StringToDate(date.substring(0, 7)) ?: return taskVoList
        val startDate = DateUtil.StringToDate(date.substring(0, 7)) ?: return taskVoList
        //查询一个月任务
        var endDate = DateUtil().addMonth(startDate, 1) ?: return taskVoList
        endDate = DateUtil().addSecond(endDate, -1)!!
        var endDate = DateUtil.addMonth(startDate, 1) ?: return taskVoList
        endDate = DateUtil.addSecond(endDate, -1)!!
        //构造查询条件
        val example = Example(Task::class.java)
        val criteria = example.createCriteria()
        //获取用户信息
        val userInfo = userinfoMapper.findOne(guid)
        val districtCode = userInfo.dGuid
        val districtCode = userInfo?.dGuid
        //添加日期过滤
@@ -521,25 +535,25 @@
        val criteria2 = example.createCriteria()
        criteria2.orBetween(
                "endtime",
                startDate,
                endDate
            "endtime",
            startDate,
            endDate
        ).andIsNull("tsguid");
        if (userType == "1") {
            criteria2.andLike("executorguids", "%$guid%")
        } else if (userType == "2"){
        } else if (userType == "2") {
            criteria2.andEqualTo("districtcode", districtCode)
        }
        example.or(criteria2)
        val criteria3 = example.createCriteria()
        criteria3.orLessThan("starttime", startDate).andGreaterThan(
                "endtime",
                endDate
            "endtime",
            endDate
        ).andIsNull("tsguid");
        if (userType == "1") {
            criteria3.andLike("executorguids", "%$guid%")
        } else if (userType == "2"){
        } else if (userType == "2") {
            criteria3.andEqualTo("districtcode", districtCode)
        }
        example.or(criteria3)
@@ -586,7 +600,7 @@
        }
        return taskVoList
    }
    override fun save(task: Task): Int {
        // FIXME: 2021/10/9 按照当前的逻辑,此函数主要用于新建顶层任务以及日任务,但是日任务内容的生成逻辑目前由前端app完成,因此会出现一个bug:
        // FIXME: 2021/10/9 当前端多个用户同时在同一个顶层任务下的新的一天创建任务时,会出现同一天有多个日任务,此时前端后续查看只能看到其中一个日任务下的子任务
@@ -594,6 +608,7 @@
        // FIXME: 2021/10/9 另外对于顶层任务也添加类似逻辑,由于顶层任务的任务名称默认系统自动生成,因此暂时只判断同名的顶层任务,当存在同名顶层任务时,不做保存操作
        var result = 0
        if (task.tguid == null) task.tguid = UUIDGenerator.generate16ShortUUID()
        // 1、首先判断是顶层任务还是日任务
        if (task.levelnum == 2) {
            //2.1、 是顶层任务时,判断是否已有同名任务
@@ -607,7 +622,7 @@
            }
        } else {
            //3.1、 当是日任务时,判断在同一个顶层任务下,是否有同名的日任务
            val r  =taskMapper.selectByExample(Example(Task::class.java).apply {
            val r = taskMapper.selectByExample(Example(Task::class.java).apply {
                createCriteria().andEqualTo("tsguid", task.tsguid)
                    .andEqualTo("name", task.name)
            })
@@ -633,6 +648,14 @@
        return result
    }
    override fun create(task: Task): Task {
        val res = save(task)
        if (res < 1)
            throw BizException("任务创建失败")
        else
            return task
    }
    //修改日任务信息,若有子任务,子任务也修改
    override fun update(task: Task): Int {
        var re = 1
@@ -645,7 +668,7 @@
                BeanUtils.copyProperties(it, subtask)
                subtask.planstarttime = task.starttime
                subtask.planendtime = task.endtime
                subtaskService.update(subtask)
                subtaskService.changeStatus(subtask)
            }
        }
        return re
@@ -664,7 +687,7 @@
        criteria.andIsNull("tsguid")
        example.orderBy("starttime").asc()
        val result = taskMapper.selectByExample(example)
        if (result.isNotEmpty()){
        if (result.isNotEmpty()) {
            result.forEach {
                var mytotalcount = 0
                var mycompletecount = 0
@@ -691,7 +714,7 @@
                    taskProgressVo.mycompletetask = mycompletecount
                    val daytaskVolist = getDayTaskByTaskID(it.tguid!!)
                    val daytasklist = mutableListOf<TaskVo>()
                    if (daytaskVolist.isNotEmpty()){
                    if (daytaskVolist.isNotEmpty()) {
                        daytaskVolist.forEach {
                            var mytotalcount1 = 0
                            var mycompletecount1 = 0
@@ -703,7 +726,7 @@
                                    if (Objects.equals(it.status!!, Constant.TaskProgress.RUNINGSTATUS3.text))
                                        mycompletecount1++
                                }
                                if(Objects.equals(it.status, Constant.TaskProgress.RUNINGSTATUS3.text))
                                if (Objects.equals(it.status, Constant.TaskProgress.RUNINGSTATUS3.text))
                                    completecount1++
                            }
                            val daytaskProgressVo = TaskVo()
@@ -725,4 +748,56 @@
        return taskProgressVolist
    }
    override fun findTask(areaVo: AreaVo): List<Task?> {
        return taskRep.findTasks(areaVo)
    }
    override fun findTaskProgress(areaVo: AreaVo): List<TaskProgressVo?> {
        val res = mutableListOf<TaskProgressVo?>()
        taskRep.findTasks(areaVo).forEach { t ->
            if (t?.tguid == null) return@forEach
            val pro = TaskProgressVo().apply {
                tguid = t.tguid
                name = t.name
                levelnum = t.levelnum
                provinceCode = t.provincecode
                provinceName = t.provincename
                cityCode = t.citycode
                cityName = t.cityname
                districtCode = t.districtcode
                districtName = t.districtname
                townCode = t.towncode
                townName = t.townname
                var count = 0
                val countByScene = mutableMapOf<String?, Int>()
                val allScenes = sceneRep.findSceneList(t.tguid!!, areaVo.scensetypeid?.toInt(), areaVo.towncode)
                taskRep.findMonitorList(t.tguid!!, areaVo.scensetypeid).forEach {
                    val c = it.monitornum ?: 1
                    count += c
                    allScenes.find { s-> s?.guid == it.sguid }?.let { s->
//                        if (!countByScene.containsKey(s.type)) {
//                            countByScene[s.type] = 0
//                        }
                        countByScene[s.type] = (countByScene[s.type] ?: 0) + c
                    }
                }
                totaltask = count
                totaltaskByScene = countByScene
                subTaskSummary = subTaskRep.findSummary(AreaVo().apply {
                    topTaskId = t.tguid
                })
                val completeByScene = mutableMapOf<String?, Int>()
                subTaskSummary?.forEach {
                    completeByScene[it.sceneType] = (completeByScene[it.sceneType] ?: 0) + 1
                }
                completetask = this.subTaskSummary?.size
                completetaskByScene = completeByScene
            }
            res.add(pro)
        }
        return res
    }
}