budget.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. if (result) {
  71. result.decimal = result.decimal ? JSON.parse(result.decimal) : {};
  72. this.ctx.helper._.defaults(result.decimal, defaultDecimal);
  73. }
  74. return result;
  75. }
  76. /**
  77. * 新增标段
  78. *
  79. * @param {Object} data - 提交的数据
  80. * @return {Boolean} - 返回新增结果
  81. */
  82. async add(transaction, data, budgetStd) {
  83. if (!transaction) throw '数据错误';
  84. data.in_time = new Date();
  85. data.std_id = budgetStd.id;
  86. const operate = await transaction.insert(this.tableName, data);
  87. if (operate.insertId === 0) throw '初始化动态投资数据失败';
  88. // 获取合同支付模板 并添加到标段
  89. await this.ctx.service.budgetGu.initByTemplate(transaction, operate.insertId, budgetStd.gu_template_id);
  90. await this.ctx.service.budgetGai.initByTemplate(transaction, operate.insertId, budgetStd.gai_template_id);
  91. await this.ctx.service.budgetYu.initByTemplate(transaction, operate.insertId, budgetStd.yu_template_id);
  92. await this.ctx.service.budgetZb.initByTemplate(transaction, operate.insertId, budgetStd.zb_template_id);
  93. return operate.insertId;
  94. }
  95. /**
  96. * 保存标段
  97. *
  98. * @param {Number} id
  99. * @param {Object} postData - 表单post过来的数
  100. * @return {Boolean} - 返回执行结果
  101. */
  102. async save(data) {
  103. const result = await this.db.update(this.tableName, data);
  104. return result.affectedRows > 0;
  105. }
  106. /**
  107. * 假删除
  108. *
  109. * @param {Number} id - 删除的id
  110. * @return {Boolean} - 删除结果
  111. */
  112. async deleteBudget(id) {
  113. const updateData = { id, status: this.status.DISABLE };
  114. const result = await this.db.update(this.tableName, updateData);
  115. return result.affectedRows > 0;
  116. }
  117. /**
  118. * 真删除
  119. * @param {Number} id - 删除的标段id
  120. * @return {Promise<boolean>} - 结果
  121. */
  122. async deleteBudgetNoBackup(id) {
  123. const transaction = await this.db.beginTransaction();
  124. try {
  125. await transaction.delete(this.tableName, { id });
  126. await transaction.delete(this.ctx.service.budgetGu.tableName, { bid: id });
  127. await transaction.delete(this.ctx.service.budgetGai.tableName, { bid: id });
  128. await transaction.delete(this.ctx.service.budgetYu.tableName, { bid: id });
  129. await transaction.delete(this.ctx.service.budgetZb.tableName, { bid: id });
  130. await transaction.commit();
  131. return true;
  132. } catch (err) {
  133. this.ctx.log(err);
  134. await transaction.rollback();
  135. return false;
  136. }
  137. }
  138. async _getGuUpdateData(newDecimal, orgDecimal) {
  139. if (newDecimal.qty >= orgDecimal.qty && newDecimal.tp >= orgDecimal.tp) return [];
  140. const datas = await this.ctx.service.budgetGu.getData(this.ctx.budget.id);
  141. const result = [];
  142. for (const d of datas) {
  143. const dgn_qty1 = this.ctx.helper.round(d.dgn_qty1, newDecimal.qty);
  144. const dgn_qty2 = this.ctx.helper.round(d.dgn_qty2, newDecimal.qty);
  145. const total_price = d.is_leaf ? this.ctx.helper.round(d.total_price, newDecimal.tp) : 0;
  146. if (dgn_qty1 !== d.dgn_qty1 || dgn_qty2 !== d.dgn_qty2 || total_price !== d.total_price) {
  147. result.push({ id: d.id, tree_id: d.tree_id, dgn_qty1, dgn_qty2, total_price });
  148. }
  149. }
  150. return result;
  151. }
  152. async _getGaiUpdateData(newDecimal, orgDecimal) {
  153. if (newDecimal.qty >= orgDecimal.qty && newDecimal.tp >= orgDecimal.tp) return [];
  154. const datas = await this.ctx.service.budgetGai.getData(this.ctx.budget.id);
  155. const result = [];
  156. for (const d of datas) {
  157. const dgn_qty1 = this.ctx.helper.round(d.dgn_qty1, newDecimal.qty);
  158. const dgn_qty2 = this.ctx.helper.round(d.dgn_qty2, newDecimal.qty);
  159. const total_price = d.is_leaf ? this.ctx.helper.round(d.total_price, newDecimal.tp) : 0;
  160. if (dgn_qty1 !== d.dgn_qty1 || dgn_qty2 !== d.dgn_qty2 || total_price !== d.total_price) {
  161. result.push({ id: d.id, tree_id: d.tree_id, dgn_qty1, dgn_qty2, total_price });
  162. }
  163. }
  164. return result;
  165. }
  166. async _getYuUpdateData(newDecimal, orgDecimal) {
  167. if (newDecimal.qty >= orgDecimal.qty && newDecimal.up >= orgDecimal.up && newDecimal.tp === orgDecimal.tp) return [];
  168. const datas = await this.ctx.service.budgetYu.getData(this.ctx.budget.id);
  169. const result = [];
  170. for (const d of datas) {
  171. if (d.b_code) {
  172. if (!d.is_leaf) continue;
  173. const quantity = this.ctx.helper.round(d.quantity, newDecimal.qty);
  174. const unit_price = this.ctx.helper.round(d.unit_price, newDecimal.up);
  175. const total_price = this.ctx.helper.mul(unit_price, quantity, newDecimal.tp);
  176. if (quantity !== d.quantity || unit_price !== d.unit_price || total_price !== d.total_price) {
  177. result.push({ id: d.id, tree_id: d.tree_id, quantity, unit_price, total_price });
  178. }
  179. } else {
  180. const dgn_qty1 = this.ctx.helper.round(d.dgn_qty1, newDecimal.qty);
  181. const dgn_qty2 = this.ctx.helper.round(d.dgn_qty2, newDecimal.qty);
  182. const total_price = d.is_leaf ? this.ctx.helper.round(d.total_price, newDecimal.tp) : 0;
  183. if (dgn_qty1 !== d.dgn_qty1 || dgn_qty2 !== d.dgn_qty2 || total_price !== d.total_price) {
  184. result.push({ id: d.id, tree_id: d.tree_id, dgn_qty1, dgn_qty2, total_price });
  185. }
  186. }
  187. }
  188. return result;
  189. }
  190. async _getZbUpdateData(newDecimal, orgDecimal) {
  191. if (newDecimal.qty >= orgDecimal.qty && newDecimal.up >= orgDecimal.up && newDecimal.tp === orgDecimal.tp) return [];
  192. const datas = await this.ctx.service.budgetZb.getData(this.ctx.budget.id);
  193. const result = [];
  194. for (const d of datas) {
  195. if (d.b_code) {
  196. if (!d.is_leaf) continue;
  197. const quantity = this.ctx.helper.round(d.quantity, newDecimal.qty);
  198. const unit_price = this.ctx.helper.round(d.unit_price, newDecimal.up);
  199. const total_price = this.ctx.helper.mul(unit_price, quantity, newDecimal.tp);
  200. if (quantity !== d.quantity || unit_price !== d.unit_price || total_price !== d.total_price) {
  201. result.push({ id: d.id, tree_id: d.tree_id, quantity, unit_price, total_price });
  202. }
  203. } else {
  204. const dgn_qty1 = this.ctx.helper.round(d.dgn_qty1, newDecimal.qty);
  205. const dgn_qty2 = this.ctx.helper.round(d.dgn_qty2, newDecimal.qty);
  206. const total_price = d.is_leaf ? this.ctx.helper.round(d.total_price, newDecimal.tp) : 0;
  207. if (dgn_qty1 !== d.dgn_qty1 || dgn_qty2 !== d.dgn_qty2 || total_price !== d.total_price) {
  208. result.push({ id: d.id, tree_id: d.tree_id, dgn_qty1, dgn_qty2, total_price });
  209. }
  210. }
  211. }
  212. return result;
  213. }
  214. async saveDecimal(decimal, page) {
  215. const newDecimal = JSON.parse(JSON.stringify(this.ctx.budget.decimal));
  216. if (decimal.qty >= 0 && decimal.qty <= 6) newDecimal.qty = decimal.qty;
  217. if (decimal.up >= 0 && decimal.up <= 6) newDecimal.up = decimal.up;
  218. if (decimal.tp >= 0 && decimal.tp <= 6) newDecimal.tp = decimal.tp;
  219. const guDatas = await this._getGuUpdateData(newDecimal, this.ctx.budget.decimal);
  220. const gaiDatas = await this._getGaiUpdateData(newDecimal, this.ctx.budget.decimal);
  221. const yuDatas = await this._getYuUpdateData(newDecimal, this.ctx.budget.decimal);
  222. const zbDatas = await this._getZbUpdateData(newDecimal, this.ctx.budget.decimal);
  223. const conn = await this.db.beginTransaction();
  224. try {
  225. const result = await conn.update(this.tableName, { id: this.ctx.budget.id, decimal: JSON.stringify(newDecimal) });
  226. if (guDatas.length > 0) await conn.updateRows(this.ctx.service.budgetGu.tableName, guDatas);
  227. if (gaiDatas.length > 0) await conn.updateRows(this.ctx.service.budgetGai.tableName, gaiDatas);
  228. if (yuDatas.length > 0) await conn.updateRows(this.ctx.service.budgetYu.tableName, yuDatas);
  229. if (zbDatas.length > 0) await conn.updateRows(this.ctx.service.budgetZb.tableName, zbDatas);
  230. await conn.commit();
  231. switch (page) {
  232. case 'gu': return { update: guDatas };
  233. case 'gai': return { update: gaiDatas };
  234. case 'yu': return { update: yuDatas };
  235. case 'zb': return { update: zbDatas };
  236. }
  237. } catch (err) {
  238. await conn.rollback();
  239. throw err;
  240. }
  241. }
  242. async doFinal(budget, final) {
  243. const finalObj = new FinalObj(this.ctx);
  244. let finalData;
  245. try {
  246. finalData = await finalObj.doFinal(budget, final);
  247. } catch (err) {
  248. this.db.update(this.ctx.service.budgetFinalList.tableName, { id: final.id, status: 4});
  249. this.ctx.log(err);
  250. throw '生成决算数据错误';
  251. }
  252. const conn = await this.db.beginTransaction();
  253. try {
  254. await conn.update(this.tableName, {id: budget.id, final_id: final.id, final_time: new Date() });
  255. await conn.insert(this.ctx.service.budgetFinal.tableName, finalData);
  256. await conn.update(this.ctx.service.budgetFinalList.tableName, { id: final.id, tender_info: JSON.stringify(final.tender_info), status: 3});
  257. await conn.commit();
  258. return finalData;
  259. } catch (err) {
  260. this.ctx.log(err);
  261. await conn.rollback();
  262. this.db.update(this.ctx.service.budgetFinalList.tableName, { id: final.id, status: 4});
  263. throw '保存决算数据错误';
  264. }
  265. }
  266. }
  267. return Budget;
  268. };