phase_pay_file.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. 'use strict';
  2. /**
  3. *
  4. * 附件
  5. * @author Ellisran
  6. * @date 2019/1/11
  7. * @version
  8. */
  9. module.exports = app => {
  10. class SubProjFile extends app.BaseService {
  11. /**
  12. * 构造函数
  13. *
  14. * @param {Object} ctx - egg全局变量
  15. * @return {void}
  16. */
  17. constructor(ctx) {
  18. super(ctx);
  19. this.tableName = 'phase_pay_file';
  20. }
  21. _analysisData(files) {
  22. const helper = this.ctx.helper;
  23. const userId = this.ctx.session.sessionUser.accountId;
  24. const ossPath = this.ctx.app.config.fujianOssPath;
  25. files.forEach(x => {
  26. x.viewpath = helper.canPreview(x.fileext) ? ossPath + x.filepath : '';
  27. x.filepath = ossPath + x.filepath;
  28. x.fileext_str = helper.fileExtStr(x.fileext);
  29. x.canEdit = x.user_id === userId;
  30. });
  31. }
  32. async getData(phase_id, type) {
  33. const data = await this.getAllDataByCondition({
  34. where: { type, phase_id, is_deleted: 0 },
  35. orders: [['create_time', 'desc']],
  36. });
  37. this._analysisData(data);
  38. return data;
  39. }
  40. async getFiles(condition) {
  41. condition.orders = [['create_time', 'desc']];
  42. const result = await this.getAllDataByCondition(condition);
  43. this._analysisData(result);
  44. return result;
  45. }
  46. async addFiles(phasePay, type, fileInfo, user) {
  47. const conn = await this.db.beginTransaction();
  48. const result = {};
  49. try {
  50. const insertData = fileInfo.map(x => {
  51. return {
  52. id: this.uuid.v4(), tid: phasePay.tid, phase_id: phasePay.id, type, rela_id: x.rela_id,
  53. user_id: user.id, user_name: user.name, user_company: user.company, user_role: user.role,
  54. filename: x.filename, fileext: x.fileext, filesize: x.filesize, filepath: x.filepath,
  55. };
  56. });
  57. await conn.insert(this.tableName, insertData);
  58. await conn.commit();
  59. result.files = { id: insertData.map(x => { return x.id; })};
  60. } catch (err) {
  61. await conn.rollback();
  62. throw err;
  63. }
  64. return await this.getFiles({ where: result.files });
  65. }
  66. async delFiles(files) {
  67. if (files.length === 0) return;
  68. const fileDatas = await this.getAllDataByCondition({ where: { id: files } });
  69. const result = {};
  70. const conn = await this.db.beginTransaction();
  71. try {
  72. const updateData = fileDatas.map(x => { return { id: x.id, is_deleted: 1 }; });
  73. if (updateData.length > 0) await conn.updateRows(this.tableName, updateData);
  74. await conn.commit();
  75. result.del = files;
  76. } catch (err) {
  77. await conn.rollback();
  78. throw err;
  79. }
  80. return result;
  81. }
  82. }
  83. return SubProjFile;
  84. };