riku
2025-07-02 3013b813e5df6977c0be921928f73b1a3adde290
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
package cn.flightfeather.thirdappmodule.module.dataanalysis
 
import android.app.DatePickerDialog
import android.app.Dialog
import android.graphics.Color
import android.icu.text.DecimalFormat
import android.icu.util.Calendar
import android.os.Build
import android.os.Bundle
import android.support.annotation.RequiresApi
import android.support.design.widget.FloatingActionButton
import android.support.v4.app.Fragment
import android.support.v7.widget.DividerItemDecoration
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
import cn.flightfeather.thirdappmodule.CommonApplication
import cn.flightfeather.thirdappmodule.R
import cn.flightfeather.thirdappmodule.adapter.AllListViewAdapter
import cn.flightfeather.thirdappmodule.adapter.AllRecyclerViewAdapter
import cn.flightfeather.thirdappmodule.bean.entity.*
import cn.flightfeather.thirdappmodule.bean.vo.AreaVo
import cn.flightfeather.thirdappmodule.bean.vo.ProblemlistVo
import cn.flightfeather.thirdappmodule.httpservice.ProblemListService
import cn.flightfeather.thirdappmodule.util.*
import cn.flightfeather.thirdappmodule.view.ScrollTextView
import com.github.mikephil.charting.animation.Easing
import com.github.mikephil.charting.charts.PieChart
import com.github.mikephil.charting.components.Legend
import com.github.mikephil.charting.data.PieData
import com.github.mikephil.charting.data.PieDataSet
import com.github.mikephil.charting.data.PieEntry
import com.github.mikephil.charting.formatter.PercentFormatter
import com.github.mikephil.charting.utils.ColorTemplate
import com.ping.greendao.gen.DomainitemDao
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import java.util.*
 
/**
 * A simple [Fragment] subclass.
 */
class AnalysisProblemFragment : Fragment(), View.OnClickListener, AdapterView.OnItemSelectedListener {
    private var application: CommonApplication? = null
    private var problemListService: ProblemListService? = null
    private var TIME_TYPE = 0//0:快速选择/1:任意时段
    private var BRIEF_TIME = "本月" //初始默认选择本月
    private var problemlists = mutableListOf<ProblemlistVo>()
    private var mChart: PieChart? = null
    private val entries = ArrayList<PieEntry>() //饼图数据
 
    //下拉框数据
    private var allProvinceData: List<Province>? = null //所有省份
    private var provinceData: List<Province> = ArrayList() //省份
    private var allCityData: List<City> = ArrayList() //所有城市
    private var cityData: MutableList<City> = ArrayList() //城市
    private var allDistrictData: List<District> = ArrayList() //所有区县
    private val districtData: MutableList<District> = ArrayList() //区县
    private var allTownData: List<Town> = ArrayList() //所有街镇
    private val townData: MutableList<Town> = ArrayList() //街镇
    private val scenseTypeData: MutableList<Domainitem> = ArrayList() //场景类型
    private val dateData: MutableList<String> = ArrayList() //时间
 
    //下拉框数据适配器
    private var provinceAdapter: AllListViewAdapter<Province>? = null
    private var cityAdapter: AllListViewAdapter<City>? = null
    private var districtAdapter: AllListViewAdapter<District>? = null
    private var townAdapter: AllListViewAdapter<Town>? = null
    private var scenseAdapter: AllListViewAdapter<Domainitem>? = null
 
    //    private AllListViewAdapter<String> dateAdapter= null;
    //RecyclerView时间快速选择列表
    private var dateAdapter: AllRecyclerViewAdapter<String>? = null
    private var entryAdapter: AllRecyclerViewAdapter<PieEntry>? = null
 
    //
    private var RV_quick_choose: RecyclerView? = null//时间快速选择
    private var rv_problem_type: RecyclerView? = null//问题类型统计
 
    //
    private var LL_any_time: LinearLayout? = null
 
    //
    private val IV_problem: ImageView? = null
    private var IV_scene_type: ImageView? = null
 
