FileServiceImpl.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. package com.caimei.service.data.impl;
  2. import com.caimei.mapper.FileMapper;
  3. import com.caimei.model.ResponseJson;
  4. import com.caimei.model.po.CourseFilePo;
  5. import com.caimei.model.po.ProductImagePo;
  6. import com.caimei.model.po.FilePo;
  7. import com.caimei.model.vo.CourseFileListVo;
  8. import com.caimei.model.vo.FileListVo;
  9. import com.caimei.model.vo.WxFileListVo;
  10. import com.caimei.service.data.FileService;
  11. import com.caimei.utils.OSSUtils;
  12. import com.github.pagehelper.PageHelper;
  13. import com.github.pagehelper.PageInfo;
  14. import lombok.extern.slf4j.Slf4j;
  15. import org.apache.commons.lang3.StringUtils;
  16. import org.springframework.beans.factory.annotation.Value;
  17. import org.springframework.stereotype.Service;
  18. import javax.annotation.Resource;
  19. import java.io.*;
  20. import java.net.HttpURLConnection;
  21. import java.net.MalformedURLException;
  22. import java.net.URL;
  23. import java.util.Date;
  24. import java.util.List;
  25. /**
  26. * Description
  27. *
  28. * @author : Aslee
  29. * @date : 2021/7/9
  30. */
  31. @Slf4j
  32. @Service
  33. public class FileServiceImpl implements FileService {
  34. @Resource
  35. private FileMapper fileMapper;
  36. @Value("${aliyunConfig.accessKeyId}")
  37. private String accessKeyId;
  38. @Value("${aliyunConfig.accessKeySecret}")
  39. private String accessKeySecret;
  40. @Value("${aliyunConfig.bucketName}")
  41. private String bucketName;
  42. @Value("${aliyunConfig.endpoint}")
  43. private String endpoint;
  44. @Value("${spring.profiles.active}")
  45. private String active;
  46. @Override
  47. public ResponseJson<PageInfo<FileListVo>> getFileList(Integer listType, Integer authUserId, String fileTitle, Integer auditStatus, Integer status, Integer pageNum, Integer pageSize) {
  48. if (null == authUserId) {
  49. return ResponseJson.error("参数异常,请输入供应商用户id", null);
  50. }
  51. listType = null == listType ? 1 : listType;
  52. PageHelper.startPage(pageNum, pageSize);
  53. List<FileListVo> fileList = fileMapper.getFileList(listType, authUserId, fileTitle, auditStatus, status);
  54. PageInfo<FileListVo> pageData = new PageInfo<>(fileList);
  55. return ResponseJson.success(pageData);
  56. }
  57. @Override
  58. public ResponseJson saveFile(Integer fileId, Integer authUserId, String fileTitle, String fileName, String filePreviewUrl, String fileDownloadUrl) {
  59. if (null == fileId && null == authUserId) {
  60. return ResponseJson.error("参数异常,请输入供应商用户id");
  61. }
  62. if (StringUtils.isEmpty(fileTitle)) {
  63. return ResponseJson.error("参数异常,请输入文件标题");
  64. }
  65. if (StringUtils.isEmpty(fileName)) {
  66. return ResponseJson.error("参数异常,请输入文件名称");
  67. }
  68. if (null == filePreviewUrl || null == fileDownloadUrl) {
  69. return ResponseJson.error("参数异常,请上传文件");
  70. }
  71. /*
  72. 组装文件数据
  73. */
  74. FilePo dataFile = new FilePo();
  75. dataFile.setTitle(fileTitle);
  76. dataFile.setPreviewUrl(filePreviewUrl);
  77. dataFile.setDownloadUrl(fileDownloadUrl);
  78. dataFile.setName(fileName);
  79. // 上线状态默认为“待上线”,审核状态为“待审核”
  80. dataFile.setStatus(2);
  81. dataFile.setAuditStatus(2);
  82. if (null != fileId) {
  83. /*
  84. 更新文件
  85. */
  86. dataFile.setId(fileId);
  87. fileMapper.updateFileSelective(dataFile);
  88. } else {
  89. /*
  90. 新增文件
  91. */
  92. dataFile.setAuthUserId(authUserId);
  93. dataFile.setCreateTime(new Date());
  94. fileMapper.insertFile(dataFile);
  95. }
  96. return ResponseJson.success("保存文件成功");
  97. }
  98. @Override
  99. public ResponseJson updateFileStatus(Integer fileId, Integer status) {
  100. if (null == fileId) {
  101. return ResponseJson.error("请输入文件id");
  102. }
  103. if (null == status) {
  104. return ResponseJson.error("请输入要更新的状态值");
  105. }
  106. fileMapper.updateFileStatusByFileId(fileId, status);
  107. if (0 == status) {
  108. return ResponseJson.success("下线文件成功");
  109. } else {
  110. return ResponseJson.success("上线文件成功");
  111. }
  112. }
  113. @Override
  114. public ResponseJson deleteFile(Integer fileId) {
  115. if (null == fileId) {
  116. return ResponseJson.error("参数异常,请输入文件id");
  117. }
  118. // 删除文件
  119. fileMapper.deleteFileByFileId(fileId);
  120. return ResponseJson.success("删除文件成功");
  121. }
  122. @Override
  123. public ResponseJson auditFile(Integer fileId, Integer auditStatus, String invalidReason, Integer auditBy) {
  124. if (null == fileId) {
  125. return ResponseJson.error("请输入文件id");
  126. }
  127. if (null == auditStatus) {
  128. return ResponseJson.error("请输入审核结果");
  129. }
  130. if (0 == auditStatus && StringUtils.isEmpty(invalidReason)) {
  131. return ResponseJson.error("请输入审核不通过的原因");
  132. }
  133. if (null == auditBy) {
  134. return ResponseJson.error("请输入审核人用户id");
  135. }
  136. Date auditTime = new Date();
  137. // 授权状态更新
  138. Integer status = null;
  139. if (0 == auditStatus) {
  140. // 审核不通过,下线文件
  141. status = 0;
  142. } else {
  143. // 审核通过,上线文件
  144. status = 1;
  145. }
  146. fileMapper.updateFileAuditStatus(fileId, status, auditStatus, invalidReason, auditBy, auditTime);
  147. return ResponseJson.success("审核文件成功");
  148. }
  149. @Override
  150. public ResponseJson<PageInfo<WxFileListVo>> getWxFileList(Integer authUserId, String fileTitle, Integer pageNum, Integer pageSize) {
  151. if (null == authUserId) {
  152. return ResponseJson.error("参数异常,供应商用户id不能为空", null);
  153. }
  154. PageHelper.startPage(pageNum, pageSize);
  155. List<WxFileListVo> fileList = fileMapper.getWxFileList(authUserId, fileTitle);
  156. PageInfo<WxFileListVo> pageData = new PageInfo<>(fileList);
  157. return ResponseJson.success(pageData);
  158. }
  159. @Override
  160. public void download() {
  161. String filePath = "/mnt/newdatadrive/data/runtime/jar-instance/zplma/atuhImage/";
  162. List<ProductImagePo> authImageList = fileMapper.getAuthImageData();
  163. authImageList.forEach(authImage->{
  164. try {
  165. String imageUrl = authImage.getCertificateImage();
  166. HttpURLConnection httpUrl = (HttpURLConnection) new URL(imageUrl).openConnection();
  167. httpUrl.connect();
  168. //通过输入流获取图片数据
  169. InputStream inStream = httpUrl.getInputStream();
  170. //得到图片的二进制数据,以二进制封装得到数据,具有通用性
  171. byte[] data = readInputStream(inStream);
  172. //new一个文件对象用来保存图片,默认保存当前工程根目录
  173. File imageFile = new File(filePath + authImage.getAuthParty() + "-" + authImage.getSnCode() + ".jpg");
  174. //创建输出流
  175. FileOutputStream outStream = new FileOutputStream(imageFile);
  176. //写入数据
  177. outStream.write(data);
  178. //关闭输出流
  179. outStream.close();
  180. } catch (FileNotFoundException e) {
  181. e.printStackTrace();
  182. } catch (MalformedURLException e) {
  183. e.printStackTrace();
  184. } catch (IOException e) {
  185. e.printStackTrace();
  186. } catch (Exception e) {
  187. e.printStackTrace();
  188. }
  189. });
  190. }
  191. private byte[] readInputStream(InputStream inStream) throws Exception{
  192. ByteArrayOutputStream outStream = new ByteArrayOutputStream();
  193. //创建一个Buffer字符串
  194. byte[] buffer = new byte[1024];
  195. //每次读取的字符串长度,如果为-1,代表全部读取完毕
  196. int len = 0;
  197. //使用一个输入流从buffer里把数据读取出来
  198. while( (len=inStream.read(buffer)) != -1 ){
  199. //用输出流往buffer里写入数据,中间参数代表从哪个位置开始读,len代表读取的长度
  200. outStream.write(buffer, 0, len);
  201. }
  202. //关闭输入流
  203. inStream.close();
  204. //把outStream里的数据写入内存
  205. return outStream.toByteArray();
  206. }
  207. @Override
  208. public ResponseJson saveCourseFile(Integer fileId, String fileTitle, String fileName, String ossName, Integer fileType, String fileModule) {
  209. if (StringUtils.isEmpty(fileTitle)) {
  210. return ResponseJson.error("参数异常,请输入文件标题");
  211. }
  212. if (StringUtils.isEmpty(fileName)) {
  213. return ResponseJson.error("参数异常,请输入文件名称");
  214. }
  215. if (StringUtils.isEmpty(ossName)) {
  216. return ResponseJson.error("参数异常,请上传文件");
  217. }
  218. if (null == fileType) {
  219. return ResponseJson.error("参数异常,请选择文件类型");
  220. }
  221. if (null == fileModule) {
  222. return ResponseJson.error("参数异常,请选择所属模块");
  223. }
  224. /*
  225. 组装文件数据
  226. */
  227. CourseFilePo courseFile = new CourseFilePo();
  228. courseFile.setTitle(fileTitle);
  229. courseFile.setOssName(ossName);
  230. courseFile.setName(fileName);
  231. courseFile.setFileType(fileType);
  232. courseFile.setFileModule(fileModule);
  233. if (null != fileId) {
  234. /*
  235. 更新文件
  236. */
  237. courseFile.setId(fileId);
  238. fileMapper.updateCourseFile(courseFile);
  239. } else {
  240. /*
  241. 新增文件
  242. */
  243. fileMapper.insertCourseFile(courseFile);
  244. }
  245. return ResponseJson.success("保存成功");
  246. }
  247. @Override
  248. public ResponseJson<PageInfo<CourseFileListVo>> getCourseFileList(Integer fileType, String fileModule, String fileTitle, Integer pageNum, Integer pageSize) {
  249. if (null == fileType) {
  250. return ResponseJson.error("参数异常,请输入文件类型", null);
  251. }
  252. PageHelper.startPage(pageNum, pageSize);
  253. List<CourseFileListVo> fileList = fileMapper.getCourseFileList(fileType, fileModule, fileTitle);
  254. fileList.forEach(file-> file.setFileUrl(OSSUtils.getOssUrl(file.getOssName())));
  255. PageInfo<CourseFileListVo> pageData = new PageInfo<>(fileList);
  256. return ResponseJson.success(pageData);
  257. }
  258. @Override
  259. public ResponseJson deleteCourseFile(Integer fileId) {
  260. if (null == fileId) {
  261. return ResponseJson.error("参数异常,请输入文件id");
  262. }
  263. // 删除文件
  264. fileMapper.deleteCourseFileByFileId(fileId);
  265. return ResponseJson.success("删除文件成功");
  266. }
  267. }