schedule_ledger_month.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. 'use strict';
  2. module.exports = app => {
  3. class ScheduleLedgerMonth extends app.BaseService {
  4. constructor(ctx) {
  5. super(ctx);
  6. this.tableName = 'schedule_ledger_month';
  7. }
  8. async save(data) {
  9. // 判断是添加,删除,还是修改
  10. const transaction = await this.db.beginTransaction();
  11. try {
  12. const info = await this.getDataByCondition({ tid: this.ctx.tender.id, lid: data.lid, yearmonth: data.yearmonth });
  13. if (info) {
  14. if (data.plan_gcl === null && data.plan_tp === null && info.sj_gcl === null && info.sj_tp === null) {
  15. await transaction.delete(this.tableName, { id: info.id });
  16. } else {
  17. const updateData = {
  18. id: info.id,
  19. plan_gcl: data.plan_gcl,
  20. plan_tp: data.plan_tp,
  21. };
  22. await transaction.update(this.tableName, updateData);
  23. }
  24. } else {
  25. const insertData = {
  26. tid: this.ctx.tender.id,
  27. lid: data.lid,
  28. yearmonth: data.yearmonth,
  29. plan_gcl: data.plan_gcl,
  30. plan_tp: data.plan_tp,
  31. };
  32. await transaction.insert(this.tableName, insertData);
  33. }
  34. // 重新计算本月、总 计划金额和计划工程量
  35. await this.calcMonthPlan(transaction, this.ctx.tender.id, data.yearmonth);
  36. await this.ctx.service.scheduleMonth.calcPlan(transaction, this.ctx.tender.id);
  37. await transaction.commit();
  38. return true;
  39. } catch (err) {
  40. await transaction.rollback();
  41. throw err;
  42. }
  43. }
  44. async saveDatas(datas) {
  45. // 判断是添加,删除,还是修改
  46. const transaction = await this.db.beginTransaction();
  47. try {
  48. const ymArray = [];
  49. for (const data of datas) {
  50. const info = await this.getDataByCondition({ tid: this.ctx.tender.id, lid: data.lid, yearmonth: data.yearmonth });
  51. if (!this._.includes(ymArray, data.yearmonth)) {
  52. ymArray.push(data.yearmonth);
  53. }
  54. if (info) {
  55. if (data.plan_gcl === null && data.plan_tp === null && info.sj_gcl === null && info.sj_tp === null) {
  56. await transaction.delete(this.tableName, { id: info.id });
  57. } else {
  58. const updateData = {
  59. id: info.id,
  60. plan_gcl: data.plan_gcl,
  61. plan_tp: data.plan_tp,
  62. };
  63. await transaction.update(this.tableName, updateData);
  64. }
  65. } else {
  66. const insertData = {
  67. tid: this.ctx.tender.id,
  68. lid: data.lid,
  69. yearmonth: data.yearmonth,
  70. plan_gcl: data.plan_gcl,
  71. plan_tp: data.plan_tp,
  72. };
  73. await transaction.insert(this.tableName, insertData);
  74. }
  75. }
  76. // 重新计算本月、总 计划金额和计划工程量
  77. for (const ym of ymArray) {
  78. await this.calcMonthPlan(transaction, this.ctx.tender.id, ym);
  79. }
  80. await this.ctx.service.scheduleMonth.calcPlan(transaction, this.ctx.tender.id);
  81. await transaction.commit();
  82. return true;
  83. } catch (err) {
  84. await transaction.rollback();
  85. throw err;
  86. }
  87. }
  88. async saveSj(data) {
  89. // 判断是添加,删除,还是修改
  90. const transaction = await this.db.beginTransaction();
  91. try {
  92. const info = await this.getDataByCondition({ tid: this.ctx.tender.id, lid: data.lid, yearmonth: data.yearmonth });
  93. if (info) {
  94. if (data.sj_gcl === null && data.sj_tp === null && info.plan_gcl === null && info.plan_tp === null) {
  95. await transaction.delete(this.tableName, { id: info.id });
  96. } else {
  97. const updateData = {
  98. id: info.id,
  99. sj_gcl: data.sj_gcl,
  100. sj_tp: data.sj_tp,
  101. };
  102. await transaction.update(this.tableName, updateData);
  103. }
  104. } else {
  105. const insertData = {
  106. tid: this.ctx.tender.id,
  107. lid: data.lid,
  108. yearmonth: data.yearmonth,
  109. sj_gcl: data.sj_gcl,
  110. sj_tp: data.sj_tp,
  111. };
  112. await transaction.insert(this.tableName, insertData);
  113. }
  114. // 重新计算本月、总 计划金额和计划工程量
  115. await this.calcMonthSj(transaction, this.ctx.tender.id, data.yearmonth);
  116. await this.ctx.service.scheduleMonth.calcSj(transaction, this.ctx.tender.id);
  117. await transaction.commit();
  118. return true;
  119. } catch (err) {
  120. await transaction.rollback();
  121. throw err;
  122. }
  123. }
  124. async saveSjDatas(datas) {
  125. // 判断是添加,删除,还是修改
  126. const transaction = await this.db.beginTransaction();
  127. try {
  128. const ymArray = [];
  129. for (const data of datas) {
  130. const info = await this.getDataByCondition({ tid: this.ctx.tender.id, lid: data.lid, yearmonth: data.yearmonth });
  131. if (!this._.includes(ymArray, data.yearmonth)) {
  132. ymArray.push(data.yearmonth);
  133. }
  134. if (info) {
  135. if (data.sj_gcl === null && data.sj_tp === null && info.plan_gcl === null && info.plan_tp === null) {
  136. await transaction.delete(this.tableName, { id: info.id });
  137. } else {
  138. const updateData = {
  139. id: info.id,
  140. sj_gcl: data.sj_gcl,
  141. sj_tp: data.sj_tp,
  142. };
  143. await transaction.update(this.tableName, updateData);
  144. }
  145. } else {
  146. const insertData = {
  147. tid: this.ctx.tender.id,
  148. lid: data.lid,
  149. yearmonth: data.yearmonth,
  150. sj_gcl: data.sj_gcl,
  151. sj_tp: data.sj_tp,
  152. };
  153. await transaction.insert(this.tableName, insertData);
  154. }
  155. }
  156. // 重新计算本月、总 计划金额和计划工程量
  157. for (const ym of ymArray) {
  158. await this.calcMonthSj(transaction, this.ctx.tender.id, ym);
  159. }
  160. await this.ctx.service.scheduleMonth.calcSj(transaction, this.ctx.tender.id);
  161. await transaction.commit();
  162. return true;
  163. } catch (err) {
  164. await transaction.rollback();
  165. throw err;
  166. }
  167. }
  168. async calcMonthSj(transaction, tid, yearmonth) {
  169. const sql = 'SELECT SUM(`sj_gcl`) as total_sj_gcl, SUM(`sj_tp`) as total_sj_tp FROM ?? WHERE tid = ? and yearmonth = ?';
  170. const sqlParam = [this.tableName, tid, yearmonth];
  171. const result = await transaction.queryOne(sql, sqlParam);
  172. const updateData = {
  173. sj_gcl: result.total_sj_gcl,
  174. sj_tp: result.total_sj_tp,
  175. };
  176. const option = {
  177. where: {
  178. tid,
  179. yearmonth,
  180. },
  181. };
  182. return await transaction.update(this.ctx.service.scheduleMonth.tableName, updateData, option);
  183. }
  184. async calcMonthPlan(transaction, tid, yearmonth) {
  185. const sql = 'SELECT SUM(`plan_gcl`) as total_plan_gcl, SUM(`plan_tp`) as total_plan_tp FROM ?? WHERE tid = ? and yearmonth = ?';
  186. const sqlParam = [this.tableName, tid, yearmonth];
  187. const result = await transaction.queryOne(sql, sqlParam);
  188. const updateData = {
  189. plan_gcl: result.total_plan_gcl,
  190. plan_tp: result.total_plan_tp,
  191. };
  192. const option = {
  193. where: {
  194. tid,
  195. yearmonth,
  196. },
  197. };
  198. return await transaction.update(this.ctx.service.scheduleMonth.tableName, updateData, option);
  199. }
  200. async getConllectionList(tid, yearmonthArray) {
  201. const sql = 'SELECT tid, lid, SUM(`plan_gcl`) as plan_gcl, SUM(`plan_tp`) as plan_tp, SUM(`sj_gcl`) as sj_gcl, SUM(`sj_tp`) as sj_tp' +
  202. ' FROM ?? WHERE `tid` = ? AND `yearmonth` in (?) GROUP BY `lid`';
  203. const sqlParam = [this.tableName, tid, yearmonthArray];
  204. const result = await this.db.query(sql, sqlParam);
  205. return result;
  206. }
  207. // async getHadDataList(tid) {
  208. // const sql = 'SELECT lid FROM ?? WHERE tid = ? '
  209. // }
  210. }
  211. return ScheduleLedgerMonth;
  212. };