payment_tender_rpt.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. 'use strict';
  2. /**
  3. * 决策大屏用户查看权限-数据模型
  4. *
  5. * @author ellisran
  6. * @date 2021/09/23
  7. * @version
  8. */
  9. const accountGroup = require('../const/account_group').group;
  10. const paymentConst = require('../const/payment');
  11. const auditConst = require('../const/audit').stage;
  12. module.exports = app => {
  13. class paymentTenderRpt extends app.BaseService {
  14. constructor(ctx) {
  15. super(ctx);
  16. this.tableName = 'payment_tender_rpt';
  17. }
  18. async getList(tid, uid) {
  19. const sql = 'SELECT * FROM ?? WHERE `tender_id` = ?' +
  20. ' AND `id` in (SELECT pda.`tr_id` FROM ?? as pda LEFT JOIN ?? as pd ON pda.`tr_id` = pd.`tr_id` WHERE pd.`status` != ' + auditConst.status.uncheck + ' AND pda.`aid` = ?)';
  21. const params = [this.tableName, tid, this.ctx.service.paymentDetailAudit.tableName, this.ctx.service.paymentDetail.tableName, uid];
  22. return await this.db.query(sql, params);
  23. }
  24. async getProcessList(id) {
  25. const sql = 'SELECT ptr.*, pa.name as user_name FROM ?? as ptr LEFT JOIN ?? as pa ON ptr.`uid` = pa.`id` WHERE ptr.`tender_id` = ? ';
  26. const params = [this.tableName, this.ctx.service.projectAccount.tableName, id];
  27. return await this.db.query(sql, params);
  28. }
  29. async checkAndUpdateList(tenderRptList, rptProjectList, formProcess = false) {
  30. if (tenderRptList.length > 0) {
  31. const transaction = await this.db.beginTransaction();
  32. try {
  33. const updateDatas = [];
  34. const delDatas = [];
  35. const delRptIds = [];
  36. const noConstRptList = this._.filter(tenderRptList, { is_const: 0 });
  37. for (const tr of noConstRptList) {
  38. const rptInfo = this._.find(rptProjectList, { ID: tr.rpt_id });
  39. // 判断是否已经新建过报表次
  40. const had_rpt = await this.ctx.service.paymentDetail.hadDetail(tr.id);
  41. if (tr.is_del === 0 && !rptInfo) {
  42. if (had_rpt) {
  43. updateDatas.push({
  44. id: tr.id,
  45. is_del: 1,
  46. });
  47. tr.is_del = 1;
  48. } else {
  49. delDatas.push(tr.id);
  50. }
  51. delRptIds.push(tr.id);
  52. } else if (rptInfo && tr.rpt_name !== rptInfo.name) {
  53. updateDatas.push({
  54. id: tr.id,
  55. rpt_name: rptInfo.name,
  56. });
  57. tr.rpt_name = rptInfo.name;
  58. }
  59. if (rptInfo) rptInfo.had_rpt = had_rpt;
  60. }
  61. if (updateDatas.length > 0) await transaction.updateRows(this.tableName, updateDatas);
  62. if (delDatas.length > 0) {
  63. this._.remove(tenderRptList, function(item) {
  64. return delDatas.indexOf(item.id) !== -1;
  65. });
  66. await transaction.delete(this.tableName, { id: delDatas });
  67. }
  68. if (delRptIds.length > 0) await this.ctx.service.paymentShenpiAudit.delDataFromtrids(transaction, delRptIds);
  69. await transaction.commit();
  70. } catch (err) {
  71. await transaction.rollback();
  72. throw err;
  73. }
  74. }
  75. return formProcess ? [tenderRptList, rptProjectList] : tenderRptList;
  76. }
  77. async setRpt(tid, rpt_list) {
  78. const transaction = await this.db.beginTransaction();
  79. try {
  80. const originList = await this.getAllDataByCondition({ where: { tender_id: tid, is_del: 0, is_const: 0 } });
  81. const insertData = [];
  82. let deleteData = [];
  83. if (originList.length === 0 && rpt_list.length !== 0) {
  84. // 添加到tender_rpt
  85. for (const rpt of rpt_list) {
  86. insertData.push({
  87. tender_id: tid,
  88. rpt_id: rpt.id,
  89. rpt_name: rpt.name,
  90. uid: this.ctx.session.sessionUser.accountId,
  91. in_time: new Date(),
  92. });
  93. }
  94. } else if (originList.length !== 0 && rpt_list.length === 0) {
  95. // 删除原有
  96. deleteData = this._.map(originList, 'id');
  97. } else if (originList.length !== 0 && rpt_list.length !== 0) {
  98. const orginRptIds = this._.map(originList, 'rpt_id');
  99. const newIds = this._.map(rpt_list, 'id');
  100. console.log(orginRptIds, newIds);
  101. const insertRptIds = this._.difference(newIds, orginRptIds);
  102. const deleteRptIds = this._.difference(orginRptIds, newIds);
  103. if (deleteRptIds.length > 0) {
  104. for (const id of deleteRptIds) {
  105. const orginInfo = this._.find(originList, { rpt_id: id });
  106. deleteData.push(orginInfo.id);
  107. }
  108. }
  109. for (const id of insertRptIds) {
  110. const info = this._.find(rpt_list, { id });
  111. insertData.push({
  112. tender_id: tid,
  113. rpt_id: id,
  114. rpt_name: info.name,
  115. uid: this.ctx.session.sessionUser.accountId,
  116. in_time: new Date(),
  117. });
  118. }
  119. }
  120. if (insertData.length > 0) await transaction.insert(this.tableName, insertData);
  121. if (deleteData.length > 0) {
  122. await transaction.delete(this.tableName, { id: deleteData });
  123. // 也要删除对应的审批流数据
  124. await this.ctx.service.paymentShenpiAudit.delDataFromtrids(transaction, deleteData);
  125. }
  126. await transaction.commit();
  127. let tenderRptList = await this.getProcessList(tid);
  128. tenderRptList = this._.filter(tenderRptList, { is_const: 0, is_del: 0 });
  129. return tenderRptList;
  130. } catch (err) {
  131. await transaction.rollback();
  132. throw err;
  133. }
  134. }
  135. async setStatus(id, sp_status) {
  136. const transaction = await this.db.beginTransaction();
  137. try {
  138. await transaction.update(this.tableName, { id, sp_status });
  139. await transaction.commit();
  140. return await this.ctx.service.paymentShenpiAudit.getShenpiAudit(id, sp_status);
  141. } catch (err) {
  142. await transaction.rollback();
  143. throw err;
  144. }
  145. }
  146. async setConstRpt(transaction, tid, uid) {
  147. const insertData = [];
  148. for (const rpt of paymentConst.const_rpt_list) {
  149. insertData.push({
  150. tender_id: tid,
  151. uid,
  152. rpt_id: rpt.rpt_id,
  153. rpt_name: rpt.rpt_name,
  154. is_const: 1,
  155. in_time: new Date(),
  156. });
  157. }
  158. if (insertData.length > 0) await transaction.insert(this.tableName, insertData);
  159. }
  160. async updateRptAudit(trInfo, rpt_audit) {
  161. const transaction = await this.db.beginTransaction();
  162. try {
  163. // 判断是否存在null
  164. if (!trInfo.is_first && this._.findIndex(rpt_audit, { uid: null }) !== -1) {
  165. throw '请绑定所有表单角色再提交';
  166. }
  167. // 判断是否存在待上报或者退回的详情,有则同步更新
  168. const detailList = await this.ctx.service.paymentDetail.getAllDataByCondition({ where: { tr_id: trInfo.id }, orders: [['id', 'desc']] });
  169. if (!trInfo.is_change && detailList.length > 0 && (detailList[0].status === auditConst.status.uncheck || detailList[0].status === auditConst.status.checkNo)) {
  170. if (this._.findIndex(rpt_audit, { uid: null }) !== -1) {
  171. throw '未配置好表单角色';
  172. }
  173. await this.ctx.service.paymentRptAudit.updateAllAuditList(transaction, detailList[0].id, rpt_audit);
  174. let report_json = JSON.parse(detailList[0].report_json);
  175. report_json = await this.ctx.service.paymentDetail.clearAllSignatureData(report_json);
  176. await transaction.update(this.ctx.service.paymentDetail.tableName, {
  177. id: detailList[0].id,
  178. report_json: JSON.stringify(report_json),
  179. });
  180. }
  181. const is_first = this._.findIndex(rpt_audit, { uid: null }) === -1 ? 0 : 1;
  182. await transaction.update(this.tableName, { id: trInfo.id, rpt_audit: JSON.stringify(rpt_audit), is_first });
  183. await transaction.commit();
  184. return { is_first };
  185. } catch (err) {
  186. await transaction.rollback();
  187. throw err;
  188. }
  189. }
  190. }
  191. return paymentTenderRpt;
  192. };