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
package cn.flightfeather.thirdappmodule.module.task
 
import android.arch.lifecycle.MutableLiveData
import cn.flightfeather.thirdappmodule.bean.entity.Domainitem
import cn.flightfeather.thirdappmodule.bean.entity.Subtask
import cn.flightfeather.thirdappmodule.bean.vo.ProblemDetailVo
import cn.flightfeather.thirdappmodule.bean.vo.TaskVo
import cn.flightfeather.thirdappmodule.common.net.ResultCallBack
import cn.flightfeather.thirdappmodule.module.base.BaseViewModel
import cn.flightfeather.thirdappmodule.repository.ProblemRepository
import cn.flightfeather.thirdappmodule.repository.SubTaskRepository
import cn.flightfeather.thirdappmodule.repository.TaskRepository
import cn.flightfeather.thirdappmodule.util.Constant
import cn.flightfeather.thirdappmodule.util.DateFormatter
import java.util.*
import kotlin.collections.ArrayList
 
/**
 * @author riku
 * Date: 2020/6/5
 */
class TaskViewModel : BaseViewModel() {
    companion object {
        const val NO_TOP_TASK = "无总任务"
        const val ALL_PROBLEM = "全部问题"
    }
 
    private val taskRepository = TaskRepository()
    private val subTaskRepository = SubTaskRepository()
    private val problemRepository = ProblemRepository()
    private val allTopTaskList = ArrayList<TaskVo>()// 系统中所有总任务
    private val problemMap = mutableMapOf<String, MutableList<String>>()// 问题类型与具体问题
 
    /**临时任务功能与任务信息导出功能***************************************************************************/
    // 总任务类型
    val allTaskTypeList = MutableLiveData<ArrayList<Domainitem>>().apply { value = ArrayList() }
    // 当前筛选条件下的总任务
    val topTaskList = MutableLiveData<ArrayList<TaskVo>>().apply { value = ArrayList() }
    // 问题类型
    val problemTypeList = MutableLiveData<ArrayList<String>>().apply { value = ArrayList() }
    // 具体问题
    val problemDetailList = MutableLiveData<ArrayList<String>>().apply { value = ArrayList() }
    // 子任务id与名称
    val subTaskNameList = MutableLiveData<ArrayList<Pair<String, String>>>().apply { value = ArrayList() }
    /***********************************************************************************************************/
 
    /**任务制订、任务提醒相关***********************************************************************************/
    
    /***********************************************************************************************************/
 
    fun getTaskType() {
        taskRepository.getTaskType(object : ResultCallBack<List<Domainitem>> {
            override fun onSuccess(result: List<Domainitem>?) {
                result?.let {
                    allTaskTypeList.value?.apply {
                        clear()
                        addAll(it)
                    }
                    allTaskTypeList.value = allTaskTypeList.value
                }
            }
 
            override fun onFailure() {
            }
        })
    }
 
    fun getTopTask(type: Int = 1) {
        taskRepository.getTopClassTaskList(type, object : ResultCallBack<ArrayList<TaskVo>> {
            override fun onSuccess(result: ArrayList<TaskVo>?) {
                result?.let {
                    allTopTaskList.clear()
                    allTopTaskList.addAll(it)
 
                    topTaskList.value?.apply {
                        clear()
                        addAll(it)
                    }
                    refreshTopTaskByTime(Date())
                }
            }
 
            override fun onFailure() {
 
            }
        })
    }
 
    fun refreshTopTaskByDistrict(districtCode: String) {
        topTaskList.value?.clear()
        allTopTaskList.forEach {
            if (it.districtcode == districtCode) {
                topTaskList.value?.add(it)
            }
        }
        if (topTaskList.value.isNullOrEmpty()) {
            topTaskList.value?.add(TaskVo().apply {
                name = NO_TOP_TASK
            })
        }
        topTaskList.value = topTaskList.value
    }
 
    fun refreshTopTaskByTime(date: Date) {
        topTaskList.value?.clear()
        allTopTaskList.forEach {
            if (it.starttime <= date && date <= it.endtime) {
                topTaskList.value?.add(it)
            }
        }
        if (topTaskList.value.isNullOrEmpty()) {
            topTaskList.value?.add(TaskVo().apply {
                name = NO_TOP_TASK
            })
        }
        topTaskList.value = topTaskList.value
    }
 
    fun getSubTaskByDate(date: Date = Date(), resultCallBack: ResultCallBack<List<Subtask>>) {
        val dateStr = DateFormatter.dateTimeFormat3.format(date)
        subTaskRepository.getSubTaskByDate(dateStr, userId, resultCallBack)
    }
 
