18034727885
2026-04-14 370014cbb9ec1ef64e7e42bb4f9a12671461d8e4
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
package com.xw.yunwei.ui.user.mine.scheduling
 
import android.annotation.SuppressLint
import android.app.Application
import android.view.View
import androidx.core.os.bundleOf
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.map
import com.xw.common.base.BaseViewModel
import com.xw.common.utils.ToastUtilCommon
import com.xw.yunwei.R
import com.xw.yunwei.model.ClockInStatusModel
import com.xw.yunwei.net.UserRepository
import com.xw.yunwei.ui.user.mine.checkin.CheckInActivity
import org.koin.core.component.inject
import java.time.LocalDate
import java.time.format.DateTimeFormatter
import java.util.Locale
import kotlin.getValue
 
@SuppressLint("SimpleDateFormat")
class SchedulingVM(application: Application) : BaseViewModel(application) {
    val userRepository: UserRepository by inject()
 
    var today = ""
    val selectTime = MutableLiveData<String>()
    val yearMonth = selectTime.map {
        try {
            val inputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd")
            val date = LocalDate.parse(it, inputFormatter)
            val outputFormatter = DateTimeFormatter.ofPattern("yyyy年M月", Locale.CHINESE)
            date.format(outputFormatter)
        } catch (e: Exception) {
            ""
        }
    }
    // 一周的中文以及格式化时间
    val showWeekDay: MutableLiveData<List<String>> = MutableLiveData()
    val showWeekDate: MutableLiveData<List<String>> = MutableLiveData()
 
    // 对应currentTime的打卡信息
    val clockModel: MutableLiveData<ClockInStatusModel?> = MutableLiveData()
    val clockTime = clockModel.map {
//        "${it?.xpShift?.startTime ?: ""} - ${it?.xpShift?.restStartTime ?: ""}  ${it?.xpShift?.restEndTime ?: ""} - ${it?.xpShift?.endTime ?: ""}"
        "${selectTime.value?:""} ${it?.xpShift?.startTime ?: ""}~${it?.xpShift?.endTime ?: ""}"
    }
 
    val onClick = View.OnClickListener {
        when (it.id) {
//            R.id.goCheck -> startActivity(
//                CheckInActivity::class.java,
//                bundleOf("clockModel" to clockModel.value)
//            )
            R.id.date1 -> selectTime.value = showWeekDate.value?.getOrNull(0) ?: ""
            R.id.date2 -> selectTime.value = showWeekDate.value?.getOrNull(1) ?: ""
            R.id.date3 -> selectTime.value = showWeekDate.value?.getOrNull(2) ?: ""
            R.id.date4 -> selectTime.value = showWeekDate.value?.getOrNull(3) ?: ""
            R.id.date5 -> selectTime.value = showWeekDate.value?.getOrNull(4) ?: ""
            R.id.date6 -> selectTime.value = showWeekDate.value?.getOrNull(5) ?: ""
            R.id.date7 -> selectTime.value = showWeekDate.value?.getOrNull(6) ?: ""
            R.id.clockIn -> startActivity(
                CheckInActivity::class.java,
                bundleOf("clockModel" to clockModel.value, "enterType" to 1)
            )
 
            R.id.clockOut -> startActivity(
                CheckInActivity::class.java,
                bundleOf("clockModel" to clockModel.value, "enterType" to 2)
            )
        }
    }
 
    fun querySchedule(time: String, showLoading: Boolean = true) {
        launchRequest(showLoading = showLoading) {
            userRepository.querySchedule(mapOf("schedulingDate" to time)).let {
                clockModel.postValue(it)
            }
        }
    }
 
    /**
     * 生成从输入日期开始后7天的星期名称
     * @param dateString 日期字符串
     */
    fun createNext7DaysWeekNames(dateString: String) {
        val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd")
        val startDate = LocalDate.parse(dateString, formatter)
 
        // 中文星期简写映射
        val weekDayMap = mapOf(
            java.time.DayOfWeek.MONDAY to "一",
            java.time.DayOfWeek.TUESDAY to "二",
            java.time.DayOfWeek.WEDNESDAY to "三",
            java.time.DayOfWeek.THURSDAY to "四",
            java.time.DayOfWeek.FRIDAY to "五",
            java.time.DayOfWeek.SATURDAY to "六",
            java.time.DayOfWeek.SUNDAY to "日"
        )
 
        val days = mutableListOf<String>()
        val weeks = mutableListOf<String>()
 
        (0 until 7).map { dayOffset ->
            val date = startDate.plusDays(dayOffset.toLong())
            days.add(date.format(formatter))  // 几号:24, 25, 26...
            weeks.add(weekDayMap[date.dayOfWeek] ?: "")
        }
        showWeekDate.value = days
        showWeekDay.value = weeks
    }
}