admin
2021-11-27 4f015b8c624484e0c3b2d88b944163ce43a48d1f
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
package com.yeshi.location.app.controller.client.api;
 
import com.google.gson.*;
import com.yeshi.location.app.entity.location.LocationTravel;
import com.yeshi.location.app.entity.location.LocationUsers;
import com.yeshi.location.app.entity.location.SimpleLocationInfo;
import com.yeshi.location.app.entity.location.UserLatestLocation;
import com.yeshi.location.app.entity.user.UserInfo;
import com.yeshi.location.app.service.inter.location.LocationTravelService;
import com.yeshi.location.app.service.inter.location.LocationUsersService;
import com.yeshi.location.app.service.inter.location.UserLatestLocationService;
import com.yeshi.location.app.service.inter.user.UserInfoService;
import com.yeshi.location.app.service.query.location.LocationTravelQuery;
import com.yeshi.location.app.utils.Constant;
import com.yeshi.location.app.utils.annotation.UserLogin;
import com.yeshi.location.app.vo.AcceptAdminData;
import com.yeshi.location.app.vo.AcceptData;
import com.yeshi.location.app.vo.user.SimpleUserInfo;
import net.sf.json.JSONObject;
import org.springframework.scheduling.support.SimpleTriggerContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.yeshi.utils.JsonUtil;
import org.yeshi.utils.StringUtil;
import org.yeshi.utils.TimeUtil;
 
import javax.annotation.Resource;
import java.lang.reflect.Type;
import java.util.*;
 
/**
 * @author hxh
 * @title: UserController
 * @description: 位置接口
 * @date 2021/11/16 17:37
 */
@Controller
@RequestMapping("api/v1/location")
public class LocationController {
 
    @Resource
    private LocationTravelService locationTravelService;
 
    @Resource
    private LocationUsersService locationUsersService;
 
    @Resource
    private UserLatestLocationService userLatestLocationService;
 
    @Resource
    private UserInfoService userInfoService;
 
 
    /**
     * 上传位置
     *
     * @param acceptData
     * @param uid
     * @param locationInfo
     * @return
     */
 
