feiyu02
2025-03-27 bde043c8fd1a076f44c402dd56c62d401afbfb16
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
package com.flightfeather.uav.lightshare.service.impl
 
import com.flightfeather.uav.biz.FactorFilter
import com.flightfeather.uav.biz.report.MissionReport
import com.flightfeather.uav.common.exception.BizException
import com.flightfeather.uav.domain.entity.Mission
import com.flightfeather.uav.domain.entity.RealTimeDataVehicle
import com.flightfeather.uav.domain.mapper.MissionMapper
import com.flightfeather.uav.domain.mapper.RealTimeDataVehicleMapper
import com.flightfeather.uav.lightshare.bean.BaseResponse
import com.flightfeather.uav.lightshare.bean.DataHead
import com.flightfeather.uav.lightshare.service.MissionService
import com.github.pagehelper.PageHelper
import org.springframework.stereotype.Service
import tk.mybatis.mapper.entity.Example
import java.io.File
import java.util.*
import javax.servlet.http.HttpServletResponse
 
@Service
class MissionServiceImpl(
    private val missionMapper: MissionMapper,
    private val realTimeDataVehicleMapper: RealTimeDataVehicleMapper,
    private val missionReport: MissionReport,
) : MissionService {
    override fun getMission(type: String?, page: Int?, perPage: Int?): BaseResponse<List<Mission>> {
        val _perPage = perPage ?: 60
        val _page = page ?: 1
        val pageInfo = PageHelper.startPage<Mission>(_page, _perPage)
        val result = mutableListOf<Mission>()
        missionMapper.selectByExample(Example(Mission::class.java).apply {
            type?.let {
                createCriteria().andEqualTo("deviceType", type)
            }
            orderBy("startTime").desc()
        }).forEach { it?.let { result.add(it) } }
        return BaseResponse(true, head = DataHead(pageInfo.pageNum, pageInfo.pages),data = result)
    }
 
    @Synchronized
    override fun createMission(mission: Mission): BaseResponse<Boolean> {
        missionMapper.selectByPrimaryKey(mission.missionCode)?.run {
            return BaseResponse(false, "任务编号已存在")
        }
        missionMapper.insert(mission).let {
            return BaseResponse(it == 1)
        }
    }
 
    override fun updateMission(mission: Mission): BaseResponse<Boolean> {
        missionMapper.updateByPrimaryKey(mission).let {
            return BaseResponse(it == 1)
        }
    }
 
    override fun deleteMission(missionCode: String): BaseResponse<Boolean> {
        missionMapper.deleteByPrimaryKey(missionCode).let {
            return BaseResponse(it == 1)
        }
    }
 
    override fun deleteMissionAndData(missionCode: String): Boolean {
        val mission = missionMapper.selectByPrimaryKey(missionCode) ?: throw BizException("任务不存在")
        missionMapper.deleteByPrimaryKey(missionCode)
        realTimeDataVehicleMapper.deleteByExample(Example(RealTimeDataVehicle::class.java).apply {
            createCriteria().andEqualTo("deviceCode", mission.deviceCode)
                .andBetween("dataTime", mission.startTime, mission.endTime)
        })
        return true
    }
 
    override fun getReport(missionCode: String, response: HttpServletResponse) {
        val path = missionReport.execute(missionCode, FactorFilter.default())
        val pathArr = path.split("/")
        val file = File(path)
        if (file.exists()) {
            response.contentType = "application/vnd.ms-excel;charset=UTF-8"
            val name = Base64.getEncoder().encodeToString(pathArr.last().toByteArray())
            response.setHeader("fileName", name)
            response.outputStream.write(file.readBytes())
        }
        return
    }
}