zhangnaisong
2024-02-21 ca8c7b4e9c4762acb2fc2c793d79ae58e05dcf36
ard-work/src/main/java/com/ruoyi/app/application/service/impl/ArdAppApplicationServiceImpl.java
@@ -1,8 +1,27 @@
package com.ruoyi.app.application.service.impl;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
import com.ruoyi.app.position.appcontainer.AppContainer;
import com.ruoyi.app.position.domain.ArdAppPosition;
import com.ruoyi.app.position.mapper.ArdAppPositionMapper;
import com.ruoyi.common.core.domain.entity.SysConfig;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.sy.domain.ArdSyUser;
import com.ruoyi.sy.mapper.ArdSyCarMapper;
import com.ruoyi.sy.mapper.ArdSyUserMapper;
import com.ruoyi.system.mapper.SysConfigMapper;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.mapper.SysRoleMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.utils.forest.SYClient;
import com.ruoyi.utils.gis.GisUtil;
import com.ruoyi.utils.websocket.util.WebSocketUtils;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@@ -14,6 +33,9 @@
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.websocket.Session;
import static com.ruoyi.utils.websocket.util.WebSocketUtils.ONLINE_USER_SESSIONS;
/**
 * 单兵端申请查看指挥端位置或三一车辆位置Service业务层处理
@@ -25,6 +47,30 @@
public class ArdAppApplicationServiceImpl implements IArdAppApplicationService {
    @Resource
    private ArdAppApplicationMapper ardAppApplicationMapper;
    @Resource
    private SysRoleMapper sysRoleMapper;
    @Resource
    private SysUserMapper userMapper;
    @Resource
    private SysDeptMapper sysDeptMapper;
    @Resource
    private ArdAppPositionMapper ardAppPositionMapper;
    @Resource
    private ArdSyUserMapper ardSyUserMapper;
    @Resource
    private SysConfigMapper sysConfigMapper;
    @Resource
    private ArdSyCarMapper ardSyCarMapper;
    @Resource
    private SYClient sYClient;
    private Map<String,Thread> threadMap = new HashMap();
@@ -182,6 +228,23 @@
                resultSingle = ardAppApplicationMapper.insertArdAppApplication(ardAppApplication);
                result = result + resultSingle;
            }
            //加入给pc端websocket转发
            List<SysRole> sysRoleList = sysRoleMapper.selectRolePermissionByUserId(commanderId);
            for(SysRole sysRole : sysRoleList){
                if(!sysRole.getRoleKey().equals("appLeader")){
                    Map<String,Object> resultMap = ardAppApplicationMapper.selectSYCarApplicationByPCIdAndSoilderId(commanderId,usersId);
                    Map<String,Map<String,Object>> msg = new HashMap();
                    msg.put("60000",resultMap);
                    for(String id : ONLINE_USER_SESSIONS.keySet()){
                        if(id.startsWith(commanderId)){
                            Session session = ONLINE_USER_SESSIONS.get(id);
                            WebSocketUtils.sendMessage(session,msg);
                            break;
                        }
                    }
                    break;//一个pc端用户可能挂接多个角色,每个pc端只转发一次
                }
            }
        }
        return result;
    }
@@ -264,29 +327,32 @@
        List<Map<String,Object>> ardAppApplicationList = ardAppApplicationMapper.selectArdAppApplicationByCommanderIdTrue(commanderId);
        Date date = new Date();
        //查看指挥端位置申请
        /*List<Map<String, Object>> resultCommanderPosition = new ArrayList();
        List<Map<String, Object>> resultCommanderPosition = new ArrayList();
        //查看车辆位置申请
        List<Map<String, Object>> resultSYCarsPosition = new ArrayList();
        for(Map<String, Object> map : ardAppApplicationList){
            Long interval = date.getTime() - Long.parseLong((String)map.get("begin"));
            interval = 24 * 60 * 60 * 1000 - interval;
            Long hour = interval / (60 * 60 * 1000);
            Long min = (interval - hour * 60 * 60 * 1000) / (60 * 1000);
            map.put("time",hour + "时" + min + "分");
            Long second = (interval - hour * 60 * 60 * 1000 - min * 60 * 1000) / 1000;
            map.put("time",hour + "时" + min + "分" + second + "秒");
            if(((String)map.get("applicationType")).equals("Commander")){
                resultCommanderPosition.add(map);
            }
        }*/
        //查看车辆位置申请
        /*List<Map<String, Object>> resultSYCarsPosition = new ArrayList();
        for(Map<String, Object> map : ardAppApplicationList){
            Long interval = date.getTime() - Long.parseLong((String)map.get("begin"));
            interval = 24 * 60 * 60 * 1000 - interval;
            Long hour = interval / (60 * 60 * 1000);
            Long min = (interval - hour * 60 * 60 * 1000) / (60 * 1000);
            map.put("time",hour + "时" + min + "分");
            if(((String)map.get("applicationType")).equals("SYCar")){
            }else if(((String)map.get("applicationType")).equals("SYCar")){
                resultSYCarsPosition.add(map);
            }
        }*/
        }
        result.put("commander",resultCommanderPosition);
        result.put("SYCars",resultSYCarsPosition);
        return result;
    }
    @Override
    public Map<String, List<Map<String, Object>>> getSendPositionTrue(String soilderId) {
        Map<String, List<Map<String, Object>>> result = new HashMap();
        List<Map<String,Object>> ardAppApplicationList = ardAppApplicationMapper.selectArdAppApplicationBySoilderIdTrue(soilderId);
        Date date = new Date();
        //查看指挥端位置申请
        List<Map<String, Object>> resultCommanderPosition = new ArrayList();
        //查看车辆位置申请
@@ -296,7 +362,8 @@
            interval = 24 * 60 * 60 * 1000 - interval;
            Long hour = interval / (60 * 60 * 1000);
            Long min = (interval - hour * 60 * 60 * 1000) / (60 * 1000);
            map.put("time",hour + "时" + min + "分");
            Long second = (interval - hour * 60 * 60 * 1000 - min * 60 * 1000) / 1000;
            map.put("time",hour + "时" + min + "分" + second + "秒");
            if(((String)map.get("applicationType")).equals("Commander")){
                resultCommanderPosition.add(map);
            }else if(((String)map.get("applicationType")).equals("SYCar")){
@@ -355,4 +422,162 @@
        int result = ardAppApplicationMapper.updateSoilderCheckByCommanderIdAndSoilderIdAndType(soilderId,commanderId,applicationType);
        return result;
    }
    @Override
    public List<Map<String, Object>> getCheckPositionNo(String commanderId) {///
        List<Map<String,Object>> ardAppApplicationList = ardAppApplicationMapper.selectArdAppApplicationByCommanderIdNoCheck(commanderId);
        Date date = new Date();
        for(Map<String, Object> map : ardAppApplicationList){
            if(((String)map.get("begin")).equals("")){
                map.put("time","");
            }else{
                Long interval = date.getTime() - Long.parseLong((String)map.get("begin"));
                interval = 24 * 60 * 60 * 1000 - interval;
                Long hour = interval / (60 * 60 * 1000);
                Long min = (interval - hour * 60 * 60 * 1000) / (60 * 1000);
                Long second = (interval - hour * 60 * 60 * 1000 - min * 60 * 1000) / 1000;
                map.put("time",hour + "时" + min + "分" + second + "秒");
            }
        }
        return ardAppApplicationList;
    }
    @Override
    public Map<String,Map<String,List<Map<String,Object>>>> getSoilderAndCarPositionByCommanderId(String commanderId,Map<String, Double> para) {
        Map<String,Map<String,List<Map<String,Object>>>> mapResault = new HashMap();
        SysUser sysUser = userMapper.selectUserById(commanderId);//获取系统用户
        List<Long> deptIdList = new ArrayList();
        deptIdList.add(sysUser.getDeptId());
        deptIdList = getOwnAndChildrenDeptIdList(deptIdList,new ArrayList());//递归查询下属部门主键
        List<SysUser> sysUsersResult = userMapper.getOwnAndChildrenSoilderListAll(deptIdList);
        List<SysUser> sysUsersResultOnline = sysUsersResult.stream().filter(sysUserTest -> sysUserTest.getAppOnlineState().equals("1")).collect(Collectors.toList());//在线用户
        List<SysUser> sysUsersResultOffline = sysUsersResult.stream().filter(sysUserTest -> !sysUserTest.getAppOnlineState().equals("1")).collect(Collectors.toList());//离线用户
        List<Map<String,Object>> sysUsersOnlineList = new ArrayList();
        List<Map<String,Object>> sysUsersOfflineList = new ArrayList();
        for(SysUser sysUserIn : sysUsersResultOnline){
            Map<String,Object> map = new HashMap();
            map.put("userId",sysUserIn.getUserId());
            map.put("deptId",sysUserIn.getDeptId());
            map.put("userName",sysUserIn.getUserName());
            map.put("nickName",sysUserIn.getNickName());
            try{
                map.put("longitude", AppContainer.getArdAppPositionMap().get(sysUserIn.getUserId()).getLongitude());
                map.put("latitude",AppContainer.getArdAppPositionMap().get(sysUserIn.getUserId()).getLatitude());
                map.put("altitude",AppContainer.getArdAppPositionMap().get(sysUserIn.getUserId()).getAltitude());
            }catch(Exception e){
                ArdAppPosition ardAppPosition = ardAppPositionMapper.getLastAPPPositionByUserId(sysUserIn.getUserId());
                map.put("longitude",ardAppPosition.getLongitude());
                map.put("latitude",ardAppPosition.getLatitude());
                map.put("altitude",ardAppPosition.getAltitude());
            }
            sysUsersOnlineList.add(map);
        }
        List<Map<String,Object>> sysUsersOnlineNearList = new ArrayList();//在200米内
        for(Map<String,Object> mapOn : sysUsersOnlineList){
            Double longitude = (Double) mapOn.get("longitude");
            Double latitude = (Double) mapOn.get("latitude");
            double distance = GisUtil.getDistance(para.get("longitude"),para.get("latitude"),longitude,latitude);
            if(distance <= 200){
                sysUsersOnlineNearList.add(mapOn);
            }
        }
        Map<String,List<Map<String,Object>>> appMap = new HashMap();
        appMap.put("onlineApp",sysUsersOnlineNearList);
        for(SysUser sysUserIn : sysUsersResultOffline){
            Map<String,Object> map = new HashMap();
            map.put("userId",sysUserIn.getUserId());
            map.put("deptId",sysUserIn.getDeptId());
            map.put("userName",sysUserIn.getUserName());
            map.put("nickName",sysUserIn.getNickName());
            sysUsersOfflineList.add(map);
        }
        appMap.put("offlineApp",sysUsersOfflineList);
        mapResault.put("app",appMap);
        ArdSyUser ardSyUserPara = new ArdSyUser();
        ardSyUserPara.setSysUserId(commanderId);
        List<ArdSyUser> ardSyUserList = ardSyUserMapper.selectArdSyUserList(ardSyUserPara);//查询挂接的三一账号
        String syURL = "";
        List<SysConfig> sysConfigResult = sysConfigMapper.selectByType("syCarPT");
        if(sysConfigResult.size() > 0){
            syURL = sysConfigResult.get(0).getConfigValue();
        }
        if(ardSyUserList.size() == 0 || syURL.equals("")){
            List<Map<String,Object>> onlineCar = new ArrayList();
            List<Map<String,Object>> offlineCar = new ArrayList();
            Map<String,List<Map<String,Object>>> carMap = new HashMap();
            carMap.put("onlineCar",onlineCar);
            carMap.put("offlineCar",offlineCar);
            mapResault.put("car",carMap);
            return mapResault;
        }
        List<String> carIdList = ardSyCarMapper.selectArdSyCarByDeptIds(deptIdList);
        String carIds = "";
        for(String carId : carIdList){
            carIds = carIds + carId + ",";
        }
        carIds = carIds.substring(0,carIds.length() - 1);
        String passwordMd5 = DigestUtils.md5Hex(ardSyUserList.get(0).getPassword());
        Map<String,Object> syLoginResult = sYClient.logIn(syURL, passwordMd5, ardSyUserList.get(0).getUserId());
        String sessionId = (String) syLoginResult.get("sessionId");
        Map<String,Object> carNearPositionResult = sYClient.getCarNearPositionByCarId(syURL, carIds, ardSyUserList.get(0).getUserId(),sessionId);
        if(((String)carNearPositionResult.get("rspCode")).equals("1")){
            List<Map<String,Object>> SYCarPositionList = (List<Map<String, Object>>) carNearPositionResult.get("list");
            //筛选在线
            List<Map<String,Object>> onlineSYCarPositionList = SYCarPositionList.stream().filter(map -> ((String)map.get("stateCn")).startsWith("在线")).collect(Collectors.toList());
            //筛选200米范围内
            List<Map<String,Object>> onlineSYCarPositionNearList = new ArrayList();//在200米内
            for(Map<String,Object> mapOn : onlineSYCarPositionList){
                Double longitude = new BigDecimal((String) mapOn.get("lng")).doubleValue();
                Double latitude = new BigDecimal((String) mapOn.get("lat")).doubleValue();
                double distance = GisUtil.getDistance(para.get("longitude"),para.get("latitude"),longitude,latitude);
                if(distance <= 200){
                    mapOn.put("longitude",longitude);
                    mapOn.put("latitude",latitude);
                    onlineSYCarPositionNearList.add(mapOn);
                }
            }
            //for(Map<String,Object> sycar : onlineSYCarPositionList){
            for(Map<String,Object> sycar : onlineSYCarPositionNearList){
                Map<String,Object> map = ardSyCarMapper.getArdSyCarAndDeptByCarId((String)sycar.get("carId"));
                sycar.putAll(map);
            }
            //筛选离线
            List<Map<String,Object>> offlineSYCarPositionList = SYCarPositionList.stream().filter(map -> ((String)map.get("stateCn")).startsWith("离线")).collect(Collectors.toList());
            for(Map<String,Object> sycar : offlineSYCarPositionList){
                Map<String,Object> map = ardSyCarMapper.getArdSyCarAndDeptByCarId((String)sycar.get("carId"));
                sycar.putAll(map);
            }
            Map<String,List<Map<String,Object>>> carMap = new HashMap();
            //carMap.put("onlineCar",onlineSYCarPositionList);
            carMap.put("onlineCar",onlineSYCarPositionNearList);
            carMap.put("offlineCar",offlineSYCarPositionList);
            mapResault.put("car",carMap);
        }else{
            List<Map<String,Object>> onlineCar = new ArrayList();
            List<Map<String,Object>> offlineCar = new ArrayList();
            Map<String,List<Map<String,Object>>> carMap = new HashMap();
            carMap.put("onlineCar",onlineCar);
            carMap.put("offlineCar",offlineCar);
            mapResault.put("car",carMap);
            return mapResault;
        }
        return mapResault;
    }
    public List<Long> getOwnAndChildrenDeptIdList(List<Long> deptIdList,List<Long> deptIdListr){
        deptIdListr.addAll(deptIdList);
        List<Long> result = sysDeptMapper.getChildrenDeptIdList(deptIdList);
        if(result.size() != 0){
            deptIdListr.addAll(result);
            result = getOwnAndChildrenDeptIdList(result,deptIdListr);
        }
        deptIdListr = deptIdListr.stream().distinct().collect(Collectors.toList());
        return deptIdListr;
    }
}