tender_cache.js 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. 'use strict';
  2. /**
  3. * xxx_flow_cur为当前流程,审批通过时为空值
  4. * xxx_flow_pre为上一流程, 未上报时 为空值/上一期终审,pre比cur多一个time值
  5. *
  6. *
  7. * @author Mai
  8. * @date
  9. * @version
  10. */
  11. const auditConst = require('../const/audit.js');
  12. const commonField = ['ledger_status', 'stage_status', 'revise_status', 'stage_count', 'stage_complete_count', 'contract_tp', 'advance_tp', 'change_tp'];
  13. module.exports = app => {
  14. class TenderCache extends app.BaseService {
  15. /**
  16. * 构造函数
  17. *
  18. * @param {Object} ctx - egg全局变量
  19. * @return {void}
  20. */
  21. constructor(ctx) {
  22. super(ctx);
  23. this.tableName = 'tender_cache';
  24. }
  25. _tranLedgerCache(tender, cache) {
  26. tender.cur_flow = (cache.ledger_status === auditConst.ledger.status.checkNo)
  27. ? JSON.parse(cache.ledger_flow_pre_info) : JSON.parse(cache.ledger_flow_cur_info || cache.ledger_flow_pre_info);
  28. tender.cur_flow.title = '台账';
  29. tender.pre_flow = cache.ledger_flow_pre_info ? JSON.parse(cache.ledger_flow_pre_info) : null;
  30. tender.stage_tp = {};
  31. tender.stage_count = 0;
  32. tender.stage_status = cache.stage_status;
  33. tender.progress = {
  34. title: '台账',
  35. status: auditConst.ledger.tiStatusString[cache.ledger_status],
  36. status_class: auditConst.ledger.tiStatusStringClass[cache.ledger_status],
  37. };
  38. }
  39. _analysisTenderCache(tender, cache, uid) {
  40. commonField.forEach(f => { tender[f] = cache[f]; });
  41. tender.ledger_tp = cache.ledger_tp ? JSON.parse(cache.ledger_tp) : {};
  42. uid = uid + '';
  43. cache.ledger_flow_cur_uid = cache.ledger_flow_cur_uid ? cache.ledger_flow_cur_uid.split(',') : [];
  44. cache.ledger_flow_pre_uid = cache.ledger_flow_pre_uid ? cache.ledger_flow_pre_uid.split(',') : [];
  45. cache.stage_flow_cur_uid = cache.stage_flow_cur_uid ? cache.stage_flow_cur_uid.split(',') : [];
  46. cache.stage_flow_pre_uid = cache.stage_flow_pre_uid ? cache.stage_flow_pre_uid.split(',') : [];
  47. if (!cache.stage_count) {
  48. this._tranLedgerCache(tender, cache);
  49. } else if (cache.stage_count === 1 && cache.stage_status === auditConst.stage.status.uncheck && cache.stage_flow_cur_uid.indexOf(uid) < 0) {
  50. this._tranLedgerCache(tender, cache);
  51. } else if (cache.stage_status === auditConst.stage.status.uncheck) {
  52. tender.stage_status = cache.stage_flow_cur_uid.indexOf(uid) >= 0 ? auditConst.stage.status.uncheck : auditConst.stage.status.checked;
  53. tender.stage_count = tender.stage_count;
  54. tender.stage_complete_count = tender.stage_complete_count;
  55. tender.cur_flow = cache.stage_flow_cur_uid.indexOf(uid) >= 0 ? JSON.parse(cache.stage_flow_cur_info || cache.stage_flow_pre_info) : JSON.parse(cache.stage_flow_pre_info);
  56. tender.pre_flow = cache.stage_flow_pre_info ? JSON.parse(cache.stage_flow_pre_info) : null;
  57. tender.stage_tp = cache.stage_flow_cur_uid.indexOf(uid) >= 0 ? JSON.parse(cache.stage_flow_cur_tp || cache.stage_flow_pre_tp) : JSON.parse(cache.stage_flow_pre_tp);
  58. try {
  59. tender.progress = {
  60. title: `第${tender.cur_flow.order === undefined ? tender.cur_flow[0].order : tender.cur_flow.order}期`,
  61. status: auditConst.stage.tiStatusString[tender.stage_status],
  62. status_class: auditConst.stage.tiStatusStringClass[tender.stage_status],
  63. };
  64. } catch (err) {
  65. tender.progress = {
  66. title: `第${tender.stage_count}期`,
  67. status: auditConst.stage.tiStatusString[tender.stage_status],
  68. status_class: auditConst.stage.tiStatusStringClass[tender.stage_status],
  69. };
  70. }
  71. } else if (cache.stage_status === auditConst.stage.status.checkNo) {
  72. tender.stage_status = auditConst.stage.status.checkNo;
  73. tender.stage_count = tender.stage_count;
  74. tender.stage_complete_count = tender.stage_complete_count;
  75. tender.cur_flow = JSON.parse(cache.stage_flow_cur_info || cache.stage_flow_pre_info);
  76. tender.pre_flow = cache.stage_flow_pre_info ? JSON.parse(cache.stage_flow_pre_info) : null;
  77. tender.stage_tp = cache.stage_flow_cur_uid.indexOf(uid) >= 0 ? JSON.parse(cache.stage_flow_cur_tp || cache.stage_flow_pre_tp) : JSON.parse(cache.stage_flow_pre_tp);
  78. try {
  79. tender.progress = {
  80. title: `第${tender.cur_flow.order === undefined ? tender.cur_flow[0].order : tender.cur_flow.order}期`,
  81. status: auditConst.stage.statusButton[tender.stage_status],
  82. status_class: auditConst.stage.tiStatusStringClass[auditConst.stage.status.uncheck],
  83. };
  84. } catch (err) {
  85. tender.progress = {
  86. title: `第${tender.stage_count}期`,
  87. status: auditConst.stage.statusButton[tender.stage_status],
  88. status_class: auditConst.stage.tiStatusStringClass[auditConst.stage.status.uncheck],
  89. };
  90. }
  91. } else if (cache.stage_status === auditConst.stage.status.checked) {
  92. tender.stage_status = auditConst.stage.status.checked;
  93. tender.stage_count = tender.stage_complete_count;
  94. tender.stage_complete_count = tender.stage_complete_count;
  95. tender.cur_flow = JSON.parse(cache.stage_flow_pre_info || cache.stage_flow_cur_info);
  96. tender.pre_flow = cache.stage_flow_pre_info ? JSON.parse(cache.stage_flow_pre_info) : null;
  97. tender.stage_tp = JSON.parse(cache.stage_flow_pre_tp);
  98. try {
  99. tender.progress = {
  100. title: `第${tender.cur_flow.order === undefined ? tender.cur_flow[0].order : tender.cur_flow.order}期`,
  101. status: auditConst.stage.tiStatusString[tender.stage_status],
  102. status_class: auditConst.stage.tiStatusStringClass[tender.stage_status],
  103. };
  104. } catch (err) {
  105. tender.progress = {
  106. title: `第${tender.stage_count}期`,
  107. status: auditConst.stage.tiStatusString[tender.stage_status],
  108. status_class: auditConst.stage.tiStatusStringClass[tender.stage_status],
  109. };
  110. }
  111. } else {
  112. tender.stage_status = auditConst.stage.status.checking;
  113. tender.stage_count = tender.stage_count;
  114. tender.stage_complete_count = tender.stage_complete_count;
  115. tender.cur_flow = JSON.parse(cache.stage_flow_cur_info || cache.stage_flow_pre_info);
  116. tender.pre_flow = cache.stage_flow_pre_info ? JSON.parse(cache.stage_flow_pre_info) : null;
  117. tender.stage_tp = JSON.parse(cache.stage_flow_cur_tp || cache.stage_flow_pre_tp);
  118. try {
  119. tender.progress = {
  120. title: `第${tender.pre_flow.order === undefined ? tender.pre_flow[0].order : tender.pre_flow.order}期`,
  121. status: auditConst.stage.tiStatusString[tender.stage_status],
  122. status_class: auditConst.stage.tiStatusStringClass[tender.stage_status],
  123. };
  124. } catch(err) {
  125. tender.progress = {
  126. title: `第${tender.stage_count}期`,
  127. status: auditConst.stage.tiStatusString[tender.stage_status],
  128. status_class: auditConst.stage.tiStatusStringClass[tender.stage_status],
  129. };
  130. }
  131. }
  132. tender.contract_price = cache.contract_price;
  133. tender.advance_tp = cache.advance_tp;
  134. tender.change_tp = cache.change_tp;
  135. }
  136. async loadTenderCache(tender, uid) {
  137. const cache = await this.getDataById(tender.id);
  138. if (cache) {
  139. try {
  140. this._analysisTenderCache(tender, cache, uid);
  141. } catch(err) {
  142. console.log(err);
  143. console.log(tender);
  144. }
  145. }
  146. }
  147. async insertTenderCache(transaction, tid, uid) {
  148. const user = await this.ctx.service.projectAccount.getAccountCacheData(uid);
  149. user.order = 0;
  150. user.time = new Date();
  151. user.status = auditConst.ledger.status.uncheck;
  152. const data = {
  153. id: tid,
  154. ledger_status: auditConst.flow.status.uncheck,
  155. ledger_flow_cur_uid: uid,
  156. ledger_flow_cur_info: JSON.stringify(user),
  157. };
  158. await transaction.insert(this.tableName, data);
  159. }
  160. async updateLedgerCache4Start(transaction, tid, status, auditors, tp) {
  161. const orgCache = await this.getDataById(tid);
  162. const preInfo = JSON.parse(orgCache.ledger_flow_cur_info);
  163. preInfo.time = new Date();
  164. const data = {
  165. id: tid, ledger_status: status,
  166. ledger_flow_pre_uid: orgCache.ledger_flow_cur_uid, ledger_flow_pre_info: JSON.stringify(preInfo),
  167. };
  168. if (auditors.length > 0) {
  169. const auditIds = auditors.map(x => { return x.audit_id; });
  170. const curInfo = await this.ctx.service.projectAccount.getAccountCacheDatas(auditIds,
  171. {audit_type: auditors[0].audit_type, audit_order: 1, status});
  172. data.ledger_flow_cur_uid = auditIds.join(',');
  173. data.ledger_flow_cur_info = JSON.stringify(curInfo);
  174. }
  175. if (tp) data.ledger_tp = JSON.stringify(tp);
  176. await transaction.update(this.tableName, data);
  177. }
  178. async updateLedgerCache(transaction, tid, status, checkType, next, tp) {
  179. const orgCache = await this.getDataById(tid);
  180. const preInfo = JSON.parse(orgCache.ledger_flow_cur_info || orgCache.ledger_flow_pre_info);
  181. preInfo.time = new Date();
  182. preInfo.status = checkType;
  183. const data = {
  184. id: tid, ledger_status: status,
  185. ledger_flow_pre_uid: orgCache.ledger_flow_cur_uid || orgCache.ledger_flow_pre_uid, ledger_flow_pre_info: JSON.stringify(preInfo),
  186. };
  187. if (next && next.length > 0) {
  188. const auditIds = next.map(x => { return x.audit_id; });
  189. const infoStatus = checkType === auditConst.ledger.status.checkNo ? status : auditConst.ledger.status.uncheck;
  190. const info = await this.ctx.service.projectAccount.getAccountCacheDatas(auditIds,
  191. {audit_type: next[0].audit_type, audit_order: next[0].audit_order, status: infoStatus});
  192. data.ledger_flow_cur_uid = auditIds.join(',');
  193. data.ledger_flow_cur_info = JSON.stringify(info);
  194. } else {
  195. data.ledger_flow_cur_uid = 0;
  196. data.ledger_flow_cur_info = '';
  197. }
  198. if (tp) data.ledger_tp = JSON.stringify(tp);
  199. await transaction.update(this.tableName, data);
  200. }
  201. async updateStageCache4Add(transaction, newStage, pcTp) {
  202. const tp = {
  203. contract_tp: 0, qc_tp: 0,
  204. positive_qc_tp: 0, negative_qc_tp: 0,
  205. yf_tp: 0, sf_tp: 0,
  206. pre_contract_tp: newStage.pre_contract_tp, pre_qc_tp: newStage.pre_qc_tp,
  207. pre_positive_qc_tp: newStage.pre_positive_qc_tp, pre_negative_qc_tp: newStage.pre_positive_qc_tp,
  208. pre_yf_tp: newStage.pre_yf_tp, pre_sf_tp: newStage.pre_sf_tp,
  209. ...pcTp,
  210. };
  211. const cur_flow_info = await this.ctx.service.projectAccount.getAccountCacheData(newStage.user_id);
  212. cur_flow_info.order = newStage.order;
  213. cur_flow_info.status = auditConst.stage.status.uncheck;
  214. await transaction.update(this.tableName, {
  215. id: newStage.tid, stage_count: newStage.order, stage_status: auditConst.stage.status.uncheck,
  216. stage_flow_cur_uid: newStage.user_id, stage_flow_cur_info: JSON.stringify(cur_flow_info),
  217. stage_flow_cur_tp: JSON.stringify(tp),
  218. });
  219. }
  220. async updateStageCache4Del(transaction, delStage) {
  221. if (delStage.order > 1) {
  222. const preStage = await this.ctx.service.stage.getDataByCondition({ tid: delStage.tid, order: delStage.order - 1 });
  223. const tp = JSON.stringify({
  224. contract_tp: preStage.contract_tp, qc_tp: preStage.qc_tp, contract_pc_tp: preStage.contract_pc_tp, qc_pc_tp: preStage.qc_pc_tp, pc_tp: preStage.pc_tp,
  225. positive_qc_tp: preStage.positive_qc_tp, positive_qc_pc_tp: preStage.positive_qc_pc_tp, negative_qc_tp: preStage.negative_qc_pc_tp, negative_qc_pc_tp: preStage.negative_qc_pc_tp,
  226. yf_tp: preStage.yf_tp, sf_tp: preStage.sf_tp,
  227. pre_contract_tp: preStage.pre_contract_tp, pre_qc_tp: preStage.pre_qc_tp,
  228. pre_positive_qc_tp: preStage.pre_positive_qc_tp, pre_negative_qc_tp: preStage.pre_positive_qc_tp,
  229. pre_yf_tp: preStage.pre_yf_tp, pre_sf_tp: preStage.pre_sf_tp,
  230. });
  231. const auditors = await this.ctx.service.stageAudit.getLastestAuditors(preStage.id, preStage.times, preStage.status);
  232. const user_info = auditors.length > 0 ? JSON.stringify(auditors.map(auditor => { return {
  233. order: preStage.order, status: preStage.status, time: auditor.end_time, audit_order: auditor.audit_order,
  234. name: auditor.name, company: auditor.company, role: auditor.role, mobile: auditor.mobile, telephone: auditor.telephone,
  235. }})) : '';
  236. await transaction.update(this.tableName, {
  237. id: delStage.tid, stage_count: preStage.order, stage_complete_count: preStage.order, stage_status: preStage.status,
  238. stage_flow_cur_uid: 0, stage_flow_cur_info: '', stage_flow_cur_tp: '',
  239. stage_flow_pre_uid: auditors.map(x => { return x.aid; }).join(','), stage_flow_pre_info: user_info, stage_flow_pre_tp: tp,
  240. });
  241. } else {
  242. await transaction.update(this.tableName, {
  243. id: delStage.tid, stage_count: 0, stage_complete_count: 0, stage_status: 0,
  244. stage_flow_cur_uid: 0, stage_flow_cur_info: '', stage_flow_cur_tp: '',
  245. stage_flow_pre_uid: 0, stage_flow_pre_info: '', stage_flow_pre_tp: '',
  246. });
  247. }
  248. }
  249. async updateStageCache4DelTimes(transaction, stage, times) {
  250. const data = { id: stage.tid };
  251. const tp = {
  252. contract_tp: stage.contract_tp || 0, qc_tp: stage.qc_tp || 0, contract_pc_tp: stage.contract_pc_tp, qc_pc_tp: stage.qc_pc_tp, pc_tp: stage.pc_tp,
  253. positive_qc_tp: stage.positive_qc_tp, positive_qc_pc_tp: stage.positive_qc_pc_tp, negative_qc_tp: stage.negative_qc_pc_tp, negative_qc_pc_tp: stage.negative_qc_pc_tp,
  254. yf_tp: stage.yf_tp, sf_tp: stage.sf_tp,
  255. pre_contract_tp: stage.pre_contract_tp, pre_qc_tp: stage.pre_qc_tp,
  256. pre_positive_qc_tp: stage.pre_positive_qc_tp, pre_negative_qc_tp: stage.pre_positive_qc_tp,
  257. pre_yf_tp: stage.pre_yf_tp, pre_sf_tp: stage.pre_sf_tp,
  258. };
  259. data.stage_flow_cur_uid = stage.user_id;
  260. const curInfo = await this.ctx.service.projectAccount.getAccountCacheData(stage.user_id,
  261. { order: stage.order, audit_order: 0, audit_type: auditConst.auditType.key.common, status: auditConst.stage.status.uncheck });
  262. data.stage_flow_cur_info = JSON.stringify(curInfo);
  263. const preAuditors = await this.ctx.service.stageAudit.getLastestAuditors(stage.id, times - 1, auditConst.stage.status.checkNo);
  264. const preAuditorIds = preAuditors.map(x => { return x.aid; });
  265. data.stage_flow_pre_uid = preAuditorIds.join(',');
  266. data.stage_flow_pre_info = preAuditors.length > 0 ? JSON.stringify(preAuditors.map(preAuditor => { return {
  267. order: stage.order, audit_type: preAuditor.audit_type, audit_order: preAuditor.order, status: preAuditor.status, time: preAuditor.end_time,
  268. name: preAuditor.name, company: preAuditor.company, role: preAuditor.role, mobile: preAuditor.mobile, telephone: preAuditor.telephone,
  269. }})) : '';
  270. const his = stage.tp_history.find(x => { return x.times === times && x.order === preAuditors[0].order; });
  271. if (his) {
  272. tp.contract_tp = his.contract_tp;
  273. tp.qc_tp = his.qc_tp;
  274. tp.positive_qc_tp = his.positive_qc_tp;
  275. tp.negative_qc_tp = his.negative_qc_tp;
  276. tp.yf_tp = his.yf_tp;
  277. tp.sf_tp = his.sf_tp;
  278. }
  279. data.stage_flow_cur_tp = JSON.stringify(tp);
  280. data.stage_flow_pre_tp = JSON.stringify(tp);
  281. await transaction.update(this.tableName, data);
  282. }
  283. async updateStageCache4Start(transaction, stage, status, auditors, ledgerTp, stageTp) {
  284. if (!stage.isCheckFirst) return;
  285. const orgCache = await this.getDataById(stage.tid);
  286. const data = { id: stage.tid, stage_status: status };
  287. if (ledgerTp) data.ledger_tp = JSON.stringify(ledgerTp);
  288. const tp = JSON.parse(orgCache.stage_flow_cur_tp);
  289. if (stageTp) this._.assign(tp, stageTp);
  290. data.stage_flow_pre_uid = orgCache.stage_flow_cur_uid;
  291. const info = JSON.parse(orgCache.stage_flow_cur_info);
  292. info.time = new Date();
  293. data.stage_flow_pre_info = JSON.stringify(info);
  294. data.stage_flow_pre_tp = JSON.stringify(tp);
  295. const auditIds = auditors.map(x => { return x.aid; });
  296. data.stage_flow_cur_uid = auditIds.join(',');
  297. const cur_flow_info = await this.ctx.service.projectAccount.getAccountCacheDatas(auditIds,
  298. {order: stage.order, audit_type: auditors[0].audit_type, audit_order: 1, status: auditConst.stage.status.checking});
  299. data.stage_flow_cur_info = JSON.stringify(cur_flow_info);
  300. data.stage_flow_cur_tp = JSON.stringify(tp);
  301. await transaction.update(this.tableName, data);
  302. }
  303. async updateStageCache4Flow(transaction, stage, status, auditors, preAuditors, ledgerTp, stageTp, pcTp) {
  304. if (!stage.isCheckFirst) return;
  305. const orgCache = await this.getDataById(stage.tid);
  306. const data = { id: stage.tid, stage_status: status };
  307. if (ledgerTp) data.ledger_tp = JSON.stringify(ledgerTp);
  308. const tp = orgCache.stage_flow_cur_tp ? JSON.parse(orgCache.stage_flow_cur_tp) : (orgCache.stage_flow_pre_tp ? JSON.parse(orgCache.stage_flow_pre_tp) : {});
  309. if (stageTp) this._.assign(tp, stageTp);
  310. if (pcTp) this._.assign(tp, pcTp);
  311. if (preAuditors && preAuditors.length > 0) {
  312. const preAuditorIds = preAuditors.map(x => { return x.aid; });
  313. data.stage_flow_pre_uid = preAuditorIds.join(',');
  314. const info = await this.ctx.service.projectAccount.getAccountCacheDatas(preAuditorIds,
  315. { order: stage.order, status: preAuditors[0].status, audit_type:preAuditors[0].audit_type, audit_order: preAuditors[0].audit_order, time: new Date() });
  316. data.stage_flow_pre_info = info.length > 0 ? JSON.stringify(info) : '';
  317. } else {
  318. data.stage_flow_pre_uid = orgCache.stage_flow_cur_uid;
  319. data.stage_flow_pre_info = orgCache.stage_flow_cur_info;
  320. }
  321. data.stage_flow_pre_tp = JSON.stringify(tp);
  322. if (auditors && auditors.length > 0) {
  323. const auditorIds = auditors.map(x => { return x.aid; });
  324. data.stage_flow_cur_uid = auditorIds.join(',');
  325. const info = await this.ctx.service.projectAccount.getAccountCacheDatas(auditorIds, { order: stage.order, audit_type: auditors[0].audit_type, status, audit_order: auditors[0].audit_order });
  326. data.stage_flow_cur_info = info.length > 0 ? JSON.stringify(info) : '';
  327. }
  328. if (status === auditConst.stage.status.checked && (!auditors || auditors.length === 0)) data.stage_complete_count = stage.order;
  329. data.stage_flow_cur_tp = JSON.stringify(tp);
  330. await transaction.update(this.tableName, data);
  331. }
  332. async updateStageCache4MultiChecked(transaction, stage, auditors, ledgerTp, stageTp, nextStage, nextStageTp) {
  333. if (!stage.isCheckFirst) return;
  334. const orgCache = await this.getDataById(stage.tid);
  335. const data = { id: stage.tid, stage_status: auditConst.stage.status.uncheck, stage_complete_count: stage.order, stage_count: stage.order + 1 };
  336. if (ledgerTp) data.ledger_tp = JSON.stringify(ledgerTp);
  337. const tp = orgCache.stage_flow_cur_tp ? JSON.parse(orgCache.stage_flow_cur_tp) : (orgCache.stage_flow_pre_tp ? JSON.parse(orgCache.stage_flow_pre_tp) : {});
  338. if (stageTp) this._.assign(tp, stageTp);
  339. if (auditors && auditors.length > 0) {
  340. const auditorIds = auditors.map(x => { return x.aid; });
  341. data.stage_flow_pre_uid = auditorIds.join(',');
  342. const info = await this.ctx.service.projectAccount.getAccountCacheDatas(auditorIds, { order: stage.order,
  343. audit_type: auditors[0].audit_type, status: auditConst.stage.status.checked, audit_order: auditors[0].audit_order });
  344. data.stage_flow_pre_info = info.length > 0 ? JSON.stringify(info) : '';
  345. }
  346. data.stage_flow_pre_tp = JSON.stringify(tp);
  347. data.stage_flow_cur_uid = nextStage.user_id;
  348. const cur_flow_info = await this.ctx.service.projectAccount.getAccountCacheData(nextStage.user_id);
  349. cur_flow_info.order = nextStage.order;
  350. cur_flow_info.status = auditConst.stage.status.uncheck;
  351. data.stage_flow_cur_info = JSON.stringify(cur_flow_info);
  352. const cur_flow_tp = {
  353. contract_tp: 0, qc_tp: 0,
  354. positive_qc_tp: 0, negative_qc_tp: 0,
  355. yf_tp: 0, sf_tp: 0,
  356. ...nextStageTp,
  357. };
  358. data.stage_flow_cur_tp = JSON.stringify(cur_flow_tp);
  359. await transaction.update(this.tableName, data);
  360. }
  361. async updateStageCache4Revise(transaction, tid, ledgerTp, pcTp) {
  362. const orgCache = await this.getDataById(tid);
  363. const data = { id: tid };
  364. if (ledgerTp) data.ledger_tp = JSON.stringify(ledgerTp);
  365. if (data.stage_status !== auditConst.stage.status.checked && pcTp) {
  366. const curTp = JSON.parse(orgCache.stage_flow_cur_tp);
  367. this._.assign(curTp, pcTp);
  368. data.stage_flow_cur_tp = JSON.stringify(curTp);
  369. if (orgCache.stage_status !== auditConst.stage.status.uncheck) {
  370. const preTp = JSON.parse(orgCache.stage_flow_pre_tp);
  371. this._.assign(preTp, pcTp);
  372. data.stage_flow_cur_tp = JSON.stringify(preTp);
  373. }
  374. }
  375. await transaction.update(this.tableName, data);
  376. }
  377. async updateAdvanceCache(tid) {
  378. const advance_tp = await this.ctx.service.advance.getSumAdvance(tid);
  379. await this.db.update(this.tableName, { id: tid, advance_tp });
  380. }
  381. async updateChangeCache(tid) {
  382. const sql = `SELECT SUM(cast (total_price as decimal(18,6))) AS total_price, SUM(cast (positive_tp as decimal(18,6))) AS positive_tp, SUM(cast (negative_tp as decimal(18,6))) AS negative_tp
  383. FROM ${this.ctx.service.change.tableName} WHERE tid = ? AND status = ? And valid = 1`;
  384. const changeSum = await this.db.queryOne(sql, [tender.id, auditConst.flow.status.checked]);
  385. await this.db.update(this.tableName, { id: tid, change_tp: changeSum.total_price || 0 });
  386. }
  387. async updateContractPriceCache(tender) {
  388. const tenderInfo = await this.ctx.service.tenderInfo.getTenderInfo(tender.id, tender.project_id);
  389. await this.db.update(this.tableName, { id: tender.id, contract_price: tenderInfo.deal_param.contractPrice || 0 });
  390. }
  391. async _refreshLedgerRela(tender, data) {
  392. data.ledger_status = tender.ledger_status || 0;
  393. if (tender.ledger_status === auditConst.ledger.status.uncheck) {
  394. data.ledger_flow_cur_uid = tender.user_id;
  395. const user = await this.ctx.service.projectAccount.getAccountCacheData(tender.user_id);
  396. user.status = tender.ledger_status;
  397. user.order = 0;
  398. user.time = tender.create_time;
  399. data.ledger_flow_cur_info = JSON.stringify(user);
  400. } else if (tender.ledger_status === auditConst.ledger.status.checked) {
  401. const pre = await this.ctx.service.ledgerAudit.getLastestAuditor(tender.id, tender.ledger_times, tender.ledger_status);
  402. data.ledger_flow_pre_uid = pre.audit_id;
  403. data.ledger_flow_pre_info = JSON.stringify({
  404. order: pre.order, status: pre.status, time: pre.end_time,
  405. name: pre.name, company: pre.company, role: pre.role, mobile: pre.mobile, telephone: pre.telephone,
  406. });
  407. } else if (tender.ledger_status === auditConst.ledger.status.checkNo) {
  408. data.ledger_flow_cur_uid = tender.user_id;
  409. const user = await this.ctx.service.projectAccount.getAccountCacheData(tender.user_id);
  410. user.order = 0;
  411. user.status = auditConst.ledger.status.uncheck;
  412. data.ledger_flow_cur_info = JSON.stringify(user);
  413. const pre = await this.ctx.service.ledgerAudit.getLastestAuditor(tender.id, tender.ledger_times - 1, tender.ledger_status);
  414. data.ledger_flow_pre_uid = pre.audit_id;
  415. data.ledger_flow_pre_info = JSON.stringify({
  416. order: pre.order, status: pre.status, time: pre.end_time,
  417. name: pre.name, company: pre.company, role: pre.role, mobile: pre.mobile, telephone: pre.telephone,
  418. });
  419. } else {
  420. const cur = await this.ctx.service.ledgerAudit.getLastestAuditor(tender.id, tender.ledger_times, tender.ledger_status);
  421. if (cur) {
  422. data.ledger_flow_cur_uid = cur.audit_id;
  423. data.ledger_flow_cur_info = JSON.stringify({
  424. order: cur.order, status: cur.status, time: cur.end_time,
  425. name: cur.name, company: cur.company, role: cur.role, mobile: cur.mobile, telephone: cur.telephone,
  426. });
  427. if (cur.audit_order === 1) {
  428. data.ledger_flow_pre_uid = tender.user_id;
  429. const user = await await this.ctx.service.projectAccount.getAccountCacheData(tender.user_id, { order: 0, time: cur.begin_time });
  430. data.ledger_flow_pre_info = JSON.stringify(user);
  431. } else {
  432. const pre = await this.ctx.service.ledgerAudit.getAuditorByOrder(tender.id, cur.audit_order - 1, cur.times);
  433. data.ledger_flow_pre_uid = pre.audit_id;
  434. data.ledger_flow_pre_info = JSON.stringify({
  435. order: pre.order, status: pre.status, time: pre.end_time,
  436. name: pre.name, company: pre.company, role: pre.role, mobile: pre.mobile, telephone: pre.telephone,
  437. });
  438. }
  439. }
  440. }
  441. data.ledger_tp = JSON.stringify(await this.ctx.service.ledger.addUp({ tender_id: tender.id/* , is_leaf: true*/ }));
  442. const revise = await this.service.ledgerRevise.getLastestRevise(tender.id);
  443. data.revise_status = revise ? revise.status : 0;
  444. }
  445. async _calcTp (lastStage, tp) {
  446. lastStage.curTimes = lastStage.times;
  447. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(lastStage);
  448. const pcSum = await this.ctx.service.stageBillsPc.getSumTotalPrice(lastStage);
  449. tp.contract_tp = tpData.contract_tp;
  450. tp.qc_tp = tpData.qc_tp;
  451. tp.positive_qc_tp = tpData.positive_qc_tp;
  452. tp.negative_qc_tp = tpData.negative_qc_tp;
  453. tp.contract_pc_tp = pcSum.contract_pc_tp;
  454. tp.qc_pc_tp = pcSum.qc_pc_tp;
  455. tp.pc_tp = pcSum.pc_tp;
  456. tp.positive_qc_pc_tp = pcSum.positive_qc_pc_tp;
  457. tp.negative_qc_pc_tp = pcSum.negative_qc_pc_tp;
  458. const payTp = await this.ctx.service.stagePay.getSpecialTotalPrice(lastStage);
  459. tp.yf_tp = payTp.yf;
  460. tp.sf_tp = payTp.sf;
  461. };
  462. async _refreshStageRela(tender, data) {
  463. if (tender.ledger_status === auditConst.ledger.status.checked) {
  464. const endLastStage = await this.ctx.service.stage.getLastestStage(tender.id, true);
  465. const lastStage = await this.ctx.service.stage.getFlowLatestStage(tender.id, true);
  466. if (!lastStage || !endLastStage) return;
  467. data.stage_count = endLastStage.order;
  468. data.stage_complete_count = lastStage.status === auditConst.stage.status.checked ? lastStage.order : lastStage.order - 1;
  469. data.stage_status = lastStage.status;
  470. const tp = {
  471. contract_tp: lastStage.contract_tp || 0, qc_tp: lastStage.qc_tp || 0, contract_pc_tp: lastStage.contract_pc_tp, qc_pc_tp: lastStage.qc_pc_tp, pc_tp: lastStage.pc_tp,
  472. positive_qc_tp: lastStage.positive_qc_tp, positive_qc_pc_tp: lastStage.positive_qc_pc_tp, negative_qc_tp: lastStage.negative_qc_pc_tp, negative_qc_pc_tp: lastStage.negative_qc_pc_tp,
  473. yf_tp: lastStage.yf_tp, sf_tp: lastStage.sf_tp,
  474. pre_contract_tp: lastStage.pre_contract_tp, pre_qc_tp: lastStage.pre_qc_tp,
  475. pre_positive_qc_tp: lastStage.pre_positive_qc_tp, pre_negative_qc_tp: lastStage.pre_positive_qc_tp,
  476. pre_yf_tp: lastStage.pre_yf_tp, pre_sf_tp: lastStage.pre_sf_tp,
  477. };
  478. const preStage = lastStage.order > 1 ? await this.ctx.service.stage.getDataByCondition({ tid: lastStage.tid, order: lastStage.order - 1}) : null;
  479. if (lastStage.status === auditConst.stage.status.uncheck) {
  480. if (preStage) {
  481. const preAuditors = await this.ctx.service.stageAudit.getLastestAuditors(preStage.id, preStage.times, preStage.status);
  482. const preAuditorIds = preAuditors.map(x => { return x.aid; });
  483. data.stage_flow_pre_uid = preAuditorIds.join(',');
  484. data.stage_flow_pre_info = preAuditors.length > 0 ? JSON.stringify(preAuditors.map(preAuditor => { return {
  485. order: preStage.order, audit_type: preAuditor.audit_type, audit_order: preAuditor.order, status: preAuditor.status, time: preAuditor.end_time,
  486. name: preAuditor.name, company: preAuditor.company, role: preAuditor.role, mobile: preAuditor.mobile, telephone: preAuditor.telephone,
  487. }})) : '';
  488. data.stage_flow_pre_tp = JSON.stringify({
  489. contract_tp: preStage.contract_tp || 0, qc_tp: preStage.qc_tp || 0, contract_pc_tp: preStage.contract_pc_tp, qc_pc_tp: preStage.qc_pc_tp, pc_tp: preStage.pc_tp,
  490. positive_qc_tp: preStage.positive_qc_tp, positive_qc_pc_tp: preStage.positive_qc_pc_tp, negative_qc_tp: preStage.negative_qc_pc_tp, negative_qc_pc_tp: preStage.negative_qc_pc_tp,
  491. yf_tp: preStage.yf_tp, sf_tp: preStage.sf_tp,
  492. pre_contract_tp: preStage.pre_contract_tp, pre_qc_tp: preStage.pre_qc_tp,
  493. pre_positive_qc_tp: preStage.pre_positive_qc_tp, pre_negative_qc_tp: preStage.pre_positive_qc_tp,
  494. pre_yf_tp: preStage.pre_yf_tp, pre_sf_tp: preStage.pre_sf_tp,
  495. });
  496. }
  497. lastStage.curOrder = 0;
  498. await this._calcTp(lastStage, tp);
  499. data.stage_flow_cur_uid = lastStage.user_id;
  500. const curInfo = await this.ctx.service.projectAccount.getAccountCacheData(lastStage.user_id, { order: lastStage.order, audit_order: 0, audit_type: auditConst.auditType.key.common, status: lastStage.status });
  501. data.stage_flow_cur_info = JSON.stringify(curInfo);
  502. data.stage_flow_cur_tp = JSON.stringify(tp);
  503. } else if (lastStage.status === auditConst.stage.status.checked) {
  504. const preAuditors = await this.ctx.service.stageAudit.getLastestAuditors(lastStage.id, lastStage.times, lastStage.status);
  505. const preAuditorIds = preAuditors.map(x => { return x.aid; });
  506. lastStage.curOrder = preAuditors[0].order;
  507. await this._calcTp(lastStage, tp);
  508. data.stage_flow_pre_uid = preAuditorIds.join(',');
  509. data.stage_flow_pre_info = preAuditors.length > 0 ? JSON.stringify(preAuditors.map(preAuditor => { return {
  510. order: lastStage.order, audit_type: preAuditor.audit_type, audit_order: preAuditor.order, status: preAuditor.status, time: preAuditor.end_time,
  511. name: preAuditor.name, company: preAuditor.company, role: preAuditor.role, mobile: preAuditor.mobile, telephone: preAuditor.telephone,
  512. }})) : '';
  513. data.stage_flow_cur_tp = JSON.stringify(tp);
  514. data.stage_flow_pre_tp = JSON.stringify(tp);
  515. } else if (lastStage.status === auditConst.stage.status.checkNo) {
  516. lastStage.curOrder = 0;
  517. await this._calcTp(lastStage, tp);
  518. data.stage_flow_cur_uid = lastStage.user_id;
  519. const curInfo = await this.ctx.service.projectAccount.getAccountCacheData(lastStage.user_id,
  520. { order: lastStage.order, audit_order: 0, audit_type: auditConst.auditType.key.common, status: auditConst.stage.status.uncheck });
  521. data.stage_flow_cur_info = JSON.stringify(curInfo);
  522. data.stage_flow_cur_tp = JSON.stringify(tp);
  523. const preAuditors = await this.ctx.service.stageAudit.getLastestAuditors(lastStage.id, lastStage.times - 1, lastStage.status);
  524. const preAuditorIds = preAuditors.map(x => { return x.aid; });
  525. data.stage_flow_pre_uid = preAuditorIds.join(',');
  526. data.stage_flow_pre_info = preAuditors.length > 0 ? JSON.stringify(preAuditors.map(preAuditor => { return {
  527. order: lastStage.order, audit_type: preAuditor.audit_type, audit_order: preAuditor.order, status: preAuditor.status, time: preAuditor.end_time,
  528. name: preAuditor.name, company: preAuditor.company, role: preAuditor.role, mobile: preAuditor.mobile, telephone: preAuditor.telephone,
  529. }})) : '';
  530. const his = lastStage.tp_history.find(x => { return x.times === preAuditors[0].times && x.order === preAuditors[0].order; });
  531. if (his) {
  532. tp.contract_tp = his.contract_tp;
  533. tp.qc_tp = his.qc_tp;
  534. tp.positive_qc_tp = his.positive_qc_tp;
  535. tp.negative_qc_tp = his.negative_qc_tp;
  536. tp.yf_tp = his.yf_tp;
  537. tp.sf_tp = his.sf_tp;
  538. }
  539. data.stage_flow_pre_tp = JSON.stringify(tp);
  540. } else {
  541. const curAuditors = await this.ctx.service.stageAudit.getLastestAuditors(lastStage.id, lastStage.times, lastStage.status);
  542. const curAuditorIds = curAuditors.map(x => { return x.aid; });
  543. lastStage.curOrder = curAuditors[0].order;
  544. await this._calcTp(lastStage, tp);
  545. data.stage_flow_cur_uid = curAuditorIds.join(',');
  546. data.stage_flow_cur_info = JSON.stringify(curAuditors.map(curAuditor => { return {
  547. order: lastStage.order, audit_order: curAuditor.order, status: curAuditor.status,
  548. name: curAuditor.name, company: curAuditor.company, role: curAuditor.role, mobile: curAuditor.mobile, telephone: curAuditor.telephone,
  549. }}));
  550. data.stage_flow_cur_tp = JSON.stringify(tp);
  551. const preAuditors = lastStage.curOrder > 1 ? await this.ctx.service.stageAudit.getAuditorsByOrder(lastStage.id, lastStage.times, lastStage.curOrder - 1) : [];
  552. if (preAuditors.length > 0) {
  553. const preAuditorIds = preAuditors.map(x => { return x.aid; });
  554. data.stage_flow_pre_uid = preAuditorIds.join(',');
  555. data.stage_flow_pre_info = JSON.stringify(preAuditors.map(preAuditor => { return {
  556. order: lastStage.order, audit_type: preAuditor.audit_type, audit_order: preAuditor.audit_order, status: preAuditor.status, time: preAuditor.end_time,
  557. name: preAuditor.name, company: preAuditor.company, role: preAuditor.role, mobile: preAuditor.mobile, telephone: preAuditor.telephone,
  558. }}));
  559. } else {
  560. data.stage_flow_pre_uid = lastStage.user_id;
  561. const preInfo = await this.ctx.service.projectAccount.getAccountCacheData(lastStage.user_id,
  562. { order: lastStage.order, audit_order: 0, audit_type: 1, status: auditConst.stage.status.uncheck, time: curAuditors[0].begin_time });
  563. data.stage_flow_pre_info = JSON.stringify(preInfo);
  564. }
  565. const his = preAuditors.length > 0
  566. ? lastStage.tp_history.find(x => { return x.times === preAuditors[0].times && x.order === preAuditors[0].order; })
  567. : lastStage.tp_history.find(x => { return x.times === lastStage.times && x.order === 0; });
  568. if (his) {
  569. tp.contract_tp = his.contract_tp;
  570. tp.qc_tp = his.qc_tp;
  571. tp.positive_qc_tp = his.positive_qc_tp;
  572. tp.negative_qc_tp = his.negative_qc_tp;
  573. tp.yf_tp = his.yf_tp;
  574. tp.sf_tp = his.sf_tp;
  575. }
  576. data.stage_flow_pre_tp = JSON.stringify(tp);
  577. }
  578. }
  579. }
  580. async refreshTenderCache(tender, transaction) {
  581. const orgCache = await this.getDataById(tender.id);
  582. const data = { id: tender.id, ledger_status: 0,
  583. ledger_flow_cur_uid: 0, ledger_flow_cur_info: '', ledger_flow_pre_uid: 0, ledger_flow_pre_info: '',
  584. stage_count: 0, stage_complete_count: 0, stage_status: 0,
  585. stage_flow_cur_uid: 0, stage_flow_cur_info: '', stage_flow_cur_tp: '',
  586. stage_flow_pre_uid: 0, stage_flow_pre_info: '', stage_flow_pre_tp: '',
  587. };
  588. // 台账
  589. await this._refreshLedgerRela(tender, data);
  590. // 计量
  591. await this._refreshStageRela(tender, data);
  592. // 其他计算项
  593. const tenderInfo = await this.ctx.service.tenderInfo.getTenderInfo(tender.id, tender.project_id);
  594. data.contract_price = tenderInfo.deal_param.contractPrice || 0;
  595. data.advance_tp = await this.ctx.service.advance.getSumAdvance(tender.id);
  596. const changeSum = await this.db.queryOne(`SELECT SUM(cast (total_price as decimal(18,6))) AS total_price FROM ${this.ctx.service.change.tableName} WHERE tid = ? AND status = ? And valid = 1`, [tender.id, auditConst.flow.status.checked]);
  597. data.change_tp = changeSum.total_price || 0;
  598. const conn = transaction || this.db;
  599. if (orgCache) {
  600. conn.update(this.tableName, data);
  601. } else {
  602. conn.insert(this.tableName, data);
  603. }
  604. }
  605. }
  606. return TenderCache;
  607. };