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
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
 * @title: UserController
 * @description: 用户接口
 * @date 2021/11/16 17:37
 */
@Controller
@RequestMapping("api/v1/sos")
public class SOSController {
 
    @Resource
    private SOSRecordService sosRecordService;
    @Resource
    private EmergencyContactsService emergencyContactsService;
    @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 获取想我求救的SOS
     * @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("");
    }
}