    //
    private val TV_problem: TextView? = null
    private var TV_area: TextView? = null
    private var tv_Cancel: TextView? = null
    private var tv_Ok: TextView? = null
    private var TV_quick_choose: TextView? = null
    private var TV_any_time: TextView? = null
    private var TV_start_time: TextView? = null
    private var TV_end_time: TextView? = null
    private val TV_date_display1: TextView? = null
    private var stv_date_display2: ScrollTextView? = null
 
    //
    private val PC_problem: PieChart? = null
 
    //
    private var SP_scense_type: Spinner? = null
 
    //    private Spinner SP_date;
    private var SP_province: Spinner? = null
    private var SP_city: Spinner? = null
    private var SP_district: Spinner? = null
    private var SP_town: Spinner? = null
 
    //
    private var FAB_menu: FloatingActionButton? = null
 
    //
    private var dialog: Dialog? = null
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_anysis_problem, container, false)
    }
 
    @RequiresApi(api = Build.VERSION_CODES.N)
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        application = activity!!.application as CommonApplication
        problemListService = application!!.retrofit.create(ProblemListService::class.java)
        initUI(view)
        initDialog()
        initPieChart(view)
        initSpinnerData()
        initSpinner(view)
        initRecyclerView(view)
    }
 
    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
    }
 
    override fun onDestroy() {
        super.onDestroy()
        dialog!!.dismiss()
    }
 
    private fun initUI(view: View) {
        IV_scene_type = view.findViewById<View>(R.id.IV_scense_type) as ImageView
        FAB_menu = view.findViewById<View>(R.id.FAB_menu) as FloatingActionButton
        TV_area = view.findViewById<View>(R.id.TV_area) as TextView
        stv_date_display2 = view.findViewById<View>(R.id.stv_date_display2) as ScrollTextView
        FAB_menu!!.setOnClickListener(this)
    }
 
    /**
     * 初始化下拉框数据(包括popupWindow)
     */
    private fun initSpinnerData() {
        val provinceDao = application!!.daoSession.provinceDao
        val cityDao = application!!.daoSession.cityDao
        val districtDao = application!!.daoSession.districtDao
        val townDao = application!!.daoSession.townDao
        val domainitemDao = application!!.daoSession.domainitemDao
        allProvinceData = provinceDao.loadAll()
        provinceData = provinceDao.loadAll()
        allCityData = cityDao.loadAll()
        cityData = cityDao.loadAll()
        allDistrictData = districtDao.loadAll()
        val emptydistrict = District()
        emptydistrict.districtname = ALLDISTRICT
        districtData.add(emptydistrict)
        allTownData = townDao.loadAll()
        val emptytown = Town()
        emptytown.townname = ALLTOWN
        townData.add(emptytown)
 
        //场景类型
        val queryBuilder = domainitemDao.queryBuilder()
            .where(DomainitemDao.Properties.Dcguid.eq(Domain.DOMAINGUID_SCENSETYPE))
            .orderAsc(DomainitemDao.Properties.Value)
        scenseTypeData.addAll(queryBuilder.list())
        dateData.add(Constant.THIS_WEEK)
        dateData.add(Constant.LAST_WEEK)
        dateData.add(Constant.THIS_MONTH)
        dateData.add(Constant.LAST_MONTH)
        dateData.add(Constant.THIS_SEASON)
        dateData.add(Constant.LAST_SEASON)
        dateData.add(Constant.THIS_YEAR)
        dateData.add(Constant.LAST_YEAR)
    }
 
    private fun initSpinner(view: View) {
        SP_scense_type = view.findViewById<View>(R.id.SP_scense_type) as Spinner
        SP_scense_type!!.onItemSelectedListener = this
        //        SP_date.setOnItemSelectedListener(this);
        initAdapter()
 
//        dialog.dismiss();
        SP_province!!.setSelection(0)
        SP_city!!.setSelection(0)
        SP_district!!.setSelection(0)
        SP_town!!.setSelection(0)
        SP_scense_type!!.setSelection(0) //会同时通过onClick时间触发setChartTitle(),所以放在最后
    }
 
    /**
     * 问题类型分布的列表
     * @param view 问题类型分布fragment
     */
    @RequiresApi(api = Build.VERSION_CODES.N)
    private fun initRecyclerView(view: View) {
        val decimalFormat = DecimalFormat("#0.00")
        rv_problem_type = view.findViewById<View>(R.id.rv_problem_type) as RecyclerView
        entryAdapter = object : AllRecyclerViewAdapter<PieEntry>(entries, R.layout.item_day_task_progress, context) {
            override fun bindView(holder: MyViewHolder, obj: PieEntry, bool: Boolean, position: Int) {
                var total = 0f
                for (entry in entries) {
                    total += entry.value
                }
                holder.setText(R.id.tv_item1, entries[position].label)
                    .setText(R.id.tv_item2, entries[position].value.toString() + "")
                    .setText(R.id.tv_item3, decimalFormat.format((entries[position].value / total * 100).toDouble()) + "%")
            }
        }
        val layoutManager = LinearLayoutManager(context)
        layoutManager.orientation = LinearLayoutManager.VERTICAL
        rv_problem_type!!.layoutManager = layoutManager
        rv_problem_type!!.addItemDecoration(DividerItemDecoration(context, DividerItemDecoration.VERTICAL))
        rv_problem_type!!.adapter = entryAdapter
    }
 
    private fun initPieChart(view: View) {
 
        //饼图样式设置***************************************************************************************************
        mChart = view.findViewById<View>(R.id.PC_problem) as PieChart
        mChart!!.setUsePercentValues(true)
        mChart!!.description.isEnabled = false
        mChart!!.setExtraOffsets(5f, 10f, 5f, 5f)
        //        mChart.setExtraOffsets(20.f, 0.f, 20.f, 0.f);
        mChart!!.dragDecelerationFrictionCoef = 0.95f
 
//        mChart.setCenterTextTypeface(mTfLight);
//        mChart.setCenterText(generateCenterSpannableText());
        mChart!!.isDrawHoleEnabled = true
        mChart!!.setHoleColor(Color.WHITE)
        mChart!!.setTransparentCircleColor(Color.WHITE)
        mChart!!.setTransparentCircleAlpha(110)
        mChart!!.holeRadius = 39f
        mChart!!.transparentCircleRadius = 41f
        mChart!!.setDrawCenterText(true)
        mChart!!.rotationAngle = 0f
        // enable rotation of the chart by touch
        mChart!!.isRotationEnabled = true
        mChart!!.isHighlightPerTapEnabled = true
        val l = mChart!!.legend
        l.verticalAlignment = Legend.LegendVerticalAlignment.TOP
        l.horizontalAlignment = Legend.LegendHorizontalAlignment.LEFT
        l.orientation = Legend.LegendOrientation.VERTICAL
        l.setDrawInside(false)
        l.xEntrySpace = 7f
        l.yEntrySpace = 0f
        l.yOffset = 0f
 
        // entry label styling
        mChart!!.setEntryLabelColor(Color.WHITE)
        //        mChart.setEntryLabelTypeface(mTfRegular);
        mChart!!.setEntryLabelTextSize(12f)
        mChart!!.setDrawEntryLabels(false)
 
        //test
//        setData(new ArrayList<ProblemlistVo>());
        //
    }
 
    private fun setData(problemlists: MutableList<ProblemlistVo>) {
        //数据初始化***************************************************************************************************
        entries.clear()
        val dataSet = PieDataSet(entries, "问题类型")
 
        //test*****
//        int count = 5;
//        int mult = 100;
//        String[] mParties = {"路面积尘", "地面局部未硬化", "站厂出入口无外围护", "无喷淋装置", REMAIN_PROBLEM};
//        for (int i = 0; i < count; i++) {
//            entries.add(new PieEntry((int) (Math.random() * mult) + mult / 5, mParties[i]));
//        }
        //*********
 
        //初始化数据
        val problemTypeMap = HashMap<String, Int>()
        while (problemlists.isNotEmpty()) {
            val problemType = problemlists[0].typename
            if (problemType == null) {
                problemlists.removeAt(0)
                continue
            }
            var problemCount = 0
            var i = 0
            while (i < problemlists.size) {
                if (problemType == problemlists[i].typename) {
                    problemCount++
                    problemlists.removeAt(i)
                    i--
                }
                i++
            }
            problemTypeMap[problemType] = problemCount
        }
        //按HashMap的value降序排序
        val list: List<Map.Entry<String, Int>> = ArrayList<Map.Entry<String, Int>>(problemTypeMap.entries)
        Collections.sort(list) { o1, o2 -> o2.value.compareTo(o1.value) }
        //        problemTypeMap.clear();
//        problemTypeMap.entrySet().addAll(list);
        //初始化Entry,限定最多显示5个扇区,超出的合并为第5个扇区
        var remainProblemType = ""
        var remainProblemCount = 0
        var chartcount = 0
        for ((key, value) in list) {
            if (chartcount < MAX_CHART_COUNT - 1) {
                entries.add(PieEntry(value.toFloat(), key))
            } else {
                remainProblemCount += value
                remainProblemType = key
            }
            chartcount++
        }
        if (chartcount == MAX_CHART_COUNT) {
            entries.add(PieEntry(remainProblemCount.toFloat(), remainProblemType))
        } else if (chartcount > MAX_CHART_COUNT) {
            entries.add(PieEntry(remainProblemCount.toFloat(), REMAIN_PROBLEM))
        }
 
        // add a lot of colors
        val colors = ArrayList<Int>()
        for (c in ColorTemplate.JOYFUL_COLORS) colors.add(c)
        for (c in ColorTemplate.COLORFUL_COLORS) colors.add(c)
        for (c in ColorTemplate.VORDIPLOM_COLORS) colors.add(c)
        for (c in ColorTemplate.LIBERTY_COLORS) colors.add(c)
        for (c in ColorTemplate.PASTEL_COLORS) colors.add(c)
        colors.add(ColorTemplate.getHoloBlue())
        dataSet.colors = colors
 
        //value line
        dataSet.valueLinePart1OffsetPercentage = 80f
        dataSet.valueLinePart1Length = 0.2f
        dataSet.valueLinePart2Length = 0.4f
        //dataSet.setXValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
        dataSet.yValuePosition = PieDataSet.ValuePosition.OUTSIDE_SLICE
 
        //
        val data = PieData(dataSet)
        data.setValueFormatter(PercentFormatter())
        data.setValueTextSize(11f)
        data.setValueTextColor(Color.BLACK)
        //        data.setValueTypeface(mTfLight);
        if (!entries.isEmpty()) { //数据全为空时,不显示
            mChart!!.data = data
        } else {
            mChart!!.data = null
        }
        mChart!!.setNoDataText("暂无数据")
 
        // undo all highlights
        mChart!!.highlightValues(null)
        mChart!!.animateY(1400, Easing.EasingOption.EaseInOutQuad)
    }
 
    /**
     * 初始化下拉框适配器
     */
    private fun initAdapter() {
        //****下面为设置适配器**********************************************
        //省份
        provinceAdapter = object : AllListViewAdapter<Province>(provinceData as ArrayList<Province>, R.layout.item_scense_detail_list) {
            override fun bindView(holder: ViewHolder, obj: Province) {
                holder.setText(R.id.tv_item, obj.provincename)
            }
        }
        SP_province!!.adapter = provinceAdapter
        provinceAdapter?.notifyDataSetChanged()
        //城市
        cityAdapter = object : AllListViewAdapter<City>(cityData as ArrayList<City>, R.layout.item_scense_detail_list) {
            override fun bindView(holder: ViewHolder, obj: City) {
                holder.setText(R.id.tv_item, obj.cityname)
            }
        }
        SP_city!!.adapter = cityAdapter
        cityAdapter?.notifyDataSetChanged()
        //区县
        districtAdapter = object : AllListViewAdapter<District>(districtData as ArrayList<District>, R.layout.item_scense_detail_list) {
            override fun bindView(holder: ViewHolder, obj: District) {
                holder.setText(R.id.tv_item, obj.districtname)
            }
        }
        SP_district!!.adapter = districtAdapter
        districtAdapter?.notifyDataSetChanged()
        //街镇
        townAdapter = object : AllListViewAdapter<Town>(townData as ArrayList<Town>, R.layout.item_scense_detail_list) {
            override fun bindView(holder: ViewHolder, obj: Town) {
                holder.setText(R.id.tv_item, obj.townname)
            }
        }
        SP_town!!.adapter = townAdapter
        //场景类型
        scenseAdapter = object : AllListViewAdapter<Domainitem>(scenseTypeData as ArrayList<Domainitem>, R.layout.item_spinner_simple_text) {
            override fun bindView(holder: ViewHolder, obj: Domainitem) {
                holder.setText(R.id.tv_item, obj.text)
            }
        }
        SP_scense_type!!.adapter = scenseAdapter
        scenseAdapter?.notifyDataSetChanged()
        //时间
        dateAdapter = object : AllRecyclerViewAdapter<String>(dateData, R.layout.item_scense_detail_list, context) {
            override fun bindView(holder: MyViewHolder, obj: String, bool: Boolean, position: Int) {
                holder.setText(R.id.tv_item, obj)
                holder.setSelected(R.id.tv_item, bool)
                holder.setOnClickListener(R.id.tv_item) {
                    dateAdapter!!.setAllFalse()
                    dateAdapter!!.setSelected(true, position)
                    dateAdapter!!.notifyDataSetChanged()
                    BRIEF_TIME = obj
                    Log.e("对象名称", obj)
                    showDialog()
                    search()
                }
                if (BRIEF_TIME == obj) {
                    holder.setSelected(R.id.tv_item, true)
                }
            }
        }
        RV_quick_choose!!.adapter = dateAdapter
        dateAdapter?.notifyDataSetChanged()
        //****上面为设置适配器**********************************************
    }
 
    /**
     * 区域选择弹出框
     */
    private fun initDialog() {
//        int mScreenWidth = getResources().getDisplayMetrics().widthPixels;
        // 讲布局文件转换成 view 对象
        dialog = Dialog(context)
        dialog!!.setContentView(R.layout.popwindow)
        //设置dialog宽度
        val dialogWindow = dialog!!.window!!
        dialogWindow.setBackgroundDrawableResource(android.R.color.transparent)
        val p = dialogWindow.attributes
        p.width = ScreenUtils.getScreenWidth(context)
        //        p.height = (int) (ScreenUtils.getScreenHeight(context) * 0.9);
        /* 获取 PopupWindow 加载布局文件中的view */SP_province = dialog!!.findViewById<View>(R.id.SP_province) as Spinner
        SP_city = dialog!!.findViewById<View>(R.id.SP_city) as Spinner
        SP_district = dialog!!.findViewById<View>(R.id.SP_district) as Spinner
        SP_town = dialog!!.findViewById<View>(R.id.SP_town) as Spinner
        SP_province!!.onItemSelectedListener = this
        SP_city!!.onItemSelectedListener = this
        SP_district!!.onItemSelectedListener = this
        SP_town!!.onItemSelectedListener = this
        //        SP_province.setSelection(0);
//        SP_city.setSelection(0);
//        SP_district.setSelection(0);
//        SP_town.setSelection(0);
        /*设置时间选择按钮*/TV_quick_choose = dialog!!.findViewById<View>(R.id.TV_quick_choose) as TextView
        TV_any_time = dialog!!.findViewById<View>(R.id.TV_any_time) as TextView
        TV_quick_choose!!.isSelected = true
        TV_quick_choose!!.setOnClickListener(this)
        TV_any_time!!.setOnClickListener(this)
        /*设置快速时间选择列表*/TV_start_time = dialog!!.findViewById<View>(R.id.TV_start_time) as TextView
        TV_end_time = dialog!!.findViewById<View>(R.id.TV_end_time) as TextView
        TV_start_time!!.text = DateFormatter.dateFormat.format(CommonUtils.getTimesMonthmorning())
        TV_end_time!!.text = DateFormatter.dateFormat.format(CommonUtils.getTimesMonthnight())
        TV_start_time!!.setOnClickListener(this)
        TV_end_time!!.setOnClickListener(this)
        //设置显示对其方式
        val layoutManager = GridLayoutManager(context, 2)
        layoutManager.orientation = GridLayoutManager.VERTICAL
        layoutManager.isAutoMeasureEnabled = true
        RV_quick_choose = dialog!!.findViewById<View>(R.id.RV_quick_choose) as RecyclerView
        RV_quick_choose!!.layoutManager = layoutManager
        RV_quick_choose!!.addItemDecoration(DividerItemDecoration(context, DividerItemDecoration.VERTICAL))
        /* 设置任意时间选择列表*/LL_any_time = dialog!!.findViewById<View>(R.id.LL_any_time) as LinearLayout
        LL_any_time!!.visibility = View.GONE
        /*设置确定、取消按钮*/tv_Cancel = dialog!!.findViewById<View>(R.id.btn_cancel) as TextView
        tv_Ok = dialog!!.findViewById<View>(R.id.btn_ok) as TextView
        tv_Cancel!!.setOnClickListener(this)
        tv_Ok!!.setOnClickListener(this)
        dialog!!.show()
        //        dialog.hide();
        // 点击会自动 PopupWindow 关闭
//        dialog.setOutsideTouchable(false);
        /* 设置 PopupWindow 出现时的动画 */
//        dialog.setAnimationStyle(R.style.anim_menu_dialog);
        /* 设置背景颜色 */
        // dialog.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
//        dialog.update();
        /* 允许 PopupWindow 获取焦点 */
//        this.dialog.setFocusable(false);
    }
 
    /**
     * 显示和隐藏Popupwindow
     */
    private fun showDialog() {
        // 设置显示的位置
        if (!dialog!!.isShowing) dialog!!.show() else dialog!!.dismiss()
    }
 
    /**
     * 按照搜索条件查询问题分布结果
     */
    private fun search() {
        val dialog: Dialog = DialogUtil.createLoadingDialog(activity, "加载中...")
        var areaVo = AreaVo()
        areaVo.provincecode = (SP_province!!.selectedItem as Province).provincecode
        areaVo.provincename = (SP_province!!.selectedItem as Province).provincename
        areaVo.citycode = (SP_city!!.selectedItem as City).citycode
        areaVo.cityname = (SP_city!!.selectedItem as City).cityname
        areaVo.districtcode = (SP_district!!.selectedItem as District).districtcode
        areaVo.districtname = (SP_district!!.selectedItem as District).districtname
        areaVo.towncode = (SP_town!!.selectedItem as Town).towncode
        areaVo.townname = (SP_town!!.selectedItem as Town).townname
        areaVo = gettime(areaVo)
        val position = SP_scense_type!!.selectedItemPosition
        areaVo.scensetypeid = scenseTypeData[position].value
        val getProblemByArea = problemListService!!.getByArea(areaVo)
        getProblemByArea.enqueue(object : Callback<List<ProblemlistVo>?> {
            override fun onResponse(call: Call<List<ProblemlistVo>?>, response: Response<List<ProblemlistVo>?>) {
                val list = response.body()
                if (list is List<ProblemlistVo>) {
                    problemlists.addAll(list)
                }
                setData(problemlists)
                entryAdapter!!.notifyDataSetChanged() //刷新列表
                DialogUtil.closeDialog(dialog)
            }
 
            override fun onFailure(call: Call<List<ProblemlistVo>?>, t: Throwable) {
                setData(problemlists)
                DialogUtil.closeDialog(dialog)
            }
        })
        setChartTitle() //刷新标题
        setDateDisplay() //刷新时间
    }
 
    /**
     * 设定饼图标题及图标
     */
    private fun setChartTitle() {
        var title = (SP_province!!.selectedItem as Province).provincename
        if (title != (SP_city!!.selectedItem as City).cityname) title = title + (SP_city!!.selectedItem as City).cityname
        if ((SP_district!!.selectedItem as District).districtname != ALLDISTRICT) title = title + (SP_district!!.selectedItem as District).districtname
        if ((SP_town!!.selectedItem as Town).townname != ALLTOWN) title = title + (SP_town!!.selectedItem as Town).townname
        title = title + "问题类型分布情况"
        TV_area!!.text = title
        IV_scene_type!!.setImageResource(CommonUtils.getIconBySceneType((SP_scense_type!!.selectedItem as Domainitem).value))
    }
 
    /**
     * 获取所选的时间
     * @param areaVo RequestBody,查询问题列表
     * @return
     */
    private fun gettime(areaVo: AreaVo): AreaVo {
        if (TIME_TYPE == 0) { //快速选择
            val date1: Date
            val date2: Date
            when (BRIEF_TIME) {
                Constant.THIS_WEEK -> {
                    date1 = CommonUtils.getTimesWeekmorning()
                    date2 = CommonUtils.getTimesWeeknight()
                }
                Constant.LAST_WEEK -> {
                    date1 = CommonUtils.getTimesLastWeekmorning()
                    date2 = CommonUtils.getTimesLastWeeknight()
                }
                Constant.THIS_MONTH -> {
                    date1 = CommonUtils.getTimesMonthmorning()
                    date2 = CommonUtils.getTimesMonthnight()
                }
                Constant.LAST_MONTH -> {
                    date1 = CommonUtils.getTimesLastMonthmorning()
                    date2 = CommonUtils.getTimesLastMonthnight()
                }
                Constant.THIS_SEASON -> {
                    date1 = CommonUtils.getTimesSeasonmorning()
                    date2 = CommonUtils.getTimesSeasonnight()
                }
                Constant.LAST_SEASON -> {
                    date1 = CommonUtils.getTimesLastSeasonmorning()
                    date2 = CommonUtils.getTimesLastSeasonnight()
                }
                Constant.THIS_YEAR -> {
                    date1 = CommonUtils.getCurrentYearStartTime()
                    date2 = CommonUtils.getCurrentYearEndTime()
                }
                Constant.LAST_YEAR -> {
                    date1 = CommonUtils.getLastYearStartTime()
                    date2 = CommonUtils.getLastYearEndTime()
                }
                else -> {
                    date1 = CommonUtils.getTimesMonthmorning()
                    date2 = CommonUtils.getTimesMonthnight()
                }
            }
            areaVo.starttime = DateFormatter.dateTimeFormat2.format(date1)
            areaVo.endtime = DateFormatter.dateTimeFormat2.format(date2)
        } else if (TIME_TYPE == 1) { //任意时段
            areaVo.starttime = TV_start_time!!.text.toString()
            areaVo.endtime = TV_end_time!!.text.toString()
            return areaVo
        }
        return areaVo
    }
 
    /**
     * 设置所选时间的显示
     */
    private fun setDateDisplay() {
        if (TIME_TYPE == 0) { //快速选择
//            TV_date_display1.setTitle(BRIEF_TIME);
            stv_date_display2!!.text = BRIEF_TIME
        } else if (TIME_TYPE == 1) { //任意时段
//            TV_date_display1.setTitle(TV_start_time.getTitle().toString());
            stv_date_display2!!.text = TV_start_time!!.text.toString() + " ~ " + TV_end_time!!.text.toString()
        }
    }
 
    @RequiresApi(api = Build.VERSION_CODES.N)
    override fun onClick(v: View) {
        when (v.id) {
            R.id.FAB_menu -> showDialog()
            R.id.btn_cancel -> showDialog()
            R.id.btn_ok -> {
                search()
                showDialog()
            }
            R.id.TV_quick_choose -> {
                TV_quick_choose!!.isSelected = true
                TV_any_time!!.isSelected = false
                TV_quick_choose!!.setTextColor(Color.WHITE)
                TV_any_time!!.setTextColor(Color.BLACK)
                RV_quick_choose!!.visibility = View.VISIBLE
                LL_any_time!!.visibility = View.GONE
                TIME_TYPE = 0
            }
            R.id.TV_any_time -> {
                TV_any_time!!.isSelected = true
                TV_quick_choose!!.isSelected = false
                TV_any_time!!.setTextColor(Color.WHITE)
                TV_quick_choose!!.setTextColor(Color.BLACK)
                LL_any_time!!.visibility = View.VISIBLE
                RV_quick_choose!!.visibility = View.GONE
                TIME_TYPE = 1
            }
            R.id.TV_start_time -> {
                dialog!!.hide()
                val calendar1 = Calendar.getInstance()
                DatePickerDialog(activity, { datePicker, year, month, day ->
                    var result = ""
                    result += year.toString() + "." + (month + 1) + "." + day + "."
                    Toast.makeText(getContext(), result, Toast.LENGTH_SHORT).show()
                    dialog!!.show()
                    TV_start_time!!.text = result
                }, calendar1[Calendar.YEAR], calendar1[Calendar.MONTH], calendar1[Calendar.DAY_OF_MONTH]).show()
            }
            R.id.TV_end_time -> {
                dialog!!.hide()
                val calendar2 = Calendar.getInstance()
                DatePickerDialog(activity, { datePicker, year, month, day ->
                    var result = ""
                    result += year.toString() + "." + (month + 1) + "." + day + "."
                    Toast.makeText(activity, result, Toast.LENGTH_SHORT).show()
                    dialog!!.show()
                    TV_end_time!!.text = result
                }, calendar2[Calendar.YEAR], calendar2[Calendar.MONTH], calendar2[Calendar.DAY_OF_MONTH]).show()
            }
        }
    }
 
    override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) {
        when (parent.id) {
            R.id.SP_province -> {
                cityData.clear()
                var y = 0
                while (y < allCityData.size) {
                    if (allCityData[y].pronvinceid == (SP_province!!.selectedItem as Province).provinceid) cityData.add(allCityData[y])
                    y++
                }
                cityAdapter!!.notifyDataSetChanged()
            }
            R.id.SP_city -> {
                districtData.clear()
                val emptydistrict = District()
                emptydistrict.districtname = ALLDISTRICT
                districtData.add(emptydistrict)
                var y = 0
                while (y < allDistrictData.size) {
                    if (allDistrictData[y].cityid == (SP_city!!.selectedItem as City).cityId) districtData.add(allDistrictData[y])
                    y++
                }
                districtAdapter!!.notifyDataSetChanged()
            }
            R.id.SP_district -> {
                townData.clear()
                val emptytown = Town()
                emptytown.townname = ALLTOWN
                townData.add(emptytown)
                var y = 0
                while (y < allTownData.size) {
                    if (allTownData[y].districtid == (SP_district!!.selectedItem as District).districtid) townData.add(allTownData[y])
                    y++
                }
                townAdapter!!.notifyDataSetChanged()
            }
            R.id.SP_town -> {
            }
            R.id.SP_scense_type -> search()
        }
    }
 
    override fun onNothingSelected(parent: AdapterView<*>?) {}
 
    companion object {
        private const val ALLDISTRICT = "全部区县"
        private const val ALLTOWN = "全部街道"
        private const val MAX_CHART_COUNT = 5 //饼图最多显示区块数
        private const val REMAIN_PROBLEM = "其余"
    }
}