material_checklist.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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, updateData) {
  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. if (updateData.length > 0) {
  41. await transaction.updateRows(this.tableName, updateData);
  42. }
  43. await transaction.commit();
  44. const materialChecklistData = await this.getAllDataByCondition({ where: { tid: this.ctx.tender.id } });
  45. const self = this;
  46. return await materialChecklistData.sort(function(a, b) {
  47. return self.ctx.helper.compareCode(a.b_code, b.b_code);
  48. });
  49. } catch (err) {
  50. console.log(err);
  51. await transaction.rollback();
  52. throw err;
  53. }
  54. }
  55. async updateHadBills(transaction, id, had_bills) {
  56. if (!this.ctx.tender || !this.ctx.material) {
  57. throw '数据错误';
  58. }
  59. return await transaction.update(this.tableName, { id, had_bills });
  60. }
  61. }
  62. return MaterialChecklist;
  63. };