whyclxw
2025-05-28 e16302f9d475c7cc4dd18c5abf1a23cb5502e362
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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 {
 
    @Resource
    private AlarmVoiceSetMapper mapper;
 
    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();
        query.select("id").eq("alarm_type",alarmType).eq("user_id",userId);
        if(battGroupId!=null){
            query.eq("batt_group_id",battGroupId);
        }else if(deviceId!=null){
            query.eq("device_id",deviceId);
        }else{
            query.eq("power_device_id",powerDeviceId);
        }
        query.last(" limit 1");
        AlarmVoiceSet alarmVoiceSetDb = mapper.selectOne(query);
        if(alarmVoiceSetDb!=null){ //修改
            UpdateWrapper<AlarmVoiceSet> update = Wrappers.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);
    }
}