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
178
179
180
181
182
183
184
package com.yanzuoguang.cloud.file;
import com.yanzuoguang.util.vo.CloudConfig;
import com.yanzuoguang.cloud.file.dao.YzgFileDao;
import com.yanzuoguang.cloud.helper.HttpFileHelper;
import com.yanzuoguang.cloud.vo.YzgFileVo;
import com.yanzuoguang.cloud.vo.req.*;
import com.yanzuoguang.util.exception.CodeException;
import com.yanzuoguang.util.exception.ExceptionHelper;
import com.yanzuoguang.util.helper.DateHelper;
import com.yanzuoguang.util.helper.JsonHelper;
import com.yanzuoguang.util.helper.StringHelper;
import com.yanzuoguang.util.vo.PageSizeData;
import com.yanzuoguang.util.vo.ResponseResult;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.Date;
/**
* Excel文件操作服务
*
* @author 颜佐光
*/
@Component
public class YzgExcelService {
private final CloudConfig cloudConfig;
private final YzgFileDao fileDao;
private final YzgFileProcedure fileProcedure;
public YzgExcelService(CloudConfig cloudConfig, YzgFileDao fileDao, YzgFileProcedure fileProcedure) {
this.cloudConfig = cloudConfig;
this.fileDao = fileDao;
this.fileProcedure = fileProcedure;
}
/**
* 创建文件,不需要开启事务
*
* @param json
* @param fileReq
*/
public void fileCreate(String json, YzgFileCreateReqVo fileReq) {
try {
YzgFileVo load = fileDao.load(fileReq, YzgFileVo.class);
if (load == null) {
load = JsonHelper.to(fileReq, YzgFileVo.class);
fileDao.create(load);
}
// 生成回调参数
String serverAbsolutePath = getServerAbsolutePath(load);
YzgFileCallbackReqVo callbackReq = new YzgFileCallbackReqVo(
fileReq.getFileId(), fileReq.getCompanyId(),
fileReq.getCallbackMQ(), fileReq.getCallbackParameter(),
serverAbsolutePath
);
// 判断文件状态
int fileStatus = StringHelper.toInt(load.getFileStatus());
switch (fileStatus) {
case YzgFileVo.FILE_STATUS_INIT:
if (fileDao.getUserCompanyCount(load) > fileReq.getCreateUserCompanyMax()) {
throw new CodeException("该用户在该公司当日生成文件达到最大次数");
}
if (fileDao.getCallbackCount(load) < fileReq.getCallBackMax()) {
YzgFileStatusReqVo statusReqVo = new YzgFileStatusReqVo(fileReq.getFileId(), fileReq.getCompanyId());
statusReqVo.setFileStatus(YzgFileVo.FILE_STATUS_RELEASE);
fileDao.updateStatus(statusReqVo);
fileProcedure.fileCallback(callbackReq);
}
// 等待15秒后再次发出请求,追踪文件进度
fileProcedure.fileCreateDelay(json);
break;
case YzgFileVo.FILE_STATUS_RELEASE:
fileProcedure.fileCallback(callbackReq);
// 等待15秒后再次发出请求,追踪文件进度
fileProcedure.fileCreateDelay(json);
break;
default:
break;
}
} catch (CodeException ex) {
if (fileReq != null && !StringHelper.isEmpty(fileReq.getFileId())) {
ResponseResult<?> error = ExceptionHelper.getError(ex);
fileDao.updateStatus(new YzgFileStatusReqVo(fileReq.getFileId(), fileReq.getCompanyId(), error.getMessage()));
}
}
}
public void updateStatus(YzgFileStatusReqVo fileReq) {
fileDao.updateStatus(fileReq);
}
/**
* 获取文件数据
*
* @param req
* @return
*/
public PageSizeData<YzgFileVo> loadPage(YzgFileQueryReqVo req) {
return fileDao.loadPage(req, YzgFileVo.class);
}
/**
* 下载文件
*
* @param req
* @param response
* @throws IOException
*/
public void donwload(YzgFileDownloadReqVo req, String downloadUserId, HttpServletResponse response) throws IOException {
YzgFileVo load = fileDao.load(req, YzgFileVo.class);
if (load == null) {
throw new CodeException("文件不存在或者已经删除");
}
int fileStatus = StringHelper.toInt(load.getFileStatus());
if (fileStatus == YzgFileVo.FILE_STATUS_ERROR) {
throw new CodeException(load.getErrorMessage());
} else if (fileStatus != YzgFileVo.FILE_STATUS_OK) {
throw new CodeException("文件正在生成中");
}
String serverPath = getServerPath(load);
File file = new File(serverPath);
if (!file.exists()) {
throw new CodeException("文件已经被删除,或者服务器之间没有共享文件目录");
}
load.setDownloadDate(DateHelper.getNow());
load.setDownloadUserId(downloadUserId);
fileDao.update(load);
HttpFileHelper.localToDown(serverPath, load.getFileName(), response);
}
/**
* 下载文件
*
* @param req
*/
public YzgFileVo load(YzgFileDownloadReqVo req) {
YzgFileVo load = fileDao.load(req, YzgFileVo.class);
if (load == null) {
throw new CodeException("文件不存在或者已经删除");
}
// 文件生成成功后,文件是否存在,不存在时则提示
if (StringHelper.toInt(load.getFileStatus()) == YzgFileVo.FILE_STATUS_OK) {
String serverPath = getServerPath(load);
File file = new File(serverPath);
if (!file.exists()) {
throw new CodeException("文件不存在或者已经删除");
}
}
return load;
}
/**
* 删除文件
*
* @param req
*/
public void remove(YzgFileDownloadReqVo req) {
YzgFileVo load = fileDao.load(req, YzgFileVo.class);
if (load == null) {
throw new CodeException("文件不存在或者已经删除");
}
String serverPath = getServerPath(load);
File file = new File(serverPath);
if (file.exists()) {
file.delete();
}
fileDao.remove(req);
}
private String getServerPath(YzgFileVo load) {
return String.format("%s/%s", cloudConfig.getServerUrl(), getServerAbsolutePath(load));
}
private String getServerAbsolutePath(YzgFileVo load) {
Date date = DateHelper.getDateTime(load.getCreateDate());
// 获取临时文件路径
String tempFolder = cloudConfig.getTempFolder(load.getCompanyId(), date);
return String.format("%s/%s.xlsx", tempFolder, load.getFileId());
}
}