    /**
     * 获取可选的问题类型(导出功能)
     */
    fun getProblemType(sceneTypeId: Int = Constant.SCENE_TYPE11.toInt()) {
        problemRepository.getProblemType(sceneTypeId, object : ResultCallBack<ArrayList<ProblemDetailVo>> {
            override fun onSuccess(result: ArrayList<ProblemDetailVo>?) {
                result?.apply {
                    problemTypeList.value?.clear()
                    problemDetailList.value?.clear()
                    problemMap.clear()
 
                    forEach {
                        problemTypeList.value?.add(ALL_PROBLEM)
                        problemTypeList.value?.add(it.problemTypeName)
                        if (!problemMap.containsKey(it.problemTypeName)) {
                            problemMap[it.problemTypeName] = mutableListOf()
                        }
                        problemMap[it.problemTypeName]?.add(ALL_PROBLEM)
                        problemMap[it.problemTypeName]?.addAll(it.problems)
                    }
 
                    problemTypeList.value = problemTypeList.value
                }
            }
 
            override fun onFailure() {
            }
        })
    }
 
    /**
     * 选择问题类型后
     * 刷新问题详情列表
     */
    fun onProblemTypeSelected(problemTypeName:String) {
        problemMap[problemTypeName].let {
            problemDetailList.value?.clear()
            if (it == null) {
                problemDetailList.value?.add(ALL_PROBLEM)
            } else {
                problemDetailList.value?.addAll(it)
            }
            problemDetailList.value = problemDetailList.value
        }
    }
 
