material_checklist.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. 'use strict';
  2. /**
  3. * 清单设置 数据模型
  4. * @author LanJianRong
  5. * @date 2020/6/30
  6. * @version
  7. */
  8. module.exports = app => {
  9. class MaterialChecklist extends app.BaseService {
  10. /**
  11. * 构造函数
  12. *
  13. * @param {Object} ctx - egg全局变量
  14. * @return {void}
  15. */
  16. constructor(ctx) {
  17. super(ctx);
  18. this.tableName = 'material_checklist';
  19. }
  20. async resetData(pushData, removeData) {
  21. if (!this.ctx.tender || !this.ctx.material) {
  22. throw '数据错误';
  23. }
  24. const transaction = await this.db.beginTransaction();
  25. try {
  26. if (pushData.length > 0) {
  27. const insertDatas = [];
  28. for (const p of pushData) {
  29. p.mid = this.ctx.material.id;
  30. p.tid = this.ctx.tender.id;
  31. insertDatas.push(p);
  32. }
  33. await transaction.insert(this.tableName, insertDatas);
  34. }
  35. if (removeData.length > 0) {
  36. for (const r of removeData) {
  37. await transaction.delete(this.tableName, { id: r });
  38. }
  39. }
  40. await transaction.commit();
  41. return await this.getAllDataByCondition({ where: { tid: this.ctx.tender.id }, orders: [['order', 'asc']] });
  42. } catch (err) {
  43. await transaction.rollback();
  44. throw err;
  45. }
  46. }
  47. async updateHadBills(transaction, id, had_bills) {
  48. if (!this.ctx.tender || !this.ctx.material) {
  49. throw '数据错误';
  50. }
  51. return await transaction.update(this.tableName, { id, had_bills });
  52. }
  53. }
  54. return MaterialChecklist;
  55. };