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
package cn.flightfeather.thirdappmodule.module.task
 
import android.app.AlertDialog
import android.arch.lifecycle.Observer
import android.arch.lifecycle.ViewModelProviders
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import cn.flightfeather.thirdappmodule.R
import cn.flightfeather.thirdappmodule.bean.entity.Scense
import kotlinx.android.synthetic.main.layout_search_bar.*
import java.util.*
import kotlin.collections.ArrayList
 
/**
 * 月度任务管理activity
 * @author riku
 * Date: 2019/9/10
 */
class MonthTaskManagementActivity : NewMonthTaskMapActivity() {
 
    override fun getViewMode(): NewSubTaskViewModel = ViewModelProviders.of(this).get(MonthTaskManagementViewModel::class.java)
 
    override fun onCreate() {
        super.onCreate()
 
        viewModel.topTaskList.observe(this, Observer {tops ->
            //初始化顶部下拉框
            tops?.let {
                val names = ArrayList<String>()
                it.forEach {task -> names.add(task.name) }
                val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, names)
                spinner_search.adapter = adapter
                spinner_search.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
                    override fun onNothingSelected(parent: AdapterView<*>?) = Unit
                    override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
                        topTask = it[position].taskVo2Task()
                        viewModel.selectedTopTask = it[position]
                        viewModel.getMOVList(it[position])//获取监管对象版本
                        viewModel.getSceneByTask(it[position].taskVo2Task(), 1)
                    }
                }
            }
        })
    }
 
    override fun initData() {
        viewModel.getTopClassTaskList(1, this)
    }
 
    override fun enableDelete(oldScene: Scense?): Boolean {
        viewModel.movList.forEach {
            if (it.sguid == oldScene?.guid) {
                return if (it.extension1 == null) {
                    true
                } else {
                    val inspectionTimes = it.extension1.toInt()
                    inspectionTimes <= 0
                }
            }
        }
        return true
    }
 
    override fun refreshMOV(type: Int, oldScene: Scense?, newScene: Scense?) {
        super.refreshMOV(type, oldScene, newScene)
        val vm = viewModel as MonthTaskManagementViewModel
        when (type) {
            //删除场景,单独维护一个删除表
            0 -> {
                //遍历原列表与新增列表,找到删除的场景,添加至删除列表,并将其从原表删除
                vm.movList.forEach {
                    if (it.sguid == oldScene?.guid) {
                        oldScene?.let {_ ->
                            if (!vm.deletedMovList.contains(it)) {
                                vm.deletedMovList.add(it)
                            }
                        }
                        vm.movList.remove(it)
                        return
                    }
                }
                vm.insertedMovList.forEach {
                    if (it.sguid == oldScene?.guid) {
                        oldScene?.let {_ ->
                            if (!vm.deletedMovList.contains(it)) {
                                vm.deletedMovList.add(it)
                            }
                        }
                        vm.insertedMovList.remove(it)
                        return
                    }
                }
            }
            //替换场景
            1 -> {
                //只需要在原列表中做场景id和名称修改即可
                vm.movList.forEach {
                    if (it.sguid == oldScene?.guid) {
                        newScene?.let { s ->
                            it.sguid = s.guid
                            it.sensename = s.name
                        }
                        return@forEach
                    }
                }
            }
            //新增场景,单独维护一个新增表
            2 -> {
                //遍历删除表,若找到该场景,则将其移动至新增列表,否则新建并添加至新增列表
                vm.deletedMovList.forEach {
                    if (it.sguid == newScene?.guid) {
                        newScene?.let {_ ->
                            if (!vm.insertedMovList.contains(it)) {
                                vm.insertedMovList.add(it)
                            }
                        }
                        vm.deletedMovList.remove(it)
                        return
                    }
                }
                newScene?.let {
                    val mov = createMOV(it)
                    vm.insertedMovList.add(mov)
                }
            }
        }
    }
 
    override fun showAlertDialog() {
        val vm = viewModel as MonthTaskManagementViewModel
 
        var msg = "监管对象版本更新后信息如下:"
        //若存在监管多次的工地,则添加以下语句:其中,部分场景需监管多次;
        msg += "总共需监管%d次。\n是否现在提交?"
        var totalMonitorNum = 0
 
        vm.movList.forEach {
            totalMonitorNum += it.monitornum ?: 0
        }
        vm.insertedMovList.forEach {
            totalMonitorNum += it.monitornum ?: 0
        }
 
        AlertDialog.Builder(this).setTitle("更新监管对象版本")
                .setMessage(String.format(Locale.CHINA, msg, totalMonitorNum))
                .setNegativeButton(R.string.cancel, null)
                .setPositiveButton(R.string.yes) { _, _ ->
                    vm.putMOV(vm.insertedMovList)
                    vm.updateMOV(vm.movList)
                    vm.deleteMOV(vm.deletedMovList)
                    viewModel.updateSceneList(viewModel.totalSceneList.value)
                }.show()
    }
}