budget.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date 2021/11/9
  7. * @version
  8. */
  9. const defaultDecimal = {
  10. qty: 3,
  11. tp: 0,
  12. up: 2,
  13. };
  14. const FinalObj = require('../lib/budget_final');
  15. module.exports = app => {
  16. class Budget extends app.BaseService {
  17. /**
  18. * 构造函数
  19. *
  20. * @param {Object} ctx - egg全局变量
  21. * @return {void}
  22. */
  23. constructor(ctx) {
  24. super(ctx);
  25. this.tableName = 'budget';
  26. }
  27. /**
  28. * 数据规则
  29. *
  30. * @param {String} scene - 场景
  31. * @return {Object} - 返回数据规则
  32. */
  33. rule(scene) {
  34. let rule = {};
  35. switch (scene) {
  36. case 'add':
  37. rule = {
  38. name: { type: 'string', required: true, min: 2 },
  39. std_id: { type: 'string', required: true, min: 1 },
  40. };
  41. break;
  42. case 'save':
  43. rule = {
  44. name: { type: 'string', required: true, min: 2, max: 100, },
  45. };
  46. default:
  47. break;
  48. }
  49. return rule;
  50. }
  51. async getBudget(admin) {
  52. let result = await this.getAllDataByCondition({
  53. where: { pid: this.ctx.session.sessionProject.id },
  54. orders: [['name', 'asc']],
  55. });
  56. if (admin) return result;
  57. const permissionConst = this.ctx.service.subProjPermission.PermissionConst.budget;
  58. const permissionBudget = await this.ctx.service.subProjPermission.getUserPermission();
  59. result = result.filter(x => {
  60. const pb = permissionBudget.find(y => { return x.id === y.bid});
  61. if (pb) {
  62. x.canEdit = pb.budget_permission.indexOf(permissionConst.edit.value) >= 0;
  63. }
  64. return !!pb;
  65. });
  66. return result;
  67. }
  68. async getCurBudget(id) {
  69. const result = await this.getDataById(id);
  70. result.decimal = result.decimal ? JSON.parse(result.decimal) : {};
  71. this.ctx.helper._.defaults(result.decimal, defaultDecimal);
  72. return result;
  73. }
  74. /**
  75. * 新增标段
  76. *
  77. * @param {Object} data - 提交的数据
  78. * @return {Boolean} - 返回新增结果
  79. */
  80. async add(transaction, data, budgetStd) {
  81. if (!transaction) throw '数据错误';
  82. data.in_time = new Date();
  83. data.std_id = budgetStd.id;
  84. const operate = await transaction.insert(this.tableName, data);
  85. if (operate.insertId === 0) throw '初始化动态投资数据失败';
  86. // 获取合同支付模板 并添加到标段
  87. await this.ctx.service.budgetGu.initByTemplate(transaction, operate.insertId, budgetStd.gu_template_id);
  88. await this.ctx.service.budgetGai.initByTemplate(transaction, operate.insertId, budgetStd.gai_template_id);
  89. await this.ctx.service.budgetYu.initByTemplate(transaction, operate.insertId, budgetStd.yu_template_id);
  90. return operate.insertId;
  91. }
  92. /**
  93. * 保存标段
  94. *
  95. * @param {Number} id
  96. * @param {Object} postData - 表单post过来的数
  97. * @return {Boolean} - 返回执行结果
  98. */
  99. async save(data) {
  100. const result = await this.db.update(this.tableName, data);
  101. return result.affectedRows > 0;
  102. }
  103. /**
  104. * 假删除
  105. *
  106. * @param {Number} id - 删除的id
  107. * @return {Boolean} - 删除结果
  108. */
  109. async deleteBudget(id) {
  110. const updateData = { id, status: this.status.DISABLE };
  111. const result = await this.db.update(this.tableName, updateData);
  112. return result.affectedRows > 0;
  113. }
  114. /**
  115. * 真删除
  116. * @param {Number} id - 删除的标段id
  117. * @return {Promise<boolean>} - 结果
  118. */
  119. async deleteBudgetNoBackup(id) {
  120. const transaction = await this.db.beginTransaction();
  121. try {
  122. await transaction.delete(this.tableName, { id });
  123. await transaction.delete(this.ctx.service.budgetGu.tableName, { bid: id });
  124. await transaction.delete(this.ctx.service.budgetGai.tableName, { bid: id });
  125. await transaction.delete(this.ctx.service.budgetYu.tableName, { bid: id });
  126. await transaction.commit();
  127. return true;
  128. } catch (err) {
  129. this.ctx.log(err);
  130. await transaction.rollback();
  131. return false;
  132. }
  133. }
  134. async _getGuUpdateData(newDecimal, orgDecimal) {
  135. if (newDecimal.qty >= orgDecimal.qty && newDecimal.tp >= orgDecimal.tp) return [];
  136. const datas = await this.ctx.service.budgetGu.getData(this.ctx.budget.id);
  137. const result = [];
  138. for (const d of datas) {
  139. const dgn_qty1 = this.ctx.helper.round(d.dgn_qty1, newDecimal.qty);
  140. const dgn_qty2 = this.ctx.helper.round(d.dgn_qty2, newDecimal.qty);
  141. const total_price = d.is_leaf ? this.ctx.helper.round(d.total_price, newDecimal.tp) : 0;
  142. if (dgn_qty1 !== d.dgn_qty1 || dgn_qty2 !== d.dgn_qty2 || total_price !== d.total_price) {
  143. result.push({ id: d.id, tree_id: d.tree_id, dgn_qty1, dgn_qty2, total_price });
  144. }
  145. }
  146. return result;
  147. }
  148. async _getGaiUpdateData(newDecimal, orgDecimal) {
  149. if (newDecimal.qty >= orgDecimal.qty && newDecimal.tp >= orgDecimal.tp) return [];
  150. const datas = await this.ctx.service.budgetGai.getData(this.ctx.budget.id);
  151. const result = [];
  152. for (const d of datas) {
  153. const dgn_qty1 = this.ctx.helper.round(d.dgn_qty1, newDecimal.qty);
  154. const dgn_qty2 = this.ctx.helper.round(d.dgn_qty2, newDecimal.qty);
  155. const total_price = d.is_leaf ? this.ctx.helper.round(d.total_price, newDecimal.tp) : 0;
  156. if (dgn_qty1 !== d.dgn_qty1 || dgn_qty2 !== d.dgn_qty2 || total_price !== d.total_price) {
  157. result.push({ id: d.id, tree_id: d.tree_id, dgn_qty1, dgn_qty2, total_price });
  158. }
  159. }
  160. return result;
  161. }
  162. async _getYuUpdateData(newDecimal, orgDecimal) {
  163. if (newDecimal.qty >= orgDecimal.qty && newDecimal.up >= orgDecimal.up && newDecimal.tp === orgDecimal.tp) return [];
  164. const datas = await this.ctx.service.budgetYu.getData(this.ctx.budget.id);
  165. const result = [];
  166. for (const d of datas) {
  167. if (d.b_code) {
  168. if (!d.is_leaf) continue;
  169. const quantity = this.ctx.helper.round(d.quantity, newDecimal.qty);
  170. const unit_price = this.ctx.helper.round(d.unit_price, newDecimal.up);
  171. const total_price = this.ctx.helper.mul(unit_price, quantity, newDecimal.tp);
  172. if (quantity !== d.quantity || unit_price !== d.unit_price || total_price !== d.total_price) {
  173. result.push({ id: d.id, tree_id: d.tree_id, quantity, unit_price, total_price });
  174. }
  175. } else {
  176. const dgn_qty1 = this.ctx.helper.round(d.dgn_qty1, newDecimal.qty);
  177. const dgn_qty2 = this.ctx.helper.round(d.dgn_qty2, newDecimal.qty);
  178. const total_price = d.is_leaf ? this.ctx.helper.round(d.total_price, newDecimal.tp) : 0;
  179. if (dgn_qty1 !== d.dgn_qty1 || dgn_qty2 !== d.dgn_qty2 || total_price !== d.total_price) {
  180. result.push({ id: d.id, tree_id: d.tree_id, dgn_qty1, dgn_qty2, total_price });
  181. }
  182. }
  183. }
  184. return result;
  185. }
  186. async saveDecimal(decimal, page) {
  187. const newDecimal = JSON.parse(JSON.stringify(this.ctx.budget.decimal));
  188. if (decimal.qty >= 0 && decimal.qty <= 6) newDecimal.qty = decimal.qty;
  189. if (decimal.up >= 0 && decimal.up <= 6) newDecimal.up = decimal.up;
  190. if (decimal.tp >= 0 && decimal.tp <= 6) newDecimal.tp = decimal.tp;
  191. const guDatas = await this._getGuUpdateData(newDecimal, this.ctx.budget.decimal);
  192. const gaiDatas = await this._getGaiUpdateData(newDecimal, this.ctx.budget.decimal);
  193. const yuDatas = await this._getYuUpdateData(newDecimal, this.ctx.budget.decimal);
  194. const conn = await this.db.beginTransaction();
  195. try {
  196. const result = await conn.update(this.tableName, { id: this.ctx.budget.id, decimal: JSON.stringify(newDecimal) });
  197. if (guDatas.length > 0) await conn.updateRows(this.ctx.service.budgetGu.tableName, guDatas);
  198. if (gaiDatas.length > 0) await conn.updateRows(this.ctx.service.budgetGai.tableName, gaiDatas);
  199. if (yuDatas.length > 0) await conn.updateRows(this.ctx.service.budgetYu.tableName, yuDatas);
  200. await conn.commit();
  201. switch (page) {
  202. case 'gu': return { update: guDatas };
  203. case 'gai': return { update: gaiDatas };
  204. case 'yu': return { update: yuDatas };
  205. }
  206. } catch (err) {
  207. await conn.rollback();
  208. throw err;
  209. }
  210. }
  211. async doFinal(budget, final) {
  212. const finalObj = new FinalObj(this.ctx);
  213. let finalData;
  214. try {
  215. finalData = await finalObj.doFinal(budget, final);
  216. } catch (err) {
  217. this.db.update(this.ctx.service.budgetFinalList.tableName, { id: final.id, status: 4});
  218. this.ctx.log(err);
  219. throw '生成决算数据错误';
  220. }
  221. const conn = await this.db.beginTransaction();
  222. try {
  223. await conn.update(this.tableName, {id: budget.id, final_id: final.id, final_time: new Date() });
  224. await conn.insert(this.ctx.service.budgetFinal.tableName, finalData);
  225. await conn.update(this.ctx.service.budgetFinalList.tableName, { id: final.id, tender_info: JSON.stringify(final.tender_info), status: 3});
  226. await conn.commit();
  227. return finalData;
  228. } catch (err) {
  229. this.ctx.log(err);
  230. await conn.rollback();
  231. this.db.update(this.ctx.service.budgetFinalList.tableName, { id: final.id, status: 4});
  232. throw '保存决算数据错误';
  233. }
  234. }
  235. }
  236. return Budget;
  237. };