ledger_tag.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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, settleId = -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. // const sql = 'SELECT la.id, la.uid, la.lid, la.share, la.color, la.comment, pa.name as u_name FROM ' + this.tableName + ' la ' +
  34. // ' LEFT JOIN ' + this.ctx.service.projectAccount.tableName + ' pa ON la.uid = pa.id' +
  35. // ' WHERE la.tid = ? and la.sid = ? and la.settle_id = ? and (la.uid = ? or la.share) ORDER BY la.create_time DESC';
  36. // return await this.db.query(sql, [tid, sid, settleId, 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. if (this.ctx.settle) {
  62. data.settle_id = this.ctx.settle.id;
  63. data.settle_order = this.ctx.settle.settle_order;
  64. }
  65. const result = await this.db.insert(this.tableName, data);
  66. data.id = result.insertId;
  67. data.u_name = this.ctx.session.sessionUser.name;
  68. return data;
  69. }
  70. async _delTag(id) {
  71. const tag = await this.getDataById(id);
  72. if (tag.uid !== this.ctx.session.sessionUser.accountId) throw '您无权删除该数据';
  73. await this.deleteById(id);
  74. return id;
  75. }
  76. async _updateTag(data) {
  77. const tag = await this.getDataById(data.id);
  78. if (tag.uid !== this.ctx.session.sessionUser.accountId) throw '您无权修改该数据';
  79. this._filterInvalidField(data);
  80. data.modify_time = new Date();
  81. data.id = tag.id;
  82. const result = await this.db.update(this.tableName, data);
  83. if (result.affectedRows === 1) return data;
  84. }
  85. async update(data) {
  86. const result = {};
  87. if (data.add) result.add = await this._addTag(data.add);
  88. if (data.del) result.del = await this._delTag(data.del);
  89. if (data.update) result.update = await this._updateTag(data.update);
  90. return result;
  91. }
  92. }
  93. return LedgerTag;
  94. };