feiyu02
2025-07-30 f75ff7a0fc566dc18b60987b3fa2e65cae4665da
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
package cn.flightfeather.supervision.lightshare.service.impl
 
import cn.flightfeather.supervision.business.autooutput.score.AopEvaluation
import cn.flightfeather.supervision.common.exception.BizException
import cn.flightfeather.supervision.domain.ds1.entity.*
import cn.flightfeather.supervision.domain.ds1.mapper.*
import cn.flightfeather.supervision.common.utils.Constant
import cn.flightfeather.supervision.common.utils.DateUtil
import cn.flightfeather.supervision.common.utils.UUIDGenerator
import cn.flightfeather.supervision.domain.ds1.repository.SubTaskRep
import cn.flightfeather.supervision.domain.ds1.repository.TaskRep
import cn.flightfeather.supervision.lightshare.service.SubtaskService
import cn.flightfeather.supervision.lightshare.service.TaskService
import cn.flightfeather.supervision.lightshare.vo.*
import com.github.pagehelper.PageHelper
import org.springframework.beans.BeanUtils
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import tk.mybatis.mapper.entity.Example
import tk.mybatis.mapper.util.StringUtil
import java.time.LocalDateTime
import java.util.*
import kotlin.math.ceil
 
@Service
class SubtaskServiceImpl(
    private val subtaskMapper: SubtaskMapper,
    private val aopEvaluation: AopEvaluation,
    private val taskRep: TaskRep,
    private val subTaskRep: SubTaskRep,
) : SubtaskService {
 
    @Autowired
    lateinit var taskService: TaskService
    @Autowired
    lateinit var taskMapper: TaskMapper
    @Autowired
    lateinit var evaluationMapper: EvaluationMapper
    @Autowired
    lateinit var problemlistMapper: ProblemlistMapper
    @Autowired
    lateinit var mediafileMapper: MediafileMapper
    @Autowired
    lateinit var scenseMapper: ScenseMapper
    @Autowired
    lateinit var inspectionMapper: InspectionMapper
    @Autowired
    lateinit var monitorobjectversionMapper: MonitorobjectversionMapper
    @Autowired
    lateinit var problemtypeMapper: ProblemtypeMapper
 
    //获取用户的子任务完成情况
    override fun getTaskProgress(areaVo: AreaVo, userGuid: String): TaskVo {
        val taskProgressVo = TaskVo()
        val example = Example(Subtask::class.java)
        val criteria = example.createCriteria()
        if (StringUtil.isNotEmpty(areaVo.provincecode))
            criteria.andEqualTo("provincecode", areaVo.provincecode)
        if (StringUtil.isNotEmpty(areaVo.citycode))
            criteria.andEqualTo("citycode", areaVo.citycode)
        if (StringUtil.isNotEmpty(areaVo.districtcode))
            criteria.andEqualTo("districtcode", areaVo.districtcode)
        if (StringUtil.isNotEmpty(areaVo.towncode))
            criteria.andEqualTo("towncode", areaVo.towncode)
        criteria.andBetween("planstarttime", areaVo.starttime, areaVo.endtime)
        if (!Objects.equals(userGuid, Constant.UserType.ALL_USER.des))
            criteria.andLike("executorguids","%"+userGuid+"%")
        var completecount = 0
        var subtasklist = subtaskMapper.selectByExample(example)
        subtasklist.forEach {
            if (Objects.equals(it.status, Constant.TaskProgress.RUNINGSTATUS3.text)){
                completecount++
            }
        }
        taskProgressVo.totaltask = subtasklist.size
        taskProgressVo.completetask = completecount
 
        return taskProgressVo
    }
 
    //根据日任务信息查询子任务
    override fun findByDayTaskID(taskId: String): List<SubtaskVo> {
        val subtaskVoList = mutableListOf<SubtaskVo>()
        val example = Example(Subtask::class.java)
        val criteria = example.createCriteria()
        criteria.andEqualTo("tsguid", taskId)
        val subtasklist = subtaskMapper.selectByExample(example)
        if (subtasklist.isNotEmpty()) {
            subtasklist.forEach {
                val subtaskVo = SubtaskVo()
                BeanUtils.copyProperties(it, subtaskVo)
                subtaskVoList.add(subtaskVo)
            }
        }
        return subtaskVoList
    }
 
    override fun findByDayTaskID(dayTaskId: String, userId: String, userType: String): List<Subtask> {
        val example = Example(Subtask::class.java)
        val criteria = example.createCriteria()
        criteria.andEqualTo("tsguid", dayTaskId)
        if (userType == "1") {
            example.and(
                    example.createCriteria().orLike("executorguids", "%$userId%")
                            .orLike("stAssessorguid", "%$userId%")
            )
        }
        example.orderBy("name")
        val result = subtaskMapper.selectByExample(example).apply{
            forEach breaking@{
                //已审核提示
                it.remark= Constant.PROBLEM_CHECK_PASS
 
                problemlistMapper.selectByExample(Example(Problemlist::class.java).apply {
                    createCriteria().andEqualTo("stguid", it.stguid)
                    and(createCriteria().orIsNull("remark")
                            .orNotEqualTo("remark", Constant.PROBLEM_DELETED))
                }).forEach {problem ->
                    //子任务中有问题未审核时,设置未审核提示
                    if (problem.extension3 == Constant.PROBLEM_UNCHECKED) {
                        it.remark= Constant.PROBLEM_UNCHECKED
                        return@breaking
                    }
                }
            }
        }
        return result
    }
 
    //根据顶层任务信息获取子任务
    override fun findByTaskID(taskId: String, time: String?): List<SubtaskVo> {
        val subtaskVoList = mutableListOf<SubtaskVo>()
        val example = Example(Subtask::class.java)
        val criteria = example.createCriteria()
        criteria.andEqualTo("tguid", taskId)
        val subtasklist = subtaskMapper.selectByExample(example)
        if (subtasklist.isNotEmpty()) {
            subtasklist.forEach {
                val subtaskVo = SubtaskVo()
                BeanUtils.copyProperties(it, subtaskVo)
                subtaskVoList.add(subtaskVo)
            }
        }
        return subtaskVoList
    }
 
    //批量保存子任务
    @Transactional
    override fun saveList(subtasklist: List<Subtask>): Int {
        subtasklist.forEach {
            if (it.stguid == null) it.stguid = UUIDGenerator.generate16ShortUUID()
            subtaskMapper.insert(it)
        }
        return subtasklist.size
    }
 
    //根据时间,执行人员guid,查询类型,用户类型,获取三个月的全部任务(临时)
    override fun getTaskPackList(date: String, guid: String, type: String, userType: String): List<TaskPack> {
        val taskPackList = mutableListOf<TaskPack>()
        //定义三个月变量
        var one: String? = null
        var two: String? = null
        var three: String? = null
 
        val guidd = if (guid == "0") {
            ""
        } else {
            guid
        }
        val userTypeId = if (userType == "主管部门"){
            "2"
        }else if (userType == "飞羽用户") {
            "1"
        } else if(userType == "管理员"){
            "0"
        }else if (userType == "场景") {
            "3"
        } else {
            userType
        }
        var dateString: String? = null
        //判断参数长度,并处理
        if (date.length < 7) {
            return taskPackList
        } else {
            dateString = date.substring(0, 7)
        }
        //判断参数是否正确
        if (DateUtil.StringToDate(dateString) == null) {
            return taskPackList
        }
        //根据type类型,得到具体是要哪三个月数据
        when (type) {
            "Middle" -> {
                one = DateUtil.addMonth(dateString, -1)
                two = dateString
                three = DateUtil.addMonth(dateString, 1)
            }
            "Left" -> {
                one = DateUtil.addMonth(dateString, 1)
                two = DateUtil.addMonth(dateString, 2)
                three = DateUtil.addMonth(dateString, 3)
            }
            "Right" -> {
                one = DateUtil.addMonth(dateString, -1)
                two = DateUtil.addMonth(dateString, -2)
                three = DateUtil.addMonth(dateString, -3)
            }
            else -> {
                //参数不正确就返回
                return taskPackList
            }
        }
        //分别调用三个月获取三个月数据
        taskPackList.add(getTaskPack(one!!, guidd, userTypeId))
        taskPackList.add(getTaskPack(two!!, guidd, userTypeId))
        taskPackList.add(getTaskPack(three!!, guidd, userTypeId))
        //返回
        return taskPackList
    }
 
    //根据时间,执行人员guid,查询类型,获取三个月的全部任务
    override fun getTaskPackList(date: String, guid: String, type: String): List<TaskPack> {
        val taskPackList = mutableListOf<TaskPack>()
        //定义三个月变量
        var one: String? = null
        var two: String? = null
        var three: String? = null
 
        val guidd = if (guid == "0") {
            ""
        } else {
            guid
        }
        var dateString: String? = null
        //判断参数长度,并处理
        if (date.length < 7) {
            return taskPackList
        } else {
            dateString = date.substring(0, 7)
        }
        //判断参数是否正确
        if (DateUtil.StringToDate(dateString) == null) {
            return taskPackList
        }
        //根据type类型,得到具体是要哪三个月数据
        when (type) {
            "Middle" -> {
                one = DateUtil.addMonth(dateString, -1)
                two = dateString
                three = DateUtil.addMonth(dateString, 1)
            }
            "Left" -> {
                one = DateUtil.addMonth(dateString, 1)
                two = DateUtil.addMonth(dateString, 2)
                three = DateUtil.addMonth(dateString, 3)
            }
            "Right" -> {
                one = DateUtil.addMonth(dateString, -1)
                two = DateUtil.addMonth(dateString, -2)
                three = DateUtil.addMonth(dateString, -3)
            }
            else -> {
                //参数不正确就返回
                return taskPackList
            }
        }
        //分别调用三个月获取三个月数据
        taskPackList.add(getTaskPack(one!!, guidd))
        taskPackList.add(getTaskPack(two!!, guidd))
        taskPackList.add(getTaskPack(three!!, guidd))
        //返回
        return taskPackList
    }
 
    override fun getTaskPack(date: String, guid: String, userType: String): TaskPack {
        //根据参数查询出所有月任务和日任务
        val taskPack = TaskPack()
        //查询出月任务
        val taskVoMonthList = taskService.getMonthTaskList(date, guid, userType)
        //根据月任务查询出日任务
        val taskVoDayList = taskService.getDayTaskList(taskVoMonthList, date, guid, userType)
        //根据日任务,循环获取子任务
        taskVoDayList.forEach {
            //获取任务的日任务
            val taskVoDays = it.daytaskList
            //判断日任务是否为空
            if (taskVoDays != null) {
                //判断日任务个数是否为空
                if (!taskVoDays.isEmpty()) {
                    //循环获取子任务
                    taskVoDays.forEach {
                        val subtaskVoList1 = mutableListOf<SubtaskVo>()
                        //构造查询条件
                        val example = Example(Subtask::class.java)
                        val criteria = example.createCriteria()
                        criteria.andEqualTo("tsguid", it.tguid)
                        if (userType == "1") {
                            criteria.andLike("executorguids", "%$guid%")
                        }
                        //根据条件查询
                        val subtaskList = subtaskMapper.selectByExample(example)
                        //判断结果是否为空
                        if (!subtaskList.isEmpty()) {
                            //循环entity转Vo,并把Vo添加到list
                            subtaskList.forEach {
                                val subtaskVo = SubtaskVo()
                                BeanUtils.copyProperties(it, subtaskVo)
                                subtaskVoList1.add(subtaskVo)
                            }
                        }
                        //把子任务赋值到任务字段
                        it.subtaskList = subtaskVoList1
                    }
                }
 
            }
        }
        //封装包
        taskPack.upperTaskList = taskVoDayList
        taskPack.date = date
        //返回
        return taskPack
    }
 
    //根据日期和执行人员guid,获取全部任务
    override fun getTaskPack(date: String, guid: String): TaskPack {
        //根据参数查询出所有月任务和日任务
        val taskPack = TaskPack()
        //查询出月任务
        val taskVoMonthList = taskService.getMonthTaskList(date, guid)
        //根据月任务查询出日任务
        val taskVoDayList = taskService.getDayTaskList(taskVoMonthList, date, guid)
        //根据日任务,循环获取子任务
        taskVoDayList.forEach {
            //获取任务的日任务
            val taskVoDays = it.daytaskList
            //判断日任务是否为空
            if (taskVoDays != null) {
                //判断日任务个数是否为空
                if (!taskVoDays.isEmpty()) {
                    //循环获取子任务
                    taskVoDays.forEach {
                        val subtaskVoList1 = mutableListOf<SubtaskVo>()
                        //构造查询条件
                        val example = Example(Subtask::class.java)
                        val criteria = example.createCriteria()
                        criteria.andEqualTo("tsguid", it.tguid)
                        criteria.andLike("executorguids", "%$guid%")
                        //根据条件查询
                        val subtaskList = subtaskMapper.selectByExample(example)
                        //判断结果是否为空
                        if (!subtaskList.isEmpty()) {
                            //循环entity转Vo,并把Vo添加到list
                            subtaskList.forEach {
                                val subtaskVo = SubtaskVo()
                                BeanUtils.copyProperties(it, subtaskVo)
                                subtaskVoList1.add(subtaskVo)
                            }
                        }
                        //把子任务赋值到任务字段
                        it.subtaskList = subtaskVoList1
                    }
                }
 
            }
        }
        //封装包
        taskPack.upperTaskList = taskVoDayList
        taskPack.date = date
        //返回
        return taskPack
    }
 
    //根据id获取子任务
    override fun findByID(id: String): SubtaskVo {
        val subtaskVo = SubtaskVo()
        val subtask = subtaskMapper.selectByPrimaryKey(id)
        if (subtask != null)
            BeanUtils.copyProperties(subtask, subtaskVo)
        return subtaskVo
    }
 
    override fun findAll(): MutableList<Subtask> = subtaskMapper.selectAll()
 
    override fun save(subtask: Subtask): Int {
        if (subtask.stguid == null) subtask.stguid = UUIDGenerator.generate16ShortUUID()
        return subtaskMapper.insert(subtask)
    }
 
    override fun update(subtask: Subtask): Int {
        var res = 0
        //根据子任务guid获取顶层任务
        val toptaskVo = taskService.findByID(subtask.tguid.toString())
 
//        //根据日期获取日任务
        val daytaskVo = taskService.findByDate(subtask.tguid!!, DateUtil.getDate(subtask.planstarttime)!!)
 
        //应该直接根据子任务的日任务id查询日任务
//        val daytaskVo = taskService.findByID(subtask.tsguid!!)
 
        //判断是否有日任务
        if (StringUtil.isEmpty(daytaskVo.tguid)) {
            val task = Task()
            //日任务继承顶层任务信息
            BeanUtils.copyProperties(toptaskVo, task)
            val guid = UUIDGenerator.generate16ShortUUID()
            task.tsguid = toptaskVo.tguid
            task.tguid = guid
            task.name = subtask.name
            task.starttime = DateUtil.StringToDate(DateUtil.getDate(subtask.planstarttime)!!.substring(0, 10))
            task.endtime =
                DateUtil.StringToDate(DateUtil.getDate(subtask.planstarttime)!!.substring(0, 10) + " 23:59:59")
            //****日任务执行状态继承子任务的状态*(修改)***
            task.runingstatus = subtask.status
            //********************************************
            taskService.save(task)
            subtask.tsguid = guid
            res = subtaskMapper.updateByPrimaryKeySelective(subtask)
        } else {
            subtask.tsguid = daytaskVo.tguid
            //*(修改)*日任务正在执行,子任务结束,遍历所有其余子任务,都是结束时才将日任务修改为结束****
            if (subtask.status == Constant.TaskProgress.RUNINGSTATUS3.text
                && daytaskVo.runingstatus == Constant.TaskProgress.RUNINGSTATUS2.text){
                val subtaskVolist = findByDayTaskID(daytaskVo.tguid!!)
                var bool = false
                subtaskVolist.forEach {
                    if (it.status != Constant.TaskProgress.RUNINGSTATUS3.text){
                        bool = true
                    }
                }
                if (!bool){
                    daytaskVo.runingstatus = Constant.TaskProgress.RUNINGSTATUS3.text
                }
 
            }
            //两者状态相同时不做修改,其余情况日任务都为正在执行
            else if (subtask.status != daytaskVo.runingstatus){
                daytaskVo.runingstatus = Constant.TaskProgress.RUNINGSTATUS2.text
            }
            val daytask = Task()
            BeanUtils.copyProperties(daytaskVo, daytask)
            taskMapper.updateByPrimaryKeySelective(daytask)
            //****************************************************************************************
            res = subtaskMapper.updateByPrimaryKeySelective(subtask)
        }
        return res
    }
 
    override fun changeStatus(subtask: Subtask): Int {
        val res = update(subtask)
        //对已结束的子任务进行自动评分
        if (subtask.status == Constant.TaskProgress.RUNINGSTATUS3.text) {
            val example = Example(Evaluation::class.java)
            val criteria = example.createCriteria()
            criteria.andEqualTo("stguid", subtask.stguid)
            val result = evaluationMapper.selectByExample(example)
            if (result.isEmpty()) {
                aopEvaluation.executeBySubTask(subtask)
            }
        }
 
        return res
    }
 
    @Transactional
    override fun delete(id: String): Int{
        val subtask = subtaskMapper.selectByPrimaryKey(id)
        val dayTaskId = subtask.tsguid
        subtaskMapper.deleteByPrimaryKey(id)
 
        // 当日任务对应的子任务为0时,同时删除日任务
        val subtasklist = findByDayTaskID(dayTaskId.toString())
        if (subtasklist.isEmpty()) {
            taskMapper.deleteByPrimaryKey(dayTaskId)
        }
 
        // 同时更新监管版本中的巡查计数
        val topTaskId = subtask.tguid
        val sceneId = subtask.scenseid
        val mList = monitorobjectversionMapper.selectByExample(Example(Monitorobjectversion::class.java).apply {
            createCriteria().andEqualTo("tid", topTaskId).andEqualTo("sguid", sceneId)
        })
        mList.forEach {
            var count = it.extension1?.toIntOrNull() ?: 0
            count--
            if (count < 0) {
                count = 0
            }
            it.extension1 = count.toString()
            monitorobjectversionMapper.updateByPrimaryKeySelective(it)
        }
 
        // 同时删除对应问题和巡查记录
        inspectionMapper.deleteByExample(Example(Inspection::class.java).apply { createCriteria().andEqualTo("stguid", id) })
        problemlistMapper.deleteByExample(Example(Problemlist::class.java).apply { createCriteria().andEqualTo("stguid", id) })
 
        return 1
    }
 
 
    override fun searchSubTask(token: String, sceneType: Int?, districtCode: String?, startTime: String?, endTime: String?, page: Int?, perPage: Int?): BaseSearchResultVo {
        if (token != "jingan") {
            return BaseSearchResultVo().apply {
                head = DataHead().apply {
                    this.page = 0
                    totalPage = 0
                }
            }
        }
 
        val result = BaseSearchResultVo()
 
        val _sceneType = sceneType ?: 1
        val _districtCode = "310106"
        val _startTime = if (startTime.isNullOrBlank()) {
            val cal = Calendar.getInstance()
            cal.set(Calendar.HOUR_OF_DAY, 0)
            cal.set(Calendar.MINUTE, 0)
            cal.set(Calendar.SECOND, 0)
            cal.set(Calendar.MILLISECOND, 0)
            cal.time
        } else {
            DateUtil.StringToDate(startTime!!)
        }
 
        val _endTime = if (endTime.isNullOrBlank()) {
            val cal = Calendar.getInstance()
            cal.set(Calendar.HOUR_OF_DAY, 23)
            cal.set(Calendar.MINUTE, 59)
            cal.set(Calendar.SECOND, 59)
            cal.set(Calendar.MILLISECOND, 999)
            cal.time
        } else {
            DateUtil.StringToDate(endTime!!)?.apply {
                time += 24 * 60 * 60 * 1000
            }
        }
 
        val subtaskMap = mutableMapOf<String?, SubtaskSearchResultVo>()
        val problemMap = mutableMapOf<String?, ProblemDetail>()
//        val  p = PageHelper.startPage<Subtask>(page ?: 1, perPage ?: 30)
        subtaskMapper.getSubtask2(null, null, _districtCode, _sceneType.toByte(), _startTime, _endTime).forEach {
            if (!subtaskMap.containsKey(it.subTaskId)) {
                val vo =SubtaskSearchResultVo()
                BeanUtils.copyProperties(it, vo)
                subtaskMap[it.subTaskId] = vo
            }
            if (!problemMap.containsKey(it.problemId)) {
                val problemDetail = ProblemDetail()
                BeanUtils.copyProperties(it, problemDetail)
                problemMap[it.problemId] = problemDetail
                subtaskMap[it.subTaskId]?.problemList?.add(problemDetail)
            }
            val url = it.mExtension1 + it.mGuid+ ".jpg"
            if (it.isChanged == true) {
                problemMap[it.problemId]?.rectificationPics?.add(url)
            } else {
                problemMap[it.problemId]?.problemPics?.add(url)
            }
        }
 
        val totalPage = ceil(subtaskMap.size.toDouble() / (perPage ?: 30).toDouble()).toInt()
        result.head = DataHead().apply {
            this.page = page ?: 1
            this.totalPage = totalPage
            this.totalCount = subtaskMap.size.toLong()
        }
        var count = 0
        val startIndex = ((page ?: 1) - 1) * (perPage ?: 30)
        val endIndex = startIndex + (perPage ?: 30) - 1
        with(subtaskMap) exit@{
            subtaskMap.forEach {
                if (count in startIndex..endIndex) {
                    result.data.add(it.value)
                }
                count++
                if (count > endIndex) return@exit
            }
        }
 
        return result
    }
 
    override fun searchSubTask2(
        token: String,
        updateTime: String?,
        sceneType: Int?,
        districtCode: String?,
        startTime: String?,
        endTime: String?,
        page: Int?,
        perPage: Int?,
    ): BaseResponse<BaseSearchResultVo> {
        if (token != "jinshan") {
            return BaseResponse(false, "请求token错误")
        }
 
        val time = if (updateTime != null) {
            DateUtil.StringToDate(updateTime)
        } else {
            null
        }
 
        val result = BaseSearchResultVo()
 
        val _sceneType = sceneType
        val _districtCode = "310116"
 
        if (!startTime.isNullOrBlank() && !endTime.isNullOrBlank()) {
            val _startTime = if (startTime.isNullOrBlank()) {
                val cal = Calendar.getInstance()
                cal.set(Calendar.HOUR_OF_DAY, 0)
                cal.set(Calendar.MINUTE, 0)
                cal.set(Calendar.SECOND, 0)
                cal.set(Calendar.MILLISECOND, 0)
                cal.time
            } else {
                DateUtil.StringToDate(startTime!!)
            }
 
            val _endTime = if (endTime.isNullOrBlank()) {
                val cal = Calendar.getInstance()
                cal.set(Calendar.HOUR_OF_DAY, 23)
                cal.set(Calendar.MINUTE, 59)
                cal.set(Calendar.SECOND, 59)
                cal.set(Calendar.MILLISECOND, 999)
                cal.time
            } else {
                DateUtil.StringToDate(endTime!!)?.apply {
                    this.time += 24 * 60 * 60 * 1000
                }
            }
 
 
            val p = PageHelper.startPage<SubtaskSearchResultVo>(page ?: 1, perPage ?: 30)
 
            val subtasklist = subtaskMapper.getSubtask(_districtCode, _startTime, _endTime, _sceneType?.toByte())
 
            subtasklist.forEach { s ->
 
                problemlistMapper.selectByExample(Example(Problemlist::class.java).apply {
                    createCriteria().andEqualTo("stguid", s.subTaskId)
                        .andNotEqualTo("extension3", Constant.PROBLEM_UNCHECKED)
                        .andNotEqualTo("extension3", Constant.PROBLEM_CHECK_FAIL)
                        .andNotEqualTo("extension3", Constant.CHANGE_CHECK_FAIL)
                        .andIsNotNull("extension3")
                    time?.let {
                        and(createCriteria().orGreaterThan("time", it)
                            .orGreaterThan("changedtime", it))
                    }
                }).forEach { p ->
                    val problem = ProblemDetail().apply {
                        this.problemId = p.guid
                        this.problemName = p.problemname
                        this.longitude = p.longitude?.toDouble()
                        this.latitude = p.latitude?.toDouble()
                        this.location = p.location
                        this.rectification = p.ischanged
                        this.rectificationTime = p.changedtime
 
                        /**
                         *
                         * 问题更新时间说明 (2020.09.04)
                         * 未整改时,提交问题时间为更新时间
                         *  整改后,整改时间即为更新时间,但有以下几种特殊情况:
                         *          1:整改有现场整改和用户提交整改两种方式,目前现场整改由于bug不会更新整改时间字段,
                         *                  因此使用问题提交时间加1小时作为更新时间,以作区分;
                         *          2:原本用户提交整改后需要经过审核才能表示整改通过,但目前由于部分原因导致大量的整改并未审核,但整改是通过的,
                         *                  因此暂时将未审核视作已审核
                         */
                        this.updateTime = when {
                            p.ischanged == false -> p.time
                            p.changedtime == null -> Date(p.time?.time?.plus(1 * 60 * 60 * 1000) ?: Date().time)
                            else -> p.changedtime
                        }
                    }
 
                    s.problemList.add(problem)
 
                    mediafileMapper.selectByExample(Example(Mediafile::class.java).apply {
                        createCriteria().andEqualTo("businessguid", p.guid)
                    }).forEach { m ->
                        val url = m.extension1 + m.guid + ".jpg"
                        if (m.ischanged == true) {
                            problem.rectificationPics.add(url)
                        } else {
                            problem.problemPics.add(url)
                        }
                    }
                }
            }
 
            result.head = DataHead().apply {
                this.page = p.pageNum
                this.totalPage = p.pages
                this.totalCount = p.total
            }
 
            subtasklist.forEach {
                if (it.problemList.isNotEmpty()) {
                    result.data.add(it)
                }
            }
        } else {
            val time2 = time?.apply {
                this.time.minus(60 * 60 * 1000)
            }
            val subtaskMap = mutableMapOf<String?, SubtaskSearchResultVo>()
            val problemMap = mutableMapOf<String?, ProblemDetail>()
//            val p = PageHelper.startPage<Subtask>(page ?: 1, perPage ?: 30)
            subtaskMapper.getSubtask2(time, time2, _districtCode, _sceneType?.toByte(), null, null).forEach {
                if (!subtaskMap.containsKey(it.subTaskId)) {
                    val vo = SubtaskSearchResultVo()
                    BeanUtils.copyProperties(it, vo)
                    subtaskMap[it.subTaskId] = vo
                }
                if (!problemMap.containsKey(it.problemId)) {
                    val problemDetail = ProblemDetail()
                    BeanUtils.copyProperties(it, problemDetail)
                    problemMap[it.problemId] = problemDetail
                    subtaskMap[it.subTaskId]?.problemList?.add(problemDetail)
                }
                val url = it.mExtension1 + it.mGuid + ".jpg"
                if (it.isChanged == true) {
                    problemMap[it.problemId]?.rectificationPics?.add(url)
                } else {
                    problemMap[it.problemId]?.problemPics?.add(url)
                }
            }
            result.head = DataHead().apply {
                this.page = 1
                this.totalPage = 1
                this.totalCount = subtaskMap.size.toLong()
            }
            subtaskMap.forEach {
                result.data.add(it.value)
            }
        }
 
 
        return BaseResponse(true, "请求成功", data = result)
    }
 
    override fun searchSubTask3(token: String, updateTime: String?, sceneType: Int?, districtCode: String?, startTime: String?, endTime: String?, page: Int?, perPage: Int?): BaseResponse<BaseSearchResultVo> {
        if (token != "jinshan") {
            return BaseResponse(false, "请求token错误")
        }
 
        val time = if (updateTime != null) {
            DateUtil.StringToDate(updateTime)
        } else {
            null
        }
 
        val result = BaseSearchResultVo()
 
        val _sceneType = sceneType
        val _districtCode = "310116"
 
        if (!startTime.isNullOrBlank() && !endTime.isNullOrBlank()) {
            val _startTime = if (startTime.isNullOrBlank()) {
                val cal = Calendar.getInstance()
                cal.set(Calendar.HOUR_OF_DAY, 0)
                cal.set(Calendar.MINUTE, 0)
                cal.set(Calendar.SECOND, 0)
                cal.set(Calendar.MILLISECOND, 0)
                cal.time
            } else {
                DateUtil.StringToDate(startTime!!)
            }
 
            val _endTime = if (endTime.isNullOrBlank()) {
                val cal = Calendar.getInstance()
                cal.set(Calendar.HOUR_OF_DAY, 23)
                cal.set(Calendar.MINUTE, 59)
                cal.set(Calendar.SECOND, 59)
                cal.set(Calendar.MILLISECOND, 999)
                cal.time
            } else {
                DateUtil.StringToDate(endTime!!)?.apply {
                    this.time += 24 * 60 * 60 * 1000
                }
            }
 
 
            val p = PageHelper.startPage<SubtaskSearchResultVo>(page ?: 1, perPage ?: 30)
 
            val subtasklist = subtaskMapper.getSubtask(_districtCode, _startTime, _endTime, _sceneType?.toByte())
 
            subtasklist.forEach { s ->
 
                problemlistMapper.selectByExample(Example(Problemlist::class.java).apply {
                    createCriteria().andEqualTo("stguid", s.subTaskId)
                        .andNotEqualTo("extension3", Constant.PROBLEM_UNCHECKED)
                        .andNotEqualTo("extension3", Constant.PROBLEM_CHECK_FAIL)
                        .andNotEqualTo("extension3", Constant.CHANGE_CHECK_FAIL)
                        .andIsNotNull("extension3")
                    time?.let {
                        and(createCriteria().orGreaterThan("time", it)
                            .orGreaterThan("changedtime", it))
                    }
                }).forEach { p ->
                    val problem = ProblemDetail().apply {
                        this.problemId = p.guid
                        this.problemName = p.problemname
                        this.longitude = p.longitude?.toDouble()
                        this.latitude = p.latitude?.toDouble()
                        this.location = p.location
                        this.rectification = p.ischanged
                        this.rectificationTime = p.changedtime
 
                        /**
                         *
                         * 问题更新时间说明 (2020.09.04)
                         * 未整改时,提交问题时间为更新时间
                         *  整改后,整改时间即为更新时间,但有以下几种特殊情况:
                         *          1:整改有现场整改和用户提交整改两种方式,目前现场整改由于bug不会更新整改时间字段,
                         *                  因此使用问题提交时间加1小时作为更新时间,以作区分;
                         *          2:原本用户提交整改后需要经过审核才能表示整改通过,但目前由于部分原因导致大量的整改并未审核,但整改是通过的,
                         *                  因此暂时将未审核视作已审核
                         */
                        this.updateTime = when {
                            p.ischanged == false -> p.time
                            p.changedtime == null -> Date(p.time?.time?.plus(1 * 60 * 60 * 1000) ?: Date().time)
                            else -> p.changedtime
                        }
                    }
 
                    // 问题唯一编号
                    problemtypeMapper.selectByPrimaryKey(p.ptguid)?.let {
                        problem.problemCode = it.extension1
                    }
 
                    s.problemList.add(problem)
 
                    mediafileMapper.selectByExample(Example(Mediafile::class.java).apply {
                        createCriteria().andEqualTo("businessguid", p.guid)
                    }).forEach { m ->
                        val url = m.extension1 + m.guid + ".jpg"
                        if (m.ischanged == true) {
                            problem.rectificationPics.add(url)
                        } else {
                            problem.problemPics.add(url)
                        }
                    }
                }
            }
 
            result.head = DataHead().apply {
                this.page = p.pageNum
                this.totalPage = p.pages
                this.totalCount = p.total
            }
 
            subtasklist.forEach {
                if (it.problemList.isNotEmpty()) {
                    result.data.add(it)
                }
            }
        } else {
            val time2 = time?.apply {
                this.time.minus(60 * 60 * 1000)
            }
            val subtaskMap = mutableMapOf<String?, SubtaskSearchResultVo>()
            val problemMap = mutableMapOf<String?, ProblemDetail>()
//            val p = PageHelper.startPage<Subtask>(page ?: 1, perPage ?: 30)
            subtaskMapper.getSubtask2(time, time2, _districtCode, _sceneType?.toByte(), null, null).forEach {
                if (!subtaskMap.containsKey(it.subTaskId)) {
                    val vo =SubtaskSearchResultVo()
                    BeanUtils.copyProperties(it, vo)
                    subtaskMap[it.subTaskId] = vo
                }
                if (!problemMap.containsKey(it.problemId)) {
                    val problemDetail = ProblemDetail()
                    BeanUtils.copyProperties(it, problemDetail)
                    problemMap[it.problemId] = problemDetail
                    subtaskMap[it.subTaskId]?.problemList?.add(problemDetail)
                }
                val url = it.mExtension1 + it.mGuid+ ".jpg"
                if (it.isChanged == true) {
                    problemMap[it.problemId]?.rectificationPics?.add(url)
                } else {
                    problemMap[it.problemId]?.problemPics?.add(url)
                }
            }
            result.head = DataHead().apply {
                this.page = 1
                this.totalPage = 1
                this.totalCount = subtaskMap.size.toLong()
            }
            subtaskMap.forEach {
                result.data.add(it.value)
            }
        }
 
 
        return BaseResponse(true, "请求成功", data = result)
    }
 
    override fun findByDate(date: String, userId: String): List<SubtaskVo> {
        val time = DateUtil.StringToDate(date)
 
        val resultList = mutableListOf<SubtaskVo>()
 
        subtaskMapper.selectByExample(Example(Subtask::class.java).apply {
            createCriteria().andLessThanOrEqualTo("planstarttime", time)
                    .andGreaterThanOrEqualTo("planendtime", time)
        }).forEach {
            val vo = SubtaskVo()
            BeanUtils.copyProperties(it, vo)
            resultList.add(vo)
        }
 
        return resultList
    }
 
    override fun getByTopTaskAndDate(
        topTaskId: String,
        startTime: String?,
        endTime: String?,
        sceneTypeId: Int?
    ): List<Subtask> {
 
        return subtaskMapper.selectByTopTask2(topTaskId, sceneTypeId)
    }
 
    override fun getSummary(topTaskId: String, sceneTypeId: Int?): List<SubTaskSummary> {
        return subtaskMapper.getSummary(topTaskId, sceneTypeId)
    }
 
    override fun getTaskProgressByArea(areaVo: AreaVo): List<TaskProgressVo> {
//        areaVo.scensetypeid ?: throw BizException("缺少场景类型参数")
        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
                areaVo.topTaskId = t.tguid
                subTaskSummary = subTaskRep.findSummary(areaVo)
            }
            res.add(pro)
        }
        return res
    }
 
    override fun getByScene(sceneId: String, startTime: LocalDateTime?, endTime: LocalDateTime?): List<SubTaskSummary> {
        return subtaskMapper.getSummaryByScene(sceneId, startTime, endTime)
    }
 
    override fun getSummaryByArea(areaVo: AreaVo): List<SubTaskSummary> {
        return subTaskRep.findSummary(areaVo)
    }
}