ledger_tag.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const validField = ['lid', 'share', 'color', 'comment'];
  10. module.exports = app => {
  11. class LedgerTag extends app.BaseService {
  12. /**
  13. * 构造函数
  14. *
  15. * @param {Object} ctx - egg全局变量
  16. * @return {void}
  17. */
  18. constructor(ctx) {
  19. super(ctx);
  20. this.tableName = 'ledger_tag';
  21. }
  22. /**
  23. * 获取台账、期所有标段
  24. * @param {Number} tid - 标段id
  25. * @param {Number} sid - 期id(-1时查询台账分解全部标签)
  26. * @returns {Promise<void>}
  27. */
  28. async getDatas(tid, sid = -1) {
  29. // return await this.db.select(this.tableName, {
  30. // where: {tid: tid, sid: -1},
  31. // columns: ['id', 'uid', 'lid', 'share', 'color', 'comment'],
  32. // orders: [['create_time', 'desc']],
  33. // });
  34. const sql = 'SELECT id, uid, lid, share, color, comment FROM ' + this.tableName +
  35. ' WHERE tid = ? and sid = ? and (uid = ? or share) ORDER BY create_time DESC';
  36. return await this.db.query(sql, [tid, sid, this.ctx.session.sessionUser.accountId]);
  37. }
  38. /**
  39. * 过滤无效字段,容错
  40. * @param data
  41. * @private
  42. */
  43. _filterInvalidField(data) {
  44. for (const prop in data) {
  45. if (validField.indexOf(prop) === -1) {
  46. delete data[prop];
  47. }
  48. }
  49. }
  50. async _addTag(data) {
  51. this._filterInvalidField(data);
  52. data.create_time = new Date();
  53. data.modify_time = data.create_time;
  54. data.uid = this.ctx.session.sessionUser.accountId;
  55. data.tid = this.ctx.tender.id;
  56. data.pid = this.ctx.tender.data.project_id;
  57. if (this.ctx.stage) {
  58. data.sid = this.ctx.stage.id;
  59. data.sorder = this.ctx.stage.order;
  60. }
  61. const result = await this.db.insert(this.tableName, data);
  62. data.id = result.insertId;
  63. return data;
  64. }
  65. async _delTag(id) {
  66. const tag = await this.getDataById(id);
  67. if (tag.uid !== this.ctx.session.sessionUser.accountId) throw '您无权删除该数据';
  68. await this.deleteById(id);
  69. return id;
  70. }
  71. async _updateTag(data) {
  72. const tag = await this.getDataById(data.id);
  73. if (tag.uid !== this.ctx.session.sessionUser.accountId) throw '您无权修改该数据';
  74. this._filterInvalidField(data);
  75. data.modify_time = new Date();
  76. data.id = tag.id;
  77. const result = await this.db.update(this.tableName, data);
  78. if (result.affectedRows === 1) return data;
  79. }
  80. async update(data) {
  81. const result = {};
  82. if (data.add) result.add = await this._addTag(data.add);
  83. if (data.del) result.del = await this._delTag(data.del);
  84. if (data.update) result.update = await this._updateTag(data.update);
  85. return result;
  86. }
  87. }
  88. return LedgerTag;
  89. };