lin
2024-04-02 6db2b7de0799ca56c24af7e917c622e2fc951f72
保存短信发送记录
2 files modified
271 ■■■■■ changed files
src/main/java/org/springblade/modules/discuss/service/impl/UserTopicsServiceImpl.java 230 ●●●● patch | view | raw | blame | history
src/main/java/org/springblade/modules/sms/service/impl/SmsSendServiceImpl.java 41 ●●●●● patch | view | raw | blame | history
src/main/java/org/springblade/modules/discuss/service/impl/UserTopicsServiceImpl.java
@@ -107,6 +107,9 @@
    @Transactional(rollbackFor = Exception.class)
    public String batchSave(TopicsVO topics) throws Exception {
        if (StringUtils.isNotBlank(topics.getPhone())) {
            if (StringUtils.isBlank(topics.getCode())) {
                return "验证码不能为空!";
            }
            Object validateCode = redisTemplate.get(SMS_VALIDATE_PHONE + topics.getPhone());
            if (validateCode == null) {
                return "验证码已过期";
@@ -262,126 +265,125 @@
        return save(userTopics);
    }
/**
 * 处理Excel文件的生成,将用户主题数据写入Excel并输出到流中。
 *
 * @param out 输出流,用于保存生成的Excel文件。
 * @param userTopics 用户主题数据,包含提案和投票记录信息。
 */
@Override
public void handleExcel(OutputStream out, UserTopicsVO userTopics) {
    /**
     * 处理Excel文件的生成,将用户主题数据写入Excel并输出到流中。
     *
     * @param out        输出流,用于保存生成的Excel文件。
     * @param userTopics 用户主题数据,包含提案和投票记录信息。
     */
    @Override
    public void handleExcel(OutputStream out, UserTopicsVO userTopics) {
    // 初始化Excel写入器
    ExcelWriter excelWriter = EasyExcel.write(out)
        .build();
        // 初始化Excel写入器
        ExcelWriter excelWriter = EasyExcel.write(out)
            .build();
    // 设置内容自动换行的样式
    WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
    contentWriteCellStyle.setWrapped(true);
        // 设置内容自动换行的样式
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        contentWriteCellStyle.setWrapped(true);
    // 处理并写入提案数据
    List<holdExcel> proposal = getProposal(userTopics);
    ExcelWriterSheetBuilder userExcelSheet = new ExcelWriterSheetBuilder();
    userExcelSheet.registerWriteHandler(new HorizontalCellStyleStrategy(null,contentWriteCellStyle));
    userExcelSheet.sheetName("小区投票人员");
    userExcelSheet.sheetNo(0);
    userExcelSheet.head(holdExcel.class);
    excelWriter.write(proposal, userExcelSheet.build());
        // 处理并写入提案数据
        List<holdExcel> proposal = getProposal(userTopics);
        ExcelWriterSheetBuilder userExcelSheet = new ExcelWriterSheetBuilder();
        userExcelSheet.registerWriteHandler(new HorizontalCellStyleStrategy(null, contentWriteCellStyle));
        userExcelSheet.sheetName("小区投票人员");
        userExcelSheet.sheetNo(0);
        userExcelSheet.head(holdExcel.class);
        excelWriter.write(proposal, userExcelSheet.build());
    // 处理并写入投票记录数据
    List<UserTopicsExcel> publicopinion = getPublicopinion(userTopics);
    ExcelWriterSheetBuilder classExcelSheet = new ExcelWriterSheetBuilder();
    classExcelSheet.registerWriteHandler(new HorizontalCellStyleStrategy(null,contentWriteCellStyle));
    classExcelSheet.sheetName("投票记录");
    classExcelSheet.sheetNo(1);
    classExcelSheet.head(UserTopicsExcel.class);
    excelWriter.write(publicopinion, classExcelSheet.build());
    // 完成Excel写入并释放资源
    excelWriter.finish();
        // 处理并写入投票记录数据
        List<UserTopicsExcel> publicopinion = getPublicopinion(userTopics);
        ExcelWriterSheetBuilder classExcelSheet = new ExcelWriterSheetBuilder();
        classExcelSheet.registerWriteHandler(new HorizontalCellStyleStrategy(null, contentWriteCellStyle));
        classExcelSheet.sheetName("投票记录");
        classExcelSheet.sheetNo(1);
        classExcelSheet.head(UserTopicsExcel.class);
        excelWriter.write(publicopinion, classExcelSheet.build());
        // 完成Excel写入并释放资源
        excelWriter.finish();
}
    }
    /**
     * 获取首页的房屋记录
     *
     * @param userTopics 包含用户话题信息的对象,其中可能含有区域ID等关键信息
     * @return 返回一个包含房屋记录信息的列表
     */
    private List<holdExcel> getProposal(UserTopicsVO userTopics) {
        List<holdExcel> resultList = new ArrayList<>();
        try {
            // 检查区域ID字符串是否有效
            JSONArray objects1 = validateAndParseDistrictId(userTopics.getDistrictId());
            // 获取区域服务和话题服务的实例
            IDistrictService districtService = SpringUtils.getBean(IDistrictService.class);
            ITopicsService topicsService = SpringUtils.getBean(ITopicsService.class);
            // 处理区域ID并查询对应的区域实体列表
            List<String> aoiCodeList = processDistrictIds(districtService, objects1);
            // 设置AOI代码列表到用户话题对象中
            userTopics.setAoiCodeList(aoiCodeList);
            // 查询并处理房屋列表信息
            List<holdExcel> householdList = processHouseholdList(topicsService, userTopics);
            resultList.addAll(householdList);
        } catch (Exception e) {
            // 适当的异常处理逻辑
            // 例如:记录日志、返回错误信息等
            e.printStackTrace();
        }
        return resultList;
    }
    /**
     * 获取首页的房屋记录
     *
     * @param userTopics 包含用户话题信息的对象,其中可能含有区域ID等关键信息
     * @return 返回一个包含房屋记录信息的列表
     */
    private List<holdExcel> getProposal(UserTopicsVO userTopics) {
        List<holdExcel> resultList = new ArrayList<>();
        try {
            // 检查区域ID字符串是否有效
            JSONArray objects1 = validateAndParseDistrictId(userTopics.getDistrictId());
            // 获取区域服务和话题服务的实例
            IDistrictService districtService = SpringUtils.getBean(IDistrictService.class);
            ITopicsService topicsService = SpringUtils.getBean(ITopicsService.class);
            // 处理区域ID并查询对应的区域实体列表
            List<String> aoiCodeList = processDistrictIds(districtService, objects1);
            // 设置AOI代码列表到用户话题对象中
            userTopics.setAoiCodeList(aoiCodeList);
            // 查询并处理房屋列表信息
            List<holdExcel> householdList = processHouseholdList(topicsService, userTopics);
            resultList.addAll(householdList);
        } catch (Exception e) {
            // 适当的异常处理逻辑
            // 例如:记录日志、返回错误信息等
            e.printStackTrace();
        }
        return resultList;
    }
    /**
     * 验证并解析区域ID字符串。
     *
     * @param districtIdStr 区域ID的字符串表示。不能为空或空白字符串。
     * @return 返回一个JSONArray对象,包含解析后的区域ID。
     * @throws IllegalArgumentException 如果区域ID字符串为空或空白字符串,抛出此异常。
     */
    private JSONArray validateAndParseDistrictId(String districtIdStr) throws IllegalArgumentException {
        // 验证区域ID字符串是否为空
        if (StringUtils.isBlank(districtIdStr)) {
            throw new IllegalArgumentException("区域ID不能为空");
        }
        // 解析区域ID字符串为JSONArray对象
        return JSON.parseArray(districtIdStr);
    }
    /**
     * 验证并解析区域ID字符串。
     *
     * @param districtIdStr 区域ID的字符串表示。不能为空或空白字符串。
     * @return 返回一个JSONArray对象,包含解析后的区域ID。
     * @throws IllegalArgumentException 如果区域ID字符串为空或空白字符串,抛出此异常。
     */
    private JSONArray validateAndParseDistrictId(String districtIdStr) throws IllegalArgumentException {
        // 验证区域ID字符串是否为空
        if (StringUtils.isBlank(districtIdStr)) {
            throw new IllegalArgumentException("区域ID不能为空");
        }
        // 解析区域ID字符串为JSONArray对象
        return JSON.parseArray(districtIdStr);
    }
    private List<String> processDistrictIds(IDistrictService districtService, JSONArray districtIds) {
        List<String> collect1 = districtIds.stream().map(item -> (String) item).collect(Collectors.toList());
        List<DistrictEntity> list3 = districtService.list(Wrappers.<DistrictEntity>lambdaQuery().in(DistrictEntity::getId, collect1));
        return list3.stream().map(item -> item.getAoiCode()).collect(Collectors.toList());
    }
    private List<String> processDistrictIds(IDistrictService districtService, JSONArray districtIds) {
        List<String> collect1 = districtIds.stream().map(item -> (String) item).collect(Collectors.toList());
        List<DistrictEntity> list3 = districtService.list(Wrappers.<DistrictEntity>lambdaQuery().in(DistrictEntity::getId, collect1));
        return list3.stream().map(item -> item.getAoiCode()).collect(Collectors.toList());
    }
    private List<holdExcel> processHouseholdList(ITopicsService topicsService, UserTopicsVO userTopics) {
        List<holdExcel> householdList = baseMapper.getHouseholdList(userTopics);
        householdList.forEach(item -> processItem(topicsService, item));
        return householdList;
    }
    private List<holdExcel> processHouseholdList(ITopicsService topicsService, UserTopicsVO userTopics) {
        List<holdExcel> householdList = baseMapper.getHouseholdList(userTopics);
        householdList.forEach(item -> processItem(topicsService, item));
        return householdList;
    }
    private void processItem(ITopicsService topicsService, holdExcel item) {
        if (StringUtils.isBlank(item.getOptionContent())) {
            item.setVoteFlag("否");
        } else {
            List<Long> longs = Func.toLongList(item.getOptionContent());
            for (Long aLong : longs) {
                TopicsEntity topics = topicsService.getById(aLong);
                setManagementRegulationsOrRulesOfProcedure(item, topics);
            }
            item.setVoteFlag("是");
        }
    }
    private void processItem(ITopicsService topicsService, holdExcel item) {
        if (StringUtils.isBlank(item.getOptionContent())) {
            item.setVoteFlag("否");
        } else {
            List<Long> longs = Func.toLongList(item.getOptionContent());
            for (Long aLong : longs) {
                TopicsEntity topics = topicsService.getById(aLong);
                setManagementRegulationsOrRulesOfProcedure(item, topics);
            }
            item.setVoteFlag("是");
        }
    }
    private void setManagementRegulationsOrRulesOfProcedure(holdExcel item, TopicsEntity topics) {
        if (StringUtils.isNotBlank(topics.getDiscussContent())) {
            if (topics.getDiscussContent().equals("管理规约")) {
                item.setManagementRegulations(topics.getOptionContent());
            } else if (topics.getDiscussContent().equals("议事规则")) {
                item.setRulesOfProcedure(topics.getOptionContent());
            }
        }
    }
    private void setManagementRegulationsOrRulesOfProcedure(holdExcel item, TopicsEntity topics) {
        if (StringUtils.isNotBlank(topics.getDiscussContent())) {
            if (topics.getDiscussContent().equals("管理规约")) {
                item.setManagementRegulations(topics.getOptionContent());
            } else if (topics.getDiscussContent().equals("议事规则")) {
                item.setRulesOfProcedure(topics.getOptionContent());
            }
        }
    }
    /**
@@ -391,11 +393,11 @@
     * @return 返回用户话题的Excel格式列表,包含所有符合条件的意见信息
     */
    private List<UserTopicsExcel> getPublicopinion(UserTopicsVO userTopics) {
        // 初始化返回列表
        List<UserTopicsExcel> list = new ArrayList<>();
        // 通过基础mapper查询结果,并将结果添加到列表中
        List<UserTopicsExcel> userTopicsExcels = baseMapper.getresultTwo(userTopics);
        list.addAll(userTopicsExcels);
        return list;
        // 初始化返回列表
        List<UserTopicsExcel> list = new ArrayList<>();
        // 通过基础mapper查询结果,并将结果添加到列表中
        List<UserTopicsExcel> userTopicsExcels = baseMapper.getresultTwo(userTopics);
        list.addAll(userTopicsExcels);
        return list;
    }
}
src/main/java/org/springblade/modules/sms/service/impl/SmsSendServiceImpl.java
@@ -24,15 +24,19 @@
import org.slf4j.LoggerFactory;
import org.springblade.common.config.SmsConfig;
import org.springblade.common.utils.HttpClientUtils;
import org.springblade.common.utils.SpringUtils;
import org.springblade.common.utils.UtilRandom;
import org.springblade.common.utils.sms.AlipaySignature;
import org.springblade.core.redis.cache.BladeRedis;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.modules.resource.entity.Sms;
import org.springblade.modules.resource.service.ISmsService;
import org.springblade.modules.sms.entity.SmsTemplateEntity;
import org.springblade.modules.sms.mapper.SmsTemplateMapper;
import org.springblade.modules.sms.service.ISmsSendService;
import org.springblade.modules.sms.service.ISmsTemplateService;
import org.springblade.modules.smsRecord.entity.SmsRecordEntity;
import org.springblade.modules.smsRecord.service.ISmsRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@@ -69,6 +73,10 @@
    @Override
    public Boolean smsSend(String phone) {
        if (phone == null || phone.length() != 11) {
            logger.error("手机号格式错误");
            return false;
        }
        Sms serviceOne = iSmsService.getOne(Wrappers.<Sms>lambdaQuery().eq(Sms::getStatus, 2));
        if (serviceOne == null) {
@@ -118,10 +126,12 @@
        params.put("type", type);
        //创建人主键
        params.put("sop_create_by", serviceOne.getSmsCode());
        String s = null;
        String result = null;
        try {
            s = testGet(params, "message.sop.differentMes", serviceOne);
            JSONObject jsonObject = JSON.parseObject(s);
            //短信发送记录
            saveSmsRecord(phone, serviceOne, code, content);
            result = testGet(params, "message.sop.differentMes", serviceOne);
            JSONObject jsonObject = JSON.parseObject(result);
            JSONObject messageSopEqualsMesResponse = (JSONObject) jsonObject.get(MESSAGE_SOP_EQUALS_MES_RESPONSE_CODE);
            if (messageSopEqualsMesResponse.get("code").equals(200)) {
                // 将验证码存入redis
@@ -136,6 +146,31 @@
        return false;
    }
    /**
     * 保存短信发送记录
     *
     * @param phone 接收短信的手机号码
     * @param serviceOne 发送短信所使用的服务
     * @param code 短信中的验证码
     * @param content 短信内容,其中可能包含占位符"#P_1#"用于被验证码替换
     */
    private static void saveSmsRecord(String phone, Sms serviceOne, Integer code, String content) {
        // 通过Spring工具类获取短信记录服务的实例
        ISmsRecordService smsRecordService = SpringUtils.getBean(ISmsRecordService.class);
        // 创建一个新的短信记录实体
        SmsRecordEntity smsRecordEntity = new SmsRecordEntity();
        // 设置短信内容,将占位符"#P_1#"替换为验证码
        smsRecordEntity.setContent(content.replace("#P_1#", code.toString()));
        // 设置手机号码
        smsRecordEntity.setPhone(phone);
        // 设置短信模板ID
        smsRecordEntity.setTemplateId(Long.valueOf(serviceOne.getTemplateId()));
        // 设置创建用户ID,来自认证工具类
        smsRecordEntity.setCreateUser(AuthUtil.getUserId());
        // 保存短信记录实体
        smsRecordService.save(smsRecordEntity);
    }
    public String testGet(Object bizContent, String method, Sms serviceOne) throws Exception {
        // 公共请求参数