sub_proj_info.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const defaultInfo = {
  10. main_quantity: [
  11. { id: 1, name: '路基土石方', unit: 'm3', dgn_qty: 0, final_qty: 0, },
  12. { id: 2, name: '特殊路基处理', unit: 'km', dgn_qty: 0, final_qty: 0, },
  13. { id: 3, name: '路基排水圬工', unit: 'm3', dgn_qty: 0, final_qty: 0, },
  14. { id: 4, name: '路基防护圬工', unit: 'm3', dgn_qty: 0, final_qty: 0, },
  15. { id: 5, name: '路面工程', unit: 'm2', dgn_qty: 0, final_qty: 0, },
  16. { id: 6, name: '大、特大桥', unit: 'm/座', dgn_qty: 0, final_qty: 0, },
  17. { id: 7, name: '中、小桥', unit: 'm/座', dgn_qty: 0, final_qty: 0, },
  18. { id: 8, name: '涵洞', unit: 'm/道', dgn_qty: 0, final_qty: 0, },
  19. { id: 9, name: '隧道', unit: 'm/座', dgn_qty: 0, final_qty: 0, },
  20. { id: 10, name: '分离式立交', unit: '处', dgn_qty: 0, final_qty: 0, },
  21. { id: 11, name: '通道、天桥', unit: '座', dgn_qty: 0, final_qty: 0, },
  22. { id: 12, name: '平面交叉', unit: '处', dgn_qty: 0, final_qty: 0, },
  23. { id: 13, name: '互通式立交', unit: 'km/处', dgn_qty: 0, final_qty: 0, },
  24. { id: 14, name: '连接线长度、辅导长度', unit: 'km', dgn_qty: 0, final_qty: 0, },
  25. { id: 15, name: '管理及养护房屋', unit: 'm2', dgn_qty: 0, final_qty: 0, },
  26. ],
  27. gcl_quantity: [
  28. { id: 1, pid: -1, full_path: '1', level: 1, is_leaf: 1, order: 1, name: '主要人工消耗', unit: '工日', dgn_qty: 0, final_qty: 0, },
  29. { id: 2, pid: -1, full_path: '2', level: 1, is_leaf: 0, order: 2, name: '主要材料消耗', unit: '', dgn_qty: 0, final_qty: 0, },
  30. { id: 3, pid: 2, full_path: '2-3', level: 2, is_leaf: 1, order: 1, name: '钢材', unit: '吨', dgn_qty: 0, final_qty: 0, },
  31. { id: 4, pid: 2, full_path: '2-4', level: 2, is_leaf: 1, order: 2, name: '沥青', unit: '吨', dgn_qty: 0, final_qty: 0, },
  32. { id: 5, pid: 2, full_path: '2-5', level: 2, is_leaf: 1, order: 3, name: '汽油、柴油', unit: '吨', dgn_qty: 0, final_qty: 0, },
  33. { id: 6, pid: 2, full_path: '2-6', level: 2, is_leaf: 1, order: 4, name: '水泥', unit: '吨', dgn_qty: 0, final_qty: 0, },
  34. { id: 7, pid: 2, full_path: '2-7', level: 2, is_leaf: 1, order: 5, name: '碎石、砂', unit: 'm3', dgn_qty: 0, final_qty: 0, },
  35. { id: 8, pid: 2, full_path: '2-8', level: 2, is_leaf: 1, order: 6, name: '电', unit: 'km.h', dgn_qty: 0, final_qty: 0, },
  36. { id: 9, pid: -1, full_path: '9', level: 1, is_leaf: 1, order: 3, name: '主要机械消耗', unit: '台班', dgn_qty: 0, final_qty: 0, },
  37. ],
  38. };
  39. module.exports = app => {
  40. class SubProjInfo extends app.BaseService {
  41. /**
  42. * 构造函数
  43. *
  44. * @param {Object} ctx - egg全局变量
  45. * @return {void}
  46. */
  47. constructor(ctx) {
  48. super(ctx);
  49. this.tableName = 'sub_project_info';
  50. }
  51. async addInfo(id, project_id, transaction) {
  52. const info = { id, project_id };
  53. for (const di in defaultInfo) {
  54. info[di] = JSON.stringify(defaultInfo[di]);
  55. }
  56. if (transaction) {
  57. await transaction.insert(this.tableName, info);
  58. } else {
  59. await this.db.insert(this.tableName, info);
  60. }
  61. return info;
  62. }
  63. /**
  64. * 获取标段相关信息
  65. * @param tenderId
  66. * @return {Promise<void>}
  67. */
  68. async getInfo(id) {
  69. let info = await this.getDataByCondition({ id });
  70. // 兼容不存在info的情况
  71. if (!info) info = await this.addInfo(id, this.ctx.session.sessionProject.id);
  72. for (const ai in defaultInfo) {
  73. info[ai] = !info[ai] || info[ai] === '' ? defaultInfo[ai] : JSON.parse(info[ai]);
  74. }
  75. return info;
  76. }
  77. async getInfo4Report(id) {
  78. let info = await this.getDataByCondition({ id });
  79. // 兼容不存在info的情况
  80. if (!info) info = await this.addInfo(id, this.ctx.session.sessionProject.id);
  81. for (const ai in defaultInfo) {
  82. info[ai] = !info[ai] || info[ai] === '' ? defaultInfo[ai] : JSON.parse(info[ai]);
  83. info[ai].forEach(x => {
  84. x.type = ai;
  85. });
  86. }
  87. return info;
  88. }
  89. async _saveCommonInfo(id, data) {
  90. const updateData = { id };
  91. for (const d in data) {
  92. if (d === 'id' || d === 'project_id') continue;
  93. if (defaultInfo[d]) continue;
  94. updateData[d] = data[d];
  95. }
  96. await this.db.update(this.tableName, updateData);
  97. return updateData;
  98. }
  99. async _saveArrayInfo(id, type, data) {
  100. const info = await this.getInfo(id);
  101. const source = info[type];
  102. const result = [];
  103. for (const d of data) {
  104. const s = source.find(x => { return x.id === d.id });
  105. if (d.dgn_qty !== undefined) s.dgn_qty = d.dgn_qty || 0;
  106. if (d.final_qty !== undefined) s.final_qty = d.final_qty || 0;
  107. result.push(s);
  108. }
  109. const updateData = { id };
  110. updateData[type] = JSON.stringify(source);
  111. await this.db.update(this.tableName, updateData);
  112. return result;
  113. }
  114. /**
  115. * 保存标段相关信息
  116. *
  117. * @param data
  118. * @return {Promise<void>}
  119. */
  120. async saveInfo(id, data) {
  121. switch (data.type) {
  122. case 'main_quantity':
  123. case 'gcl_quantity':
  124. return await this._saveArrayInfo(id, data.type, data.updateData);
  125. default:
  126. return await this._saveCommonInfo(id, data.updateData);
  127. }
  128. }
  129. }
  130. return SubProjInfo;
  131. };