budget.js 13 KB

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