datacollect_tender.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. 'use strict';
  2. /**
  3. * 决策大屏标段管理-数据模型
  4. *
  5. * @author ellisran
  6. * @date 2021/09/23
  7. * @version
  8. */
  9. // const accountGroup = require('../const/account_group').group;
  10. module.exports = app => {
  11. class datacollectTender extends app.BaseService {
  12. constructor(ctx) {
  13. super(ctx);
  14. this.tableName = 'datacollect_tender';
  15. }
  16. // async getGroupInfo(pid, groupid) {
  17. // const sql = 'SELECT * FROM ?? WHERE pid = ? AND groupid = ? AND uid is NULL';
  18. // const sqlParam = [this.tableName, pid, groupid];
  19. // return await this.db.queryOne(sql, sqlParam);
  20. // }
  21. //
  22. // async saveAudit(pid, groupid, uid) {
  23. // const data = {
  24. // pid,
  25. // groupid,
  26. // uid,
  27. // create_time: new Date(),
  28. // };
  29. // return await this.db.insert(this.tableName, data);
  30. // }
  31. //
  32. // async saveGroup(pid, groupid) {
  33. // const transaction = await this.db.beginTransaction();
  34. // try {
  35. // // 删除所在组的用户
  36. // await transaction.delete(this.tableName, { pid, groupid });
  37. // const data = {
  38. // pid,
  39. // groupid,
  40. // create_time: new Date(),
  41. // };
  42. // await transaction.insert(this.tableName, data);
  43. // await transaction.commit();
  44. // return true;
  45. // } catch (err) {
  46. // await transaction.rollback();
  47. // throw err;
  48. // }
  49. // }
  50. //
  51. // async delAudit(id) {
  52. // return await this.db.delete(this.tableName, { id });
  53. // }
  54. async getList(pid, spid = this.ctx.subProject.id || '') {
  55. const sql = 'SELECT dt.* FROM ?? AS dt LEFT JOIN ?? AS t ON dt.tid = t.id WHERE dt.pid = ? AND t.spid = ?';
  56. const sqlParam = [this.tableName, this.ctx.service.tender.tableName, pid, spid];
  57. return await this.db.query(sql, sqlParam);
  58. }
  59. async updateList(pid, tids) {
  60. // 初始化事务
  61. const transaction = await this.db.beginTransaction();
  62. try {
  63. const oldList = await this.getList(pid, this.ctx.subProject.id);
  64. const oldIds = this._.map(oldList, 'tid');
  65. const insertTids = this._.difference(tids, oldIds);
  66. const delTids = this._.difference(oldIds, tids);
  67. if (insertTids.length > 0) {
  68. const insertDatas = [];
  69. for (const i of insertTids) {
  70. insertDatas.push({
  71. pid,
  72. tid: i,
  73. });
  74. }
  75. await transaction.insert(this.tableName, insertDatas);
  76. }
  77. if (delTids.length > 0) {
  78. for (const d of delTids) {
  79. await transaction.delete(this.tableName, { pid, tid: d });
  80. }
  81. }
  82. transaction.commit();
  83. } catch (e) {
  84. console.log(e);
  85. transaction.rollback();
  86. }
  87. }
  88. async add(pid, tid) {
  89. const data = {
  90. pid,
  91. tid,
  92. };
  93. return await this.db.insert(this.tableName, data);
  94. }
  95. }
  96. return datacollectTender;
  97. };