budget.js 13 KB

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