riku
2025-10-27 0f58aa8ea118c3bd0b28396febc58fdbd94eef75
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
package cn.flightfeather.thirdappmodule.module.dataanalysis
 
import android.app.Dialog
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.support.v4.app.Fragment
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import butterknife.ButterKnife
import butterknife.Unbinder
import cn.flightfeather.thirdappmodule.CommonApplication
import cn.flightfeather.thirdappmodule.R
import cn.flightfeather.thirdappmodule.adapter.AllRecyclerViewAdapter
import cn.flightfeather.thirdappmodule.bean.entity.Domainitem
import cn.flightfeather.thirdappmodule.bean.vo.RankVo
import cn.flightfeather.thirdappmodule.bean.vo.RankVo.SceneRank
import cn.flightfeather.thirdappmodule.bean.vo.RankVo.TownRank
import cn.flightfeather.thirdappmodule.bean.vo.StatisticsVo
import cn.flightfeather.thirdappmodule.bean.vo.TaskFrequencyVo
import cn.flightfeather.thirdappmodule.bean.vo.TaskFrequencyVo.FrequencyInfo
import cn.flightfeather.thirdappmodule.bean.vo.TaskVo
import cn.flightfeather.thirdappmodule.module.dataanalysis.rank.AnalysisRankActivity
import cn.flightfeather.thirdappmodule.httpservice.ProblemListService
import cn.flightfeather.thirdappmodule.httpservice.TaskService
import cn.flightfeather.thirdappmodule.util.*
import com.ping.greendao.gen.DomainitemDao
import kotlinx.android.synthetic.main.fragment_analysis_over_view.*
import kotlinx.android.synthetic.main.layout_no_data.*
import kotlinx.android.synthetic.main.tab_time_swtich.*
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import retrofit2.Retrofit
import java.util.*
 
/**
 * 2018.12.25 created by riku
 * 分析总览界面
 * 综合简略展示各统计概况:
 * 任务进度、街镇排名、场景排名、问题类型分布
 */