    /**
     * 获取条件筛选后的子任务(导出功能)
     */
    fun getSubTask(topTaskId: String, startTime: String, endTime: String) {
        subTaskRepository.getByTopTaskAndDate(topTaskId,startTime,endTime, object : ResultCallBack<List<Subtask>> {
            override fun onSuccess(result: List<Subtask>?) {
                result?.let {
                    subTaskNameList.value?.clear()
                    it.forEach {s ->
                        subTaskNameList.value?.add(Pair(s.stguid, s.name))
                    }
                    subTaskNameList.value = subTaskNameList.value
                }
            }
 
            override fun onFailure() {
 
            }
        })
    }
 
//    fun createSubtask(topClassTask: TaskVo) {
//        val subtaskService: SubTaskService = mRetrofit.create(SubTaskService::class.java)
//        val subtask = Subtask()
//        var currentDayTask: TaskVo? = null
//        val now = Calendar.getInstance()
//        for (taskVo in mDayTaskList) {
//            if (taskVo.tsguid == topClassTask.tguid) {
//                val cal = Calendar.getInstance()
//                cal.time = taskVo.starttime
//                if (now[Calendar.YEAR] == cal[Calendar.YEAR] && now[Calendar.MONTH] == cal[Calendar.MONTH] && now[Calendar.DAY_OF_MONTH] == cal[Calendar.DAY_OF_MONTH]) {
//                    currentDayTask = taskVo
//                    break
//                }
//            }
//        }
//        if (currentDayTask == null) {
//            currentDayTask = createDayTask(topClassTask)
//            callPutDayTask(topClassTask, subtaskService, subtask, currentDayTask, now)
//        } else {
//            callPutSubtask(topClassTask, subtaskService, subtask, currentDayTask, now)
//        }
//    }
//
//    private fun createDayTask(topClassTask: TaskVo): TaskVo? {
//        val dayTask = topClassTask.clone()
//        dayTask.tguid = UUIDGenerator.generate16ShortUUID()
//        dayTask.tsguid = topClassTask.tguid
//        dayTask.deadlinetype = null
//        dayTask.extension1 = null
//        dayTask.levelnum = null
//        val cal = Calendar.getInstance()
//        dayTask.settime = cal.time
//        cal[Calendar.HOUR_OF_DAY] = 0
//        cal[Calendar.MINUTE] = 0
//        cal[Calendar.SECOND] = 0
//        dayTask.starttime = cal.time
//        cal[Calendar.HOUR_OF_DAY] = 23
//        cal[Calendar.MINUTE] = 59
//        cal[Calendar.SECOND] = 59
//        dayTask.endtime = cal.time
//        dayTask.plannerguid = application.currentUser.guid
//        dayTask.plannerusername = application.currentUser.acountname
//        dayTask.plannerrealname = application.currentUser.realname
//        dayTask.t1stverifierguid = application.currentUser.guid
//        dayTask.t1stverifierusername = application.currentUser.acountname
//        dayTask.t1stverifierrealname = application.currentUser.realname
//        val template = "%d月%d日%s无固定场景巡查任务"
//        cal.time = dayTask.starttime
//        val area = dayTask.cityname + dayTask.districtname
//        dayTask.name = String.format(
//            Locale.CHINA, template, cal[Calendar.MONTH] + 1,
//            cal[Calendar.DAY_OF_MONTH], area
//        )
//        return dayTask
//    }
//
//    private fun callPutDayTask(
//        topClassTask: TaskVo, subtaskService: SubTaskService,
//        subtask: Subtask, currentDayTask: TaskVo, now: Calendar
//    ) {
//        val call: retrofit2.Call<ResponseBody> = mRetrofit.create(TaskService::class.java).putTask(currentDayTask.taskVo2Task())
//        call.enqueue(object : retrofit2.Callback<ResponseBody?>() {
//            fun onResponse(call: retrofit2.Call<ResponseBody>, response: retrofit2.Response<ResponseBody>) {
//                if (response.body() != null) {
//                    Toast.makeText(getActivity(), "日任务创建成功。", Toast.LENGTH_SHORT).show()
//                    callPutSubtask(topClassTask, subtaskService, subtask, currentDayTask, now)
//                } else if (response.errorBody() != null) {
//                    Toast.makeText(getActivity(), "日任务创建失败。", Toast.LENGTH_SHORT).show()
//                }
//            }
//
//            fun onFailure(call: retrofit2.Call<ResponseBody>, t: Throwable) {
//                Toast.makeText(getActivity(), "日任务创建失败。", Toast.LENGTH_SHORT).show()
//            }
//        })
//    }
//
//    private fun callPutSubtask(
//        topClassTask: TaskVo, subtaskService: SubTaskService,
//        subtask: Subtask, currentDayTask: TaskVo, now: Calendar
//    ) {
//        subtask.setStguid(UUIDGenerator.generate16ShortUUID())
//        subtask.setTguid(topClassTask.tguid)
//        subtask.setTsguid(currentDayTask.tguid)
//        subtask.setTypeno(topClassTask.typeno)
//        subtask.setType(topClassTask.typename)
//        subtask.setProvincecode(topClassTask.provincecode)
//        subtask.setProvincename(topClassTask.provincename)
//        subtask.setCitycode(topClassTask.citycode)
//        subtask.setCityname(topClassTask.cityname)
//        subtask.setDistrictcode(topClassTask.districtcode)
//        subtask.setDistrictname(topClassTask.districtname)
//        subtask.setTowncode(topClassTask.towncode)
//        subtask.setTownname(topClassTask.townname)
//        val qb: QueryBuilder<Scense> = application.daoSession.scenseDao.queryBuilder()
//        val scense: Scense
//        scense = if (subtask.getTowncode() == null) {
//            qb.where(ScenseDao.Properties.Provincecode.eq(subtask.getProvincecode()), ScenseDao.Properties.Citycode.eq(subtask.getCitycode()), ScenseDao.Properties.Districtcode.eq(subtask.getDistrictcode()), ScenseDao.Properties.Typeid.eq(Domain.SCENSETYPE_UNFIXED_SCENSE)).unique()
//        } else {
//            qb.where(
//                ScenseDao.Properties.Provincecode.eq(subtask.getProvincecode()),
//                ScenseDao.Properties.Citycode.eq(subtask.getCitycode()),
//                ScenseDao.Properties.Districtcode.eq(subtask.getDistrictcode()),
//                ScenseDao.Properties.Towncode.eq(subtask.getTowncode()),
//                ScenseDao.Properties.Typeid.eq(Domain.SCENSETYPE_UNFIXED_SCENSE)
//            ).unique()
//        }
//        subtask.setScenseid(scense.getGuid())
//        subtask.setScensename(scense.getName())
//        subtask.setDeploytime(now.time)
//        subtask.setPlanstarttime(now.time)
//        now[Calendar.HOUR_OF_DAY] = 23
//        now[Calendar.MINUTE] = 59
//        now[Calendar.SECOND] = 59
//        subtask.setPlanendtime(now.time)
//        val currentUser: Userinfo = application.currentUser
//        subtask.setDeployerguid(currentUser.getGuid())
//        subtask.setDeployerrealname(currentUser.getRealname())
//        subtask.setDeployerusername(currentUser.getAcountname())
//        subtask.setExecutorguids(currentUser.getGuid())
//        subtask.setExecutorrealtimes(currentUser.getRealname())
//        subtask.setExecutorusernames(currentUser.getAcountname())
//        subtask.setStatus(Domain.TASK_STATUS_WAITING)
//        val template = "%s%s的%s无固定场景巡查"
//        val area: String
//        area = if (subtask.getTownname() == null) {
//            subtask.getCityname() + subtask.getDistrictname()
//        } else {
//            subtask.getCityname() + subtask.getDistrictname() + subtask.getTownname()
//        }
//        subtask.setName(
//            java.lang.String.format(
//                Locale.CHINA, template, DateFormatter.dateTimeFormat_month_2_minute.format(subtask.getPlanstarttime()), subtask.getExecutorrealtimes(), area
//            )
//        )
//        val call: retrofit2.Call<ResponseBody> = subtaskService.putSubTask(subtask)
//        call.enqueue(object : retrofit2.Callback<ResponseBody?>() {
//            fun onResponse(call: retrofit2.Call<ResponseBody>, response: retrofit2.Response<ResponseBody>) {
//                if (response.body() != null) {
//                    Toast.makeText(getActivity(), "子任务创建成功。", Toast.LENGTH_SHORT).show()
//                    showTurnToInspectionDialog(subtask)
//                } else if (response.errorBody() != null) {
//                    Toast.makeText(getActivity(), "子任务创建失败。", Toast.LENGTH_SHORT).show()
//                }
//            }
//
//            fun onFailure(call: retrofit2.Call<ResponseBody>, t: Throwable) {
//                Toast.makeText(getActivity(), "接口调用失败。", Toast.LENGTH_SHORT).show()
//            }
//        })
//    }
 
}