From 4f015b8c624484e0c3b2d88b944163ce43a48d1f Mon Sep 17 00:00:00 2001
From: admin <weikou2014>
Date: 星期六, 27 十一月 2021 17:15:28 +0800
Subject: [PATCH] 功能完善

---
 app/src/main/java/com/yeshi/location/app/controller/client/api/SOSController.java |  362 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 361 insertions(+), 1 deletions(-)

diff --git a/app/src/main/java/com/yeshi/location/app/controller/client/api/SOSController.java b/app/src/main/java/com/yeshi/location/app/controller/client/api/SOSController.java
index 3711a83..f1a38a5 100644
--- a/app/src/main/java/com/yeshi/location/app/controller/client/api/SOSController.java
+++ b/app/src/main/java/com/yeshi/location/app/controller/client/api/SOSController.java
@@ -1,12 +1,38 @@
 package com.yeshi.location.app.controller.client.api;
 
+import com.google.gson.*;
+import com.yeshi.location.app.entity.location.SimpleLocationInfo;
+import com.yeshi.location.app.entity.sos.EmergencyContacts;
+import com.yeshi.location.app.entity.sos.SOSRecord;
+import com.yeshi.location.app.entity.sos.SOSRecordList;
+import com.yeshi.location.app.entity.sos.SOSTargetInfo;
+import com.yeshi.location.app.entity.user.UserInfo;
 import com.yeshi.location.app.service.inter.sos.EmergencyContactsService;
+import com.yeshi.location.app.service.inter.sos.SOSRecordListService;
 import com.yeshi.location.app.service.inter.sos.SOSRecordService;
 import com.yeshi.location.app.service.inter.sos.SOSTargetInfoService;
+import com.yeshi.location.app.service.inter.user.UserInfoService;
+import com.yeshi.location.app.service.query.sos.EmergencyContactsQuery;
+import com.yeshi.location.app.service.query.sos.SOSRecordQuery;
+import com.yeshi.location.app.service.query.sos.SOSTargetInfoQuery;
+import com.yeshi.location.app.utils.Constant;
+import com.yeshi.location.app.utils.annotation.UserLogin;
+import com.yeshi.location.app.vo.AcceptData;
+import com.yeshi.location.app.vo.sos.EmergencyContactsVO;
+import com.yeshi.location.app.vo.sos.SOSRecordVO;
+import com.yeshi.location.app.vo.user.SimpleUserInfo;
+import net.sf.json.JSONObject;
+import net.sf.json.util.JSONUtils;
 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
@@ -15,7 +41,7 @@
  * @date 2021/11/16 17:37
  */
 @Controller
