payment_tender_rpt.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 typeValues = [];
  20. for (const m in this.ctx.payment.project_setting) {
  21. if (this.ctx.payment.project_setting[m].checked) {
  22. typeValues.push(this.ctx.payment.project_setting[m].value);
  23. }
  24. }
  25. if (typeValues.length === 0) {
  26. return -1;
  27. }
  28. const sql = 'SELECT * FROM ?? WHERE `tender_id` = ? AND `type` IN (' + this.ctx.helper.getInArrStrSqlFilter(typeValues) + ')' +
  29. ' AND (`uid` = ?' +
  30. ' OR `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` = ?)' +
  31. ' OR `id` in (SELECT pra.`tr_id` FROM ?? as pra LEFT JOIN ?? as pd ON pra.`tr_id` = pd.`tr_id` WHERE pd.`status` != ' + auditConst.status.uncheck + ' AND pd.`status` != ' + auditConst.status.checkNo + ' AND pra.`uid` = ?))' +
  32. ' ORDER BY FIELD(`type`, 1, 0), id asc';
  33. const params = [this.tableName, tid, uid, this.ctx.service.paymentDetailAudit.tableName, this.ctx.service.paymentDetail.tableName, uid,
  34. this.ctx.service.paymentRptAudit.tableName, this.ctx.service.paymentDetail.tableName, uid];
  35. return await this.db.query(sql, params);
  36. }
  37. async getProcessList(id) {
  38. const typeValues = [];
  39. for (const m in this.ctx.payment.project_setting) {
  40. if (this.ctx.payment.project_setting[m].checked) {
  41. typeValues.push(this.ctx.payment.project_setting[m].value);
  42. }
  43. }
  44. if (typeValues.length === 0) {
  45. return -1;
  46. }
  47. const sql = 'SELECT ptr.*, pa.name as user_name FROM ?? as ptr LEFT JOIN ?? as pa ON ptr.`create_uid` = pa.`id` WHERE ptr.`tender_id` = ? AND ptr.`type` IN (' + this.ctx.helper.getInArrStrSqlFilter(typeValues) + ')';
  48. const params = [this.tableName, this.ctx.service.projectAccount.tableName, id];
  49. return await this.db.query(sql, params);
  50. }
  51. async checkAndUpdateList(tenderRptList, rptProjectList, formProcess = false) {
  52. if (tenderRptList.length > 0) {
  53. const transaction = await this.db.beginTransaction();
  54. try {
  55. const updateDatas = [];
  56. const delDatas = [];
  57. const delRptIds = [];
  58. const noConstRptList = this._.filter(tenderRptList, { type: 0 });
  59. for (const tr of noConstRptList) {
  60. const rptInfo = this._.find(rptProjectList, { ID: tr.rpt_id });
  61. // 判断是否已经新建过报表次
  62. const had_rpt = await this.ctx.service.paymentDetail.hadDetail(tr.id);
  63. if (tr.is_del === 0 && !rptInfo) {
  64. if (had_rpt) {
  65. updateDatas.push({
  66. id: tr.id,
  67. is_del: 1,
  68. });
  69. tr.is_del = 1;
  70. } else {
  71. delDatas.push(tr.id);
  72. }
  73. delRptIds.push(tr.id);
  74. } else if (rptInfo && tr.rpt_name !== rptInfo.name) {
  75. updateDatas.push({
  76. id: tr.id,
  77. rpt_name: rptInfo.name,
  78. });
  79. tr.rpt_name = rptInfo.name;
  80. }
  81. if (rptInfo) rptInfo.had_rpt = had_rpt;
  82. }
  83. if (updateDatas.length > 0) await transaction.updateRows(this.tableName, updateDatas);
  84. if (delDatas.length > 0) {
  85. this._.remove(tenderRptList, function(item) {
  86. return delDatas.indexOf(item.id) !== -1;
  87. });
  88. await transaction.delete(this.tableName, { id: delDatas });
  89. }
  90. if (delRptIds.length > 0) await this.ctx.service.paymentShenpiAudit.delDataFromtrids(transaction, delRptIds);
  91. await transaction.commit();
  92. } catch (err) {
  93. await transaction.rollback();
  94. throw err;
  95. }
  96. }
  97. return formProcess ? [tenderRptList, rptProjectList] : tenderRptList;
  98. }
  99. async setRpt(tid, rpt_list) {
  100. const transaction = await this.db.beginTransaction();
  101. try {
  102. const originList = await this.getAllDataByCondition({ where: { tender_id: tid, is_del: 0, type: 0 } });
  103. const insertData = [];
  104. let deleteData = [];
  105. if (originList.length === 0 && rpt_list.length !== 0) {
  106. // 添加到tender_rpt
  107. for (const rpt of rpt_list) {
  108. insertData.push({
  109. tender_id: tid,
  110. rpt_id: rpt.id,
  111. rpt_name: rpt.name,
  112. create_uid: this.ctx.session.sessionUser.accountId,
  113. in_time: new Date(),
  114. });
  115. }
  116. } else if (originList.length !== 0 && rpt_list.length === 0) {
  117. // 删除原有
  118. deleteData = this._.map(originList, 'id');
  119. } else if (originList.length !== 0 && rpt_list.length !== 0) {
  120. const orginRptIds = this._.map(originList, 'rpt_id');
  121. const newIds = this._.map(rpt_list, 'id');
  122. console.log(orginRptIds, newIds);
  123. const insertRptIds = this._.difference(newIds, orginRptIds);
  124. const deleteRptIds = this._.difference(orginRptIds, newIds);
  125. if (deleteRptIds.length > 0) {
  126. for (const id of deleteRptIds) {
  127. const orginInfo = this._.find(originList, { rpt_id: id });
  128. deleteData.push(orginInfo.id);
  129. }
  130. }
  131. for (const id of insertRptIds) {
  132. const info = this._.find(rpt_list, { id });
  133. insertData.push({
  134. tender_id: tid,
  135. rpt_id: id,
  136. rpt_name: info.name,
  137. create_uid: this.ctx.session.sessionUser.accountId,
  138. in_time: new Date(),
  139. });
  140. }
  141. }
  142. if (insertData.length > 0) await transaction.insert(this.tableName, insertData);
  143. if (deleteData.length > 0) {
  144. await transaction.delete(this.tableName, { id: deleteData });
  145. // 也要删除对应的审批流数据
  146. await this.ctx.service.paymentShenpiAudit.delDataFromtrids(transaction, deleteData);
  147. }
  148. await transaction.commit();
  149. let tenderRptList = await this.getProcessList(tid);
  150. tenderRptList = this._.filter(tenderRptList, { type: 0, is_del: 0 });
  151. return tenderRptList;
  152. } catch (err) {
  153. await transaction.rollback();
  154. throw err;
  155. }
  156. }
  157. async setStatus(id, sp_status) {
  158. const transaction = await this.db.beginTransaction();
  159. try {
  160. await transaction.update(this.tableName, { id, sp_status });
  161. await transaction.commit();
  162. return await this.ctx.service.paymentShenpiAudit.getShenpiAudit(id, sp_status);
  163. } catch (err) {
  164. await transaction.rollback();
  165. throw err;
  166. }
  167. }
  168. async setConstRpt(transaction, tid, uid) {
  169. const insertData = [];
  170. for (const rpt of paymentConst.const_rpt_list) {
  171. insertData.push({
  172. tender_id: tid,
  173. create_uid: uid,
  174. rpt_id: rpt.rpt_id,
  175. rpt_name: rpt.rpt_name,
  176. type: 1,
  177. in_time: new Date(),
  178. });
  179. }
  180. if (insertData.length > 0) await transaction.insert(this.tableName, insertData);
  181. }
  182. async updateRptAudit(trInfo, rpt_audit) {
  183. const transaction = await this.db.beginTransaction();
  184. try {
  185. // 判断是否存在null
  186. if (!trInfo.is_first && this._.findIndex(rpt_audit, { uid: null }) !== -1) {
  187. throw '请绑定所有表单角色再提交';
  188. }
  189. // 判断是否存在待上报或者退回的详情,有则同步更新
  190. const detailList = await this.ctx.service.paymentDetail.getAllDataByCondition({ where: { tr_id: trInfo.id }, orders: [['id', 'desc']] });
  191. if (!trInfo.is_change && detailList.length > 0 && (detailList[0].status === auditConst.status.uncheck || detailList[0].status === auditConst.status.checkNo)) {
  192. if (this._.findIndex(rpt_audit, { uid: null }) !== -1) {
  193. throw '未配置好表单角色';
  194. }
  195. await this.ctx.service.paymentRptAudit.updateAllAuditList(transaction, detailList[0].id, rpt_audit);
  196. let report_json = JSON.parse(detailList[0].report_json);
  197. report_json = await this.ctx.service.paymentDetail.clearAllSignatureData(report_json);
  198. await transaction.update(this.ctx.service.paymentDetail.tableName, {
  199. id: detailList[0].id,
  200. report_json: JSON.stringify(report_json),
  201. });
  202. }
  203. const is_first = this._.findIndex(rpt_audit, { uid: null }) === -1 ? 0 : 1;
  204. await transaction.update(this.tableName, { id: trInfo.id, rpt_audit: JSON.stringify(rpt_audit), is_first });
  205. await transaction.commit();
  206. return { is_first };
  207. } catch (err) {
  208. await transaction.rollback();
  209. throw err;
  210. }
  211. }
  212. async updateReport(data) {
  213. const transaction = await this.db.beginTransaction();
  214. try {
  215. const tr_id = data.tr_id;
  216. if (!tr_id) throw '参数有误';
  217. const trInfo = await this.getDataById(tr_id);
  218. if (!trInfo) throw '表单不存在';
  219. const userInfo = await this.ctx.service.projectAccount.getDataById(data.uid);
  220. if (!userInfo) throw '用户不存在';
  221. const updateData = {
  222. id: trInfo.id,
  223. uid: data.uid,
  224. };
  225. await transaction.update(this.tableName, updateData);
  226. // 判断是否存在待上报或重新上报的表单详情,有则更改原报人
  227. const result1 = await this.ctx.service.paymentDetail.updateReport(transaction, tr_id, data.uid);
  228. // 判断固定审批流或固定终审是否存在上报人,有则移除
  229. const result2 = await this.ctx.service.paymentShenpiAudit.removeAuditByReport(transaction, tr_id, data.uid);
  230. await this.ctx.service.subProjPermission.savePaymentPermissionAudits(this.ctx.subProject.id, [data.uid], 'add', transaction);
  231. await transaction.commit();
  232. let tenderRptList = await this.getProcessList(this.ctx.paymentTender.id);
  233. tenderRptList = this._.filter(tenderRptList, { type: 0, is_del: 0 });
  234. return { list: tenderRptList, updateRows: result2.affectedRows, updateData };
  235. } catch (err) {
  236. await transaction.rollback();
  237. throw err;
  238. }
  239. }
  240. }
  241. return paymentTenderRpt;
  242. };