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
package com.flightfeather.ducha.module.task
 
import cn.flightfeather.thirdappmodule.bean.entity.Scense
import cn.flightfeather.thirdappmodule.bean.entity.Subtask
import cn.flightfeather.thirdappmodule.bean.entity.Task
import cn.flightfeather.thirdappmodule.bean.vo.TaskVo
import cn.flightfeather.thirdappmodule.common.net.ResultCallBack
import cn.flightfeather.thirdappmodule.model.event.NewTaskEvent
import cn.flightfeather.thirdappmodule.module.base.BaseViewModel
import cn.flightfeather.thirdappmodule.module.task.NewSubTaskViewModel
import cn.flightfeather.thirdappmodule.repository.SceneRepository
import cn.flightfeather.thirdappmodule.repository.TaskRepository
import cn.flightfeather.thirdappmodule.util.DateFormatter
import cn.flightfeather.thirdappmodule.util.Domain
import cn.flightfeather.thirdappmodule.util.UUIDGenerator
import okhttp3.ResponseBody
import org.greenrobot.eventbus.EventBus
import org.jetbrains.anko.toast
import java.util.*
 
/**
 * @author riku
 * Date: 2020/6/10
 */
class DCNewTempTaskViewModel : BaseViewModel() {
    private val taskRepository = TaskRepository()
    private val sceneRepository = SceneRepository()
 
    val subTaskExecutor = NewSubTaskViewModel.SubTaskExecutor()
 
    val subTaskLeader = NewSubTaskViewModel.SubTaskLeader()
 
    /**
     * 获取已有的日任务或生成新的日任务
     */
    fun updateDayTask(topTask: TaskVo, startTime: Date, endTime: Date): Task {
        val today = DateFormatter.dateFormat.format(startTime)
        topTask.daytaskList.forEach {
            val day = DateFormatter.dateFormat.format(it.starttime)
            if (today == day) {
                return it.taskVo2Task()
            }
        }
        topTask.taskVo2Task().apply {
            var area = cityname + districtname
            area += if (extension1 == Domain.DOMAIN_SUITABLE_TOWN) {
                townname
            } else {
                ""
            }
            tguid = UUIDGenerator.generate16ShortUUID()
            tsguid = topTask.tguid
            levelnum = null
            name = "$today$area${typename}任务"
            starttime = startTime
            endtime = endTime
            plannerguid = application.currentUser.guid
            plannerusername = application.currentUser.acountname
            plannerrealname = application.currentUser.realname
            settime = Date()
 
            taskRepository.putTask(this, object : ResultCallBack<ResponseBody> {
                override fun onSuccess(result: ResponseBody?) {
                    application.toast("日任务信息上传成功")
                }
 
                override fun onFailure() {
                    application.toast("日任务信息上传失败,请重试。")
                }
 
            })
            return this
        }
    }
 
    fun uploadScene(scene: Scense, s: () -> Unit) {
        sceneRepository.putScene(scene, object : ResultCallBack<ResponseBody> {
            override fun onSuccess(result: ResponseBody?) {
                application.toast("场景上传成功")
                s()
            }
 
            override fun onFailure() {
                application.toast("场景上传失败")
            }
 
        })
    }
 
    fun uploadSubTask(subTask: Subtask, s: () -> Unit) {
        taskRepository.putSubTask(arrayListOf(subTask), object : ResultCallBack<ResponseBody> {
            override fun onSuccess(result: ResponseBody?) {
                application.toast("子任务信息上传成功")
 
                //新建子任务,提示刷新
                EventBus.getDefault().post(NewTaskEvent())
                s()
            }
 
            override fun onFailure() {
                application.toast("子任务信息上传失败,请重试")
            }
 
        })
    }
}