budget.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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.budgetPermission.PermissionConst;
  58. const permissionBudget = await this.ctx.service.budgetPermission.getUserPermission();
  59. result = result.filter(x => {
  60. const pb = permissionBudget.find(y => { return x.id === y.bid});
  61. if (pb) {
  62. x.canEdit = pb.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(data) {
  81. const budgetStd = await this.ctx.service.budgetStd.getDataById(data.std_id);
  82. if (!budgetStd) throw '选择的概算标准不存在,请刷新页面重试';
  83. const conn = await this.db.beginTransaction();
  84. try {
  85. // 获取当前用户信息
  86. const sessionUser = this.ctx.session.sessionUser;
  87. // 获取当前项目信息
  88. const sessionProject = this.ctx.session.sessionProject;
  89. const insertData = {
  90. pid: sessionProject.id, user_id: sessionUser.accountId, in_time: new Date(),
  91. name: data.name, std_id: data.std_id,
  92. };
  93. const operate = await conn.insert(this.tableName, insertData);
  94. if (operate.insertId === 0) throw '新增标段数据失败';
  95. // 获取合同支付模板 并添加到标段
  96. await this.ctx.service.budgetGu.initByTemplate(conn, operate.insertId, budgetStd.gu_template_id);
  97. await this.ctx.service.budgetGai.initByTemplate(conn, operate.insertId, budgetStd.gai_template_id);
  98. await this.ctx.service.budgetYu.initByTemplate(conn, operate.insertId, budgetStd.yu_template_id);
  99. await conn.commit();
  100. return await this.getDataById(operate.insertId);
  101. } catch (error) {
  102. await conn.rollback();
  103. throw error;
  104. }
  105. }
  106. /**
  107. * 保存标段
  108. *
  109. * @param {Number} id
  110. * @param {Object} postData - 表单post过来的数
  111. * @return {Boolean} - 返回执行结果
  112. */
  113. async save(data) {
  114. const result = await this.db.update(this.tableName, data);
  115. return result.affectedRows > 0;
  116. }
  117. /**
  118. * 假删除
  119. *
  120. * @param {Number} id - 删除的id
  121. * @return {Boolean} - 删除结果
  122. */
  123. async deleteBudget(id) {
  124. const updateData = { id, status: this.status.DISABLE };
  125. const result = await this.db.update(this.tableName, updateData);
  126. return result.affectedRows > 0;
  127. }
  128. /**
  129. * 真删除
  130. * @param {Number} id - 删除的标段id
  131. * @return {Promise<boolean>} - 结果
  132. */
  133. async deleteBudgetNoBackup(id) {
  134. const transaction = await this.db.beginTransaction();
  135. try {
  136. await transaction.delete(this.tableName, { id });
  137. await transaction.delete(this.ctx.service.budgetGu.tableName, { bid: id });
  138. await transaction.delete(this.ctx.service.budgetGai.tableName, { bid: id });
  139. await transaction.delete(this.ctx.service.budgetYu.tableName, { bid: id });
  140. await transaction.commit();
  141. return true;
  142. } catch (err) {
  143. this.ctx.log(err);
  144. await transaction.rollback();
  145. return false;
  146. }
  147. }
  148. async _getGuUpdateData(newDecimal, orgDecimal) {
  149. if (newDecimal.qty >= orgDecimal.qty && newDecimal.tp >= orgDecimal.tp) return [];
  150. const datas = await this.ctx.service.budgetGu.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 _getGaiUpdateData(newDecimal, orgDecimal) {
  163. if (newDecimal.qty >= orgDecimal.qty && newDecimal.tp >= orgDecimal.tp) return [];
  164. const datas = await this.ctx.service.budgetGai.getData(this.ctx.budget.id);
  165. const result = [];
  166. for (const d of datas) {
  167. const dgn_qty1 = this.ctx.helper.round(d.dgn_qty1, newDecimal.qty);
  168. const dgn_qty2 = this.ctx.helper.round(d.dgn_qty2, newDecimal.qty);
  169. const total_price = d.is_leaf ? this.ctx.helper.round(d.total_price, newDecimal.tp) : 0;
  170. if (dgn_qty1 !== d.dgn_qty1 || dgn_qty2 !== d.dgn_qty2 || total_price !== d.total_price) {
  171. result.push({ id: d.id, tree_id: d.tree_id, dgn_qty1, dgn_qty2, total_price });
  172. }
  173. }
  174. return result;
  175. }
  176. async _getYuUpdateData(newDecimal, orgDecimal) {
  177. if (newDecimal.qty >= orgDecimal.qty && newDecimal.up >= orgDecimal.up && newDecimal.tp === orgDecimal.tp) return [];
  178. const datas = await this.ctx.service.budgetYu.getData(this.ctx.budget.id);
  179. const result = [];
  180. for (const d of datas) {
  181. if (d.b_code) {
  182. if (!d.is_leaf) continue;
  183. const quantity = this.ctx.helper.round(d.quantity, newDecimal.qty);
  184. const unit_price = this.ctx.helper.round(d.unit_price, newDecimal.up);
  185. const total_price = this.ctx.helper.mul(unit_price, quantity, newDecimal.tp);
  186. if (quantity !== d.quantity || unit_price !== d.unit_price || total_price !== d.total_price) {
  187. result.push({ id: d.id, tree_id: d.tree_id, quantity, unit_price, total_price });
  188. }
  189. } else {
  190. const dgn_qty1 = this.ctx.helper.round(d.dgn_qty1, newDecimal.qty);
  191. const dgn_qty2 = this.ctx.helper.round(d.dgn_qty2, newDecimal.qty);
  192. const total_price = d.is_leaf ? this.ctx.helper.round(d.total_price, newDecimal.tp) : 0;
  193. if (dgn_qty1 !== d.dgn_qty1 || dgn_qty2 !== d.dgn_qty2 || total_price !== d.total_price) {
  194. result.push({ id: d.id, tree_id: d.tree_id, dgn_qty1, dgn_qty2, total_price });
  195. }
  196. }
  197. }
  198. return result;
  199. }
  200. async saveDecimal(decimal, page) {
  201. const newDecimal = JSON.parse(JSON.stringify(this.ctx.budget.decimal));
  202. if (decimal.qty >= 0 && decimal.qty <= 6) newDecimal.qty = decimal.qty;
  203. if (decimal.up >= 0 && decimal.up <= 6) newDecimal.up = decimal.up;
  204. if (decimal.tp >= 0 && decimal.tp <= 6) newDecimal.tp = decimal.tp;
  205. const guDatas = await this._getGuUpdateData(newDecimal, this.ctx.budget.decimal);
  206. const gaiDatas = await this._getGaiUpdateData(newDecimal, this.ctx.budget.decimal);
  207. const yuDatas = await this._getYuUpdateData(newDecimal, this.ctx.budget.decimal);
  208. const conn = await this.db.beginTransaction();
  209. try {
  210. const result = await conn.update(this.tableName, { id: this.ctx.budget.id, decimal: JSON.stringify(newDecimal) });
  211. if (guDatas.length > 0) await conn.updateRows(this.ctx.service.budgetGu.tableName, guDatas);
  212. if (gaiDatas.length > 0) await conn.updateRows(this.ctx.service.budgetGai.tableName, gaiDatas);
  213. if (yuDatas.length > 0) await conn.updateRows(this.ctx.service.budgetYu.tableName, yuDatas);
  214. await conn.commit();
  215. switch (page) {
  216. case 'gu': return { update: guDatas };
  217. case 'gai': return { update: gaiDatas };
  218. case 'yu': return { update: yuDatas };
  219. }
  220. } catch (err) {
  221. await conn.rollback();
  222. throw err;
  223. }
  224. }
  225. async doFinal(budget, final) {
  226. const finalObj = new FinalObj(this.ctx);
  227. let finalData;
  228. try {
  229. finalData = await finalObj.doFinal(budget, final);
  230. } catch (err) {
  231. this.db.update(this.ctx.service.budgetFinalList.tableName, { id: final.id, status: 4});
  232. this.ctx.log(err);
  233. throw '生成决算数据错误';
  234. }
  235. const conn = await this.db.beginTransaction();
  236. try {
  237. await conn.update(this.tableName, {id: budget.id, final_id: final.id, final_time: new Date() });
  238. await conn.insert(this.ctx.service.budgetFinal.tableName, finalData);
  239. await conn.update(this.ctx.service.budgetFinalList.tableName, { id: final.id, tender_info: JSON.stringify(final.tender_info), status: 3});
  240. await conn.commit();
  241. return finalData;
  242. } catch (err) {
  243. this.ctx.log(err);
  244. await conn.rollback();
  245. this.db.update(this.ctx.service.budgetFinalList.tableName, { id: final.id, status: 4});
  246. throw '保存决算数据错误';
  247. }
  248. }
  249. }
  250. return Budget;
  251. };