feiyu02
2024-11-19 752e00503f672ddfe2066afb6c235721a3a912b5
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
package cn.flightfeather.supervision.lightshare.vo
 
import cn.flightfeather.supervision.domain.entity.EnvironmentalSchedule
import java.time.Duration
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.ZoneId
import java.time.format.DateTimeFormatter
import java.util.*
import kotlin.math.abs
 
class ScheduleVo {
 
    inner class Step{
        var title: String? = null
        var content: String? = null
    }
 
    // 日程编号
    var id: Int? = null
 
    // 日程名称
    var title: String? = null
 
    // 日程描述
    var content: String? = null
 
    // 日程步骤解说
    var steps: MutableList<Step> = mutableListOf()
 
    // 日程类型,0:环保日程;1:环保事务
    var type: Byte? = null
 
    // 日程生效时间
    var time: Date? = null
        set(value) {
            if (value != null) {
                val now = LocalDateTime.now().withHour(0).withMinute(0).withSecond(0)
                val date = LocalDateTime.ofInstant(value.toInstant(), ZoneId.systemDefault()).withHour(0).withMinute(0).withSecond(0)
                this.diffDays = Duration.between(now, date).toDays().toInt()
            }
            field = value
        }
 
    // 日程是否需要签收
    var needSign: Boolean = false
 
    // 日程和服务器时间相差天数
    var diffDays: Int = 0
 
    // 日程是否有已经签收完成
    var finished: Boolean = false
 
    // 签收记录id
    var recordId: Int? = null
 
    companion object {
 
        fun toScheduleVo(schedule: EnvironmentalSchedule) = ScheduleVo().apply {
            id = schedule.scId
            title = schedule.scTitle
            content = schedule.scContent
            type = schedule.scType
            needSign = schedule.scNeedSign ?: false
 
            if (schedule.scStepName != null && schedule.scStepDetail != null) {
                val stepNames = schedule.scStepName.split(";")
                val stepDetail = schedule.scStepDetail.split(";")
                val count = if (stepNames.size > stepDetail.size) stepDetail.size else stepNames.size
                for (i in 0 until count) {
                    steps.add(Step().apply {
                        title = stepNames[i]
                        content = stepDetail[i]
                    })
                }
            }
        }
 
        fun toScheduleVoList(schedule: EnvironmentalSchedule, option: ScheduleOption): List<ScheduleVo> {
            var sT = LocalDateTime.ofInstant(option.startTime.toInstant(), ZoneId.systemDefault()).toLocalDate()
            val eT = LocalDateTime.ofInstant(option.endTime.toInstant(), ZoneId.systemDefault()).toLocalDate()
                .plusDays(1)
            val dateList = mutableListOf<Date>()
            when (schedule.scPeriodType.toInt()) {
                // 每日
                0 -> {
                    do {
                        dateList.add(Date.from(sT.atTime(0, 0, 0).atZone(ZoneId.systemDefault()).toInstant()))
                        sT = sT.plusDays(1)
                    } while (sT.isBefore(eT))
                }
                // 每周
                1 -> {
                    val weekList = schedule.scEveryWeek.split(";")
                    do {
                        if (weekList.contains(sT.dayOfWeek.value.toString())) {
                            dateList.add(Date.from(sT.atTime(0, 0, 0).atZone(ZoneId.systemDefault()).toInstant()))
                        }
                        sT = sT.plusDays(1)
                    } while (sT.isBefore(eT))
                }
                // 每月
                2 -> {
                    val dayList = schedule.scEveryMonth.split(";")
                    do {
                        if (dayList.contains(sT.dayOfMonth.toString())) {
                            dateList.add(Date.from(sT.atTime(0, 0, 0).atZone(ZoneId.systemDefault()).toInstant()))
                        }
                        sT = sT.plusDays(1)
                    } while (sT.isBefore(eT))
//                    var diffDays: Int? = null
//                    var mostRecentSchedule: Date? = null
//                    dayList.forEach {
//                        val diff = abs(it.toInt() - sT.dayOfMonth)
//                        if (diffDays == null || diff < diffDays!!) {
//                            mostRecentSchedule = Date.from(sT.withDayOfMonth(it.toInt()).atTime(0, 0, 0).atZone(ZoneId
//                                .systemDefault())
//                                .toInstant())
//                            diffDays = diff
//                        }
//                    }
//                    mostRecentSchedule?.let { dateList.add(it) }
                }
                // 每年
                3 -> {
                    val dayList = schedule.scEveryYear.split(";")
                    do {
                        val timeFormat = sT.format(DateTimeFormatter.ofPattern("MM-dd"))
                        if (dayList.contains(timeFormat)) {
                            dateList.add(Date.from(sT.atTime(0, 0, 0).atZone(ZoneId.systemDefault()).toInstant()))
                        }
                        sT = sT.plusDays(1)
                    } while (sT.isBefore(eT))
                }
                // 固定日期
                4 -> {
                    val time = LocalDateTime.ofInstant(schedule.scEffectiveTime.toInstant(), ZoneId.systemDefault()).toLocalDate()
                    if (
                        (sT.isBefore(time) || sT.isEqual(time)) && (eT.isAfter(time) || eT.isEqual(time))
                    ) {
                        dateList.add(schedule.scEffectiveTime)
                    }
                }
            }
            return dateList.map { toScheduleVo(schedule).apply { time = it } }
        }
    }
}