riku
2021-10-09 01ac08186355e85cab4a7c6f4403180184894f23
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
package com.flightfeather.uav.lightshare.service.impl
 
import com.flightfeather.uav.common.utils.DateUtil
import com.flightfeather.uav.domain.entity.CompanyDevice
import com.flightfeather.uav.domain.entity.ElectricMinuteValue
import com.flightfeather.uav.domain.entity.toAirData
import com.flightfeather.uav.domain.mapper.CompanyDeviceMapper
import com.flightfeather.uav.domain.mapper.ElectricMinuteValueMapper
import com.flightfeather.uav.lightshare.bean.BaseResponse
import com.flightfeather.uav.lightshare.bean.DataHead
import com.flightfeather.uav.lightshare.bean.DataVo
import com.flightfeather.uav.lightshare.service.ElectricityService
import com.flightfeather.uav.socket.bean.AirData
import com.github.pagehelper.PageHelper
import org.springframework.stereotype.Service
import tk.mybatis.mapper.entity.Example
import java.text.SimpleDateFormat
 
@Service
class ElectricityServiceImpl(
    private val electricMinuteValueMapper: ElectricMinuteValueMapper, private val companyDeviceMapper: CompanyDeviceMapper
) : ElectricityService {
 
    private var dateFormatter = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
 
    override fun getMinuteData(
        deviceCode: String, startTime: String?,
        endTime: String?, page: Int?, perPage: Int?
    ): BaseResponse<List<ElectricMinuteValue>> {
        val perP = perPage ?: 60
        val p = page ?: 1
        val sTime = startTime?.let { dateFormatter.parse(it) }
        val eTime = endTime?.let { dateFormatter.parse(it) }
        val pageInfo = PageHelper.startPage<ElectricMinuteValue>(p, perP)
        val result = mutableListOf<ElectricMinuteValue>()
        electricMinuteValueMapper.selectByExample(Example(ElectricMinuteValue::class.java).apply {
            createCriteria().andEqualTo("mvStatCode", deviceCode)
                .apply {
                    sTime?.let { andGreaterThanOrEqualTo("mvDataTime", it) }
                    eTime?.let { andLessThanOrEqualTo("mvDataTime", it) }
                }
            orderBy("mvDataTime").apply {
                // 当请求接口不传递起始时间,默认获取最新的数据
                if (startTime == null && endTime == null) {
                    desc()
                }
            }
        }).forEach {
            it?.let { result.add(it) }
        }
        if (startTime == null && endTime == null) {
            result.reverse()
        }
        return BaseResponse(true, head = DataHead(pageInfo.pageNum, pageInfo.pages), data = result)
    }
 
    override fun getMinuteData2(deviceCode: String, startTime: String?, endTime: String?, page: Int?, perPage: Int?): BaseResponse<List<DataVo>> {
        val result = mutableListOf<DataVo>()
        getMinuteData(deviceCode, startTime, endTime, page, perPage).run {
            data?.forEach {
                result.add(DataVo(
                    DateUtil.instance.dateToString(it.mvDataTime, DateUtil.DateStyle.YYYY_MM_DD_HH_MM_SS),
                    it.mvStatCode,
                    it.toAirData()
                ))
            }
            return BaseResponse(success, head = head, data = result)
        }
    }
 
    override fun getByCompany(cId: String): BaseResponse<List<ElectricMinuteValue>> {
        val result = mutableListOf<ElectricMinuteValue>()
        companyDeviceMapper.selectByExample(Example(CompanyDevice::class.java).apply {
            createCriteria().andEqualTo("cdCompanyId", cId)
        }).forEach {
            val p = PageHelper.startPage<ElectricMinuteValue>(1, 1)
            electricMinuteValueMapper.selectByExample(Example(ElectricMinuteValue::class.java).apply {
                createCriteria().andEqualTo("mvStatCode", it?.cdDeviceCode)
                orderBy("mvDataTime").desc()
            })?.let {
                if (it.isNotEmpty()) {
                    it[0]?.let {e-> result.add(e) }
                }
            }
        }
 
        return BaseResponse(true, data = result)
    }
}