    @UserLogin(uid = "#uid")
    @RequestMapping("uploadLocation")
    @ResponseBody
    public String uploadLocation(AcceptData acceptData, Long uid, SimpleLocationInfo locationInfo) {
 
        LocationTravel locationTravel = new LocationTravel();
        locationTravel.setUid(uid);
        locationTravel.setLocation(locationInfo);
        try {
            locationTravelService.add(locationTravel);
            return JsonUtil.loadTrueResult("");
        } catch (Exception e) {
            return JsonUtil.loadFalseResult(e.getMessage());
        }
    }
 
 
    /**
     * 获取最新的位置信息
     *
     * @param acceptData
     * @param uid
     * @param targetUid
     * @return
     */
    @UserLogin(uid = "#uid")
    @RequestMapping("getLocation")
    @ResponseBody
    public String getLocation(AcceptData acceptData, Long uid, Long targetUid) {
 
        if (targetUid == null) {
            targetUid = uid;
        }
 
        if (!targetUid.equals(uid)) {
            LocationUsers users = locationUsersService.selectByUidAndTargetUid(uid, targetUid);
            if (users == null) {
                return JsonUtil.loadFalseResult("对方不是您的定位对象");
            } else if (users.getStatus() != LocationUsers.LocationInviteStatus.agree) {
                return JsonUtil.loadFalseResult("定位对象尚未同意");
            }
        }
 
        Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().registerTypeAdapter(Date.class, new JsonSerializer<Date>() {
 
            @Override
            public JsonElement serialize(Date date, Type type, JsonSerializationContext jsonSerializationContext) {
                return date == null ? new JsonPrimitive("") : new JsonPrimitive(TimeUtil.getGernalTime(date.getTime(), "yyyy.MM.dd HH:mm:ss"));
            }
        }).create();
 
        UserLatestLocation location = userLatestLocationService.get(targetUid);
        if (location == null) {
            return JsonUtil.loadFalseResult("尚未获取到对方的位置信息");
        }
        if (location.getUpdateTime() == null) {
            location.setUpdateTime(location.getCreateTime());
        }
 
        LocationTravelQuery query = new LocationTravelQuery();
        query.setUid(uid);
        location.setLocationCount((int) locationTravelService.count(query));
        return JsonUtil.loadTrueResult(gson.toJson(location));
    }
 
 
    /**
     * 获取定位对象
     *
     * @param acceptData
     * @param uid
     * @param page
     * @return
     */
    @UserLogin(uid = "#uid")
    @RequestMapping("getLocationUsers")
    @ResponseBody
    public String getLocationUsers(AcceptData acceptData, Long uid, int page) {
 
        List<LocationUsers> list = locationUsersService.listMyLocationUser(uid, null, null, page, Constant.PAGE_SIZE);
        if (list != null) {
            List<Long> uidList = new ArrayList<>();
            for (LocationUsers u : list) {
                uidList.add(u.getTargetUid());
            }
 
            List<UserInfo> userInfoList = userInfoService.list(uidList);
            Map<Long, UserInfo> map = new HashMap<>();
            for (UserInfo user : userInfoList) {
                map.put(user.getId(), user);
            }
 
            for (LocationUsers users : list) {
                if (map.get(users.getTargetUid()) != null) {
                    users.setUserInfo(SimpleUserInfo.create(map.get(users.getTargetUid())));
                }
            }
 
 
        }
 
        GsonBuilder gb = new GsonBuilder();
        gb.excludeFieldsWithoutExposeAnnotation();
        Gson gson = gb.create();
        long count = locationUsersService.countMyLocationUser(uid, null);
        JSONObject data = new JSONObject();
        data.put("count", count);
        data.put("list", gson.toJson(list));
        return JsonUtil.loadTrueResult(data);
    }
 
 
    /**
     * 添加定位对象
     *
     * @param acceptData
     * @param uid
     * @param phone
     * @param name
     * @return
     */
    @UserLogin(uid = "#uid")
    @RequestMapping("addLocationUser")
    @ResponseBody
    public String addLocationUser(AcceptData acceptData, Long uid, String phone, String name, LocationUsers.LocationUserType type) {
 
        UserInfo user = null;
        if (StringUtil.isMobile(phone)) {
            user = userInfoService.selectValidByPhone(acceptData.getSystem(), phone);
        } else {
            user = userInfoService.get(Long.parseLong(phone));
        }
        if (user == null) {
            return JsonUtil.loadFalseResult("该电话号码/用户ID不存在,请先注册");
        }
 
        LocationUsers locationUsers = locationUsersService.selectByUidAndTargetUid(uid, user.getId());
        if (locationUsers != null) {
 
            if (locationUsers.getStatus() == LocationUsers.LocationInviteStatus.sentInvite) {
                return JsonUtil.loadFalseResult("等待对方同意");
            }
 
            if (locationUsers.getStatus() == LocationUsers.LocationInviteStatus.reject) {
                return JsonUtil.loadFalseResult("对方已拒绝");
            }
 
            if (locationUsers.getStatus() == LocationUsers.LocationInviteStatus.agree) {
                return JsonUtil.loadFalseResult("对方已是你的定位对象");
            }
 
        }
 
        locationUsers = new LocationUsers();
        locationUsers.setUid(uid);
        locationUsers.setTargetName(name);
        locationUsers.setTargetUid(user.getId());
        locationUsers.setTargetPhone(user.getPhone());
 
        locationUsers.setUserType(type);
        try {
            locationUsersService.add(locationUsers);
        } catch (Exception e) {
            return JsonUtil.loadFalseResult(e.getMessage());
        }
 
        return JsonUtil.loadTrueResult("");
    }
 
 
    /**
     * 删除定位对象
     *
     * @param acceptData
     * @param uid
     * @param id
     * @return
     */
    @UserLogin(uid = "#uid")
    @RequestMapping("deleteLocationUser")
    @ResponseBody
    public String deleteLocationUser(AcceptData acceptData, Long uid, String id) {
 
        LocationUsers users = locationUsersService.get(id);
        if (users == null) {
            return JsonUtil.loadFalseResult("定位对象不存在");
        }
 
        if (users.getUid().longValue() != uid) {
            return JsonUtil.loadFalseResult("定位对象不存在");
        }
        locationUsersService.delete(Arrays.asList(new String[]{id}));
        return JsonUtil.loadTrueResult("");
    }
 
 
    @UserLogin(uid = "#uid")
    @RequestMapping("updateLocationUser")
    @ResponseBody
    public String updateLocationUser(AcceptData acceptData, Long uid, String id, String phone, LocationUsers.LocationUserType type) {
 
        LocationUsers users = locationUsersService.get(id);
        if (users == null) {
            return JsonUtil.loadFalseResult("定位对象不存在");
        }
 
        if (users.getUid().longValue() != uid) {
            return JsonUtil.loadFalseResult("定位对象不存在");
        }
        LocationUsers update = new LocationUsers();
        update.setId(id);
        update.setUserType(type);
        if (type != null) {
            update.setTargetName(type.getName());
        }
        update.setTargetPhone(phone);
        locationUsersService.update(update);
        return JsonUtil.loadTrueResult("");
    }
 
 
    /**
     * 同意定位邀请
     *
     * @param acceptData
     * @param uid
     * @param id
     * @return
     */
    @UserLogin(uid = "#uid")
    @RequestMapping("agreeInviteLocation")
    @ResponseBody
    public String agreeInviteLocation(AcceptData acceptData, Long uid, String id) {
        LocationUsers locationUsers = locationUsersService.get(id);
        if (locationUsers == null) {
            return JsonUtil.loadFalseResult("邀请记录不存在");
        }
 
        if (!locationUsers.getTargetUid().equals(uid)) {
            return JsonUtil.loadFalseResult("不属于您的邀请记录");
        }
 
        if (locationUsers.getStatus() != LocationUsers.LocationInviteStatus.sentInvite) {
            return JsonUtil.loadFalseResult("邀请状态错误");
        }
 
        LocationUsers update = new LocationUsers();
        update.setId(id);
        update.setStatus(LocationUsers.LocationInviteStatus.agree);
        update.setAccessTime(new Date());
        locationUsersService.update(update);
        return JsonUtil.loadTrueResult("");
 
    }
 
