whyczh
2022-06-15 4ed95ead307dfd8d81beab2d5180758e9420b591
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package com.whyc.service;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.whyc.dto.Response;
import com.whyc.dto.paramter.UserWorkAlarmParam;
import com.whyc.mapper.UserWorkMapper;
import com.whyc.mapper.WorkAlarmMapper;
import com.whyc.pojo.UserWork;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
 
import javax.annotation.Resource;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
@Service
public class UserWorkService {
    @Resource
    private UserWorkMapper mapper;
    @Resource
    private WorkAlarmMapper workAlarmMapper;
    @Autowired
    private Environment environment;
 
    public Response addOrUpdate(UserWork userWork){
        if (userWork.getId()==null || userWork.getId()==0){
            mapper.insert(userWork);
        }else {
            mapper.updateById(userWork);
        }
        //当状态为-1时,驳回操作,需要重新创建已派单订单;如果是3则为完成 同时更改wolkAlarm表的status
        if ("-1".equals(userWork.getCheckStatus())||userWork.getCheckStatus()==-1){
            UserWork uw = new UserWork();
            uw.setWorkId(userWork.getWorkId());
            uw.setUserId(userWork.getUserId());
            uw.setCreateTime(new Date());
            uw.setEndTime(new Date());
            uw.setCheckStatus(1);
            if(mapper.insert(uw)>0){
                return new Response().set(1,true,"操作成功");
            }else {
                return new Response().set(1,false,"操作失败");
            }
        }
        if ("3".equals(userWork.getCheckStatus())|| userWork.getCheckStatus()==3){
            int bool = workAlarmMapper.updateStatus(userWork.getWorkId(),3);
            if (bool>0){
                return new Response().set(1,true,"操作成功");
            }else {
                return new Response().set(1,false,"操作失败");
            }
        }
        return new Response().set(1,true,"操作成功");
    }
 
    public Response searchByCondition(UserWork userWork){
        QueryWrapper<UserWork> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(userWork.getUserId() != null && userWork.getUserId() != 0, "userId", userWork.getUserId());
        queryWrapper.eq(userWork.getManagerId() != null && userWork.getManagerId() != 0, "managerId", userWork.getManagerId());
        queryWrapper.eq(userWork.getWorkId() != null && userWork.getWorkId() != 0, "workId", userWork.getWorkId());
        List<UserWork> list = mapper.selectList(queryWrapper);
        return new Response().set(1, list, "查询成功");
    }
 
 
 
    public Response uploadAlarmFile(List<MultipartFile> file, UserWorkAlarmParam param) {
        String fileDirName = "";
        int configType = Integer.parseInt(environment.getProperty("configFile.type"));
        ApplicationHome applicationHome = new ApplicationHome(getClass());
        File jarFile = applicationHome.getDir();
        //测试版
        if(configType==1){
            fileDirName = jarFile.getParentFile().toString();
        }else{
            //打包版
            fileDirName = jarFile.toString();
        }
        String root=fileDirName+"/fg_photo/stationsrc/alarm/"+ param.getStationId() + "/" + param.getAfterOrBefore() + "/";
        List<String> filePathList = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        boolean isSuccess = false;
        try {
            for (int i = 0; i < file.size() && param != null; i++) {
                String fileFileName = file.get(i).getOriginalFilename();
                fileFileName= fileFileName.replace(".","_"+sdf.format(new Date())+".");
                String filePath = root + fileFileName;
                createFilefolderIFNotExist(filePath);
                file.get(i).transferTo(new File(filePath));
                isSuccess = true;
                filePathList.add(filePath);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (isSuccess) {
            return new Response().set(1,filePathList,"上传成功");
        } else {
            return new Response().set(0,false,"上传失败");
        }
 
    }
    //将文件复制到指定的目录
    public void copyFile(File oldFile, String newPath){
        File file = new File(newPath);
        FileInputStream in = null;
        FileOutputStream out = null;
        try {
            in = new FileInputStream(oldFile);
            out = new FileOutputStream(file);;
 
            byte[] buffer=new byte[2097152];
            int readByte = 0;
            while((readByte = in.read(buffer)) != -1){
                out.write(buffer, 0, readByte);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
 
    public void createFilefolderIFNotExist(String filePath) {
        File f = new File(filePath);
        if (!f.exists()) {
            if (!f.getParentFile().exists()) {
                f.getParentFile().mkdirs();
            }
        }
    }
 
 
    public Response deleteAlarmFile(String fileNames, int stationId, String afterOrBefore) {
        String names[] = fileNames.split(",");
        String fileDirName = "";
        int configType = Integer.parseInt(environment.getProperty("configFile.type"));
        ApplicationHome applicationHome = new ApplicationHome(getClass());
        File jarFile = applicationHome.getDir();
        //测试版
        if (configType == 1) {
            fileDirName = jarFile.getParentFile().toString();
        } else {
            //打包版
            fileDirName = jarFile.toString();
        }
        String root = fileDirName + "/fg_photo/stationsrc/alarm/" + stationId + File.separator + afterOrBefore + File.separator;
        for (String name : names) {
            String targetFilePath = root + name;
            File file = new File(targetFilePath);
            file.delete();
        }
        return new Response().set(1, "删除成功");
    }
 
}