change_apply_list.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 ChangeApplyList 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_apply_list';
  21. }
  22. /**
  23. * 取出变更令清单列表,并按台账清单在前,空白清单在后排序
  24. * @return {void}
  25. */
  26. async getList(caid, transaction = false) {
  27. const sql = 'SELECT * FROM ?? WHERE `caid` = ? ORDER BY `id` asc';
  28. const sqlParam = [this.tableName, caid];
  29. return transaction ? await transaction.query(sql, sqlParam) : 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. caid: 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. caid: 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(ids) {
  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: ids });
  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. const insertArray = [];
  142. const updateArray = [];
  143. for (const d of datas) {
  144. if (d.id) {
  145. updateArray.push(d);
  146. } else {
  147. d.tid = this.ctx.tender.id;
  148. d.caid = this.ctx.change.id;
  149. insertArray.push(d);
  150. }
  151. }
  152. if (insertArray.length > 0) await transaction.insert(this.tableName, insertArray);
  153. if (updateArray.length > 0) await transaction.updateRows(this.tableName, updateArray);
  154. await this.calcCamountSum(transaction);
  155. await transaction.commit();
  156. return true;
  157. } catch (err) {
  158. await transaction.rollback();
  159. throw err;
  160. }
  161. }
  162. async calcCamountSum(transaction, updateTpDecimal = false) {
  163. // const sql = 'SELECT SUM(ROUND(`camount`*`unit_price`, )) as total_price FROM ?? WHERE cid = ?';
  164. // const sqlParam = [this.tableName, this.change.cid];
  165. // const tp = await transaction.queryOne(sql, sqlParam);
  166. // 防止小数位不精确,采用取值计算
  167. const sql = 'SELECT unit_price, camount FROM ?? WHERE caid = ?';
  168. const sqlParam = [this.tableName, this.ctx.change.id];
  169. const changeList = await transaction.query(sql, sqlParam);
  170. let total_price = 0;
  171. const tp_decimal = this.ctx.change.decimal.tp;
  172. for (const cl of changeList) {
  173. total_price = this.ctx.helper.accAdd(total_price, this.ctx.helper.mul(cl.unit_price, cl.camount, tp_decimal));
  174. }
  175. const updateData = {
  176. total_price,
  177. };
  178. // if (updateTpDecimal) {
  179. // updateData.tp_decimal = tp_decimal;
  180. // }
  181. const options = {
  182. where: {
  183. id: this.ctx.change.id,
  184. },
  185. };
  186. await transaction.update(this.ctx.service.changeApply.tableName, updateData, options);
  187. }
  188. /**
  189. * 报表用
  190. * Tony Kang
  191. * @param {tid} tid - 标段id
  192. * @return {void}
  193. */
  194. async getChangeBills(tid, onlyChecked) {
  195. const sql = 'SELECT cb.*' +
  196. ' FROM ' + this.tableName + ' cb' +
  197. ' LEFT JOIN ' + this.ctx.service.changeApply.tableName + ' c ON cb.caid = c.id' +
  198. ' WHERE c.tid = ? ' + (onlyChecked ? `and c.status = ${audit.changeApply.status.checked}` : '');
  199. const param = [tid];
  200. const result = await this.db.query(sql, param);
  201. return result;
  202. }
  203. }
  204. return ChangeApplyList;
  205. };