diff --git a/zc-business/pom.xml b/zc-business/pom.xml index 6b870a73..6f82f0b0 100644 --- a/zc-business/pom.xml +++ b/zc-business/pom.xml @@ -71,6 +71,18 @@ poi-tl 1.9.1 + + + com.aliyun + aliyun-java-sdk-core + 4.6.3 + + + + com.aliyun + aliyun-java-sdk-dysmsapi + 2.2.1 + com.zc diff --git a/zc-business/src/main/java/com/zc/business/controller/DcWarningController.java b/zc-business/src/main/java/com/zc/business/controller/DcWarningController.java index c1c34e59..a102cdca 100644 --- a/zc-business/src/main/java/com/zc/business/controller/DcWarningController.java +++ b/zc-business/src/main/java/com/zc/business/controller/DcWarningController.java @@ -1,5 +1,6 @@ package com.zc.business.controller; +import com.ruoyi.common.utils.spring.SpringUtils; import com.ruoyi.common.utils.uuid.IdUtils; import com.zc.business.domain.DcWarningBatchConvert; import com.zc.business.service.IDcWarningService; @@ -10,12 +11,16 @@ import com.ruoyi.common.core.page.TableDataInfo; import com.ruoyi.common.enums.BusinessType; import com.ruoyi.common.utils.poi.ExcelUtil; import com.zc.business.domain.DcWarning; +import com.zc.business.service.IDeviceService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.scheduling.annotation.EnableScheduling; +import org.springframework.scheduling.annotation.Scheduled; import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletResponse; @@ -29,6 +34,7 @@ import java.util.List; * @date 2024-01-26 */ @Api(tags = "感知事件") +@Component @RestController @RequestMapping("/business/warning") public class DcWarningController extends BaseController @@ -36,6 +42,11 @@ public class DcWarningController extends BaseController @Autowired private IDcWarningService dcWarningService; + //定时任务,定时结束部分事件 + public void updateEndSection(){ + IDcWarningService ben = SpringUtils.getBean(IDcWarningService.class); + ben.updateEndSection(); + } /** * 查询预警信息列表 */ @@ -136,9 +147,14 @@ public class DcWarningController extends BaseController return dcWarningService.batchConvert(dcWarningBatchConvert); } - //定时任务,定时结束部分事件 - @PostMapping("/endSection") - public AjaxResult updateEndSection(){ - return toAjax(dcWarningService.updateEndSection()); + + //指挥调度 + @PostMapping("/commandAndDispatch") + public AjaxResult commandAndDispatch(@RequestBody DcWarning dcWarning){ + if (StringUtils.isBlank(dcWarning.getStakeMark())){ + return AjaxResult.error("参数错误"); + } + return (dcWarningService.commandAndDispatch(dcWarning)); } + } diff --git a/zc-business/src/main/java/com/zc/business/controller/MsmController.java b/zc-business/src/main/java/com/zc/business/controller/MsmController.java new file mode 100644 index 00000000..128bd52f --- /dev/null +++ b/zc-business/src/main/java/com/zc/business/controller/MsmController.java @@ -0,0 +1,38 @@ +package com.zc.business.controller; + +import com.ruoyi.common.core.controller.BaseController; +import com.ruoyi.common.core.domain.AjaxResult; +import com.zc.business.domain.DcWarning; +import com.zc.business.service.IMsmService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.HashMap; + +/** + * 王思祥 + * 推送业务 + */ +@RestController +@RequestMapping("/business/sms") +public class MsmController extends BaseController { + @Autowired + private IMsmService msmService; + + /** + * 短信推送业务,阿里云短信业务 + */ + @PostMapping("/push") + public Boolean alibabaCloudSms() + { + return msmService.send("19806114248"); + } + //调用微信推送 + @PostMapping("/wenXinPush") + public AjaxResult commandAndDispatch(){ + return AjaxResult.success(msmService.wenXinSend("wx9ee0e3babfd8d2af","de2ecb80b30d63135918ba3ae6ffb711")); + } +} \ No newline at end of file diff --git a/zc-business/src/main/java/com/zc/business/domain/DcEmployees.java b/zc-business/src/main/java/com/zc/business/domain/DcEmployees.java index ef714391..ef2635f2 100644 --- a/zc-business/src/main/java/com/zc/business/domain/DcEmployees.java +++ b/zc-business/src/main/java/com/zc/business/domain/DcEmployees.java @@ -50,7 +50,7 @@ public class DcEmployees extends BaseEntity private Long parentId; //人员类型1为路管人员2为应急人员 - @Excel(name = "人员类型 1-路管人员 2-应急人员") + @Excel(name = "人员类型 1-路管人员 2-交警人员") private Long employeesType; //类型参数 private String type; diff --git a/zc-business/src/main/java/com/zc/business/mapper/DcWarningMapper.java b/zc-business/src/main/java/com/zc/business/mapper/DcWarningMapper.java index 322a0dd7..174f24dd 100644 --- a/zc-business/src/main/java/com/zc/business/mapper/DcWarningMapper.java +++ b/zc-business/src/main/java/com/zc/business/mapper/DcWarningMapper.java @@ -5,9 +5,11 @@ import java.util.HashMap; import java.util.List; import cn.hutool.core.lang.hash.Hash; +import com.zc.business.domain.DcRoadSection; import com.zc.business.domain.DcWaringStrategy; import com.zc.business.domain.DcWarning; import org.apache.ibatis.annotations.Param; +import org.springframework.security.core.parameters.P; /** * 预警信息Mapper接口 @@ -90,4 +92,14 @@ public interface DcWarningMapper public Integer updateEndSection(@Param("dcWarningList") List dcWarningList); //查询当天全部可以结束的事件 public List selectEndSection(); + //依据桩号返回驻点id + public Long selectSectionId(@Param("stakeMark")String stakeMark,@Param("direction")String direction); + //依据桩号获取最近距离的驻点 + public List> selectRecentlySection(); + //查询某个机构的全部应急人员信息 + public List> selectOrganizationEmployees(@Param("organizationId")Long organizationId); + //查询某个机构当天的值班信息 + public List> selectShifts(@Param("station") Long station); + //查询某个机构的车辆信息 + public List> selectVehicles(@Param("organizationId")Long organizationId); } diff --git a/zc-business/src/main/java/com/zc/business/service/IDcWarningService.java b/zc-business/src/main/java/com/zc/business/service/IDcWarningService.java index 54c65999..33a68e4e 100644 --- a/zc-business/src/main/java/com/zc/business/service/IDcWarningService.java +++ b/zc-business/src/main/java/com/zc/business/service/IDcWarningService.java @@ -84,4 +84,6 @@ public interface IDcWarningService //定时结束部分事件 Integer updateEndSection(); + //指挥调度 + AjaxResult commandAndDispatch(DcWarning dcWarning); } diff --git a/zc-business/src/main/java/com/zc/business/service/IMsmService.java b/zc-business/src/main/java/com/zc/business/service/IMsmService.java new file mode 100644 index 00000000..5dc10cdf --- /dev/null +++ b/zc-business/src/main/java/com/zc/business/service/IMsmService.java @@ -0,0 +1,21 @@ +package com.zc.business.service; + +import com.alibaba.fastjson.JSONArray; +import com.ruoyi.common.core.domain.AjaxResult; + +public interface IMsmService { + /** + * 发送短信 + * + * @param phone 手机号 + * @return + */ + public boolean send(String phone); + /** + * 微信推送 + * + * @return + */ + JSONArray wenXinSend(String appId, String appSecret); + +} \ No newline at end of file diff --git a/zc-business/src/main/java/com/zc/business/service/impl/DcWarningServiceImpl.java b/zc-business/src/main/java/com/zc/business/service/impl/DcWarningServiceImpl.java index c2203f31..db0e0729 100644 --- a/zc-business/src/main/java/com/zc/business/service/impl/DcWarningServiceImpl.java +++ b/zc-business/src/main/java/com/zc/business/service/impl/DcWarningServiceImpl.java @@ -1,7 +1,6 @@ package com.zc.business.service.impl; - import com.ruoyi.common.core.domain.AjaxResult; import com.ruoyi.common.core.redis.RedisCache; import com.ruoyi.common.utils.DateUtils; @@ -16,19 +15,22 @@ import com.zc.business.mapper.DcEventMapper; import com.zc.business.mapper.DcWarningMapper; import com.zc.business.service.IDcEventService; import com.zc.business.service.IDcWarningService; +import com.zc.business.utils.StakeMarkUtils; import org.apache.commons.lang3.StringUtils; +import org.json.JSONArray; +import org.json.JSONObject; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.util.ArrayList; +import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.TimeUnit; -import org.json.JSONObject; -import org.json.JSONArray; +import java.util.stream.Collectors; /** * 预警信息Service业务层处理 * @@ -543,4 +545,34 @@ public class DcWarningServiceImpl implements IDcWarningService return dcWarningMapper.updateEndSection(dcWarnings); } + @Override + public AjaxResult commandAndDispatch(DcWarning dcWarning) { + //逻辑,调用全部的机构信息,计算传入的桩号与机构桩号的距离作为排序使用,使用机构的id调出车辆,人员,值班等信息 + List> mapList = dcWarningMapper.selectRecentlySection();//全部机构的桩号和名称 + ArrayList> hashMaps = new ArrayList<>(); + for (HashMap map:mapList){ + if (StringUtils.isBlank(map.get("stakeMark").toString())){ + continue; + } + String stakeMark = map.get("stakeMark").toString(); + StakeMarkUtils stakeMarkUtils = new StakeMarkUtils(); + Integer itselfStakeMark = stakeMarkUtils.stakeMarkToInt(stakeMark);//桩号本身的米数 + Integer afferentStakeMark = stakeMarkUtils.stakeMarkToInt(dcWarning.getStakeMark());//传入的桩号米数 + Integer difference =Math.abs(afferentStakeMark-itselfStakeMark); //计算距离绝对值 + map.put("difference",difference/1000.0);//米转公里加入到map + Long id = (Long) map.get("id");//机构的id,用户获取人员、值班、车辆等信息 + List> employeesMap = dcWarningMapper.selectOrganizationEmployees(id);//人员map + List> shiftsMap = dcWarningMapper.selectShifts(id);//值班map + List> vehiclesMap = dcWarningMapper.selectVehicles(id);//车辆map + map.put("employeesMap",employeesMap); + map.put("shiftsMap",shiftsMap); + map.put("vehiclesMap",vehiclesMap); + hashMaps.add(map); + } + List> sortedHashMaps = hashMaps.stream().sorted(Comparator.comparing(map -> { + return (Double) map.get("difference"); })).collect(Collectors.toList()); + return AjaxResult.success(sortedHashMaps); + } + + } diff --git a/zc-business/src/main/java/com/zc/business/service/impl/MsmServiceImpl.java b/zc-business/src/main/java/com/zc/business/service/impl/MsmServiceImpl.java new file mode 100644 index 00000000..fef5b2a4 --- /dev/null +++ b/zc-business/src/main/java/com/zc/business/service/impl/MsmServiceImpl.java @@ -0,0 +1,201 @@ +package com.zc.business.service.impl; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONArray; +import com.aliyuncs.DefaultAcsClient; +import com.aliyuncs.IAcsClient; +import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest; +import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse; +import com.aliyuncs.profile.DefaultProfile; +import com.ruoyi.common.core.domain.AjaxResult; +import com.ruoyi.common.core.redis.RedisCache; +import com.ruoyi.common.utils.spring.SpringUtils; +import com.zc.business.service.IMsmService; +import com.zc.business.utils.HttpUtil; +import org.apache.commons.lang3.StringUtils; +import org.json.JSONObject; +import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import java.io.InputStream; + +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Random; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; + +/** + * 短信业务实现类 + */ +@Service +public class MsmServiceImpl implements IMsmService { + private static final String WEIXINTUISONTOKEN = "weixintuisontoken";//redis策略缓存的key + + /** + * 发送短信 + * + * @param phone 手机号 + * @return + */ + public boolean send(String phone) { + //地域节点、阿里云的id、秘钥 + DefaultProfile profile = DefaultProfile.getProfile("cn-qingdao", + "LTAI5tENd3j1tP5t2fF4fxaX", "i86Yenj13XRd2aeZMtybpKxsqI1VRU"); + IAcsClient client = new DefaultAcsClient(profile); + SendSmsRequest request = new SendSmsRequest(); + request.setSysRegionId("cn-qingdao");//地域节点 + request.putQueryParameter("PhoneNumbers", phone); //手机号,要填绑定测试的手机号码 + request.putQueryParameter("SignName", "阿里云短信测试"); //阿里云签名名称 + request.putQueryParameter("TemplateCode", "SMS_154950909"); //阿里云模板code + String code = String.format("%04d", new Random().nextInt(10000));//验证码 + HashMap map = new HashMap<>(); + map.put("code", code); //参数加入到map + JSONObject json = new JSONObject(map); //map参数转为JSON传入到模板即可 + request.putQueryParameter("TemplateParam", json); + try { + SendSmsResponse response = client.getAcsResponse(request); + if (response != null && response.getCode() != null && response.getCode().equals("OK")) { + return true; + } + return false; + } catch (Exception e) { + e.printStackTrace(); + } + return false; + } + + //测试短信推送 +// public static void main(String[] args) { +// MsmServiceImpl msmService = new MsmServiceImpl(); +// boolean send = msmService.send("19806114248"); +// System.out.println(send); +// } + //微信推送 + @Override + public JSONArray wenXinSend(String appId, String appSecret) { + MsmServiceImpl msmService = new MsmServiceImpl(); + return msmService.sendTemp(appId, appSecret); + } + + //获取微信token + public static com.alibaba.fastjson.JSONObject getAccessToken(String appId, String appSecret) { + String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appId + "&secret=" + appSecret; + String accessToken = null; + com.alibaba.fastjson.JSONObject jsonObj = null; + try { + URL urlGet = new URL(url); + HttpURLConnection http = (HttpURLConnection) urlGet.openConnection(); + http.setRequestMethod("GET"); // 必须是get方式请求 + http.setDoInput(true); + http.connect(); + InputStream is = http.getInputStream(); + int size = is.available(); + byte[] jsonBytes = new byte[size]; + is.read(jsonBytes); + accessToken = new String(jsonBytes, "UTF-8"); + System.err.println(accessToken); + jsonObj = com.alibaba.fastjson.JSONObject.parseObject(accessToken); + is.close(); + } catch (Exception e) { + e.printStackTrace(); + } + return jsonObj; + } + + //执行微信推送 + public JSONArray sendTemp(String appId, String appSecret) { + JSONArray objects = new JSONArray(); + ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(100); + MsmServiceImpl msmService = new MsmServiceImpl(); + RedisCache redisCache = SpringUtils.getBean(RedisCache.class); + String accessToken = redisCache.getCacheObject(WEIXINTUISONTOKEN); + if (StringUtils.isBlank(accessToken)) { + accessToken = msmService.getAccessToken(appId, appSecret).getString("access_token");//获取到了access_token + String expiresIn = msmService.getAccessToken(appId, appSecret).getString("expires_in");//token有效秒数 + redisCache.setCacheObject(WEIXINTUISONTOKEN, accessToken, Integer.parseInt(expiresIn), TimeUnit.SECONDS);//把token存入到redis中,并设置过期时间(时间来自于微信接口返回) + } + JSONArray userListOpenid = msmService.getUserListOpenid(accessToken); + CountDownLatch latch = new CountDownLatch(userListOpenid.size()); + for (Object openid : userListOpenid) { + String token = accessToken; + executor.execute(() -> { + // 封装要发送的json + Map map = new HashMap(); + map.put("template_id", "D_yBiN7aDl2pw3bhSqU_Fbi0D8BHXL0qeBjhusWVO68");//模板消息id + map.put("touser", openid.toString());//这里的openid是全部关注公众号的openid + // 封装data + com.alibaba.fastjson.JSONObject data = new com.alibaba.fastjson.JSONObject(); + com.alibaba.fastjson.JSONObject name = new com.alibaba.fastjson.JSONObject(); + name.put("value", "做一个身体和心灵都勇敢的人,趁着身体未老,心灵还透明。晚安!"); + data.put("name", name); + com.alibaba.fastjson.JSONObject weather = new com.alibaba.fastjson.JSONObject(); + weather.put("value", "阴天 19°c"); + data.put("weather", weather); + com.alibaba.fastjson.JSONObject birthday = new com.alibaba.fastjson.JSONObject(); + birthday.put("value", "53 天"); + data.put("birthday", birthday); + map.put("data", data); + String r = HttpUtil.getJsonData(com.alibaba.fastjson.JSONObject.parseObject(JSON.toJSONString(map)), + "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + token); //发送模板消息 + System.out.println("-->" + r); + try { + JSONObject jsonObject = new JSONObject(r); + String errmsg = jsonObject.getString("errmsg"); + Object errcode = jsonObject.get("errcode"); + objects.add(errcode); + } catch (Exception e) { + e.printStackTrace(); + } finally { + latch.countDown(); + } + }); + } + try { + latch.await(); // 等待所有线程执行完毕 + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } finally { + executor.shutdown(); + } + return objects; + //map.put("url","https://www.vipkes.cn");//用户点击模板消息,要跳转的地址 + // 封装miniprogram 跳转小程序用,不跳不要填 +// Map mapA = new HashMap<>(); +// mapA.put("appid", ""); //小程序appid +// mapA.put("pagepath", ""); //小程序页面pagepath +// map.put("miniprogram", mapA); + } + + //获取微信公众号全部关注人的openid + public static JSONArray getUserListOpenid(String accessToken) { + String url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=" + accessToken; + ; + com.alibaba.fastjson.JSONObject jsonObj = null; + try { + URL urlGet = new URL(url); + HttpURLConnection http = (HttpURLConnection) urlGet.openConnection(); + http.setRequestMethod("GET"); // 必须是get方式请求 + http.setDoInput(true); + http.connect(); + InputStream is = http.getInputStream(); + int size = is.available(); + byte[] jsonBytes = new byte[size]; + is.read(jsonBytes); + String userOpenid = new String(jsonBytes, "UTF-8"); + jsonObj = com.alibaba.fastjson.JSONObject.parseObject(userOpenid); + is.close(); + } catch (Exception e) { + e.printStackTrace(); + } + com.alibaba.fastjson.JSONObject data = jsonObj.getJSONObject("data"); + return data.getJSONArray("openid"); + } +} \ No newline at end of file diff --git a/zc-business/src/main/java/com/zc/business/utils/HttpUtil.java b/zc-business/src/main/java/com/zc/business/utils/HttpUtil.java new file mode 100644 index 00000000..cb2d25bc --- /dev/null +++ b/zc-business/src/main/java/com/zc/business/utils/HttpUtil.java @@ -0,0 +1,83 @@ +package com.zc.business.utils; + + +import com.alibaba.fastjson.JSONObject; + +import java.io.BufferedReader; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.net.HttpURLConnection; +import java.net.URL; + +public class HttpUtil { + + public static String getJsonData(JSONObject jsonParam, String urls) { + StringBuffer sb = new StringBuffer(); + try { + // 创建url资源 + URL url = new URL(urls); + // 建立http连接 + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + // 设置允许输出 + conn.setDoOutput(true); + // 设置允许输入 + conn.setDoInput(true); + // 设置不用缓存 + conn.setUseCaches(false); + // 设置传递方式 + conn.setRequestMethod("POST"); + // 设置维持长连接 + conn.setRequestProperty("Connection", "Keep-Alive"); + // 设置文件字符集: + conn.setRequestProperty("Charset", "UTF-8"); + // 转换为字节数组 + byte[] data = (jsonParam.toString()).getBytes(); + // 设置文件长度 + conn.setRequestProperty("Content-Length", String.valueOf(data.length)); + // 设置文件类型: + conn.setRequestProperty("contentType", "application/json"); + // 开始连接请求 + conn.connect(); + //OutputStream out = new DataOutputStream(conn.getOutputStream()) ; + //防止消息乱码 + OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(), "utf-8"); + // 写入请求的字符串 + out.write(jsonParam.toString()); + out.flush(); + out.close(); + + System.out.println(conn.getResponseCode()); + + // 请求返回的状态 + if (HttpURLConnection.HTTP_OK == conn.getResponseCode()) { + System.out.println("连接成功"); + // 请求返回的数据 + InputStream in1 = conn.getInputStream(); + try { + String readLine = new String(); + BufferedReader responseReader = new BufferedReader(new InputStreamReader(in1, "UTF-8")); + while ((readLine = responseReader.readLine()) != null) { + sb.append(readLine).append("\n"); + } + responseReader.close(); + System.out.println(sb.toString()); + + } catch (Exception e1) { + e1.printStackTrace(); + } + } else { + System.out.println("error++"); + + } + + } catch (Exception e) { + + } + + return sb.toString(); + + } + + +} \ No newline at end of file diff --git a/zc-business/src/main/resources/mapper/business/DcEmployeesMapper.xml b/zc-business/src/main/resources/mapper/business/DcEmployeesMapper.xml index 3462b34c..5b64de76 100644 --- a/zc-business/src/main/resources/mapper/business/DcEmployeesMapper.xml +++ b/zc-business/src/main/resources/mapper/business/DcEmployeesMapper.xml @@ -24,7 +24,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" employees.create_time, employees.update_time, CASE WHEN employees.employees_type = 1 THEN '路管人员' - WHEN employees.employees_type = 2 THEN '应急人员' + WHEN employees.employees_type = 2 THEN '交警人员' END AS type from dc_employees as employees left join dc_organization as organization on organization.id=employees.organization_id diff --git a/zc-business/src/main/resources/mapper/business/DcWarningMapper.xml b/zc-business/src/main/resources/mapper/business/DcWarningMapper.xml index 7b3d8c07..750d9bdc 100644 --- a/zc-business/src/main/resources/mapper/business/DcWarningMapper.xml +++ b/zc-business/src/main/resources/mapper/business/DcWarningMapper.xml @@ -245,5 +245,23 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" + + + + +