whyclxw
2025-05-28 e16302f9d475c7cc4dd18c5abf1a23cb5502e362
src/main/java/com/whyc/service/AlarmVoiceSetService.java
@@ -1,16 +1,23 @@
package com.whyc.service;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.whyc.dto.Response;
import com.whyc.mapper.AlarmVoiceSetMapper;
import com.whyc.pojo.AlarmVoiceSet;
import com.whyc.pojo.BattalarmData;
import com.whyc.pojo.UserInf;
import com.whyc.util.ActionUtil;
import com.whyc.util.CommonUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.List;
@Service
public class AlarmVoiceSetService {
@@ -18,13 +25,83 @@
    @Resource
    private AlarmVoiceSetMapper mapper;
    public Response InsertNextTimeInterval(AlarmVoiceSet set) {
        int userId = ActionUtil.getUser().getUId().intValue();
        set.setUserId(userId);
    public Response InsertNextTimeInterval(AlarmVoiceSet set, HttpServletRequest request) {
        //int userId = ActionUtil.getUser().getUId().intValue();
        HttpSession session = request.getSession();
        Object obj=session.getAttribute("user");
        UserInf user = new UserInf();
        if(obj==null){
            user.setUName("未登录的用户账号");
            user.setUId(0L);
            user.setURole(0);
        }else{
            user=(UserInf) obj;
        }
        int userId = user.getUId().intValue();
        long currentTimeMillis = System.currentTimeMillis();
        Integer nextTimeInterval = set.getNextTimeInterval();
        long nextTimestamp = currentTimeMillis+nextTimeInterval*60*1000;
        set.setNextTimestamp(nextTimestamp);
        set.setUserId(user.getUId().intValue());
        set.setStatus(0);
        Integer battGroupId = set.getBattGroupId();
        Long deviceId = set.getDeviceId();
        Long powerDeviceId = set.getPowerDeviceId();
        Integer alarmType = set.getAlarmType();
        //查询所有语音文件中存在的,更新语音文件下次时间戳
        //获取绝对文件夹路径
        String readSuffixPath = "wav" + File.separator + "alarm" + File.separator + user.getUId().intValue() + File.separator;
        String readDirPath = CommonUtil.getRootFile() + readSuffixPath;
        File readDir = new File(readDirPath);
        String[] arr = readDir.list();
        for (String fileName : arr) {
            String[] fileNameSplit = fileName.split("_");
            long fileSpecialId = Long.parseLong(fileNameSplit[3]);
            long fileAlarmType = Long.parseLong(fileNameSplit[4]);
            if(battGroupId!=null){
                if(fileSpecialId==battGroupId && fileAlarmType == alarmType){ //有匹配的,文件名更新下次时间戳
                    String readableVoiceTimeWithWavNew = nextTimestamp+".wav";
                    //文件下次播放时间确定新的文件名
                    String readableVoiceTimeWithWav = fileNameSplit[5];
                    String renameToFileFullName = fileName.replace(readableVoiceTimeWithWav,readableVoiceTimeWithWavNew);
                    String renameToFilePath = CommonUtil.getRootFile() + readSuffixPath + renameToFileFullName;
                    //播报完毕后,将播报时间更新到文件名上
                    String fromFilePath = CommonUtil.getRootFile() + readSuffixPath + fileName;
                    File fromFile = new File(fromFilePath);
                    File renameToFile = new File(renameToFilePath);
                    fromFile.renameTo(renameToFile);
                }
            }else if(deviceId!=null){
                if(fileSpecialId==deviceId && fileAlarmType == alarmType){ //有匹配的,文件名更新下次时间戳
                    String readableVoiceTimeWithWavNew = nextTimestamp+".wav";
                    //文件下次播放时间确定新的文件名
                    String readableVoiceTimeWithWav = fileNameSplit[5];
                    String renameToFileFullName = fileName.replace(readableVoiceTimeWithWav,readableVoiceTimeWithWavNew);
                    String renameToFilePath = CommonUtil.getRootFile() + readSuffixPath + renameToFileFullName;
                    //播报完毕后,将播报时间更新到文件名上
                    String fromFilePath = CommonUtil.getRootFile() + readSuffixPath + fileName;
                    File fromFile = new File(fromFilePath);
                    File renameToFile = new File(renameToFilePath);
                    fromFile.renameTo(renameToFile);
                }
            }else{
                if(fileSpecialId==powerDeviceId && fileAlarmType == alarmType){ //有匹配的,文件名更新下次时间戳
                    String readableVoiceTimeWithWavNew = nextTimestamp+".wav";
                    //文件下次播放时间确定新的文件名
                    String readableVoiceTimeWithWav = fileNameSplit[5];
                    String renameToFileFullName = fileName.replace(readableVoiceTimeWithWav,readableVoiceTimeWithWavNew);
                    String renameToFilePath = CommonUtil.getRootFile() + readSuffixPath + renameToFileFullName;
                    //播报完毕后,将播报时间更新到文件名上
                    String fromFilePath = CommonUtil.getRootFile() + readSuffixPath + fileName;
                    File fromFile = new File(fromFilePath);
                    File renameToFile = new File(renameToFilePath);
                    fromFile.renameTo(renameToFile);
                }
            }
        }
        //判断新增还是修改
        //查询表中是否存在 设备id+告警类型一致的,存在则修改;不存在则新增
        QueryWrapper<AlarmVoiceSet> query = Wrappers.query();
@@ -40,12 +117,38 @@
        AlarmVoiceSet alarmVoiceSetDb = mapper.selectOne(query);
        if(alarmVoiceSetDb!=null){ //修改
            UpdateWrapper<AlarmVoiceSet> update = Wrappers.update();
            update.set("next_time_interval",set.getNextTimeInterval()).eq("id",alarmVoiceSetDb.getId());
            mapper.update(null,update);
            update.set("next_time_interval", nextTimeInterval).set("next_timestamp",nextTimestamp).eq("id",alarmVoiceSetDb.getId());
            mapper.update((AlarmVoiceSet) ActionUtil.objeNull,update);
        }else{ //新增
            mapper.insert(set);
        }
        return new Response().set(1,true,"设置完成");
    }
    public AlarmVoiceSet getNextTimeInterval(Long userId, int type, Long specialId, int alarmType) {
        QueryWrapper<AlarmVoiceSet> query = Wrappers.query();
        query.eq("user_id",userId).eq("alarm_type",alarmType).eq("status",1);
        if(type == 1){
            query.eq("batt_group_id",specialId);
        }else if(type ==2){
            query.eq("device_id",specialId);
        }else{
            query.eq("power_device_id",specialId);
        }
        query.last(" limit 1");
        return mapper.selectOne(query);
    }
    public void delete(Integer id) {
        UpdateWrapper<AlarmVoiceSet> update = Wrappers.update();
        update.set("status",0).eq("id",id);
        mapper.update((AlarmVoiceSet) ActionUtil.objeNull,update);
    }
    public List<AlarmVoiceSet> getAllByUserId(Long userId) {
        QueryWrapper<AlarmVoiceSet> query = Wrappers.query();
        query.eq("user_id",userId);
        return mapper.selectList(query);
    }
}