material_list_gcl.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. old_quantity: d.quantity,
  37. old_expr: d.expr,
  38. });
  39. }
  40. if (insertArray.length > 0) await transaction.insert(this.tableName, insertArray);
  41. await transaction.update(this.ctx.service.material.tableName, { id: mid, is_new: 1 });
  42. await transaction.commit();
  43. return true;
  44. } catch (err) {
  45. console.log(err);
  46. await transaction.rollback();
  47. throw err;
  48. }
  49. }
  50. async insertOrDelGcl(transaction, insertGclList, removeGclList, mid) {
  51. if (insertGclList && insertGclList.length > 0) {
  52. for (const gcl of insertGclList) {
  53. gcl.tid = this.ctx.tender.id;
  54. gcl.mid = mid;
  55. }
  56. await transaction.insert(this.tableName, insertGclList);
  57. }
  58. if (removeGclList && removeGclList.length > 0) {
  59. for (const gcl of removeGclList) {
  60. await transaction.delete(this.tableName, { id: gcl.id });
  61. }
  62. }
  63. }
  64. async setNewOldData(transaction, tid, type = 'new2old') {
  65. if (type === 'new2old') {
  66. const sql = 'UPDATE ?? SET `old_quantity`=`quantity`, `old_expr`=`expr` WHERE tid=?';
  67. const sqlParam = [this.tableName, this.ctx.tender.id];
  68. return await transaction.query(sql, sqlParam);
  69. }
  70. const sql = 'UPDATE ?? SET `quantity`=`old_quantity`, `expr`=`old_expr` WHERE tid=?';
  71. const sqlParam = [this.tableName, this.ctx.tender.id];
  72. return await transaction.query(sql, sqlParam);
  73. }
  74. async setOldFromLast(transaction, tid, order) {
  75. if (order >= 1) {
  76. // 获取上一期的list值
  77. const materialInfo = await this.ctx.service.material.getDataByCondition({ tid, order });
  78. const materialList = await this.ctx.service.materialList.getAllDataByCondition({ where: { mid: materialInfo.id } });
  79. console.log(materialList);
  80. const lastMaterialGclList = this._.unionWith(materialList, function(item1, item2) {
  81. return item1.gcl_id === item2.gcl_id && item1.mb_id === item2.mb_id;
  82. });
  83. const updateArray = [];
  84. for (const lm of lastMaterialGclList) {
  85. const updateInfo = {
  86. row: {
  87. old_quantity: lm.quantity,
  88. old_expr: lm.expr,
  89. },
  90. where: {
  91. tid: this.ctx.tender.id,
  92. gcl_id: lm.gcl_id,
  93. mb_id: lm.mb_id,
  94. },
  95. };
  96. updateArray.push(updateInfo);
  97. }
  98. if (updateArray.length > 0) await transaction.updateRows(this.tableName, updateArray);
  99. }
  100. }
  101. }
  102. return MaterialListGcl;
  103. };