ledger_template.js 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. module.exports = app => {
  10. class LedgerTemplate extends app.BaseService {
  11. /**
  12. * 构造函数
  13. *
  14. * @param {Object} ctx - egg全局变量
  15. * @return {void}
  16. */
  17. constructor(ctx) {
  18. super(ctx);
  19. this.tableName = 'ledger_template';
  20. }
  21. async getAllTemplate(pid) {
  22. // const result = await this.getAllDataByCondition({
  23. // columns: ['id', 'create_user_id', 'user_name', 'create_time', 'name', 'source', 'is_share'],
  24. // where: { pid },
  25. // orders: [['create_time', 'ASC']],
  26. // });
  27. const sql = `SELECT id, create_user_id, user_name, create_time, name, source, is_share FROM ${this.tableName} WHERE pid = ? AND (create_user_id = ? OR is_share) ORDER BY create_time ASC`;
  28. const result = await this.db.query(sql, [pid, this.ctx.session.sessionUser.accountId]);
  29. return result;
  30. }
  31. async getTemplateData(id) {
  32. const template = await this.getDataById(id);
  33. if (!template) throw '模板不存在';
  34. return JSON.parse(template.content);
  35. }
  36. async checkTemplateEdit(id) {
  37. const template = await this.getDataById(id);
  38. if (!template) throw '编辑的模板不存在';
  39. if (template.pid !== this.ctx.session.sessionProject.id) throw '模板不属于当前项目,请刷新后重试';
  40. if (template.create_user_id !== this.ctx.session.sessionUser.accountId) throw '您无权修改该模板';
  41. return template;
  42. }
  43. async _addTemplate(data) {
  44. const user = await this.ctx.service.projectAccount.getDataById(this.ctx.session.sessionUser.accountId);
  45. const addData = {
  46. id: this.uuid.v4(), pid: this.ctx.session.sessionProject.id,
  47. create_user_id: user.id, user_name: user.name, user_company: user.company, user_role: user.role,
  48. name: data.name || '', source: data.source || '', content: JSON.stringify(data.content),
  49. is_share: data.is_share ? 1 :0,
  50. };
  51. const result = await this.db.insert(this.tableName, addData);
  52. return addData;
  53. }
  54. async _delTemplate(id) {
  55. const template = await this.checkTemplateEdit(id);
  56. await this.deleteById(id);
  57. return id;
  58. }
  59. async _updateTemplate(data) {
  60. const template = await this.checkTemplateEdit(data.id);
  61. const updateData = { id: template.id };
  62. if (data.name !== undefined) updateData.name = data.name || '';
  63. if (data.is_share !== undefined) updateData.is_share = data.is_share ? 1 : 0;
  64. const result = await this.db.update(this.tableName, updateData);
  65. if (result.affectedRows === 1) return updateData;
  66. }
  67. async saveTemplate(data) {
  68. const result = {};
  69. if (data.add) result.add = await this._addTemplate(data.add);
  70. if (data.del) result.del = await this._delTemplate(data.del);
  71. if (data.update) result.update = await this._updateTemplate(data.update);
  72. return result;
  73. }
  74. }
  75. return LedgerTemplate;
  76. };