material_list.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. 'use strict';
  2. /**
  3. * 调差清单关联工料表 数据模型
  4. *
  5. * @author Mai
  6. * @date 2018/8/13
  7. * @version
  8. */
  9. const auditConst = require('../const/audit').material;
  10. module.exports = app => {
  11. class MaterialList extends app.BaseService {
  12. /**
  13. * 构造函数
  14. *
  15. * @param {Object} ctx - egg全局变量
  16. * @return {void}
  17. */
  18. constructor(ctx) {
  19. super(ctx);
  20. this.tableName = 'material_list';
  21. }
  22. /**
  23. * 添加工料清单关联
  24. * @return {void}
  25. */
  26. async add(data) {
  27. if (!this.ctx.tender || !this.ctx.material) {
  28. throw '数据错误';
  29. }
  30. const list = [];
  31. for (const mb of data.mb_id) {
  32. const newLists = {
  33. tid: this.ctx.tender.id,
  34. mid: this.ctx.material.id,
  35. mb_id: mb,
  36. gcl_id: data.gcl_id,
  37. xmj_id: data.xmj_id,
  38. mx_id: data.mx_id,
  39. in_time: new Date(),
  40. };
  41. list.push(newLists);
  42. }
  43. // 新增工料
  44. const result = await this.db.insert(this.tableName, list);
  45. if (result.affectedRows === 0) {
  46. throw '新增工料数据失败';
  47. }
  48. return await this.getMaterialData(this.ctx.tender.id);
  49. }
  50. /**
  51. * 删除工料清单关联
  52. * @param {int} id 工料id
  53. * @return {void}
  54. */
  55. async del(id) {
  56. if (!this.ctx.tender || !this.ctx.material) {
  57. throw '数据错误';
  58. }
  59. // 判断是否可删
  60. return await this.deleteById(id);
  61. }
  62. /**
  63. * 修改工料清单关联信息
  64. * @param {Object} data 工料内容
  65. * @return {void}
  66. */
  67. async save(data) {
  68. if (!this.ctx.tender || !this.ctx.material) {
  69. throw '数据错误';
  70. }
  71. // 判断是否可修改
  72. return await this.db.update(this.tableName, data);
  73. }
  74. /**
  75. * 应用工料清单到其它清单中
  76. * @return {void}
  77. */
  78. async addOther(data) {
  79. if (!this.ctx.tender || !this.ctx.material) {
  80. throw '数据错误';
  81. }
  82. const list = [];
  83. const select = data.select;
  84. for (const mb of data.mx_id) {
  85. const newLists = {
  86. tid: this.ctx.tender.id,
  87. mid: this.ctx.material.id,
  88. mb_id: select.mb_id,
  89. gcl_id: select.gcl_id,
  90. xmj_id: select.xmj_id,
  91. mx_id: mb,
  92. quantity: select.quantity,
  93. in_time: new Date(),
  94. };
  95. list.push(newLists);
  96. }
  97. // 新增工料
  98. const result = await this.db.insert(this.tableName, list);
  99. if (result.affectedRows === 0) {
  100. throw '新增工料数据失败';
  101. }
  102. return await this.getMaterialData(this.ctx.tender.id);
  103. }
  104. /**
  105. * 获取工料清单关联表
  106. * @param {int} tid 标段id
  107. * @return {void}
  108. */
  109. async getMaterialData(tid) {
  110. const sql = 'SELECT ml.`id`, mb.`code`, mb.`name`, mb.`unit`, ml.`quantity`, ml.`mb_id`, ml.`gcl_id`, ml.`xmj_id`, ml.`mx_id`, ml.`tid`, ml.`mid`' +
  111. ' FROM ' + this.tableName + ' as ml' +
  112. ' LEFT JOIN ' + this.ctx.service.materialBills.tableName + ' as mb' +
  113. ' ON ml.`mb_id` = mb.`id`' +
  114. ' WHERE ml.`tid` = ?';
  115. const sqlParam = [tid];
  116. return await this.db.query(sql, sqlParam);
  117. }
  118. }
  119. return MaterialList;
  120. };