ledger_tag.js 3.1 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 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. const sql = 'SELECT la.id, la.uid, la.lid, la.share, la.color, la.comment, pa.name as u_name FROM ' + this.tableName + ' la ' +
  30. ' LEFT JOIN ' + this.ctx.service.projectAccount.tableName + ' pa ON la.uid = pa.id' +
  31. ' WHERE la.tid = ? and la.sid = ? and (la.uid = ? or la.share) ORDER BY la.create_time DESC';
  32. return await this.db.query(sql, [tid, sid, this.ctx.session.sessionUser.accountId]);
  33. }
  34. /**
  35. * 过滤无效字段,容错
  36. * @param data
  37. * @private
  38. */
  39. _filterInvalidField(data) {
  40. for (const prop in data) {
  41. if (validField.indexOf(prop) === -1) {
  42. delete data[prop];
  43. }
  44. }
  45. }
  46. async _addTag(data) {
  47. this._filterInvalidField(data);
  48. data.create_time = new Date();
  49. data.modify_time = data.create_time;
  50. data.uid = this.ctx.session.sessionUser.accountId;
  51. data.tid = this.ctx.tender.id;
  52. data.pid = this.ctx.tender.data.project_id;
  53. if (this.ctx.stage) {
  54. data.sid = this.ctx.stage.id;
  55. data.sorder = this.ctx.stage.order;
  56. }
  57. const result = await this.db.insert(this.tableName, data);
  58. data.id = result.insertId;
  59. data.u_name = this.ctx.session.sessionUser.name;
  60. return data;
  61. }
  62. async _delTag(id) {
  63. const tag = await 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 = await 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.update(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 LedgerTag;
  86. };