<p>//2.批量修改具体规则信息(同时生成对应语音包)
@PostMapping(&quot;/updateSpecificRegulars&quot;)
public Result<Object> updateSpecificRegulars(@RequestBody Map&lt;String, Object&gt; params, HttpServletRequest request) {</p>
<pre><code>//权限验证
String token = (String) request.getAttribute(&quot;claims_admin&quot;);
if (token == null || &quot;&quot;.equals(token)) {
    throw new RuntimeException(&quot;权限不足!&quot;);
}

log.info(&quot;updateSpecificRegulars的请求报文:&quot; + params);

JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(params));
JSONArray jsonArray = jsonObject.getJSONArray(&quot;specificRegular&quot;);
List&lt;SpecificRegular&gt; specificRegulars = JSONObject.parseArray(jsonArray.toJSONString(), SpecificRegular.class);//集合(前端传递的要修改的对象集合)
List&lt;SpecificRegular&gt; specificRegularList = new ArrayList&lt;&gt;(); //存放所有修改过的对象

String phone = (String) params.get(&quot;phone&quot;);

String userCode = (String) params.get(&quot;code&quot;);

String specificRegularUpdate_uuid = redisTemplate.opsForValue().get(&quot;specificRegularUpdate_uuid&quot; + phone);
String myCode = redisTemplate.opsForValue().get(&quot;sRegularUpdate_&quot; + specificRegularUpdate_uuid + phone);

if (!userCode.equals(myCode)) {
    log.error(&quot;验证码错误!&quot;);
    return new Result&lt;&gt;(ResultCode.CODE_FALSE);
}

log.info(&quot;验证码正确!&quot;);

