/java/src/main/java/com/mogujie/ares/model/FileModel.java

https://gitlab.com/lisit1003/TTServer · Java · 215 lines · 154 code · 20 blank · 41 comment · 16 complexity · c9ec6cb8f9a565815ea05aa3ad852c92 MD5 · raw file

  1. package com.mogujie.ares.model;
  2. import java.sql.Connection;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. import com.mogujie.ares.data.TransmitFile;
  9. import com.mogujie.ares.lib.logger.Logger;
  10. import com.mogujie.ares.lib.logger.LoggerFactory;
  11. import com.mogujie.ares.manager.DBManager;
  12. import com.mogujie.ares.manager.DBManager.DBPoolName;
  13. /**
  14. * @Description: 用户相关的model,包括获取用户信息等
  15. * @author ziye
  16. *
  17. */
  18. public class FileModel {
  19. private static final Logger logger = LoggerFactory
  20. .getLogger(FileModel.class);
  21. private static FileModel instance = new FileModel();
  22. public static FileModel getInstance() {
  23. if (instance == null) {
  24. instance = new FileModel();
  25. }
  26. return instance;
  27. }
  28. /*
  29. *
  30. * @Description: 保存一个未接收的离线文件
  31. *
  32. * @param userId
  33. *
  34. * @param toUserId
  35. *
  36. * @param filePath
  37. *
  38. * @return
  39. */
  40. public boolean saveFileRecord(int userId, int toUserId, String taskId, String filePath,
  41. int fileSize) {
  42. if (userId <= 0 || toUserId <= 0 || filePath == null
  43. || "".equals(filePath)) {
  44. return false;
  45. }
  46. DBManager dbManager = DBManager.getInstance();
  47. Connection conn = dbManager.getConnection(DBPoolName.macim_master);
  48. PreparedStatement statement = null;
  49. try {
  50. int time = (int) (System.currentTimeMillis() / 1000);
  51. String sql = " insert into IMTransmitFile (userId, toUserId, taskId, filePath, created, updated,fsize) "
  52. + "values(?,?,?,?,?,?,?)";
  53. statement = conn.prepareStatement(sql);
  54. int index = 1;
  55. statement.setInt(index++, userId);
  56. statement.setInt(index++, toUserId);
  57. statement.setString(index++, taskId);
  58. statement.setString(index++, filePath);
  59. statement.setInt(index++, time);
  60. statement.setInt(index++, time);
  61. statement.setInt(index++, fileSize);
  62. statement.executeUpdate();
  63. } catch (SQLException e) {
  64. logger.error(userId + " : " + toUserId + " : " + filePath, e);
  65. return false;
  66. } finally {
  67. dbManager.release(DBPoolName.macim_master, conn, statement, null);
  68. }
  69. return true;
  70. }
  71. /*
  72. *
  73. * @Description: 设置一个文件已经被对方接收
  74. *
  75. * @param id
  76. *
  77. * @return
  78. */
  79. public boolean deleteFileRecord(String taskId) {
  80. if (null == taskId || taskId.isEmpty()) {
  81. return false;
  82. }
  83. DBManager dbManager = DBManager.getInstance();
  84. Connection conn = dbManager.getConnection(DBPoolName.macim_master);
  85. PreparedStatement statement = null;
  86. try {
  87. int time = (int) (System.currentTimeMillis() / 1000);
  88. String sql = "update IMTransmitFile set status = 0, updated = ? where "
  89. + "taskId = ? limit 1";
  90. statement = conn.prepareStatement(sql);
  91. int index = 1;
  92. statement.setInt(index++, time);
  93. statement.setString(index++, taskId);
  94. statement.executeUpdate();
  95. } catch (SQLException e) {
  96. logger.error("taskId : " + taskId, e);
  97. return false;
  98. } finally {
  99. dbManager.release(DBPoolName.macim_master, conn, statement, null);
  100. }
  101. return true;
  102. }
  103. /*
  104. *
  105. * @Description: 获取一个用户未接收的所有文件列表
  106. *
  107. * @param toUserId
  108. *
  109. * @return
  110. */
  111. public List<TransmitFile> getUserFiles(int toUserId) {
  112. List<TransmitFile> fileList = new ArrayList<TransmitFile>();
  113. if (toUserId <= 0) {
  114. return fileList;
  115. }
  116. DBManager dbManager = DBManager.getInstance();
  117. Connection conn = dbManager.getConnection(DBPoolName.macim_slave);
  118. PreparedStatement statement = null;
  119. ResultSet rs = null;
  120. TransmitFile file = null;
  121. try {
  122. int time = (int) (System.currentTimeMillis() / 1000) - 7 * 24 * 3600;
  123. String sql = "select * from IMTransmitFile where "
  124. + "toUserId = ? and created > ? and status = 1 order by created desc limit 50";
  125. statement = conn.prepareStatement(sql);
  126. int index = 1;
  127. statement.setInt(index++, toUserId);
  128. statement.setInt(index++, time);
  129. rs = statement.executeQuery();
  130. while (rs.next()) {
  131. file = new TransmitFile();
  132. file.setId(rs.getInt("id"));
  133. file.setFromUserId(rs.getInt("userId"));
  134. file.setToUserId(rs.getInt("toUserId"));
  135. file.setTaskId(rs.getString("taskId"));
  136. file.setFilePath(rs.getString("filePath"));
  137. file.setStatus(rs.getInt("status"));
  138. file.setCreated(rs.getInt("created"));
  139. file.setUpdated(rs.getInt("updated"));
  140. file.setFileSize(rs.getInt("fsize"));
  141. fileList.add(file);
  142. }
  143. } catch (SQLException e) {
  144. logger.error("userId : " + toUserId, e);
  145. return new ArrayList<TransmitFile>();
  146. } finally {
  147. dbManager.release(DBPoolName.macim_slave, conn, statement, rs);
  148. }
  149. return fileList;
  150. }
  151. /*
  152. *
  153. * @Description: 获取一个发送的文件的记录
  154. *
  155. * @param toUserId
  156. *
  157. * @return
  158. */
  159. public TransmitFile getFileRecord(String taskId) {
  160. if (null == taskId || taskId.isEmpty()) {
  161. return null;
  162. }
  163. DBManager dbManager = DBManager.getInstance();
  164. Connection conn = dbManager.getConnection(DBPoolName.macim_slave);
  165. PreparedStatement statement = null;
  166. ResultSet rs = null;
  167. TransmitFile file = null;
  168. try {
  169. String sql = "select * from IMTransmitFile where "
  170. + "taskId = ? limit 1";
  171. statement = conn.prepareStatement(sql);
  172. int index = 1;
  173. statement.setString(index++, taskId);
  174. rs = statement.executeQuery();
  175. if (rs.next()) {
  176. file = new TransmitFile();
  177. file.setId(rs.getInt("id"));
  178. file.setFromUserId(rs.getInt("userId"));
  179. file.setToUserId(rs.getInt("toUserId"));
  180. file.setTaskId(rs.getString("taskId"));
  181. file.setFilePath(rs.getString("filePath"));
  182. file.setStatus(rs.getInt("status"));
  183. file.setCreated(rs.getInt("created"));
  184. file.setUpdated(rs.getInt("updated"));
  185. file.setFileSize(rs.getInt("fsize"));
  186. }
  187. } catch (SQLException e) {
  188. logger.error("taskId : " + taskId, e);
  189. return null;
  190. } finally {
  191. dbManager.release(DBPoolName.macim_slave, conn, statement, rs);
  192. }
  193. return file;
  194. }
  195. }