吉安感知网项目-后端
linwei
2026-06-22 3ff3de60e9274da9601fba4d2165fe46be4d7f07
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
149
150
151
152
153
154
/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.sxkj.gd.workorder.service.impl;
 
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import org.springblade.core.tool.utils.StringUtil;
import org.sxkj.gd.workorder.dto.GdDeviceCallDetailExternalSaveDTO;
import org.sxkj.gd.workorder.entity.GdDeviceCallDetailEntity;
import org.sxkj.gd.workorder.entity.GdDeviceCallEntity;
import org.sxkj.gd.workorder.entity.GdManageDeviceEntity;
import org.sxkj.gd.workorder.entity.GdPatrolTaskEntity;
import org.sxkj.gd.workorder.service.IGdDeviceCallDetailService;
import org.sxkj.gd.workorder.service.IGdManageDeviceService;
import org.sxkj.gd.workorder.service.IGdPatrolTaskService;
import org.sxkj.gd.workorder.vo.GdDeviceCallVO;
import org.sxkj.gd.workorder.excel.GdDeviceCallExcel;
import org.sxkj.gd.workorder.mapper.GdDeviceCallMapper;
import org.sxkj.gd.workorder.service.IGdDeviceCallService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.sxkj.system.cache.SysCache;
import org.sxkj.system.cache.UserCache;
 
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
 
/**
 * 设备调用表 服务实现类
 *
 * @author Aix
 * @since 2026-02-26
 */
@Service
@AllArgsConstructor
public class GdDeviceCallServiceImpl extends BaseServiceImpl<GdDeviceCallMapper, GdDeviceCallEntity> implements IGdDeviceCallService {
 
    private final IGdDeviceCallDetailService gdDeviceCallDetailService;
    private final IGdPatrolTaskService gdPatrolTaskService;
    private final IGdManageDeviceService gdManageDeviceService;
 
    @Override
    public IPage<GdDeviceCallVO> selectGdDeviceCallPage(IPage<GdDeviceCallVO> page, GdDeviceCallVO gdDeviceCall) {
        return page.setRecords(baseMapper.selectGdDeviceCallPage(page, gdDeviceCall));
    }
 
    @Override
    public boolean saveGdDeviceCallExternal(List<GdDeviceCallDetailExternalSaveDTO> dtos) {
 
        // 保存设备调用
        GdPatrolTaskEntity patrolTask = gdPatrolTaskService.getById(dtos.get(0).getPatrolTaskId());
        if (patrolTask == null) {
            log.warn("巡查任务不存在:" + dtos.get(0).getPatrolTaskId());
            return false;
        }
        GdDeviceCallEntity callEntity = new GdDeviceCallEntity();
        callEntity.setId(patrolTask.getId());
        callEntity.setPatrolTaskName(patrolTask.getPatrolTaskName());
        callEntity.setTaskDepartment(SysCache.getDeptName(patrolTask.getCreateDept()));
        callEntity.setTaskDepartmentId(patrolTask.getCreateDept());
        callEntity.setTaskInitiator(UserCache.getUser(patrolTask.getCreateUser()).getName());
        callEntity.setTaskInitiatorId(patrolTask.getCreateUser());
        callEntity.setPlanExecuteTime(patrolTask.getExecuteTime());
        callEntity.setActualExecuteTime(dtos.get(0).getOccurTime());// 第一条数据的发生时间
 
        // 计算飞行时长(毫秒转分钟)
        // 计算飞行时长(处理可能的空值情况)
        Date startTime = dtos.get(0).getOccurTime();
        Date endTime = dtos.get(dtos.size() - 1).getOccurTime();
 
        if (startTime == null || endTime == null) {
            log.warn("时间数据不完整,无法计算飞行时长");
            callEntity.setFlightDuration(0L);
        } else {
            long flightDurationMs = endTime.getTime() - startTime.getTime();
            // 可以选择不同的时间单位
            callEntity.setFlightDuration(flightDurationMs / 1000); // 秒
        }
 
 
        // 计算总飞行距离
        double totalDistance = dtos.stream()
            .mapToDouble(GdDeviceCallDetailExternalSaveDTO::getFlightDistance)
            .sum();
        callEntity.setFlightDistance(totalDistance);
 
        // 设备ID&名称
        String deviceSn = dtos.get(0).getDeviceSn();
        Long deviceId = null;
        String deviceName = null;
 
        if (StringUtil.hasText(deviceSn)) {
            GdManageDeviceEntity gdManageDevice = gdManageDeviceService.getOne(
                Wrappers.lambdaQuery(GdManageDeviceEntity.class)
                    .eq(GdManageDeviceEntity::getDeviceSn, deviceSn),
                false
            );
 
            if (gdManageDevice == null) {
                log.warn("设备不存在:" + deviceSn);
                return false;
            }
            deviceId = gdManageDevice.getId();
            deviceName = gdManageDevice.getDeviceName();
        }
 
        callEntity.setDeviceId(deviceId);
        callEntity.setDeviceName(deviceName);
 
        // 保存设备调用详情
        if (baseMapper.insert(callEntity) <= 0) {
            return false;
        }
 
        List<GdDeviceCallDetailEntity> detailEntities = dtos.stream().map(dto -> {
            GdDeviceCallDetailEntity entity = new GdDeviceCallDetailEntity();
            entity.setCallId(dto.getPatrolTaskId());
            entity.setDeviceStatus(dto.getDeviceStatus());
            entity.setOccurTime(dto.getOccurTime());
            entity.setFlightDistance(dto.getFlightDistance());
            entity.setSort(dto.getSort());
            return entity;
        }).collect(Collectors.toList());
 
        return gdDeviceCallDetailService.saveBatch(detailEntities);
    }
 
    @Override
    public List<GdDeviceCallExcel> exportGdDeviceCall(Wrapper<GdDeviceCallEntity> queryWrapper) {
        List<GdDeviceCallExcel> gdDeviceCallList = baseMapper.exportGdDeviceCall(queryWrapper);
        //gdDeviceCallList.forEach(gdDeviceCall -> {
        //    gdDeviceCall.setTypeName(DictCache.getValue(DictEnum.YES_NO, GdDeviceCall.getType()));
        //});
        return gdDeviceCallList;
    }
 
}