ledger_tag.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const validField = ['lid', 'pos_id', '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, settleId = -1) {
  29. const sql = 'SELECT la.id, la.uid, la.lid, la.pos_id, 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.settle_id = ? and (la.uid = ? or la.share) ORDER BY la.create_time DESC';
  32. return await this.db.query(sql, [tid, sid, settleId, 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. if (this.ctx.settle) {
  58. data.settle_id = this.ctx.settle.id;
  59. data.settle_order = this.ctx.settle.settle_order;
  60. }
  61. const result = await this.db.insert(this.tableName, data);
  62. data.id = result.insertId;
  63. data.u_name = this.ctx.session.sessionUser.name;
  64. return data;
  65. }
  66. async _delTag(id) {
  67. const tag = await this.getDataById(id);
  68. if (tag.uid !== this.ctx.session.sessionUser.accountId) throw '您无权删除该数据';
  69. await this.deleteById(id);
  70. return id;
  71. }
  72. async _updateTag(data) {
  73. const tag = await this.getDataById(data.id);
  74. if (tag.uid !== this.ctx.session.sessionUser.accountId) throw '您无权修改该数据';
  75. this._filterInvalidField(data);
  76. data.modify_time = new Date();
  77. data.id = tag.id;
  78. const result = await this.db.update(this.tableName, data);
  79. if (result.affectedRows === 1) return data;
  80. }
  81. async update(data) {
  82. const result = {};
  83. if (data.add) result.add = await this._addTag(data.add);
  84. if (data.del) result.del = await this._delTag(data.del);
  85. if (data.update) result.update = await this._updateTag(data.update);
  86. return result;
  87. }
  88. }
  89. return LedgerTag;
  90. };