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
package cn.flightfeather.supervision.lightshare.service.Impl
 
import cn.flightfeather.supervision.common.exception.BizException
import cn.flightfeather.supervision.domain.entity.ScheduleSignRecord
import cn.flightfeather.supervision.domain.mapper.BaseInfoMapper
import cn.flightfeather.supervision.domain.mapper.EnvironmentalScheduleMapper
import cn.flightfeather.supervision.domain.mapper.ScheduleSignRecordMapper
import cn.flightfeather.supervision.domain.mapper.UserinfoMapper
import cn.flightfeather.supervision.lightshare.repository.ScheduleRepository
import cn.flightfeather.supervision.lightshare.repository.ScheduleSignRecordRepository
import cn.flightfeather.supervision.domain.repository.UserConfigRep
import cn.flightfeather.supervision.lightshare.service.ScheduleService
import cn.flightfeather.supervision.lightshare.vo.BaseResponse
import cn.flightfeather.supervision.lightshare.vo.ScheduleOption
import cn.flightfeather.supervision.lightshare.vo.ScheduleVo
import com.github.pagehelper.PageHelper
import org.springframework.stereotype.Service
import tk.mybatis.mapper.entity.Example
import java.time.LocalDate
import java.time.ZoneId
import java.util.*
 
@Service
class ScheduleServiceImpl(
    private val scheduleMapper: EnvironmentalScheduleMapper,
    private val scheduleSignRecordMapper: ScheduleSignRecordMapper,
    private val userinfoMapper: UserinfoMapper,
    private val baseInfoMapper: BaseInfoMapper,
    private val userConfigRep: UserConfigRep,
    private val scheduleRepository: ScheduleRepository,
    private val scheduleSignRecordRepository: ScheduleSignRecordRepository
) : ScheduleService {
 
    override fun getSchedules(option: ScheduleOption): BaseResponse<List<ScheduleVo>> {
        option.userId ?: return BaseResponse(false, "用户id不能为空")
        val userInfo = userinfoMapper.selectByPrimaryKey(option.userId) ?: return BaseResponse(false, "用户不存在")
        val baseInfo = baseInfoMapper.selectByPrimaryKey(option.userId)
        val config = userConfigRep.getUserConfig(userInfo, baseInfo)
        val res = mutableListOf<ScheduleVo>()
        scheduleRepository.getSchedules(userInfo, config, option).forEach{
            it?.let {
                res.addAll(ScheduleVo.toScheduleVoList(it, option))
            }
        }
        res.sortBy { it.time }
 
        res.forEach {
            val millisecond = it.time?.time!! + 1000 * 60 * 60 * 24
            val eTime = Date(millisecond)
            val r = scheduleSignRecordRepository.getRecord(option.userId, it.id, it.time, eTime)
            if (r.isNotEmpty()) {
                it.recordId = r[0]?.srId
                it.finished = r[0]?.srSignStatus ?: false
            }
        }
 
        return BaseResponse(true, data = res)
    }
 
//    override fun getYearSchedules(option: ScheduleOption): List<ScheduleVo> {
//        option.userId ?: throw BizException("用户id不能为空")
//        val userInfo = userinfoMapper.selectByPrimaryKey(option.userId) ?: throw BizException("用户不存在")
//        val baseInfo = baseInfoMapper.selectByPrimaryKey(option.userId)
//        val config = userConfigRep.getUserConfig(userInfo, baseInfo)
//        val res = mutableListOf<ScheduleVo>()
//        scheduleRepository.getSchedules(userInfo, config, option).forEach{
//            it?.let {
//
//                res.addAll(ScheduleVo.toScheduleVoList(it, option))
//            }
//        }
//
//        // 查询签收记录
//        res.forEach {
//            if (!it.needSign) return@forEach
//            val millisecond = it.time?.time!! + 1000 * 60 * 60 * 24
//            val eTime = Date(millisecond)
//            val r = scheduleSignRecordRepository.getRecord(option.userId, it.id, it.time, eTime)
//            if (r.isNotEmpty()) {
//                it.recordId = r[0]?.srId
//                it.finished = r[0]?.srSignStatus ?: false
//            }
//        }
//    }
 
    override fun completeSchedule(userId: String, id: Int): BaseResponse<ScheduleSignRecord> {
        val now = LocalDate.now().atTime(0,0,0)
        val sTime = Date.from(now.atZone(ZoneId.systemDefault()).toInstant())
        val eTime = Date.from(now.plusDays(1).atZone(ZoneId.systemDefault()).toInstant())
        val records = scheduleSignRecordRepository.getRecord(userId, id, sTime, eTime)
        return if (records.isNotEmpty()) {
            records[0]?.srSignStatus = true
            val r = scheduleSignRecordMapper.updateByPrimaryKeySelective(records[0])
            BaseResponse(r == 1, data = records[0])
        } else {
            PageHelper.startPage<ScheduleSignRecord>(1, 1)
            val latestOne = scheduleSignRecordMapper.selectByExample(Example(ScheduleSignRecord::class.java).apply {
                orderBy("srId").desc()
            })
            val index = if (latestOne.isEmpty()) {
                1
            } else {
                (latestOne[0]?.srId ?: 0) + 1
            }
            val record = ScheduleSignRecord().apply {
                srId = index
                srScheduleId = id
                srUserId = userId
                srSignStatus = true
                srSignTime = Date()
            }
            val r = scheduleSignRecordMapper.insert(record)
            BaseResponse(r == 1, data = record)
        }
    }
 
    override fun revokeSchedule(userId: String, recordId: Int): BaseResponse<Boolean> {
        val record = scheduleSignRecordMapper.selectByPrimaryKey(recordId) ?: return BaseResponse(false, "该日程不存在")
        record.srSignStatus = false
        val r = scheduleSignRecordMapper.updateByPrimaryKeySelective(record)
        return BaseResponse(r == 1)
    }
}