-@RequestMapping("api/v1/location")
+@RequestMapping("api/v1/sos")
 public class SOSController {
 
     @Resource
@@ -25,6 +51,340 @@
     @Resource
     private SOSTargetInfoService sosTargetInfoService;
 
+    @Resource
+    private SOSRecordListService sosRecordListService;
+
+    @Resource
+    private UserInfoService userInfoService;
 
 
+    @UserLogin(uid = "#uid")
+    @ResponseBody
+    @RequestMapping("addEmergencyContacts")
+    public String addEmergencyContacts(AcceptData acceptData, Long uid, String name, String phone, String remarks) {
+        if (StringUtil.isNullOrEmpty(name)) {
+            return JsonUtil.loadFalseResult("璇蜂笂浼犳樀绉�");
+        }
+
+        if (StringUtil.isNullOrEmpty(phone) || !StringUtil.isMobile(phone)) {
+            return JsonUtil.loadFalseResult("鐢佃瘽鍙风爜鏍煎紡涓嶅");
+        }
+
+        if (remarks != null && remarks.length() > 128) {
+            return JsonUtil.loadFalseResult("澶囨敞杩囬暱");
+        }
+
+        UserInfo userInfo = userInfoService.selectValidByPhone(acceptData.getSystem(), phone);
+
+        if (userInfo == null) {
+            return JsonUtil.loadFalseResult("璇ユ墜鏈哄彿灏氭湭娉ㄥ唽搴旂敤");
+        }
+
+        EmergencyContacts contacts = new EmergencyContacts();
+        contacts.setTargetName(name);
+        contacts.setTargetPhone(phone);
+        contacts.setTargetUid(userInfo.getId());
+        contacts.setUid(uid);
+        contacts.setRemarks(remarks);
+
+        try {
+            emergencyContactsService.add(contacts);
+            return JsonUtil.loadTrueResult("");
+        } catch (Exception e) {
+            return JsonUtil.loadFalseResult(e.getMessage());
+        }
+    }
+
+
+    @UserLogin(uid = "#uid")
+    @ResponseBody
+    @RequestMapping("listEmergencyContacts")
+    public String listEmergencyContacts(AcceptData acceptData, Long uid) {
+        EmergencyContactsQuery query = new EmergencyContactsQuery();
+        query.setUid(uid);
+        List<EmergencyContacts> list = emergencyContactsService.list(query, 1, Constant.PAGE_SIZE);
+        List<EmergencyContactsVO> voList = new ArrayList<>();
+        if (list != null && list.size() > 0) {
+            List<Long> uidList = new ArrayList<>();
+            for (EmergencyContacts contacts : list) {
+                uidList.add(contacts.getTargetUid());
+            }
+            List<UserInfo> userInfoList = userInfoService.list(uidList);
+            Map<Long, SimpleUserInfo> map = new HashMap<>();
+            for (UserInfo user : userInfoList) {
+                map.put(user.getId(), new SimpleUserInfo(user.getId(), user.getNickName(), user.getPortrait()));
+            }
+            for (EmergencyContacts contacts : list) {
+                voList.add(EmergencyContactsVO.create(contacts, map.get(contacts.getTargetUid())));
+            }
+        }
+        long count = emergencyContactsService.count(query);
+        JSONObject data = new JSONObject();
+        data.put("list", voList);
+        data.put("count", count);
+        return JsonUtil.loadTrueResult(data);
+    }
+
+
+    @UserLogin(uid = "#uid")
+    @ResponseBody
+    @RequestMapping("deleteEmergencyContacts")
+    public String deleteEmergencyContacts(AcceptData acceptData, Long uid, String id) {
+        EmergencyContacts contacts = emergencyContactsService.get(id);
+        if (contacts == null) {
+            return JsonUtil.loadFalseResult("璇ョ揣鎬ヨ仈绯讳汉涓嶅瓨鍦�");
+        }
+        if (!contacts.getUid().equals(uid)) {
+            return JsonUtil.loadFalseResult("鏃犲垹闄ゆ潈闄�");
+        }
+        emergencyContactsService.delete(Arrays.asList(new String[]{contacts.getId()}));
+        return JsonUtil.loadTrueResult("");
+    }
+
+    @UserLogin(uid = "#uid")
+    @ResponseBody
+    @RequestMapping("updateEmergencyContacts")
+    public String updateEmergencyContacts(AcceptData acceptData, Long uid, String id, String name, String phone, String remarks) {
+        EmergencyContacts contacts = emergencyContactsService.get(id);
+        if (contacts == null) {
+            return JsonUtil.loadFalseResult("璇ョ揣鎬ヨ仈绯讳汉涓嶅瓨鍦�");
+        }
+        if (!contacts.getUid().equals(uid)) {
+            return JsonUtil.loadFalseResult("鏃犱慨鏀规潈闄�");
+        }
+        EmergencyContacts update = new EmergencyContacts();
+        update.setId(id);
+        if (!StringUtil.isNullOrEmpty(phone)) {
+            UserInfo userInfo = userInfoService.selectValidByPhone(acceptData.getSystem(), phone);
+            if (userInfo == null) {
+                return JsonUtil.loadFalseResult("璇ユ墜鏈哄彿灏氭湭娉ㄥ唽搴旂敤");
+            }
+            update.setTargetUid(userInfo.getId());
+            update.setTargetPhone(phone);
+        }
+        update.setTargetName(name);
+        update.setRemarks(remarks);
+        emergencyContactsService.update(update);
+        return JsonUtil.loadTrueResult("");
+    }
+
+
+    @UserLogin(uid = "#uid")
+    @ResponseBody
+    @RequestMapping("addSOSRecord")
+    public String addSOSRecord(AcceptData acceptData, Long uid, SimpleLocationInfo location) {
+        if (location == null || location.getLongitude() == null || location.getLatitude() == null || location.getAddress() == null) {
+            return JsonUtil.loadFalseResult("瀹氫綅澶辫触");
+        }
+
+        EmergencyContactsQuery query = new EmergencyContactsQuery();
+        query.setUid(uid);
+        long count = emergencyContactsService.count(query);
+        if (count == 0) {
+            return JsonUtil.loadFalseResult("璇峰厛娣诲姞绱ф�ヨ仈绯讳汉");
+        }
+        List<EmergencyContacts> list = emergencyContactsService.list(query, 1, Constant.PAGE_SIZE);
+
+        SOSRecord record = new SOSRecord();
+        record.setUid(uid);
+        record.setLocation(location);
+        record.setContent("");
+        try {
+            sosRecordService.add(record);
+
+            for (EmergencyContacts contacts : list) {
+                SOSTargetInfo info = new SOSTargetInfo();
+                info.setSosId(record.getId());
+                info.setUid(uid);
+                info.setTargetUid(contacts.getTargetUid());
+                sosTargetInfoService.add(info);
+            }
+            return JsonUtil.loadTrueResult("");
+        } catch (Exception e) {
+            return JsonUtil.loadFalseResult(e.getMessage());
+        }
+    }
+
+    /**
+     * @return java.lang.String
+     * @author hxh
+     * @description 鑾峰彇SOS璁板綍
+     * @date 11:55 2021/11/22
+     * @param: acceptData
+     * @param: uid
+     * @param: page 椤电爜
+     **/
+    @UserLogin(uid = "#uid")
+    @ResponseBody
+    @RequestMapping("getSOSRecordList")
+    public String getSOSRecordList(AcceptData acceptData, Long uid, int page) {
+        UserInfo user = userInfoService.get(uid);
+
+        List<SOSRecordList> recordLists = sosRecordListService.listByUid(uid, page, Constant.PAGE_SIZE);
+        //鏌ヨ璇︽儏
+        List<String> sosIds = new ArrayList<>();
+        List<String> targetIds = new ArrayList<>();
+        for (SOSRecordList recordList : recordLists) {
+            if (recordList.getType() == SOSRecordList.SOSRecordType.initiative) {
+                sosIds.add(recordList.getSourceId());
+            } else {
+                targetIds.add(recordList.getSourceId());
+            }
+        }
+
+        List<SOSRecordVO> voList = new ArrayList<>();
+        List<Long> targetUids = new ArrayList<>();
+        List<SOSRecord> recordList;
+        List<UserInfo> userList;
+
+        //--------姹傚姪-------
+        if (sosIds.size() > 0) {
+            recordList = sosRecordService.listDetail(sosIds);
+
+            for (SOSRecord record : recordList) {
+                for (SOSTargetInfo targetInfo : record.getTargetList()) {
+                    targetUids.add(targetInfo.getTargetUid());
+                }
+            }
+            //鏌ヨ澶囨敞鏄电О
+            Map<Long, String> nickNameMap = new HashMap<>();
+            EmergencyContactsQuery query = new EmergencyContactsQuery();
+            query.setUid(uid);
+            List<EmergencyContacts> contacts = emergencyContactsService.list(query, 1, Constant.PAGE_SIZE);
+            for (EmergencyContacts contact : contacts) {
+                nickNameMap.put(contact.getTargetUid(), contact.getTargetName());
+                //鍒犻櫎鍘熷鏄电О
+                if (!StringUtil.isNullOrEmpty(contact.getTargetName())) {
+                    targetUids.remove(contact.getTargetUid());
+                }
+            }
+            //鏌ヨ鍘熷鏄电О
+            userList = userInfoService.list(targetUids);
+            for (UserInfo u : userList) {
+                nickNameMap.put(u.getId(), u.getNickName());
+            }
+
+
+            for (SOSRecord record : recordList) {
+                SOSRecordVO vo = new SOSRecordVO();
+                vo.setCreateTime(record.getCreateTime());
+                vo.setDesc(SOSRecordVO.getDesc(record, true));
+                vo.setFrom("鐢辫嚜宸卞彂鍑�");
+                vo.setLocation(record.getLocation());
+                vo.setPhone(null);
+                vo.setPortrait(user.getPortrait());
+                vo.setTargetDesc(SOSRecordVO.getTargetDesc(record.getTargetList(), nickNameMap));
+                voList.add(vo);
+            }
+        }
+
+
+        if (targetIds.size() > 0) {
+            //---------琚眰鍔�--------
+            targetUids.clear();
+
+            //杞崲
+            List<SOSTargetInfo> targetInfoList = sosTargetInfoService.listByIds(targetIds);
+            sosIds = new ArrayList<>();
+            for (SOSTargetInfo info : targetInfoList) {
+                sosIds.add(info.getSosId());
+            }
+
+            //杞崲
+            recordList = sosRecordService.listByIds(sosIds);
+            Map<String, SOSRecord> recordMap = new HashMap<>();
+            for (SOSRecord r : recordList) {
+                recordMap.put(r.getId(), r);
+                targetUids.add(r.getUid());
+            }
+
+            Map<Long, UserInfo> userInfoMap = new HashMap<>();
+            userList = userInfoService.list(targetUids);
+            for (UserInfo u : userList) {
+                userInfoMap.put(u.getId(), u);
+            }
+
+
+            for (SOSTargetInfo targetInfo : targetInfoList) {
+                SOSRecordVO vo = new SOSRecordVO();
+                vo.setCreateTime(targetInfo.getCreateTime());
+                vo.setDesc(SOSRecordVO.getDesc(recordMap.get(targetInfo.getSosId()), false));
+                vo.setFrom("鐢卞ソ鍙嬪彂鍑�");
+                vo.setLocation(recordMap.get(targetInfo.getSosId()).getLocation());
+                vo.setPhone(userInfoMap.get(recordMap.get(targetInfo.getSosId()).getUid()).getPhone());
+                vo.setPortrait(userInfoMap.get(recordMap.get(targetInfo.getSosId()).getUid()).getPortrait());
+                vo.setTargetDesc(null);
+                voList.add(vo);
+            }
+        }
+        long count = sosRecordListService.countByUid(uid);
+        Gson gson = new GsonBuilder().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();
+
+        JSONObject data = new JSONObject();
+        data.put("count", count);
+        data.put("list", gson.toJson(voList));
+        return JsonUtil.loadTrueResult(data);
+    }
+
+
+    @UserLogin(uid = "#uid")
+    @ResponseBody
+    @RequestMapping("clearSOSRecord")
+    public String clearSOSRecord(AcceptData acceptData, Long uid) {
+        sosRecordListService.deleteAll(uid);
+        return JsonUtil.loadTrueResult("");
+    }
+
+
+    /**
+     * @return java.lang.String
+     * @author hxh
+     * @description 鑾峰彇鎯虫垜姹傛晳鐨凷OS
+     * @date 13:08 2021/11/27
+     * @param: acceptData
+     * @param: uid
+     **/
+    @UserLogin(uid = "#uid")
+    @ResponseBody
+    @RequestMapping("getTargetSOSRecord")
+    public String getTargetSOSRecord(AcceptData acceptData, Long uid) {
+
+        SOSTargetInfoQuery query = new SOSTargetInfoQuery();
+        query.setTargetUid(uid);
+        query.setStatus(SOSTargetInfo.STATUS_UNREAD);
+        List<SOSTargetInfo> list = sosTargetInfoService.list(query, 1, 1);
+        if (list == null || list.size() == 0) {
+            return JsonUtil.loadFalseResult("鏆傛棤姹傚姪");
+        }
+        return JsonUtil.loadTrueResult(list.get(0));
+
+    }
+
+    @UserLogin(uid = "#uid")
+    @ResponseBody
+    @RequestMapping("readTargetSOS")
+    public String readTargetSOS(AcceptData acceptData, Long uid, String id) {
+
+        SOSTargetInfo targetInfo = sosTargetInfoService.get(id);
+        if (targetInfo == null) {
+            return JsonUtil.loadFalseResult("姹傚姪淇℃伅涓嶅瓨鍦�");
+        }
+
+        if (!targetInfo.getTargetUid().equals(uid)) {
+            return JsonUtil.loadFalseResult("涓嶆槸浣犵殑姹傚姪淇℃伅");
+        }
+        SOSTargetInfo update = new SOSTargetInfo();
+        update.setId(id);
+        update.setStatus(SOSTargetInfo.STATUS_READ);
+        update.setStatusDesc("宸叉煡闃�");
+        update.setReadTime(new Date());
+        sosTargetInfoService.update(update);
+        return JsonUtil.loadTrueResult("");
+    }
 }

--
Gitblit v1.8.0