change_plan_list.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date 2018/8/14
  7. * @version
  8. */
  9. const audit = require('../const/audit');
  10. module.exports = app => {
  11. class ChangePlanList extends app.BaseService {
  12. /**
  13. * 构造函数
  14. *
  15. * @param {Object} ctx - egg全局变量
  16. * @return {void}
  17. */
  18. constructor(ctx) {
  19. super(ctx);
  20. this.tableName = 'change_plan_list';
  21. }
  22. /**
  23. * 取出变更令清单列表,并按台账清单在前,空白清单在后排序
  24. * @return {void}
  25. */
  26. async getList(cpid) {
  27. const sql = 'SELECT * FROM ?? WHERE `cpid` = ? ORDER BY `id` asc';
  28. const sqlParam = [this.tableName, cpid];
  29. return await this.db.query(sql, sqlParam);
  30. }
  31. /**
  32. * 添加空白变更清单
  33. * @return {void}
  34. */
  35. async add(data) {
  36. if (!this.ctx.tender || !this.ctx.change) {
  37. throw '数据错误';
  38. }
  39. const insertData = {
  40. tid: this.ctx.tender.id,
  41. cpid: this.ctx.change.id,
  42. };
  43. const newData = this._.assign(insertData, data);
  44. // 新增工料
  45. const result = await this.db.insert(this.tableName, newData);
  46. if (result.affectedRows === 0) {
  47. throw '新增空白清单数据失败';
  48. }
  49. return await this.getDataById(result.insertId);
  50. }
  51. /**
  52. * 批量添加空白变更清单
  53. * @return {void}
  54. */
  55. async batchAdd(data) {
  56. if (!this.ctx.tender || !this.ctx.change) {
  57. throw '数据错误';
  58. }
  59. const num = data.num ? parseInt(data.num) : 0;
  60. if (num < 1 || num > 100) {
  61. throw '批量添加的空白清单数目不能小于1或大于100';
  62. }
  63. const insertArray = [];
  64. for (let i = 0; i < num; i++) {
  65. const insertData = {
  66. tid: this.ctx.tender.id,
  67. cpid: this.ctx.change.id,
  68. };
  69. insertArray.push(insertData);
  70. }
  71. // 新增工料
  72. const result = await this.db.insert(this.tableName, insertArray);
  73. if (result.affectedRows !== num) {
  74. throw '批量添加空白清单数据失败';
  75. }
  76. // 获取刚批量添加的所有list
  77. for (let j = 0; j < num; j++) {
  78. insertArray[j].id = result.insertId + j;
  79. }
  80. return insertArray;
  81. }
  82. /**
  83. * 删除变更清单
  84. * @param {int} id 清单id
  85. * @return {void}
  86. */
  87. async del(id) {
  88. if (!this.ctx.tender || !this.ctx.change) {
  89. throw '数据错误';
  90. }
  91. const transaction = await this.db.beginTransaction();
  92. try {
  93. // 判断是否可删
  94. await transaction.delete(this.tableName, { id });
  95. // 重新算变更令总额
  96. await this.calcCamountSum(transaction);
  97. await transaction.commit();
  98. return true;
  99. } catch (err) {
  100. await transaction.rollback();
  101. throw err;
  102. }
  103. }
  104. /**
  105. * 修改变更清单
  106. * @param {Object} data 工料内容
  107. * @param {int} order 期数
  108. * @return {void}
  109. */
  110. async save(data, order) {
  111. if (!this.ctx.tender || !this.ctx.change) {
  112. throw '数据错误';
  113. }
  114. const transaction = await this.db.beginTransaction();
  115. try {
  116. // const mb_id = data.mb_id;
  117. // delete data.mb_id;
  118. await transaction.update(this.tableName, data);
  119. // await this.calcQuantityByML(transaction, mb_id);
  120. await this.calcCamountSum(transaction);
  121. await transaction.commit();
  122. return true;
  123. } catch (err) {
  124. await transaction.rollback();
  125. throw err;
  126. }
  127. }
  128. /**
  129. * 修改变更清单 复制粘贴
  130. * @param {Object} datas 修改内容
  131. * @return {void}
  132. */
  133. async saveDatas(datas) {
  134. if (!this.ctx.tender || !this.ctx.change) {
  135. throw '数据错误';
  136. }
  137. // 判断是否可修改
  138. // 判断t_type是否为费用
  139. const transaction = await this.db.beginTransaction();
  140. try {
  141. // for (const data of datas) {
  142. // const mb_id = data.mb_id;
  143. // delete data.mb_id;
  144. // await transaction.update(this.tableName, data);
  145. // await this.calcQuantityByML(transaction, mb_id);
  146. // }
  147. await transaction.updateRows(this.tableName, datas);
  148. await this.calcCamountSum(transaction);
  149. await transaction.commit();
  150. return true;
  151. } catch (err) {
  152. await transaction.rollback();
  153. throw err;
  154. }
  155. }
  156. async calcCamountSum(transaction, updateTpDecimal = false) {
  157. // const sql = 'SELECT SUM(ROUND(`camount`*`unit_price`, )) as total_price FROM ?? WHERE cid = ?';
  158. // const sqlParam = [this.tableName, this.change.cid];
  159. // const tp = await transaction.queryOne(sql, sqlParam);
  160. // 防止小数位不精确,采用取值计算
  161. const sql = 'SELECT unit_price, spamount FROM ?? WHERE cpid = ?';
  162. const sqlParam = [this.tableName, this.ctx.change.id];
  163. const changeList = await transaction.query(sql, sqlParam);
  164. let total_price = 0;
  165. const tp_decimal = this.ctx.tender.info.decimal.tp;
  166. for (const cl of changeList) {
  167. total_price = this.ctx.helper.accAdd(total_price, this.ctx.helper.mul(cl.unit_price, cl.spamount, tp_decimal));
  168. }
  169. const updateData = {
  170. total_price,
  171. };
  172. // if (updateTpDecimal) {
  173. // updateData.tp_decimal = tp_decimal;
  174. // }
  175. const options = {
  176. where: {
  177. id: this.ctx.change.id,
  178. },
  179. };
  180. await transaction.update(this.ctx.service.changePlan.tableName, updateData, options);
  181. }
  182. /**
  183. * 用户数据数量提交
  184. * @param {Object} data 内容
  185. * @return {void}
  186. */
  187. async saveAmountData(data) {
  188. if (!this.ctx.tender || !this.ctx.change) {
  189. throw '数据错误';
  190. }
  191. // 判断是否可修改
  192. // 判断t_type是否为费用
  193. const transaction = await this.db.beginTransaction();
  194. try {
  195. await transaction.update(this.tableName, data);
  196. await this.calcCamountSum(transaction);
  197. await transaction.commit();
  198. return true;
  199. } catch (err) {
  200. await transaction.rollback();
  201. throw err;
  202. }
  203. }
  204. }
  205. return ChangePlanList;
  206. };