budget.js 10 KB

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