material_list_gcl.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. 'use strict';
  2. /**
  3. * 清单设置 数据模型
  4. * @author LanJianRong
  5. * @date 2020/6/30
  6. * @version
  7. */
  8. module.exports = app => {
  9. class MaterialListGcl 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_list_gcl';
  19. }
  20. async setData(mid, data) {
  21. if (!this.ctx.tender) {
  22. throw '数据错误';
  23. }
  24. const transaction = await this.db.beginTransaction();
  25. try {
  26. const insertArray = [];
  27. for (const d of data) {
  28. insertArray.push({
  29. tid: this.ctx.tender.id,
  30. mid,
  31. order: d.order,
  32. gcl_id: d.gcl_id,
  33. mb_id: d.mb_id,
  34. quantity: d.quantity,
  35. expr: d.expr,
  36. });
  37. }
  38. if (insertArray.length > 0) await transaction.insert(this.tableName, insertArray);
  39. await transaction.update(this.ctx.service.material.tableName, { id: mid, is_new: 1 });
  40. await transaction.commit();
  41. return true;
  42. } catch (err) {
  43. console.log(err);
  44. await transaction.rollback();
  45. throw err;
  46. }
  47. }
  48. async insertOrDelGcl(transaction, insertGclList, removeGclList, mid) {
  49. if (insertGclList.length > 0) {
  50. for (const gcl of insertGclList) {
  51. gcl.tid = this.ctx.tender.id;
  52. gcl.mid = mid;
  53. }
  54. await transaction.insert(this.tableName, insertGclList);
  55. }
  56. if (removeGclList.length > 0) {
  57. for (const gcl of removeGclList) {
  58. await transaction.delete(this.tableName, { id: gcl.id });
  59. }
  60. }
  61. }
  62. }
  63. return MaterialListGcl;
  64. };