class AnalysisOverViewFragment : Fragment() {
    private val TITLE = "总览"
    private var app: CommonApplication? = null
    private var retrofit: Retrofit? = null
    private var unbinder: Unbinder? = null
    private var curSceneTypeId = "1" //当前场景类型
    private var curSceneTypeName = "工地" //当前场景类型
    private var rankVo: RankVo? = RankVo()
    private val maxShowItems = 3
    private var dialog: Dialog? = null
    private var container: ViewGroup? = null
 
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        this.container = container
        val view = inflater.inflate(R.layout.fragment_analysis_over_view, container, false)
        unbinder = ButterKnife.bind(this, view)
        return view
    }
 
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        app = activity?.application as CommonApplication
        retrofit = app?.retrofit
        initPopup()
        initToolBar()
        initBlock1()
        initBlock2()
        initBlock3()
        initBlock4()
        initPanel()
        initClickListener()
    }
 
    override fun onAttach(context: Context) {
        super.onAttach(context)
    }
 
    override fun onDetach() {
        super.onDetach()
    }
 
    /**
     * Called when the fragment is no longer in use.  This is called
     * after [.onStop] and before [.onDetach].
     */
    override fun onDestroy() {
        super.onDestroy()
        if (unbinder != null) {
            unbinder!!.unbind()
        }
    }
 
    //</editor-fold>
    //<editor-fold desc="标题栏">
    private fun initToolBar() {
        action_bar!!.findViewById<View>(R.id.spinner_topclass_task).visibility = View.GONE
        action_bar!!.findViewById<View>(R.id.img_left).visibility = View.GONE
        action_bar!!.findViewById<View>(R.id.img_right).visibility = View.INVISIBLE
        action_bar!!.findViewById<View>(R.id.ll_menu_text).visibility = View.VISIBLE
        val title = action_bar!!.findViewById<TextView>(R.id.actionbar_title)
        title.text = TITLE
        //        title.setGravity(Gravity.CENTER_HORIZONTAL);
//        ViewGroup.LayoutParams layoutParams = title.getLayoutParams();
//        layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
    }
 
    var allTaskVoList: MutableList<TaskVo> = ArrayList()
    var showingTasks: MutableList<TaskVo> = ArrayList()
    var offset = 0
    private fun initPanel() {
        showDialog()
        //顶层任务
        val getTopClassTaskList = retrofit!!.create(TaskService::class.java).getTopClassTaskList(0)
        getTopClassTaskList.enqueue(object : Callback<ArrayList<TaskVo>?> {
            override fun onResponse(call: Call<ArrayList<TaskVo>?>, response: Response<ArrayList<TaskVo>?>) {
                showingTasks.clear()
                allTaskVoList.clear()
                if (response.body() != null) {
                    allTaskVoList.addAll(response.body()!!) //返回结果默认按时间降序排序
                    for (t in allTaskVoList) {
                        if (t.districtcode == app!!.currentUser.dguid) { //主管部门的部门id就是区县id
                            showingTasks.add(t)
                        }
                    }
                }
                refreshPanel()
            }
 
            override fun onFailure(call: Call<ArrayList<TaskVo>?>, t: Throwable) {
                refreshPanel()
            }
        })
    }
 
    var text: TextView? = null
    private fun refreshPanel() {
        showDialog()
        text = tab_time_pannel.findViewById(R.id.text_time)
        if (showingTasks.isNotEmpty()) {
            val date = showingTasks[offset].starttime
            val s = DateFormatter.YearMonthFormat_CN.format(date)
            text?.text = s
            refresh()
        }
    }
 
    var freAdapter: AllRecyclerViewAdapter<FrequencyInfo>? = null
    var frequencyInfos: MutableList<FrequencyInfo> = ArrayList()
    private fun initBlock1() {
        freAdapter = object : AllRecyclerViewAdapter<FrequencyInfo>(frequencyInfos, R.layout.item_common_news, activity) {
            override fun bindView(holder: MyViewHolder, obj: FrequencyInfo, isSelected: Boolean, position: Int) {
                val totalFrequency = StringBuilder("总监管频次: " + obj.totalFrequency + "次")
                val clearFrequency = StringBuilder("已监管频次: " + obj.clearFrequency + "次")
                val tmpLeft = obj.totalFrequency - obj.clearFrequency
                val leftFrequency =
                    StringBuilder("剩余监管频次: " + (if (tmpLeft >= 0) tmpLeft else 0) + "次")
                holder.setText(R.id.text_head, obj.sceneTypeName)
                    .setText(R.id.text_1_1, totalFrequency)
                    .setText(R.id.text_2_1, clearFrequency)
                    .setText(R.id.text_3_1, leftFrequency)
                    .setVisibility(R.id.text_1_2, View.GONE)
                    .setVisibility(R.id.text_2_2, View.GONE)
                    .setVisibility(R.id.text_3_2, View.GONE)
                    .setImageResource(
                        R.id.image_head,
                        CommonUtils.getIconBySceneType(obj.sceneTypeId)
                    )
            }
        }
        val lm = LinearLayoutManager(activity)
        lm.orientation = LinearLayoutManager.VERTICAL
        rv_task_progress!!.layoutManager = lm
        rv_task_progress!!.adapter = freAdapter
    }
 
    private fun refreshBlock1() {
        val id = topTaskId
        val getFrequency = retrofit!!.create(
            TaskService::class.java
        ).getFrequency(id)
        getFrequency.enqueue(object : Callback<TaskFrequencyVo?> {
            override fun onResponse(
                call: Call<TaskFrequencyVo?>,
                response: Response<TaskFrequencyVo?>
            ) {
                if (response.body() != null) {
                    val frequencyVo = response.body()
                    frequencyInfos.clear()
                    frequencyInfos.addAll(frequencyVo!!.frequencyInfos)
                    freAdapter!!.notifyDataSetChanged()
                } else {
                    loadingOver(false)
                }
            }
 
            override fun onFailure(call: Call<TaskFrequencyVo?>, t: Throwable) {
                loadingOver(false)
            }
        })
    }
 
 
    var townRankAdapter: AllRecyclerViewAdapter<TownRank>? = null
    var townRanks: MutableList<TownRank> = ArrayList()
    var allTownRanks: MutableList<TownRank> = ArrayList()
    private fun initBlock2() {
        townRankAdapter = object :
            AllRecyclerViewAdapter<TownRank>(townRanks, R.layout.item_problem_count, activity) {
            override fun bindView(holder: MyViewHolder, obj: TownRank, isSelected: Boolean, position: Int) {
                if (position < 3) {
                    val rank = obj.rankNo
                    holder.setText(R.id.text_name, obj.townName)
                        .setText(R.id.text_firstCount, getScore(obj.averageScore))
                        .setVisibility(R.id.text_secondCount, View.GONE)
                        .setVisibility(R.id.image_rank, View.GONE)
                        .setText(R.id.text_no, rank.toString())
                    when (rank) {
                        1 -> holder.setImageResource(R.id.image_no, R.drawable.icon_rank_4)
                        2 -> holder.setImageResource(R.id.image_no, R.drawable.icon_rank_3)
                        3 -> holder.setImageResource(R.id.image_no, R.drawable.icon_rank_2)
                        else -> holder.setImageResource(R.id.image_no, R.drawable.icon_rank_1)
                    }
                }
            }
        }
        val lm = LinearLayoutManager(activity)
        lm.orientation = LinearLayoutManager.VERTICAL
        rv_rank_town!!.layoutManager = lm
        rv_rank_town!!.adapter = townRankAdapter
    }
 
    private fun refreshBlock2and3() {
        val id = topTaskId
        val getRank = retrofit!!.create(
            TaskService::class.java
        ).getRank(id, curSceneTypeId, null)
        getRank.enqueue(object : Callback<RankVo?> {
            override fun onResponse(call: Call<RankVo?>, response: Response<RankVo?>) {
                if (response.body() != null) {
                    rankVo = response.body()
                    rankS(rankVo!!.sceneRanks)
                    rankT(rankVo!!.townRanks)
                    townRanks.clear()
                    allTownRanks.clear()
                    allTownRanks.addAll(rankVo!!.townRanks)
                    val tmpMax =
                        if (maxShowItems <= allTownRanks.size) maxShowItems else allTownRanks.size
                    for (i in 0 until tmpMax) {
                        townRanks.add(allTownRanks[i])
                    }
                    townRankAdapter!!.notifyDataSetChanged()
                    sceneRanks.clear()
                    allSceneRanks.clear()
                    allSceneRanks.addAll(rankVo!!.sceneRanks)
                    val tmpMax1 =
                        if (maxShowItems <= allSceneRanks.size) maxShowItems else allSceneRanks.size
                    for (i in 0 until tmpMax1) {
                        sceneRanks.add(allSceneRanks[i])
                    }
                    sceneRankAdapter!!.notifyDataSetChanged()
                } else {
                    loadingOver(false)
                }
            }
 
            override fun onFailure(call: Call<RankVo?>, t: Throwable) {
                loadingOver(false)
            }
        })
    }
 
    //<editor-fold desc="场景排名">
    var sceneRankAdapter: AllRecyclerViewAdapter<SceneRank>? = null
    var sceneRanks: MutableList<SceneRank> = ArrayList()
    var allSceneRanks: MutableList<SceneRank> = ArrayList()
    private fun initBlock3() {
        sceneRankAdapter = object :
            AllRecyclerViewAdapter<SceneRank>(sceneRanks, R.layout.item_problem_count, activity) {
            override fun bindView(
                holder: MyViewHolder,
                obj: SceneRank,
                isSelected: Boolean,
                position: Int
            ) {
                if (position < 3) {
                    val rank = obj.rankNo
                    holder.setText(R.id.text_name, obj.sceneName)
                        .setText(R.id.text_firstCount, getScore(obj.score))
                        .setVisibility(R.id.text_secondCount, View.GONE)
                        .setVisibility(R.id.image_rank, View.GONE)
                        .setText(R.id.text_no, rank.toString())
                    when (rank) {
                        1 -> holder.setImageResource(R.id.image_no, R.drawable.icon_rank_4)
                        2 -> holder.setImageResource(R.id.image_no, R.drawable.icon_rank_3)
                        3 -> holder.setImageResource(R.id.image_no, R.drawable.icon_rank_2)
                        else -> holder.setImageResource(R.id.image_no, R.drawable.icon_rank_1)
                    }
                }
            }
        }
        val lm = LinearLayoutManager(activity)
        lm.orientation = LinearLayoutManager.VERTICAL
        rv_rank_scene!!.layoutManager = lm
        rv_rank_scene!!.adapter = sceneRankAdapter
    }
 
    //</editor-fold>
    //<editor-fold desc="问题分析">
    var datas = HashMap<String, Float>()
    private fun initBlock4() {
        ChartGenerator.quickSetupChart(pie_chart_problem)
    }
 
    private fun refreshBlock4() {
        val id = topTaskId
        val getStatisticalResultbyId = retrofit!!.create(
            ProblemListService::class.java
        ).getStatisticalResultbyId(id, curSceneTypeId)
        getStatisticalResultbyId.enqueue(object : Callback<List<StatisticsVo>?> {
            override fun onResponse(
                call: Call<List<StatisticsVo>?>,
                response: Response<List<StatisticsVo>?>
            ) {
                datas.clear()
                if (response.body() != null) {
                    val statisticsVos = response.body()
                    for (s in statisticsVos!!) {
                        datas[s.name] = s.count.toFloat()
                    }
                    if (datas.isEmpty()) {
                        pie_chart_problem!!.visibility = View.GONE
                        ll_no_data!!.visibility = View.VISIBLE
                    } else {
                        pie_chart_problem!!.visibility = View.VISIBLE
                        ll_no_data!!.visibility = View.GONE
                    }
                    ChartGenerator.quickSetData(pie_chart_problem, datas, "问题统计")
                    loadingOver(true)
                } else {
                    loadingOver(false)
                }
            }
 
            override fun onFailure(call: Call<List<StatisticsVo>?>, t: Throwable) {
                datas.clear()
                ChartGenerator.quickSetData(pie_chart_problem, datas, "问题统计")
                loadingOver(false)
            }
        })
    }//返回顶层任务id
 
    //</editor-fold>
    //<editor-fold desc="获取查询参数">
    private val topTaskId: String
        get() = showingTasks[offset].tguid //返回顶层任务id
 
    //</editor-fold>
    //<editor-fold desc="联网刷新">
    private fun refresh() {
        showDialog()
        refreshBlock1()
        refreshBlock2and3()
        refreshBlock4()
    }
 
    //</editor-fold>
    //<editor-fold desc="获取弹出框">
    var rv_popup: RecyclerView? = null
    var sceneAdapter: AllRecyclerViewAdapter<Domainitem>? = null
    private val sceneTypeData: MutableList<Domainitem> = ArrayList() //场景类型
    var popup: PopupGenerator? = null
    private fun initPopup() {
        val domainitemDao = app!!.daoSession.domainitemDao
        //场景类型
        val queryBuilder = domainitemDao.queryBuilder()
            .where(DomainitemDao.Properties.Dcguid.eq(Domain.DOMAINGUID_SCENSETYPE))
            .orderAsc(DomainitemDao.Properties.Value)
        sceneTypeData.addAll(queryBuilder.list())
        popup = PopupGenerator(activity, R.layout.popup_scene_selector)
        rv_popup = popup!!.findViewById(R.id.rv_popup)
        sceneAdapter = object :
            AllRecyclerViewAdapter<Domainitem>(sceneTypeData, R.layout.item_scene, activity) {
            override fun bindView(
                holder: MyViewHolder,
                obj: Domainitem,
                isSelected: Boolean,
                position: Int
            ) {
                val sceneType = obj.value.toString()
                holder.setText(R.id.text_scene, obj.text)
                    .setImageResource(R.id.image_scene, CommonUtils.getIconBySceneType(sceneType))
                    .setOnItemClickListener {
                        curSceneTypeId = obj.value
                        curSceneTypeName = obj.text
                    }
            }
        }
        val layoutManager = GridLayoutManager(activity, 3)
        layoutManager.orientation = GridLayoutManager.VERTICAL
        layoutManager.isAutoMeasureEnabled = true
        rv_popup?.layoutManager = layoutManager
        rv_popup?.adapter = sceneAdapter
        popup!!.setPopupWindowFullScreen(false)
    }
 
    //</editor-fold>
    //<editor-fold desc="点击事件">
    fun initClickListener() {
        //切换顶层任务
        image_left.setOnClickListener { onClickChangeTask(it) }
        image_right.setOnClickListener { onClickChangeTask(it) }
 
        //场景选择菜单
//        ll_menu_text.setOnClickListener {  }
 
        //详细内容按钮
        text_all_2.setOnClickListener { onClickMoreInfo(it) }
        text_all_3.setOnClickListener { onClickMoreInfo(it) }
 
        //重新加载按钮
        button_refresh.setOnClickListener { refresh() }
    }
 
    //切换顶层任务
    private fun onClickChangeTask(v: View) {
        val tmpOffset = offset
        when (v) {
            image_left -> {
                val tmp = offset + 1
                offset = if (tmp >= showingTasks.size) offset else tmp
            }
            image_right -> {
                val tmp1 = offset - 1
                offset = if (tmp1 < 0) offset else tmp1
            }
        }
        if (tmpOffset != offset) {
            refreshPanel()
        }
    }
 
    //详细内容按钮
    private fun onClickMoreInfo(v: View) {
        val intent = Intent(activity, AnalysisRankActivity::class.java)
        val bundle = Bundle()
        bundle.putSerializable(AnalysisRankActivity.ARG_PARAM1, rankVo)
        bundle.putString(AnalysisRankActivity.ARG_PARAM2, rankVo!!.topTaskName)
        bundle.putString(AnalysisRankActivity.ARG_PARAM3, curSceneTypeName)
        when (v) {
            text_all_2 -> bundle.putInt(AnalysisRankActivity.ARG_PARAM4, 1)
            text_all_3 -> bundle.putInt(AnalysisRankActivity.ARG_PARAM4, 0)
        }
        intent.putExtras(bundle)
        startActivity(intent)
    }
 
    //</editor-fold>
    //<editor-fold desc="更新当前场景">
    private fun refreshCurScene(id: String, name: String) {
        curSceneTypeId = id
        curSceneTypeName = name
    }
 
    //</editor-fold>
    //<editor-fold desc="网络加载完成">
    private fun loadingOver(b: Boolean) {
        Handler().postDelayed({
            if (dialog!!.isShowing) {
                dialog!!.dismiss()
            }
        }, 500)
        if (b) { //加载成功
            sl_content!!.visibility = View.VISIBLE
            no_data!!.visibility = View.GONE
        } else { //加载失败
            sl_content!!.visibility = View.GONE
            no_data!!.visibility = View.VISIBLE
        }
    }
 
    //</editor-fold>
    private fun showDialog() {
        if (dialog == null) {
            dialog = DialogUtil.createLoadingDialog(activity, "")
        }
        if (!dialog!!.isShowing) {
            dialog!!.show()
        }
    }
 
    private fun getScore(score: Int): String {
        return if (score == -1) "未评分" else score.toString()
    }
 
    //刷新排名信息
    private fun rankT(townRanks: List<TownRank>?) {
        if (townRanks == null) return
        var last: TownRank? = null
        var current: TownRank? = null
        var rankNo = 1
        for (i in townRanks.indices) {
            if (i > 0) {
                last = townRanks[i - 1]
            }
            current = townRanks[i]
            if (last != null) {
                //同分时并列排名
                if (current.averageScore == last.averageScore) {
                    current.rankNo = last.rankNo
                } else {
                    current.rankNo = rankNo
                }
            } else {
                current.rankNo = rankNo
            }
            rankNo++
        }
    }
 
    companion object {
        @JvmStatic
        fun newInstance(): AnalysisOverViewFragment {
            return AnalysisOverViewFragment()
        }
 
        //刷新排名信息
        @JvmStatic
        fun rankS(sceneRanks: List<SceneRank?>?) {
            if (sceneRanks == null) return
            var last: SceneRank? = null
            var current: SceneRank? = null
            var rankNo = 1
            for (i in sceneRanks.indices) {
                if (i > 0) {
                    last = sceneRanks[i - 1]
                }
                current = sceneRanks[i]
                if (last != null) {
                    //同分时并列排名
                    if (current!!.score == last.score) {
                        current.rankNo = last.rankNo
                    } else {
                        current.rankNo = rankNo
                    }
                } else {
                    current!!.rankNo = rankNo
                }
                rankNo++
            }
        }
    }
}