    /**
     * 拒绝定位邀请
     *
     * @param acceptData
     * @param uid
     * @param id
     * @return
     */
    @UserLogin(uid = "#uid")
    @RequestMapping("rejectInviteLocation")
    @ResponseBody
    public String rejectInviteLocation(AcceptData acceptData, Long uid, String id) {
        LocationUsers locationUsers = locationUsersService.get(id);
        if (locationUsers == null) {
            return JsonUtil.loadFalseResult("邀请记录不存在");
        }
 
        if (!locationUsers.getTargetUid().equals(uid)) {
            return JsonUtil.loadFalseResult("不属于您的邀请记录");
        }
 
        if (locationUsers.getStatus() != LocationUsers.LocationInviteStatus.sentInvite) {
            return JsonUtil.loadFalseResult("邀请状态错误");
        }
 
        LocationUsers update = new LocationUsers();
        update.setId(id);
        update.setStatus(LocationUsers.LocationInviteStatus.reject);
        update.setRejectTime(new Date());
        locationUsersService.update(update);
        return JsonUtil.loadTrueResult("");
    }
 
    /**
     * 获取需要处理的定位邀请
     *
     * @param acceptData
     * @param uid
     * @return
     */
    @UserLogin(uid = "#uid")
    @RequestMapping("getInviteLocation")
    @ResponseBody
    public String getInviteLocation(AcceptData acceptData, Long uid) {
        List<LocationUsers> list = locationUsersService.listMyLocationUser(null, uid, LocationUsers.LocationInviteStatus.sentInvite, 1, 1);
        if (list == null || list.size() == 0) {
            return JsonUtil.loadFalseResult("暂无记录");
        }
        return JsonUtil.loadTrueResult(JsonUtil.getApiCommonGson().toJson(list.get(0)));
    }
 
    /**
     * 获取轨迹
     *
     * @param acceptData
     * @param uid
     * @param targetUid
     * @return
     */
    @UserLogin(uid = "#uid")
    @RequestMapping("getTravel")
    @ResponseBody
    public String getTravel(AcceptData acceptData, Long uid, Long targetUid, Long startTime, Long endTime, int page) {
 
        if (targetUid == null) {
            targetUid = uid;
        }
 
        if (!targetUid.equals(uid)) {
            LocationUsers locationUsers = locationUsersService.selectByUidAndTargetUid(uid, targetUid);
            if (locationUsers == null || locationUsers.getStatus() != LocationUsers.LocationInviteStatus.agree) {
                return JsonUtil.loadFalseResult("暂无对方定位权限");
            }
        }
 
        //获取轨迹记录
        List<LocationTravel> locationTravelList = locationTravelService.getTravelList(targetUid, startTime == null ? null : new Date(startTime), endTime == null ? null : new Date(endTime), page, 1);
        List<UserLatestLocation> simpleLocationInfoList = new ArrayList<>();
        if (locationTravelList != null) {
            for (LocationTravel lt : locationTravelList) {
                UserLatestLocation location = new UserLatestLocation();
                location.setLocation(lt.getLocation());
                location.setUpdateTime(lt.getCreateTime());
                location.setUid(targetUid);
                location.setLocationCount(1);
                simpleLocationInfoList.add(location);
            }
        }
 
        Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().registerTypeAdapter(Date.class, new JsonSerializer<Date>() {
 
            @Override
            public JsonElement serialize(Date date, Type type, JsonSerializationContext jsonSerializationContext) {
                return date == null ? new JsonPrimitive("") : new JsonPrimitive(TimeUtil.getGernalTime(date.getTime(), "yyyy.MM.dd HH:mm"));
            }
        }).create();
 
        long count = locationTravelService.countTravel(targetUid, startTime == null ? null : new Date(startTime), endTime == null ? null : new Date(endTime));
        JSONObject data = new JSONObject();
        data.put("list", gson.toJson(simpleLocationInfoList));
        data.put("count", count);
 
        return JsonUtil.loadTrueResult(data);
    }
 
 
}