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, 100);
|
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);
|
}
|
|
|
}
|