//判断集合不为空
if (!CollectionUtils.isEmpty(specificRegulars)) {

    //遍历前端发来的要修改信息的对象集合
    for (SpecificRegular specificRegular : specificRegulars) {

        //通过前端传递的具体规则id在数据库找到对应的具体规则
        SpecificRegular specificRegularMy = specificRegularService.queryOneSpecificRegularByID(specificRegular.getSpecificRegularId());

        if (specificRegularMy != null) {

            //语音播报文字不为空,扣分情况不为0
            if (StringUtils.hasText(specificRegular.getVoiceBroadcastText()) &amp;&amp; specificRegular.getDemeritState() != 0) {
                log.info(&quot;语音播报文字不为空,扣分情况不为0&quot;);

                //判断前端传来的参数与数据库里的参数是否相等
                if (!specificRegular.getVoiceBroadcastText().equals(specificRegularMy.getVoiceBroadcastText()) || !specificRegular.getDemeritState().equals(specificRegularMy.getDemeritState())) {
                    log.info(&quot;前端传来的参数与数据库里的参数不相等!&quot;);

                    //生成语音包
                    String audioText = specificRegular.getVoiceBroadcastText() + ',扣' + specificRegular.getDemeritState() + '分';
                    //拿到当前项目类型(语音包名称需要)
                    Project project = projectService.queryOneProjectByID(specificRegularMy.getProjectId());
                    //名称(2(科目)-1(倒车入库的编号)-10(所有项目扣分参考编号))
                    String audioFileFirstName = project.getProjectTypeId().toString() + '-' + specificRegularMy.getProjectId().toString() + '-' + specificRegularMy.getAllPrDemeritInfoId().toString();
                    //文件名(当前时间.音频格式)
                    String audioFileSecondName = System.currentTimeMillis() + '.wma';
                    //路径D:/new_audio/2(科目)-1(倒车入库的编号)-10(所有项目扣分参考编号)_yyyy-MM-dd-HH-mm-ss.wma
                    String pathName = audioPath + audioFileFirstName + '_' + audioFileSecondName;

                    //生成语音包前先去D:/new_audio/看有没有存在语音包,存在了删除
                    File folder = new File(audioPath);
                    File[] files = folder.listFiles();

                    if (files != null) {
                        log.info(&quot;D:/new_audio/不为空!&quot;);
                        for (File file : files) {
                            String fileName = file.getName();
                            if (fileName.contains('_')) {
                                String[] fileNames = fileName.split('_');

                                if (fileNames.length &gt; 1 &amp;&amp; fileNames[0].equals(audioFileFirstName)) {
                                    boolean b = file.delete();
                                    if (b) {
                                        log.info(&quot;文件删除成功!&quot;);

                                        if (specificRegularMy.getAudioFileId() != null) {
                                            AudioFile audioFile = audioFileService.queryOneAudioFileByID(specificRegularMy.getAudioFileId());

                                            if (audioFile == null) {
                                                log.error(&quot;文件查找不到!&quot;);
                                                return new Result&lt;&gt;(ResultCode.FAIL);
                                            }

                                            int ret = audioFileService.deleteOneAudioFileByIdAndName(specificRegularMy.getAudioFileId(), fileNames[1]);

                                            if (ret &gt; 0) {
                                                log.info(&quot;数据库语音数据删除成功!&quot;);
                                                //通知游戏端删除语音包
                                                log.info(&quot;需要删除的语音包:&quot; + audioFile);
                                                springToGameService.reloadDeleteAudioFile(audioFile);

                                            } else {
                                                log.info(&quot;找不到id:&quot; + specificRegularMy.getAudioFileId() + &quot;文件名:&quot; + fileNames[1] + &quot;的数据库语音数据!&quot;);
                                            }
                                        }

                                    } else {
                                        log.error(&quot;文件删除失败!&quot;);
                                        return new Result&lt;&gt;(ResultCode.FAIL);
                                    }
                                }
                            }
                        }
                    }

                    //调用语音合成方法合成语音
                    boolean b = SpeechUtil.SpeechSynthesizer(audioText, pathName);

                    if (b) {
                        log.info(&quot;语音包合成成功!&quot;);

                        //新增一个语音包
                        AudioFile audioFile = new AudioFile();
                        audioFile.setAudioFileFirstName(audioFileFirstName);
                        audioFile.setAudioFileSecondName(audioFileSecondName);
                        audioFile.setAudioFilePath(pathName);

                        int ret = audioFileService.addOneAudioFile(audioFile);

                        if (ret &gt; 0) {
                            log.info(&quot;新增语音包成功!&quot;);

                            springToGameService.reloadAddAudioFile(audioFile);

                            //将前端传过来需要修改的信息赋值给上面查出的对象
                            specificRegularMy.setJudgeState(specificRegular.getJudgeState());//评判状态
                            specificRegularMy.setSpecificJudgeParam(specificRegular.getSpecificJudgeParam());//评判参数
                            specificRegularMy.setVoiceBroadcastText(specificRegular.getVoiceBroadcastText());//语音播报文字
                            specificRegularMy.setDemeritState(specificRegular.getDemeritState());//扣分情况
                            specificRegularMy.setAudioFileId(audioFile.getAudioFileId());//语音文件id

                            //把修改的对象放入集合
                            specificRegularList.add(specificRegularMy);

                        } else {
                            log.error(&quot;新增语音包失败!&quot;);
                            return new Result&lt;&gt;(ResultCode.FAIL);
                        }

                    } else {
                        log.error(&quot;语音包合成失败&quot;);
                        return new Result&lt;&gt;(ResultCode.FAIL);
                    }

                }
            }
        }
    }
    //for循环结束开始操作数据库
    //调用方法批量修改具体规则
    boolean batchUpdate = specificRegularService.updateBatchById(specificRegularList);

    if (batchUpdate) {
        log.info(&quot;具体规则批量修改成功!&quot;);
        //通知游戏端修改的具体规则
        for (SpecificRegular specificRegular : specificRegularList) {
            List&lt;DeviceEntity&gt; deviceEntities = deviceEntityService.queryDevicesByRegularId(specificRegular.getRegularId());

            //此具体规则的总规则绑定了设备才去通知游戏端修改了具体规则
            if (!CollectionUtils.isEmpty(deviceEntities)) {
                springToGameService.reloadUpdateSpecificRegular(specificRegularList);
            }
        }

        return new Result&lt;&gt;(ResultCode.SUCCESS);
    } else {
        log.error(&quot;具体规则批量修改失败!&quot;);
        return new Result&lt;&gt;(ResultCode.FAIL);
    }
} else {
    log.error(&quot;前端传回集合为空&quot;);
    return new Result&lt;&gt;(ResultCode.FAIL);
}
</code></pre>
<p>}</p>

原文地址: https://www.cveoy.top/t/topic/qqUk 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录