package com.ydtech.modules.admin.controller.websocket; import cn.hutool.core.bean.BeanUtil; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.ydtech.config.SpringContextUtils; import com.ydtech.exception.SystemException; import com.ydtech.modules.admin.dao.SysDeptMapper; import com.ydtech.modules.admin.model.SysDept; import com.ydtech.modules.admin.model.SysGeographyPosition; import com.ydtech.modules.admin.model.SysGeographyPositionRecord; import com.ydtech.modules.admin.model.SysUser; import com.ydtech.modules.admin.service.SysDeptService; import com.ydtech.modules.admin.service.SysGeographyPositionRecordService; import com.ydtech.modules.admin.service.SysGeographyPositionService; import com.ydtech.modules.admin.service.SysUserService; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; import javax.websocket.*; import javax.websocket.server.PathParam; import javax.websocket.server.ServerEndpoint; import java.util.Date; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicInteger; @Slf4j @ServerEndpoint(value = "/websocket/{userId}") @Component public class WebSocketServer { private String userId; private Session session; private static Map sessionClientMap = new ConcurrentHashMap<>(); /**静态变量,用来记录当前在线连接数*/ private static final AtomicInteger onlineCount = new AtomicInteger(0); /** * 连接成功调用的方法 * @param userId * @param session */ @OnOpen public void onOpen(@PathParam("userId") String userId, Session session) { this.session=session; this.userId=userId; if (sessionClientMap.containsKey(userId)) { sessionClientMap.remove(userId); // 加入map中 sessionClientMap.put(userId, session); } else { // 加入map中 sessionClientMap.put(userId, session); // 在线数加1 onlineCount.incrementAndGet(); } editStatus(userId); log.info("APP用户连接成功:" + userId + ",当前在线人数为:" + onlineCount); sendMessage("APP用户连接成功"); } @OnMessage public void onMessage(String message, Session session) { JSONObject jsonObject = JSONObject.parseObject(message); String userId = jsonObject.getString("userId"); String longitude = jsonObject.getString("longitude"); String latitude = jsonObject.getString("latitude"); String province = jsonObject.getString("province"); String city = jsonObject.getString("city"); String district = jsonObject.getString("district"); String street = jsonObject.getString("street"); String streetNum = jsonObject.getString("streetNum"); String poiName = jsonObject.getString("poiName"); String cityCode = jsonObject.getString("cityCode"); saveLocationData(userId, latitude, longitude,province,city,district,street,streetNum,poiName,cityCode); log.info("服务端收到APP用户消息:{} ,主体:{}", message, userId); sendMessage("服务端已收到APP用户信息:" +message); } @OnClose public void onClose(@PathParam("userId") String userId, Session session) { delStatus(userId); sessionClientMap.remove(userId); log.info("APP用户断联成功!"); } @OnError public void onError(Throwable throwable) { log.info("websocket异常,错误信息:{}", throwable.getMessage()); } private void delStatus(String userId) { SysGeographyPositionService sysGeographyPositionService= SpringContextUtils.getBean(SysGeographyPositionService.class); SysGeographyPosition sysGeographyPosition = sysGeographyPositionService.getById(userId); if (sysGeographyPosition != null) { sysGeographyPosition.setStatus(0); sysGeographyPositionService.updateById(sysGeographyPosition); } } private void editStatus(String userId) { SysGeographyPositionService sysGeographyPositionService= SpringContextUtils.getBean(SysGeographyPositionService.class); SysGeographyPosition sysGeographyPosition = sysGeographyPositionService.getById(userId); if (sysGeographyPosition != null) { sysGeographyPosition.setStatus(1); sysGeographyPositionService.updateById(sysGeographyPosition); } } public void sendMessage(String message) { this.session.getAsyncRemote().sendText(message); } public void saveLocationData(String userId, String latitude, String longitude, String province,String city,String district, String street,String streetNum,String poiName,String cityCode) { SysGeographyPositionRecordService sysGeographyPositionRecordService = SpringContextUtils.getBean(SysGeographyPositionRecordService.class); SysGeographyPositionService sysGeographyPositionService= SpringContextUtils.getBean(SysGeographyPositionService.class); SysGeographyPosition sysGeographyPosition1 = sysGeographyPositionService.getById(userId); SysDeptMapper sysDeptMapper = SpringContextUtils.getBean(SysDeptMapper.class); SysUserService sysUserService = SpringContextUtils.getBean(SysUserService.class); SysUser sysUser = sysUserService.getById(userId); SysDept sysDept = sysDeptMapper.selectOne(new LambdaQueryWrapper() .eq(SysDept::getId, sysUser.getDeptId()).eq(SysDept::getDelFlag, 0)); if (sysGeographyPosition1 == null) { //不存在新增 SysGeographyPosition sysGeographyPosition = new SysGeographyPosition(); sysGeographyPosition.setId(userId); sysGeographyPosition.setUserName(sysUser.getName()); sysGeographyPosition.setLatitude(latitude); sysGeographyPosition.setLongitude(longitude); sysGeographyPosition.setProvince(province); sysGeographyPosition.setCity(city); sysGeographyPosition.setDistrict(district); sysGeographyPosition.setStreet(street); sysGeographyPosition.setStreetNum(streetNum); sysGeographyPosition.setPoiName(poiName); sysGeographyPosition.setCityCode(cityCode); sysGeographyPosition.setAddress(province + city + district + street+streetNum+poiName+cityCode); sysGeographyPosition.setCreateTime(new Date()); sysGeographyPosition.setCreateBy(sysUser.getName()); sysGeographyPosition.setSource(sysDept.getManagementSource()); sysGeographyPosition.setLeaderName(sysUser.getLeaderName()); sysGeographyPositionService.save(sysGeographyPosition); SysGeographyPositionRecord sysGeographyPositionRecord = BeanUtil.copyProperties(sysGeographyPosition, SysGeographyPositionRecord.class); sysGeographyPositionRecord.setId(null); sysGeographyPositionRecord.setUserId(sysGeographyPosition.getId()); sysGeographyPositionRecordService.save(sysGeographyPositionRecord); }else { //存在更新 sysGeographyPosition1.setLongitude(longitude); sysGeographyPosition1.setLatitude(latitude); sysGeographyPosition1.setProvince(province); sysGeographyPosition1.setCity(city); sysGeographyPosition1.setDistrict(district); sysGeographyPosition1.setStreet(street); sysGeographyPosition1.setStreetNum(streetNum); sysGeographyPosition1.setPoiName(poiName); sysGeographyPosition1.setCityCode(cityCode); sysGeographyPosition1.setAddress(province + city + district + street+streetNum+poiName+cityCode); sysGeographyPosition1.setCreateTime(new Date()); sysGeographyPosition1.setSource(sysDept.getManagementSource()); sysGeographyPosition1.setLeaderName(sysUser.getLeaderName()); sysGeographyPositionService.updateById(sysGeographyPosition1); SysGeographyPositionRecord sysGeographyPositionRecord = BeanUtil.copyProperties(sysGeographyPosition1, SysGeographyPositionRecord.class); sysGeographyPositionRecord.setId(null); sysGeographyPositionRecord.setUserId(sysGeographyPosition1.getId()); sysGeographyPositionRecordService.save(sysGeographyPositionRecord); } } }