浏览代码

feat(qhse-common): 添加阿里云短信服务和流动车辆处理功能

- 新增 AliyunSmsService 接口和实现类,用于发送短信
- 添加 MobileVehicleProcessingController、Dao、Do 和 Service
- 实现流动车辆处理和短信发送功能
- 更新 pom.xml,添加阿里云短信 SDK 依赖
lg 3 周之前
父节点
当前提交
b1b572ff2e

+ 6 - 0
qhse-common/pom.xml

@@ -72,6 +72,12 @@
             <artifactId>spring-cloud-starter-openfeign</artifactId>
             <version>2.1.0.RELEASE</version>
         </dependency>
+        <!-- 阿里云短信 sdk -->
+        <dependency>
+            <groupId>com.aliyun</groupId>
+            <artifactId>dysmsapi20170525</artifactId>
+            <version>4.1.2</version>
+        </dependency>
 <!--        <dependency>-->
 <!--            <groupId>com.aspose</groupId>-->
 <!--            <artifactId>aspose-cells</artifactId>-->

+ 7 - 0
qhse-common/src/main/java/com/rongwei/bscommon/sys/dao/MobileVehicleProcessingDao.java

@@ -0,0 +1,7 @@
+package com.rongwei.bscommon.sys.dao;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.rongwei.bsentity.domain.MobileVehicleProcessingDo;
+
+public interface MobileVehicleProcessingDao extends BaseMapper<MobileVehicleProcessingDo> {
+}

+ 17 - 0
qhse-common/src/main/java/com/rongwei/bscommon/sys/service/AliyunSmsService.java

@@ -0,0 +1,17 @@
+package com.rongwei.bscommon.sys.service;
+
+import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
+
+import java.util.Map;
+
+public interface AliyunSmsService {
+    /**
+     * 发送短信
+     *
+     * @param templateId       模板ID
+     * @param templateParamMap 模板参数
+     * @param phone            手机号
+     * @return 发送结果
+     */
+    SendSmsResponse sendSms(String templateId, Map<String, String> templateParamMap, String phone) throws Exception;
+}

+ 16 - 0
qhse-common/src/main/java/com/rongwei/bscommon/sys/service/MobileVehicleProcessingService.java

@@ -0,0 +1,16 @@
+package com.rongwei.bscommon.sys.service;
+
+import com.baomidou.mybatisplus.extension.service.IService;
+import com.rongwei.bsentity.domain.MobileVehicleProcessingDo;
+
+import java.util.List;
+
+public interface MobileVehicleProcessingService extends IService<MobileVehicleProcessingDo> {
+    /**
+     * 发送短信
+     *
+     * @param idList id列表
+     * @return 发送结果
+     */
+    boolean sendTextMessage(List<String> idList);
+}

+ 80 - 0
qhse-common/src/main/java/com/rongwei/bscommon/sys/service/impl/AliyunSmsServiceImpl.java

