ledger_tag.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 StageBillsDgn 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. }
  35. /**
  36. * 过滤无效字段,容错
  37. * @param data
  38. * @private
  39. */
  40. _filterInvalidField(data) {
  41. for (const prop in data) {
  42. if (validField.indexOf(prop) === -1) {
  43. delete data[prop];
  44. }
  45. }
  46. }
  47. async _addTag(data) {
  48. this._filterInvalidField(data);
  49. data.create_time = new Date();
  50. data.modify_time = data.create_time;
  51. data.uid = this.ctx.session.sessionUser.accountId;
  52. data.tid = this.ctx.tender.id;
  53. data.pid = this.ctx.tender.data.project_id;
  54. if (this.ctx.stage) {
  55. data.sid = this.ctx.stage.id;
  56. data.sorder = this.ctx.stage.order;
  57. }
  58. const result = await this.db.insert(this.tableName, data);
  59. data.id = result.insertId;
  60. return data;
  61. }
  62. async _delTag(id) {
  63. const tag = this.getDataById(id);
  64. if (tag.uid !== this.ctx.session.sessionUser.accountId) throw '您无权删除该数据';
  65. await this.deleteById(id);
  66. return id;
  67. }
  68. async _updateTag(data) {
  69. const tag = this.getDataById(data.id);
  70. if (tag.uid !== this.ctx.session.sessionUser.accountId) throw '您无权删除该数据';
  71. this._filterInvalidField(data);
  72. data.modify_time = new Date();
  73. data.id = tag.id;
  74. const result = await this.db.udpate(this.tableName, data);
  75. if (result.affectedRows === 1) return data;
  76. }
  77. async update(data) {
  78. const result = {};
  79. if (data.add) result.add = await this._addTag(data.add);
  80. if (data.del) result.del = await this._delTag(data.del);
  81. if (data.update) result.update = await this._updateTag(data.update);
  82. return result;
  83. }
  84. }
  85. return StageBillsDgn;
  86. };