admin
2021-11-27 4f015b8c624484e0c3b2d88b944163ce43a48d1f
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 获取想我求救的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("");
    }
}