feiyu02
2025-09-30 6904763f0e74d9a9fa4dbc39f635d2aee39416c6
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
package cn.flightfeather.supervision.lightshare.service.Impl
 
import cn.flightfeather.supervision.common.nlp.NlpController
import cn.flightfeather.supervision.domain.entity.*
import cn.flightfeather.supervision.domain.enumeration.*
import cn.flightfeather.supervision.domain.mapper.*
import cn.flightfeather.supervision.infrastructure.utils.DateUtil
import cn.flightfeather.supervision.lightshare.service.OnLineQuestionService
import cn.flightfeather.supervision.lightshare.vo.*
import com.github.pagehelper.PageHelper
import org.springframework.beans.BeanUtils
import org.springframework.stereotype.Component
import org.springframework.stereotype.Service
import tk.mybatis.mapper.entity.Example
import java.util.*
import javax.annotation.Resource
import javax.servlet.http.HttpServletResponse
 
/**
 * @Description: 月问题实现类
 */
@Service
@Component
class OnLineQuestionServiceImpl(
    private val mgtFileMapper: MgtFileMapper,
    private val mgtItemMapper: MgtItemMapper,
    private val userinfoMapper: UserinfoMapper,
    private val cstQuestionMapper: CstQuestionMapper,
    private val settingAnswerMapper: SettingAnswerMapper,
    private val enforceCaseMapper: EnforceCaseMapper,
    private val nlpController: NlpController
) : OnLineQuestionService {
    @Resource
    private val onLineQuestionMapper: OnLineQuestionMapper? = null
    override fun addQuestions(userId: String, questions: List<OnLineQuestion>): Int {
        var r = 0
        for (q in questions) {
            r += onLineQuestionMapper!!.insert(q)
        }
        return r
    }
 
    override fun getQuestions(userId: String, inUse: Boolean, page: Int, perPage: Int, httpServletResponse: HttpServletResponse): List<OnLineQuestion> {
 
        // 1.计算出起始位置
        val startPosition = (page - 1) * perPage
        //2.分页的方法查询数据
        PageHelper.startPage<Any>(startPosition, perPage)
        val allMeetings: List<OnLineQuestion>
        //3.分页显示数据
        allMeetings = if (inUse) {
            onLineQuestionMapper!!.findAll()
        } else {
            onLineQuestionMapper!!.findAllfalse()
        }
        val counts = onLineQuestionMapper.count()
        val totalPage = Math.ceil(counts / perPage * 1.0).toInt()
        httpServletResponse.setIntHeader("totalPage", totalPage)
        httpServletResponse.setIntHeader("currentPage", page)
        return allMeetings
    }
 
    override fun getQDescription(userId: String, questionId: String): String {
        return onLineQuestionMapper!!.getQDescription(questionId)
    }
 
    override fun getQResource(userId: String, questionId: String, page: Int, perPage: Int, httpServletResponse: HttpServletResponse): List<String> {
        // 1.计算出起始位置
        val startPosition = (page - 1) * perPage
        val getQDescription = onLineQuestionMapper!!.getQResource(questionId)
        val lis = Arrays.asList(*getQDescription.split("、").toTypedArray())
        val result: List<String>
        result = if (startPosition + perPage < lis.size) {
            lis.subList(startPosition, startPosition + perPage)
        } else {
            lis.subList(startPosition, lis.size)
        }
        val counts = lis.size
        val totalPage = Math.ceil(counts / perPage * 1.0).toInt()
        httpServletResponse.setIntHeader("totalPage", totalPage)
        httpServletResponse.setIntHeader("currentPage", page)
        return result
    }
 
    override fun getQByConditions(userId: String, inUse: Boolean, qCondition: QCondition, page: Int, perPage: Int, httpServletResponse: HttpServletResponse): List<OnLineQuestion> {
        val keywords = qCondition.keywords
        val factorType = qCondition.factorType
        val resourceKind = qCondition.resourceKind
        val showLevel = qCondition.showLevel
        val worKind = qCondition.worKind
        // 1.计算出起始位置
        val startPosition = (page - 1) * perPage
        //2.分页的方法查询数据
        PageHelper.startPage<Any>(startPosition, perPage)
        //3.分页显示数据  //模糊查询
        val result = onLineQuestionMapper!!.getQByConditions(keywords, factorType, resourceKind, showLevel, worKind)
        val counts = onLineQuestionMapper.getQByConditionsCounts(keywords, factorType, resourceKind, showLevel, worKind)
        val totalPage = Math.ceil(counts / perPage * 1.0).toInt()
        httpServletResponse.setIntHeader("totalPage", totalPage)
        httpServletResponse.setIntHeader("currentPage", page)
        return result
    }
 
    override fun searchLaw(userId: String, keyword: String, type: Byte?, page: Int, perPage: Int): BaseResponse<List<ConsultResultVo>> {
//        val keywordList = if (keyword.isBlank()) {
//            emptyList()
//        } else {
//            nlpController.execute(keyword)
//        }
//        val userInfo = userinfoMapper.selectByPrimaryKey(userId) ?: return BaseResponse(false)
//        val response = BaseResponse<List<ConsultResultVo>>(false, head = DataHead(page, perPage))
//        val result = mutableListOf<ConsultResultVo>()
//
//        //当搜索不限制类型时,默认每种类型只获取前5项;当限制类型时,则根据前端请求分页数返回
//        val _perPage = if (type == null) 5 else perPage
//        val _page = page
//
//        //法律法规文件
//        if (type == null || type == ConsultResultType.TYPE1.value) {
//            val p = PageHelper.startPage<MgtFile>(_page, _perPage)
//            val example = Example(MgtFile::class.java).apply {
//                if (keywordList.isNotEmpty()) {
//                    createCriteria().apply {
//                        keywordList.forEach { k ->
//                            orLike("mfName", "%${k}%")
//                            orLike("mfShortName", "%${k}%")
//                            orLike("mfSummary", "%${k}%")
//                            orLike("mfKeywordLv1", "%${k}%")
//                            orLike("mfKeywordLv2", "%${k}%")
//                            orLike("mfKeywordLv3", "%${k}%")
//                            orLike("mfKeywordLv4", "%${k}%")
//                        }
//                    }
//                    and(createCriteria().orLike("mfExtension1", "%${userInfo.extension2}%")
//                        .orEqualTo("mfExtension1", "")
//                        .orIsNull("mfExtension1"))
//                } else {
//                    // TODO: 2022/9/8 没有关键字时,按照热门获取机制获取
//                    createCriteria().orLike("mfExtension1", "%${userInfo.extension2}%")
//                        .orEqualTo("mfExtension1", "")
//                        .orIsNull("mfExtension1")
//                }
//            }
//            mgtFileMapper.selectByExample(example).forEach {
//                result.add(ConsultResultFileVo().apply {
//                    id = it.mfGuid
//                    name = it.mfName
//                    des = it.mfSummary
//                    typeId = ConsultResultType.TYPE1.value
//                    typeName = ConsultResultType.TYPE1.des
//
//                    fileIndustry = IndustryType.getNameByValue(it.mfFileIndustry)
//                    itemType = EnElementType.getNameByValue(it.mfEpItemType)
//                    itemSubType = EnElementSubType.getSubType(it.mfEpItemType, it.mfEpItemSubtype)
//                    fileUrl = it.mfFileUrl
//                    val keyList = it.mfKeywordLv1.split("、")
//                    for (i in keyList.indices) {
//                        if (i < 5) {
//                            keywords.add(keyList[i])
//                        } else {
//                            break
//                        }
//                    }
//                    time = it.mfUpdateTime
//                    fileType = ConsultFileType.getNameByValue(it.mfFileType)
//                    referenceNumber = it.mfReferenceNumber
//                    effectiveDate = DateUtil.DateToString(it.mfEffectiveDate, DateUtil.DateStyle.YYYY_MM_DD)
//                    closingDate = DateUtil.DateToString(it.mfClosingDate, DateUtil.DateStyle.YYYY_MM_DD)
//                    effective = Date().time > (it.mfClosingDate?.time ?: 0)
//                })
//            }
//            response.success = true
//            response.head?.run {
//                this.page = p.pageNum
//                this.totalPage = p.pages
//            }
//        }
//        //法律法规条目
//        if (type == null || type == ConsultResultType.TYPE2.value) {
//            val map = mutableMapOf<String, MgtFile>()
//            val p = PageHelper.startPage<MgtItem>(_page, _perPage)
//            val example = Example(MgtItem::class.java).apply {
//                if (keywordList.isNotEmpty()) {
//                    createCriteria().apply {
//                        keywordList.forEach { k ->
//                            orLike("miChapterKeyword", "%${k}%")
//                            orLike("miKeyword", "%${k}%")
//                            orLike("miItemContent", "%${k}%")
//                        }
//                    }
//                } else {
//                    // TODO: 2022/9/8 没有关键字时,按照热门获取机制获取条目
//                }
//            }
//            mgtItemMapper.selectByExample(example).forEach {
//
//                if (!map.containsKey(it.mfGuid)) {
//                    map[it.mfGuid] = mgtFileMapper.selectByPrimaryKey(it.mfGuid)
//                }
//                result.add(ConsultResultItemVo().apply {
//                    id = it.miGuid
//                    name = it.miItemName
//                    des = it.miItemContent
//                    typeId = ConsultResultType.TYPE2.value
//                    typeName = ConsultResultType.TYPE2.des
//
////                    fileIndustry = IndustryType.getNameByValue(it.mfFileIndustry)
////                    itemType = EnElementType.getNameByValue(it.mfEpItemType)
////                    fileUrl = it.mfFileUrl
//                    val keyList = it.miKeyword.split("、")
//                    for (i in keyList.indices) {
//                        if (i < 5) {
//                            keywords.add(keyList[i])
//                        } else {
//                            break
//                        }
//                    }
//                    time = it.miUpdateTime
//
//                    chapterName = it.miChapterName
//                    fileId = it.mfGuid
//                    fileName = it.mfName
//                    referenceNumber = it.mfReferenceNumber
//                    effectiveDate = DateUtil.DateToString(map[it.mfGuid]?.mfEffectiveDate, DateUtil.DateStyle.YYYY_MM_DD)
//                    closingDate = DateUtil.DateToString(map[it.mfGuid]?.mfClosingDate, DateUtil.DateStyle.YYYY_MM_DD)
//                    effective = Date().time > (map[it.mfGuid]?.mfClosingDate?.time ?: 0)
//                    relatedItems = it.miRelatedItems?.split(";")?.size ?: 0
//                })
//            }
//            response.success = true
//            response.head?.run {
//                this.page = p.pageNum
//                this.totalPage = p.pages
//            }
//        }
//        //环保问题
//        if (type == null || type == ConsultResultType.TYPE4.value) {
//            val p = PageHelper.startPage<CstQuestion>(_page, _perPage)
//            val example = Example(CstQuestion::class.java).apply {
//                if (keywordList.isNotEmpty()) {
//                    createCriteria().orLike("cqScenes", "%${userInfo.extension2}%")
//                        .orEqualTo("cqScenes", "")
//                        .orIsNull("cqScenes")
//                    and(
//                        createCriteria().apply {
//                            keywordList.forEach { k ->
//                                orLike("cqContent", "%${k}%")
//                                orLike("cqKeywords", "%${k}%")
//                            }
//                        }
//                    )
//                } else {
//                    // TODO: 2022/9/8 没有关键字时,按照热门获取机制获取
//                    createCriteria().orLike("cqScenes", "%${userInfo.extension2}%")
//                    orderBy("cqTotalnum")
//                }
//            }
//            cstQuestionMapper.selectByExample(example).forEach {
//                val answers = settingAnswerMapper.selectByExample(Example(SettingAnswer::class.java).apply {
//                    createCriteria().andEqualTo("cqGuid", it.cqGuid)
//                })
//
//                result.add(ConsultResultQAVo().apply {
//                    id = it.cqGuid
//                    name = it.cqContent
//                    if (answers.isNotEmpty()) {
//                        des = answers[0].saContent
//                    }
//                    typeId = ConsultResultType.TYPE4.value
//                    typeName = ConsultResultType.TYPE4.des
//
////                    fileIndustry = IndustryType.getNameByValue(it.mfFileIndustry)
//                    itemType = EnElementType.getNameByValue(it.cqKind)
//                    itemSubType = EnElementSubType.getSubType(it.cqKind, it.cqSubkind)
////                    fileUrl = it.mfFileUrl
////                    val keyList = it.mfKeywordLv1.split("、")
////                    for (i in keyList.indices) {
////                        if (i < 5) {
////                            keywords.add(keyList[i])
////                        } else {
////                            break
////                        }
////                    }
//                    time = it.cqCreateTime
//                    punish = it.cqIsPunish
//                    illegal = it.cqIsIllegal
//                    shotSpot = it.cqIsShotspot
//                    supervise = it.cqIsSupervise
//                })
//            }
//            response.success = true
//            response.head?.run {
//                this.page = p.pageNum
//                this.totalPage = p.pages
//            }
//        }
//        //执法案例
//        if (type == null || type == ConsultResultType.TYPE3.value) {
//            val p = PageHelper.startPage<EnforceCase>(_page, _perPage)
//            val example = Example(EnforceCase::class.java).apply {
//                if (keywordList.isNotEmpty()) {
//                    createCriteria().orLike("ecScenes", "%${userInfo.extension2}%")
//                        .orEqualTo("ecScenes", "")
//                        .orIsNull("ecScenes")
//                    and(
//                        createCriteria().apply {
//                            keywordList.forEach { k ->
//                                orLike("ecTitle", "%${k}%")
//                                orLike("ecKeywords", "%${k}%")
//                            }
//                        }
//                    )
//                } else {
//                    // TODO: 2022/9/8 没有关键字时,按照热门获取机制获取
//                    createCriteria().orLike("ecScenes", "%${userInfo.extension2}%")
//                        .orEqualTo("ecScenes", "")
//                        .orIsNull("ecScenes")
//                    orderBy("ecOccurDate").desc()
//                }
//            }
//            enforceCaseMapper.selectByExample(example).forEach {
//                result.add(ConsultResultCaseVo().apply {
//                    id = it.ecGuid
//                    name = it.ecTitle
//                    des = it.ecSummary
//                    typeId = ConsultResultType.TYPE3.value
//                    typeName = ConsultResultType.TYPE3.des
//
////                    fileIndustry = IndustryType.getNameByValue(it.mfFileIndustry)
//                    itemType = EnElementType.getNameByValue(it.ecEpItemType)
//                    itemSubType = EnElementSubType.getSubType(it.ecEpItemType, it.ecEpItemSubtype)
//                    imgUrl = it.ecAppendixUrl?.split(";")?.get(0)
//                    time = it.ecCreateTime
//
//                    provinceName = it.ecProvinceName
//                    cityName = it.ecCityName
//                    occurDate = DateUtil.DateToString(it.ecOccurDate, DateUtil.DateStyle.YYYY_MM_DD)
//                    punish = it.ecIsPunish
//                    detained = it.ecIsDetained
//                    illegal = it.ecIsIllegal
//                    shotSpot = it.ecIsShotspot
//                    supervise = it.ecIsSupervise
//                    minor = it.ecIsMinor
//                })
//            }
//            response.success = true
//            response.head?.run {
//                this.page = p.pageNum
//                this.totalPage = p.pages
//            }
//        }
//
//
//        return response.apply { data = result }
 
        return searchLaw2(userId, keyword, type, page, perPage)
    }
 
    override fun getTopicLaw(userId: String): List<MgtFileVo> {
        val userInfo = userinfoMapper.selectByPrimaryKey(userId) ?: return emptyList()
        val perPage = 5
        val p = PageHelper.startPage<MgtFile>(1, perPage)
        val result = mutableListOf<MgtFileVo>()
        mgtFileMapper.selectByExample(Example(MgtFile::class.java).apply {
            createCriteria().orLike("mfExtension1", "%${userInfo.extension2}%")
                .orEqualTo("mfExtension1", "")
                .orIsNull("mfExtension1")
        }).forEach {
            val vo = MgtFileVo()
            BeanUtils.copyProperties(it, vo)
            vo.apply {
                mfFileIndustry = IndustryType.getNameByValue(it.mfFileIndustry)
                mfMgtLevel = ManageLevelType.getNameByValue(it.mfMgtLevel)
                mfFileType = ConsultFileType.getNameByValue(it.mfFileType)
                mfEpItemType = EnElementType.getNameByValue(it.mfEpItemType)
                mfEpItemSubtype = EnElementSubType.getSubType(it.mfEpItemType, it.mfEpItemSubtype)
                mfSaveType = FileSaveType.getNameByValue(it.mfSaveType)
            }
            result.add(vo)
        }
        return result
    }
 
    override fun getMgtFile(userId: String, fileId: String): MgtFileVo {
        val result = MgtFileVo()
        mgtFileMapper.selectByPrimaryKey(fileId)?.let {
            BeanUtils.copyProperties(it, result)
            result.apply {
                mfFileIndustry = IndustryType.getNameByValue(it.mfFileIndustry)
                mfMgtLevel = ManageLevelType.getNameByValue(it.mfMgtLevel)
                mfFileType = ConsultFileType.getNameByValue(it.mfFileType)
                mfEpItemType = EnElementType.getNameByValue(it.mfEpItemType)
                mfEpItemSubtype = EnElementSubType.getSubType(it.mfEpItemType, it.mfEpItemSubtype)
                mfSaveType = FileSaveType.getNameByValue(it.mfSaveType)
            }
        }
 
        return result
    }
 
    override fun getTopicMgtItems(userId: String): List<MgtItem> {
        val perPage = 5
        val p = PageHelper.startPage<MgtFile>(1, perPage)
        return mgtItemMapper.selectAll()
    }
 
    override fun getMgtItem(userId: String, itemId: String): MgtItem {
        return mgtItemMapper.selectByPrimaryKey(itemId) ?: MgtItem()
    }
 
    override fun getTopicQA(userId: String): List<CstQuestionVo> {
        val userInfo = userinfoMapper.selectByPrimaryKey(userId) ?: return emptyList()
        val perPage = 5
        val p = PageHelper.startPage<CstQuestion>(1, perPage)
        val result = mutableListOf<CstQuestionVo>()
        cstQuestionMapper.selectByExample(Example(CstQuestion::class.java).apply {
            createCriteria().orLike("cqScenes", "%${userInfo.extension2}%")
            orderBy("cqTotalnum")
        }).forEach {
            val answers = settingAnswerMapper.selectByExample(Example(SettingAnswer::class.java).apply {
                createCriteria().andEqualTo("cqGuid", it.cqGuid)
            })
            val vo = CstQuestionVo()
            BeanUtils.copyProperties(it, vo)
            vo.apply {
                cqKind = EnElementType.getNameByValue(it.cqKind)
                cqSubkind = EnElementSubType.getSubType(it.cqKind, it.cqSubkind)
            }
 
            if (answers.isNotEmpty()) {
                vo.answer = answers[0].saContent
            }
            result.add(vo)
        }
        return result
    }
 
    override fun getQuestionsByType(userId: String, kind: Byte?, subKind: Byte?, page: Int, perPage: Int): BaseResponse<List<ConsultResultVo>> {
        val userInfo = userinfoMapper.selectByPrimaryKey(userId) ?: return BaseResponse(false, "用户不存在")
        val result = mutableListOf<ConsultResultVo>()
        val p = PageHelper.startPage<CstQuestion>(page, perPage)
        val example = Example(CstQuestion::class.java).apply {
            createCriteria().apply {
                kind?.let { andEqualTo("cqKind", kind) }
                subKind?.let { andEqualTo("cqSubkind", subKind) }
            }
                .andLike("cqScenes", "%${userInfo.extension2}%")
            orderBy("cqTotalnum")
        }
        cstQuestionMapper.selectByExample(example).forEach {
            val answers = settingAnswerMapper.selectByExample(Example(SettingAnswer::class.java).apply {
                createCriteria().andEqualTo("cqGuid", it.cqGuid)
            })
 
            result.add(ConsultResultQAVo().apply {
                id = it.cqGuid
                name = it.cqContent
                if (answers.isNotEmpty()) {
                    des = answers[0].saContent
                }
                typeId = ConsultResultType.TYPE4.value
                typeName = ConsultResultType.TYPE4.des
                itemType = EnElementType.getNameByValue(it.cqKind)
                itemSubType = EnElementSubType.getSubType(it.cqKind, it.cqSubkind)
                time = it.cqCreateTime
            })
 
//            val vo = CstQuestionVo()
//            BeanUtils.copyProperties(it, vo)
//            vo.apply {
//                cqKind = EnElementType.getNameByValue(it.cqKind)
//                cqSubkind = EnElementSubType.getSubType(it.ecEpItemType, it.cqSubkind)
//            }
//
//            if (answers.isNotEmpty()) {
//                vo.answer = answers[0].saContent
//            }
//            result.add(vo)
        }
        return BaseResponse(true, head = DataHead(p.pageNum, p.pages, p.total), data = result)
    }
 
    override fun getQuestion(userId: String, qId: String): CstQuestionVo {
        val result = CstQuestionVo()
        cstQuestionMapper.selectByPrimaryKey(qId)?.let {
            BeanUtils.copyProperties(it, result)
            result.apply {
                cqKind = EnElementType.getNameByValue(it.cqKind)
                cqSubkind = EnElementSubType.getSubType(it.cqKind, it.cqSubkind)
            }
        }
 
        return result
    }
 
    override fun getAnswers(userId: String, qId: String): List<SettingAnswer> {
        return settingAnswerMapper.selectByExample(Example(SettingAnswer::class.java).apply {
            createCriteria().andEqualTo("cqGuid", qId)
        })
    }
 
    override fun getTopicCase(userId: String): List<EnforceCaseVo> {
        val userInfo = userinfoMapper.selectByPrimaryKey(userId) ?: return emptyList()
        val perPage = 5
        val p = PageHelper.startPage<MgtFile>(1, perPage)
        val result = mutableListOf<EnforceCaseVo>()
        enforceCaseMapper.selectByExample(Example(EnforceCase::class.java).apply {
            createCriteria().orLike("ecScenes", "%${userInfo.extension2}%")
                .orEqualTo("ecScenes", "")
                .orIsNull("ecScenes")
            orderBy("ecOccurDate").desc()
        }).forEach {
            val vo = EnforceCaseVo()
            BeanUtils.copyProperties(it, vo)
            vo.apply {
                ecEpItemType = EnElementType.getNameByValue(it.ecEpItemType)
                ecEpItemSubtype = EnElementSubType.getSubType(it.ecEpItemType, it.ecEpItemSubtype)
                ecType = EnforceCaseType.getNameByValue(it.ecType)
            }
            result.add(vo)
        }
        return result
    }
 
    override fun getCase(userId: String, caseId: String): EnforceCaseVo {
        val result = EnforceCaseVo()
        enforceCaseMapper.selectByPrimaryKey(caseId)?.let {
            BeanUtils.copyProperties(it, result)
            result.apply {
                ecEpItemType = EnElementType.getNameByValue(it.ecEpItemType)
                ecEpItemSubtype = EnElementSubType.getSubType(it.ecEpItemType, it.ecEpItemSubtype)
                ecType = EnforceCaseType.getNameByValue(it.ecType)
            }
        }
 
        return result
    }
 
    override fun getEnElementTypes(userId: String): List<Pair<String, Byte>> {
        return listOf(
            Pair(EnElementType.TYPE1.des, EnElementType.TYPE1.value),
            Pair(EnElementType.TYPE2.des, EnElementType.TYPE2.value),
            Pair(EnElementType.TYPE3.des, EnElementType.TYPE3.value),
            Pair(EnElementType.TYPE4.des, EnElementType.TYPE4.value),
            Pair(EnElementType.TYPE5.des, EnElementType.TYPE5.value),
            Pair(EnElementType.TYPE6.des, EnElementType.TYPE6.value),
            Pair(EnElementType.TYPE7.des, EnElementType.TYPE7.value),
            Pair(EnElementType.TYPE8.des, EnElementType.TYPE8.value),
            Pair(EnElementType.TYPE9.des, EnElementType.TYPE9.value),
            Pair(EnElementType.TYPE10.des, EnElementType.TYPE10.value),
            Pair(EnElementType.TYPE21.des, EnElementType.TYPE21.value),
            Pair(EnElementType.TYPE31.des, EnElementType.TYPE31.value),
            Pair(EnElementType.TYPE41.des, EnElementType.TYPE41.value),
            Pair(EnElementType.TYPE51.des, EnElementType.TYPE51.value),
            Pair(EnElementType.TYPE61.des, EnElementType.TYPE61.value),
            Pair(EnElementType.TYPE99.des, EnElementType.TYPE99.value),
        )
    }
 
    override fun getEnElementSubTypes(userId: String): List<List<Pair<String, Byte>>> {
        val result = mutableListOf<List<Pair<String, Byte>>>()
        EnElementSubType.subTypes.forEach { sList ->
            val list = mutableListOf<Pair<String, Byte>>()
            for (i in sList.indices) {
                if (i == sList.lastIndex) {
                    list.add(Pair(sList[i], 99.toByte()))
                } else {
                    list.add(Pair(sList[i], (i + 1).toByte()))
                }
            }
            result.add(list)
        }
        return result
    }
 
    private fun searchLaw2(userId: String, keyword: String, type: Byte?, page: Int, perPage: Int): BaseResponse<List<ConsultResultVo>> {
        val keywordList = if (keyword.isBlank()) {
            emptyList()
        } else {
            nlpController.execute(keyword)
        }
        val userInfo = userinfoMapper.selectByPrimaryKey(userId) ?: return BaseResponse(false)
        val response = BaseResponse<List<ConsultResultVo>>(false, head = DataHead(page, perPage))
        val result = mutableListOf<ConsultResultVo>()
 
        //当搜索不限制类型时,默认每种类型只获取前5项;当限制类型时,则根据前端请求分页数返回
        val _perPage = if (type == null) 5 else perPage
        val _page = page
 
        //法律法规文件
        if (type == null || type == ConsultResultType.TYPE1.value) {
            val p = PageHelper.startPage<MgtFile>(_page, _perPage)
            val resultMap = mutableMapOf<String, Pair<MgtFile, Int>>()
            val tempList = mutableListOf<Pair<MgtFile, Int>>()
            if (keywordList.isNotEmpty()) {
                keywordList.forEach { k ->
                    mgtFileMapper.selectByExample(Example(MgtFile::class.java).apply {
                        createCriteria().orLike("mfName", "%${k}%")
                            .orLike("mfShortName", "%${k}%")
                            .orLike("mfSummary", "%${k}%")
                            .orLike("mfKeywordLv1", "%${k}%")
                            .orLike("mfKeywordLv2", "%${k}%")
                            .orLike("mfKeywordLv3", "%${k}%")
                            .orLike("mfKeywordLv4", "%${k}%")
                    }).forEach { f ->
                        if (!resultMap.containsKey(f.mfGuid)) {
                            resultMap[f.mfGuid] = Pair(f, 0)
                        }
                        resultMap[f.mfGuid] = Pair(f, resultMap[f.mfGuid]?.second?.plus(1) ?: 0)
                    }
                }
                resultMap.forEach { (_, u) ->
                    tempList.add(u)
                }
                tempList.sortWith { o1, o2 ->
                    //排序条件一:关键词匹配次数
                    if (o1.second != o2.second) {
                        return@sortWith o2.second - o1.second
                    }
                    //排序条件二:浏览量
                    if (o1.first.mfRemark != o2.first.mfRemark) {
                        return@sortWith o2.first.mfRemark.toIntOrNull()?.minus(o1.first.mfRemark.toIntOrNull() ?: 0) ?: 0
                    }
                    //排序条件三:文件发布时间
                    (o2.first.mfReleaseDate.time - o1.first.mfReleaseDate.time).toInt()
                }
            } else {
                mgtFileMapper.selectByExample(Example(MgtFile::class.java).apply {
                    createCriteria().orLike("mfExtension1", "%${userInfo.extension2}%")
                        .orEqualTo("mfExtension1", "")
                        .orIsNull("mfExtension1")
                }).forEach {
                    tempList.add(Pair(it, 0))
                }
            }
 
            tempList.forEach {p ->
                val it = p.first
                result.add(ConsultResultFileVo().apply {
                    id = it.mfGuid
                    name = it.mfName
                    des = it.mfSummary
                    typeId = ConsultResultType.TYPE1.value
                    typeName = ConsultResultType.TYPE1.des
 
                    fileIndustry = IndustryType.getNameByValue(it.mfFileIndustry)
                    itemType = EnElementType.getNameByValue(it.mfEpItemType)
                    itemSubType = EnElementSubType.getSubType(it.mfEpItemType, it.mfEpItemSubtype)
                    fileUrl = it.mfFileUrl
                    val keyList = it.mfKeywordLv1.split("、")
                    for (i in keyList.indices) {
                        if (i < 5) {
                            keywords.add(keyList[i])
                        } else {
                            break
                        }
                    }
                    time = it.mfUpdateTime
                    fileType = ConsultFileType.getNameByValue(it.mfFileType)
                    referenceNumber = it.mfReferenceNumber
                    effectiveDate = DateUtil.DateToString(it.mfEffectiveDate, DateUtil.DateStyle.YYYY_MM_DD)
                    closingDate = DateUtil.DateToString(it.mfClosingDate, DateUtil.DateStyle.YYYY_MM_DD)
                    effective = Date().time > (it.mfClosingDate?.time ?: 0)
                })
            }
            response.success = true
            response.head?.run {
                this.page = p.pageNum
                this.totalPage = p.pages
            }
        }
        //法律法规条目
        if (type == null || type == ConsultResultType.TYPE2.value) {
            val map = mutableMapOf<String, MgtFile>()
            val p = PageHelper.startPage<MgtItem>(_page, _perPage)
 
            val resultMap = mutableMapOf<String, Pair<MgtItem, Int>>()
            val tempList = mutableListOf<Pair<MgtItem, Int>>()
            if (keywordList.isNotEmpty()) {
                mgtItemMapper.selectByExample(Example(MgtItem::class.java).apply {
                    createCriteria().apply {
                        keywordList.forEach { k ->
                            orLike("miChapterKeyword", "%${k}%")
                            orLike("miKeyword", "%${k}%")
                            orLike("miItemContent", "%${k}%")
                        }
                    }
                }).forEach { f ->
                    if (!resultMap.containsKey(f.miGuid)) {
                        resultMap[f.miGuid] = Pair(f, 0)
                    }
                    resultMap[f.miGuid] = Pair(f, resultMap[f.miGuid]?.second?.plus(1) ?: 0)
                }
                resultMap.forEach { (_, u) ->
                    tempList.add(u)
                }
                tempList.sortWith { o1, o2 ->
                    //排序条件一:关键词匹配次数
                    if (o1.second != o2.second) {
                        return@sortWith o2.second - o1.second
                    }
                    //排序条件二:浏览量
                    return@sortWith o2.first.miRemark.toIntOrNull()?.minus(o1.first.miRemark.toIntOrNull() ?: 0) ?: 0
                }
            } else {
                // TODO: 2022/9/8 没有关键字时,按照热门获取机制获取条目
                mgtItemMapper.selectByExample(Example(MgtItem::class.java).apply {
                    orderBy("miCreateTime").desc()
                }).forEach {
                    tempList.add(Pair(it, 0))
                }
            }
 
            tempList.forEach {p ->
                val it = p.first
                if (!map.containsKey(it.mfGuid)) {
                    map[it.mfGuid] = mgtFileMapper.selectByPrimaryKey(it.mfGuid)
                }
                result.add(ConsultResultItemVo().apply {
                    id = it.miGuid
                    name = it.miItemName
                    des = it.miItemContent
                    typeId = ConsultResultType.TYPE2.value
                    typeName = ConsultResultType.TYPE2.des
 
//                    fileIndustry = IndustryType.getNameByValue(it.mfFileIndustry)
//                    itemType = EnElementType.getNameByValue(it.mfEpItemType)
//                    fileUrl = it.mfFileUrl
                    val keyList = it.miKeyword.split("、")
                    for (i in keyList.indices) {
                        if (i < 5) {
                            keywords.add(keyList[i])
                        } else {
                            break
                        }
                    }
                    time = it.miUpdateTime
 
                    chapterName = it.miChapterName
                    fileId = it.mfGuid
                    fileName = it.mfName
                    referenceNumber = it.mfReferenceNumber
                    effectiveDate = DateUtil.DateToString(map[it.mfGuid]?.mfEffectiveDate, DateUtil.DateStyle.YYYY_MM_DD)
                    closingDate = DateUtil.DateToString(map[it.mfGuid]?.mfClosingDate, DateUtil.DateStyle.YYYY_MM_DD)
                    effective = Date().time > (map[it.mfGuid]?.mfClosingDate?.time ?: 0)
                    relatedItems = it.miRelatedItems?.split(";")?.size ?: 0
                })
            }
            response.success = true
            response.head?.run {
                this.page = p.pageNum
                this.totalPage = p.pages
            }
        }
        //环保问题
        if (type == null || type == ConsultResultType.TYPE4.value) {
            val p = PageHelper.startPage<CstQuestion>(_page, _perPage)
 
            val resultMap = mutableMapOf<String, Pair<CstQuestion, Int>>()
            val tempList = mutableListOf<Pair<CstQuestion, Int>>()
            if (keywordList.isNotEmpty()) {
                cstQuestionMapper.selectByExample(Example(CstQuestion::class.java).apply {
                    createCriteria().orLike("cqScenes", "%${userInfo.extension2}%")
                        .orEqualTo("cqScenes", "")
                        .orIsNull("cqScenes")
                    and(
                        createCriteria().apply {
                            keywordList.forEach { k ->
                                orLike("cqContent", "%${k}%")
                                orLike("cqKeywords", "%${k}%")
                            }
                        }
                    )
                }).forEach { f ->
                    if (!resultMap.containsKey(f.cqGuid)) {
                        resultMap[f.cqGuid] = Pair(f, 0)
                    }
                    resultMap[f.cqGuid] = Pair(f, resultMap[f.cqGuid]?.second?.plus(1) ?: 0)
                }
                resultMap.forEach { (_, u) ->
                    tempList.add(u)
                }
                tempList.sortWith { o1, o2 ->
                    //排序条件一:关键词匹配次数
                    if (o1.second != o2.second) {
                        return@sortWith o2.second - o1.second
                    }
                    //排序条件二:浏览量
                    return@sortWith o2.first.cqRemark?.toIntOrNull()?.minus(o1.first.cqRemark.toIntOrNull() ?: 0) ?: 0
                }
            } else {
                // TODO: 2022/9/8 没有关键字时,按照热门获取机制获取
                cstQuestionMapper.selectByExample(Example(CstQuestion::class.java).apply {
                    createCriteria().orLike("cqScenes", "%${userInfo.extension2}%")
                    orderBy("cqTotalnum")
                }).forEach {
                    tempList.add(Pair(it, 0))
                }
            }
            tempList.forEach {p ->
                val it = p.first
                val answers = settingAnswerMapper.selectByExample(Example(SettingAnswer::class.java).apply {
                    createCriteria().andEqualTo("cqGuid", it.cqGuid)
                })
 
                result.add(ConsultResultQAVo().apply {
                    id = it.cqGuid
                    name = it.cqContent
                    if (answers.isNotEmpty()) {
                        des = answers[0].saContent
                    }
                    typeId = ConsultResultType.TYPE4.value
                    typeName = ConsultResultType.TYPE4.des
 
//                    fileIndustry = IndustryType.getNameByValue(it.mfFileIndustry)
                    itemType = EnElementType.getNameByValue(it.cqKind)
                    itemSubType = EnElementSubType.getSubType(it.cqKind, it.cqSubkind)
//                    fileUrl = it.mfFileUrl
//                    val keyList = it.mfKeywordLv1.split("、")
//                    for (i in keyList.indices) {
//                        if (i < 5) {
//                            keywords.add(keyList[i])
//                        } else {
//                            break
//                        }
//                    }
                    time = it.cqCreateTime
                    punish = it.cqIsPunish
                    illegal = it.cqIsIllegal
                    shotSpot = it.cqIsShotspot
                    supervise = it.cqIsSupervise
                })
            }
            response.success = true
            response.head?.run {
                this.page = p.pageNum
                this.totalPage = p.pages
            }
        }
        //执法案例
        if (type == null || type == ConsultResultType.TYPE3.value) {
            val p = PageHelper.startPage<EnforceCase>(_page, _perPage)
 
            val resultMap = mutableMapOf<String, Pair<EnforceCase, Int>>()
            val tempList = mutableListOf<Pair<EnforceCase, Int>>()
            if (keywordList.isNotEmpty()) {
                enforceCaseMapper.selectByExample(Example(EnforceCase::class.java).apply {
 
                    createCriteria().orLike("ecScenes", "%${userInfo.extension2}%")
                        .orEqualTo("ecScenes", "")
                        .orIsNull("ecScenes")
                    and(
                        createCriteria().apply {
                            keywordList.forEach { k ->
                                orLike("ecTitle", "%${k}%")
                                orLike("ecKeywords", "%${k}%")
                            }
                        }
                    )
                }).forEach { f ->
                    if (!resultMap.containsKey(f.ecGuid)) {
                        resultMap[f.ecGuid] = Pair(f, 0)
                    }
                    resultMap[f.ecGuid] = Pair(f, resultMap[f.ecGuid]?.second?.plus(1) ?: 0)
                }
                resultMap.forEach { (_, u) ->
                    tempList.add(u)
                }
                tempList.sortWith { o1, o2 ->
                    //排序条件一:关键词匹配次数
                    if (o1.second != o2.second) {
                        return@sortWith o2.second - o1.second
                    }
                    //排序条件二:浏览量
                    return@sortWith o2.first.ecRemark.toIntOrNull()?.minus(o1.first.ecRemark.toIntOrNull() ?: 0) ?: 0
                }
            } else {
                // TODO: 2022/9/8 没有关键字时,按照热门获取机制获取
                enforceCaseMapper.selectByExample(Example(EnforceCase::class.java).apply {
                    createCriteria().orLike("ecScenes", "%${userInfo.extension2}%")
                        .orEqualTo("ecScenes", "")
                        .orIsNull("ecScenes")
                    orderBy("ecOccurDate").desc()
                }).forEach {
                    tempList.add(Pair(it, 0))
                }
            }
            tempList.forEach {p ->
                val it = p.first
                result.add(ConsultResultCaseVo().apply {
                    id = it.ecGuid
                    name = it.ecTitle
                    des = it.ecSummary
                    typeId = ConsultResultType.TYPE3.value
                    typeName = ConsultResultType.TYPE3.des
 
//                    fileIndustry = IndustryType.getNameByValue(it.mfFileIndustry)
                    itemType = EnElementType.getNameByValue(it.ecEpItemType)
                    itemSubType = EnElementSubType.getSubType(it.ecEpItemType, it.ecEpItemSubtype)
                    imgUrl = it.ecAppendixUrl?.split(";")?.get(0)
                    time = it.ecCreateTime
 
                    provinceName = it.ecProvinceName
                    cityName = it.ecCityName
                    occurDate = DateUtil.DateToString(it.ecOccurDate, DateUtil.DateStyle.YYYY_MM_DD)
                    punish = it.ecIsPunish
                    detained = it.ecIsDetained
                    illegal = it.ecIsIllegal
                    shotSpot = it.ecIsShotspot
                    supervise = it.ecIsSupervise
                    minor = it.ecIsMinor
                })
            }
            response.success = true
            response.head?.run {
                this.page = p.pageNum
                this.totalPage = p.pages
            }
        }
 
 
        return response.apply { data = result }
    }
}