@@ -0,0 +1,80 @@
+package com.rongwei.bscommon.sys.service.impl;
+
+import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
+import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
+import com.aliyun.tea.TeaException;
+import com.aliyun.teautil.models.RuntimeOptions;
+import com.rongwei.bscommon.sys.service.AliyunSmsService;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Service;
+
+import java.util.Map;
+
+@Service
+@Slf4j
+public class AliyunSmsServiceImpl implements AliyunSmsService {
+
+    @Value("${aliyun-sms.access-key-id:#{null}}")
+    private String accessKeyId;
+    @Value("${aliyun-sms.access-key-secret:#{null}}")
+    private String accessKeySecret;
+    @Value("${aliyun-sms.endpoint:#{null}}")
+    private String endpoint;
+    @Value("${aliyun-sms.sign-name:#{null}}")
+    private String signName;
+
+    private com.aliyun.dysmsapi20170525.Client createClient() throws Exception {
+        // 工程代码建议使用更安全的无AK方式,凭据配置方式请参见:https://help.aliyun.com/document_detail/378657.html。
+        com.aliyun.credentials.Client credential = new com.aliyun.credentials.Client();
+        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
+                .setCredential(credential);
+        config.accessKeyId = accessKeyId;
+        config.accessKeySecret = accessKeySecret;
+        // Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
+        config.endpoint = endpoint;
+        return new com.aliyun.dysmsapi20170525.Client(config);
+    }
+
+    /**
+     * 发送短信
+     *
+     * @param templateId       模板ID
+     * @param templateParamMap 模板参数
+     * @param phone            手机号
+     * @return 发送结果
+     */
+    @Override
+    public SendSmsResponse sendSms(String templateId,
+                                   Map<String, String> templateParamMap, String phone) throws Exception {
+        String templateParam = com.aliyun.teautil.Common.toJSONString(templateParamMap);
+        com.aliyun.dysmsapi20170525.Client client = createClient();
+        SendSmsRequest sendSmsRequest = new SendSmsRequest();
+        sendSmsRequest.setSignName(signName);
+        sendSmsRequest.setTemplateCode(templateId);
+        sendSmsRequest.setTemplateParam(templateParam);
+        sendSmsRequest.setPhoneNumbers(phone);
+        RuntimeOptions runtime = new RuntimeOptions();
+        SendSmsResponse resp = null;
+        try {
+            resp = client.sendSmsWithOptions(sendSmsRequest, runtime);
+            log.info("发送短信结果:{}", com.aliyun.teautil.Common.toJSONString(resp));
+        } catch (TeaException error) {
+            // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
+            // 错误 message
+            log.error("发送短信结果:{}", error.getMessage());
+            // 诊断地址
+            log.error("诊断地址:{}", error.getData().get("Recommend"));
+            com.aliyun.teautil.Common.assertAsString(error.message);
+        } catch (Exception _error) {
+            TeaException error = new TeaException(_error.getMessage(), _error);
+            // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
+            // 错误 message
+            log.error("发送短信结果:{}", error.getMessage());
+            // 诊断地址
+            log.error("诊断地址:{}", error.getData().get("Recommend"));
+            com.aliyun.teautil.Common.assertAsString(error.message);
+        }
+        return resp;
+    }
+}

+ 65 - 0
qhse-common/src/main/java/com/rongwei/bscommon/sys/service/impl/MobileVehicleProcessingServiceImpl.java

@@ -0,0 +1,65 @@
+package com.rongwei.bscommon.sys.service.impl;
+
+import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.rongwei.bscommon.sys.dao.MobileVehicleProcessingDao;
+import com.rongwei.bscommon.sys.service.AliyunSmsService;
+import com.rongwei.bscommon.sys.service.MobileVehicleProcessingService;
+import com.rongwei.bsentity.domain.MobileVehicleProcessingDo;
+import com.rongwei.rwcommon.utils.DateUtils;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.*;
+import java.util.stream.Collectors;
+
+@Service
+@Slf4j
+public class MobileVehicleProcessingServiceImpl
+        extends ServiceImpl<MobileVehicleProcessingDao, MobileVehicleProcessingDo>
+        implements MobileVehicleProcessingService {
+    private static final String TEMPLATE_ID = "SMS_493170198";
+    private static final String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm";
+    @Autowired
+    public AliyunSmsService aliyunSmsService;
+
+    /**
+     * 发送短信
+     *
+     * @param idList id列表
+     * @return 发送结果
+     */
+    @Override
+    public boolean sendTextMessage(List<String> idList) {
+        List<MobileVehicleProcessingDo> list = lambdaQuery().in(MobileVehicleProcessingDo::getId, idList).list();
+        List<MobileVehicleProcessingDo> updateList = new ArrayList<>();
+
+        for (MobileVehicleProcessingDo item : list.stream()
+                .filter(item -> item.getPhone() != null).collect(Collectors.toList())) {
+            log.info("开始发送短信,手机号:{}", item.getPhone());
+            // 发送短信
+            try {
+                Map<String, String> templateParamMap = new HashMap<>();
+                templateParamMap.put("type", "车辆超速");
+                templateParamMap.put("desc",
+                        String.format("%s车于%s厂内行驶车速%dkm/h,已超速,限速30km/h,请减速慢行",
+                                item.getPlateNumber(),
+                                DateUtils.format(item.getProcessDate(), DATE_TIME_PATTERN),
+                                item.getSpeed()));
+                SendSmsResponse res = aliyunSmsService.sendSms(TEMPLATE_ID, templateParamMap, item.getPhone());
+                if (res.statusCode != 200) {
+                    log.error("发送短信失败,手机号:{}", item.getPhone());
+                    return false;
+                }
+            } catch (Exception e) {
+                throw new RuntimeException(e);
+            }
+            log.info("发送短信成功,手机号:{}", item.getPhone());
+            item.setProcessDate(new Date());
+            updateList.add(item);
+        }
+        updateBatchById(updateList);
+        return true;
+    }
+}

+ 115 - 0
qhse-entity/src/main/java/com/rongwei/bsentity/domain/MobileVehicleProcessingDo.java

@@ -0,0 +1,115 @@
+package com.rongwei.bsentity.domain;
+
+import com.baomidou.mybatisplus.annotation.TableField;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableName;
+import com.rongwei.rwcommon.base.BaseDo;
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+
+import java.util.Date;
+
+/**
+ * 流动车辆处理表
+ */
+@EqualsAndHashCode(callSuper = true)
+@Data
+@TableName(value = "qhse_mobile_vehicle_processing")
+public class MobileVehicleProcessingDo extends BaseDo {
+
+    /**
+     * 主键
+     */
+    @TableId(value = "ID")
+    private String id;
+
+    /**
+     * 处理时间
+     */
+    @TableField(value = "PROCESSDATE")
+    private Date processDate;
+
+    /**
+     * 车牌号
+     */
+    @TableField(value = "PLATENUMBER")
+    private String plateNumber;
+
+    /**
+     * 车速(码)
+     */
+    @TableField(value = "SPEED")
+    private Integer speed;
+
+    /**
+     * 姓名
+     */
+    @TableField(value = "NAME")
+    private String name;
+
+    /**
+     * 部门ID
+     */
+    @TableField(value = "DEPARTMENTID")
+    private String departmentId;
+
+    /**
+     * 部门
+     */
+    @TableField(value = "DEPARTMENT")
+    private String department;
+
+    /**
+     * 分包商ID
+     */
+    @TableField(value = "SUBCONTRACTORID")
+    private String subcontractorId;
+
+    /**
+     * 分包商
+     */
+    @TableField(value = "SUBCONTRACTOR")
+    private String subcontractor;
+
+    /**
+     * 照片
+     */
+    @TableField(value = "PHOTO")
+    private String photo;
+
+    /**
+     * 扣分
+     */
+    @TableField(value = "DEDUCTION")
+    private Integer deduction;
+
+    /**
+     * 是否确认
+     */
+    @TableField(value = "ISCONFIRMED")
+    private String isConfirmed = "0";
+
+    /**
+     * 电话
+     */
+    @TableField(value = "PHONE")
+    private String phone;
+
+    /**
+     * 通知车主时间
+     */
+    @TableField(value = "NOTIFICATIONTIME")
+    private Date notificationTime;
+
+    /**
+     * 扩展json格式配置
+     */
+    @TableField(value = "ROPTION")
+    private String roption;
+
+    /**
+     * 租户ID
+     */
+    @TableField(value = "TENANTID")
+    private String tenantId;
+}

+ 35 - 0
qhse-server/src/main/java/com/rongwei/controller/MobileVehicleProcessingController.java

@@ -0,0 +1,35 @@
+package com.rongwei.controller;
+
+import com.rongwei.bscommon.sys.service.MobileVehicleProcessingService;
+import com.rongwei.rwcommon.base.R;
+import lombok.extern.slf4j.Slf4j;
+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.List;
+
+/**
+ * 流动车辆处理
+ */
+@RestController
+@RequestMapping("/mobile/vehicle/processing")
+@Slf4j
+public class MobileVehicleProcessingController {
+    @Autowired
+    private MobileVehicleProcessingService mobileVehicleProcessingService;
+
+    /**
+     * 发送短信
+     *
+     * @param idList id列表
+     * @return 发送结果
+     */
+    @PostMapping("/send/text/message")
+    public R sendTextMessage(@RequestBody List<String> idList) {
+        log.info("流动车辆处理,开始发送短信");
+        return mobileVehicleProcessingService.sendTextMessage(idList) ? R.ok() : R.error();
+    }
+}