tender_cache.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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. _analysisTenderCache(tender, cache, uid) {
  26. commonField.forEach(f => { tender[f] = cache[f]; });
  27. tender.ledger_tp = cache.ledger_tp ? JSON.parse(cache.ledger_tp) : {};
  28. if (!cache.stage_count || (cache.stage_count === 1 && cache.stage_status === auditConst.stage.status.uncheck && cache.stage_flow_cur_uid !== uid)) {
  29. tender.cur_flow = JSON.parse(cache.ledger_flow_cur_info || cache.ledger_flow_pre_info);
  30. tender.cur_flow.title = '台账';
  31. tender.pre_flow = cache.ledger_flow_pre_info ? JSON.parse(cache.ledger_flow_pre_info) : {};
  32. tender.stage_tp = {};
  33. tender.progress = {
  34. title: '台账',
  35. status: auditConst.ledger.tiStatusString[cache.ledger_status],
  36. status_class: auditConst.ledger.tiStatusStringClass[cache.ledger_status],
  37. };
  38. } else if (cache.stage_status !== auditConst.stage.status.uncheck || cache.stage_flow_cur_uid === uid) {
  39. tender.stage_status = cache.stage_status;
  40. tender.stage_count = tender.stage_count;
  41. tender.stage_complete_count = tender.stage_complete_count;
  42. tender.cur_flow = cache.stage_status === auditConst.stage.status.checkNo
  43. ? JSON.parse(cache.stage_flow_pre_info)
  44. : JSON.parse(cache.stage_flow_cur_info || cache.stage_flow_pre_info);
  45. tender.pre_flow = cache.stage_flow_pre_info ? JSON.parse(cache.stage_flow_pre_info) : {};
  46. tender.stage_tp = JSON.parse(cache.stage_flow_cur_tp || cache.stage_flow_pre_tp);
  47. tender.progress = {
  48. title: `第${cache.stage_count}期`,
  49. status: auditConst.stage.tiStatusString[cache.stage_status],
  50. status_class: auditConst.stage.tiStatusStringClass[cache.stage_status],
  51. };
  52. } else {
  53. tender.stage_status = auditConst.stage.status.checked;
  54. tender.stage_count = tender.stage_complete_count;
  55. tender.stage_complete_count = tender.stage_complete_count;
  56. tender.cur_flow = (cache.stage_status !== auditConst.stage.status.uncheck || cache.stage_flow_cur_uid !== uid)
  57. ? JSON.parse(cache.stage_flow_pre_info) : JSON.parse(cache.stage_flow_cur_info || cache.stage_flow_pre_info);
  58. tender.pre_flow = cache.stage_flow_pre_info ? JSON.parse(cache.stage_flow_pre_info) : {};
  59. tender.stage_tp = cache.stage_flow_pre_tp ? JSON.parse(cache.stage_flow_pre_tp) : {};
  60. tender.progress = {
  61. title: `第${cache.stage_complete_count}期`,
  62. status: auditConst.stage.tiStatusString[auditConst.stage.status.checked],
  63. status_class: auditConst.stage.tiStatusStringClass[auditConst.stage.status.checked],
  64. };
  65. }
  66. tender.contract_price = cache.contract_price;
  67. tender.advance_tp = cache.advance_tp;
  68. tender.change_tp = cache.change_tp;
  69. }
  70. async loadTenderCache(tender, uid) {
  71. const cache = await this.getDataById(tender.id);
  72. if (cache) this._analysisTenderCache(tender, cache, uid);
  73. }
  74. async insertTenderCache(transaction, tid, uid) {
  75. const user = await this.ctx.service.projectAccount.getAccountCacheData(uid);
  76. user.order = 0;
  77. user.status = auditConst.ledger.status.uncheck;
  78. const data = {
  79. id: tid,
  80. ledger_status: auditConst.flow.status.uncheck,
  81. ledger_flow_cur_uid: uid,
  82. ledger_flow_cur_info: JSON.stringify(user),
  83. };
  84. await transaction.insert(this.tableName, data);
  85. }
  86. async updateLedgerCache4Start(transaction, tid, status, audit, tp) {
  87. const orgCache = await this.getDataById(tid);
  88. const preInfo = JSON.parse(orgCache.ledger_flow_cur_info);
  89. preInfo.time = new Date();
  90. const data = {
  91. id: tid, ledger_status: status,
  92. ledger_flow_pre_uid: orgCache.ledger_flow_cur_uid, ledger_flow_pre_info: JSON.stringify(preInfo),
  93. };
  94. if (audit) {
  95. const info = await this.ctx.service.projectAccount.getAccountCacheData(audit.audit_id);
  96. info.order = audit.audit_order;
  97. info.status = status;
  98. data.ledger_flow_cur_uid = audit.audit_id;
  99. data.ledger_flow_cur_info = JSON.stringify(info);
  100. }
  101. if (tp) data.ledger_tp = JSON.stringify(tp);
  102. await transaction.update(this.tableName, data);
  103. }
  104. async updateLedgerCache(transaction, tid, status, checkType, next, tp) {
  105. const orgCache = await this.getDataById(tid);
  106. const preInfo = JSON.parse(orgCache.ledger_flow_cur_info);
  107. preInfo.time = new Date();
  108. preInfo.status = checkType;
  109. const data = {
  110. id: tid, ledger_status: status,
  111. ledger_flow_pre_uid: orgCache.ledger_flow_cur_uid, ledger_flow_pre_info: JSON.stringify(preInfo),
  112. };
  113. if (next) {
  114. const info = await this.ctx.service.projectAccount.getAccountCacheData(next.audit_id);
  115. info.status = checkType === auditConst.ledger.status.checkNo ? status : auditConst.ledger.status.uncheck;
  116. data.ledger_flow_cur_uid = next.audit_id;
  117. data.ledger_flow_cur_info = JSON.stringify(info);
  118. } else {
  119. data.ledger_flow_cur_uid = 0;
  120. data.ledger_flow_cur_info = '';
  121. }
  122. if (tp) data.ledger_tp = JSON.stringify(tp);
  123. await transaction.update(this.tableName, data);
  124. }
  125. async updateStageCache4Add(transaction, newStage, pcTp) {
  126. const tp = {
  127. contract_tp: 0, qc_tp: 0,
  128. positive_qc_tp: 0, negative_qc_tp: 0,
  129. yf_tp: 0, sf_tp: 0,
  130. pre_contract_tp: newStage.pre_contract_tp, pre_qc_tp: newStage.pre_qc_tp,
  131. pre_positive_qc_tp: newStage.pre_positive_qc_tp, pre_negative_qc_tp: newStage.pre_positive_qc_tp,
  132. pre_yf_tp: newStage.pre_yf_tp, pre_sf_tp: newStage.pre_sf_tp,
  133. ...pcTp,
  134. };
  135. const cur_flow_info = await this.ctx.service.projectAccount.getAccountCacheData(newStage.user_id);
  136. cur_flow_info.order = newStage.order;
  137. cur_flow_info.status = auditConst.stage.status.uncheck;
  138. await transaction.update(this.tableName, {
  139. id: newStage.tid, stage_count: newStage.order, stage_status: auditConst.stage.status.uncheck,
  140. stage_flow_cur_uid: newStage.user_id, stage_flow_cur_info: JSON.stringify(cur_flow_info),
  141. stage_flow_cur_tp: JSON.stringify(tp),
  142. });
  143. }
  144. async updateStageCache4Del(transaction, delStage) {
  145. if (delStage.order > 1) {
  146. const preStage = await this.ctx.service.stage.getDataByCondition({ tid: delStage.tid, order: delStage.order - 1 });
  147. const tp = JSON.stringify({
  148. 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,
  149. 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,
  150. yf_tp: preStage.yf_tp, sf_tp: preStage.sf_tp,
  151. pre_contract_tp: preStage.pre_contract_tp, pre_qc_tp: preStage.pre_qc_tp,
  152. pre_positive_qc_tp: preStage.pre_positive_qc_tp, pre_negative_qc_tp: preStage.pre_positive_qc_tp,
  153. pre_yf_tp: preStage.pre_yf_tp, pre_sf_tp: preStage.pre_sf_tp,
  154. });
  155. const auditor = await this.ctx.service.stageAudit.getLastestAuditor(preStage.id, preStage.times, preStage.status);
  156. const user_info = JSON.stringify({
  157. order: preStage.order, status: preStage.status, time: auditor.end_time,
  158. name: auditor.name, company: auditor.company, role: auditor.role, mobile: auditor.mobile, telephone: auditor.telephone,
  159. });
  160. await transaction.update(this.tableName, {
  161. id: delStage.tid, stage_count: preStage.order, stage_complete_count: preStage.order, stage_status: preStage.status,
  162. stage_flow_cur_uid: 0, stage_flow_cur_info: '', stage_flow_cur_tp: '',
  163. stage_flow_pre_uid: auditor.aid, stage_flow_pre_info: user_info, stage_flow_pre_tp: tp,
  164. });
  165. } else {
  166. await transaction.update(this.tableName, {
  167. id: delStage.tid, stage_count: 0, stage_complete_count: 0, stage_status: 0,
  168. stage_flow_cur_uid: 0, stage_flow_cur_info: '', stage_flow_cur_tp: '',
  169. stage_flow_pre_uid: 0, stage_flow_pre_info: '', stage_flow_pre_tp: '',
  170. });
  171. }
  172. }
  173. async updateStageCache4Start(transaction, stage, status, auditor, ledgerTp, stageTp) {
  174. const orgCache = await this.getDataById(stage.tid);
  175. const data = { id: stage.tid, stage_status: status };
  176. if (ledgerTp) data.ledger_tp = JSON.stringify(ledgerTp);
  177. const tp = JSON.parse(orgCache.stage_flow_cur_tp);
  178. if (stageTp) this._.assign(tp, stageTp);
  179. data.stage_flow_pre_uid = orgCache.stage_flow_cur_uid;
  180. const info = JSON.parse(orgCache.stage_flow_cur_info);
  181. info.time = new Date();
  182. data.stage_flow_pre_info = JSON.stringify(info);
  183. data.stage_flow_pre_tp = JSON.stringify(tp);
  184. data.stage_flow_cur_uid = auditor.aid;
  185. const cur_flow_info = await this.ctx.service.projectAccount.getAccountCacheData(auditor.aid);
  186. cur_flow_info.order = stage.order;
  187. cur_flow_info.status = auditConst.stage.status.checking;
  188. data.stage_flow_cur_info = JSON.stringify(cur_flow_info);
  189. data.stage_flow_cur_tp = JSON.stringify(tp);
  190. await transaction.update(this.tableName, data);
  191. }
  192. async updateStageCache4Flow(transaction, stage, status, auditor, preAuditor, ledgerTp, stageTp, pcTp) {
  193. const orgCache = await this.getDataById(stage.tid);
  194. const data = { id: stage.tid, stage_status: status };
  195. if (ledgerTp) data.ledger_tp = JSON.stringify(ledgerTp);
  196. 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) : {});
  197. if (stageTp) this._.assign(tp, stageTp);
  198. if (pcTp) this._.assign(tp, pcTp);
  199. if (preAuditor) {
  200. data.stage_flow_pre_uid = preAuditor.aid;
  201. const info = await this.ctx.service.projectAccount.getAccountCacheData(preAuditor.aid);
  202. info.order = preAuditor.order;
  203. info.status = preAuditor.status;
  204. info.time = new Date();
  205. data.stage_flow_pre_info = JSON.stringify(info);
  206. } else {
  207. data.stage_flow_pre_uid = orgCache.stage_flow_cur_uid;
  208. data.stage_flow_pre_info = orgCache.stage_flow_cur_info;
  209. }
  210. data.stage_flow_pre_tp = JSON.stringify(tp);
  211. if (auditor) {
  212. data.stage_flow_cur_uid = auditor.aid;
  213. const info = await this.ctx.service.projectAccount.getAccountCacheData(auditor.aid);
  214. info.order = auditor.order;
  215. info.status = status;
  216. data.stage_flow_cur_info = JSON.stringify(info);
  217. }
  218. if (status === auditConst.stage.status.checked && !auditor) data.stage_complete_count = stage.order;
  219. data.stage_flow_cur_tp = JSON.stringify(tp);
  220. await transaction.update(this.tableName, data);
  221. }
  222. async updateStageCache4Revise(transaction, tid, ledgerTp, pcTp) {
  223. const orgCache = await this.getDataById(tid);
  224. const data = { id: tid };
  225. if (ledgerTp) data.ledger_tp = JSON.stringify(ledgerTp);
  226. if (data.stage_status !== auditConst.stage.status.checked && pcTp) {
  227. const curTp = JSON.parse(orgCache.stage_flow_cur_tp);
  228. this._.assign(curTp, pcTp);
  229. data.stage_flow_cur_tp = JSON.stringify(curTp);
  230. if (orgCache.stage_status !== auditConst.stage.status.uncheck) {
  231. const preTp = JSON.parse(orgCache.stage_flow_pre_tp);
  232. this._.assign(preTp, pcTp);
  233. data.stage_flow_cur_tp = JSON.stringify(preTp);
  234. }
  235. }
  236. await transaction.update(this.tableName, data);
  237. }
  238. async updateAdvanceCache(tid) {
  239. const advance_tp = await this.ctx.service.advance.getSumAdvance(tid);
  240. await this.db.update(this.tableName, { id: tid, advance_tp });
  241. }
  242. async updateChangeCache(tid) {
  243. 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
  244. FROM ${this.ctx.service.change.tableName} WHERE tid = ? AND status = ? And valid = 1`;
  245. const changeSum = await this.db.queryOne(sql, [tender.id, auditConst.flow.status.checked]);
  246. await this.db.update(this.tableName, { id: tid, change_tp: changeSum.total_price || 0 });
  247. }
  248. async updateContractPriceCache(tender) {
  249. const tenderInfo = await this.ctx.service.tenderInfo.getTenderInfo(tender.id, tender.project_id);
  250. await this.db.update(this.tableName, { id: tender.id, contract_price: tenderInfo.deal_param.contractPrice || 0 });
  251. }
  252. async _refreshLedgerRela(tender, data) {
  253. data.ledger_status = tender.ledger_status || 0;
  254. if (tender.ledger_status === auditConst.ledger.status.uncheck) {
  255. data.ledger_flow_cur_uid = tender.user_id;
  256. const user = await this.ctx.service.projectAccount.getAccountCacheData(tender.user_id);
  257. user.status = tender.ledger_status;
  258. user.order = 0;
  259. data.ledger_flow_cur_info = JSON.stringify(user);
  260. } else if (tender.ledger_status === auditConst.ledger.status.checked) {
  261. const pre = await this.ctx.service.ledgerAudit.getLastestAuditor(tender.id, tender.ledger_times, tender.ledger_status);
  262. data.ledger_flow_pre_uid = pre.audit_id;
  263. data.ledger_flow_pre_info = JSON.stringify({
  264. order: pre.order, status: pre.status, time: pre.end_time,
  265. name: pre.name, company: pre.company, role: pre.role, mobile: pre.mobile, telephone: pre.telephone,
  266. });
  267. } else if (tender.ledger_status === auditConst.ledger.status.checkNo) {
  268. data.ledger_flow_cur_uid = tender.user_id;
  269. const user = await this.ctx.service.projectAccount.getAccountCacheData(tender.user_id);
  270. user.order = 0;
  271. user.status = auditConst.ledger.status.uncheck;
  272. data.ledger_flow_cur_info = JSON.stringify(user);
  273. const pre = await this.ctx.service.ledgerAudit.getLastestAuditor(tender.id, tender.ledger_times - 1, tender.ledger_status);
  274. data.ledger_flow_pre_uid = pre.audit_id;
  275. data.ledger_flow_pre_info = JSON.stringify({
  276. order: pre.order, status: pre.status, time: pre.end_time,
  277. name: pre.name, company: pre.company, role: pre.role, mobile: pre.mobile, telephone: pre.telephone,
  278. });
  279. } else {
  280. const cur = await this.ctx.service.ledgerAudit.getLastestAuditor(tender.id, tender.ledger_times, tender.ledger_status);
  281. if (cur) {
  282. data.ledger_flow_cur_uid = cur.audit_id;
  283. data.ledger_flow_cur_info = JSON.stringify({
  284. order: cur.order, status: cur.status, time: cur.end_time,
  285. name: cur.name, company: cur.company, role: cur.role, mobile: cur.mobile, telephone: cur.telephone,
  286. });
  287. if (cur.audit_order === 1) {
  288. data.ledger_flow_pre_uid = tender.user_id;
  289. const user = await await this.ctx.service.projectAccount.getAccountCacheData(tender.user_id, { order: 0, time: cur.begin_time });
  290. data.ledger_flow_pre_info = JSON.stringify(user);
  291. } else {
  292. const pre = await this.ctx.service.ledgerAudit.getAuditorByOrder(tender.id, cur.audit_order - 1, cur.times);
  293. data.ledger_flow_pre_uid = pre.audit_id;
  294. data.ledger_flow_pre_info = JSON.stringify({
  295. order: pre.order, status: pre.status, time: pre.end_time,
  296. name: pre.name, company: pre.company, role: pre.role, mobile: pre.mobile, telephone: pre.telephone,
  297. });
  298. }
  299. }
  300. }
  301. data.ledger_tp = JSON.stringify(await this.ctx.service.ledger.addUp({ tender_id: tender.id/* , is_leaf: true*/ }));
  302. const revise = await this.service.ledgerRevise.getLastestRevise(tender.id);
  303. data.revise_status = revise ? revise.status : 0;
  304. }
  305. async _calcTp (lastStage, tp) {
  306. lastStage.curTimes = lastStage.times;
  307. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(lastStage);
  308. const pcSum = await this.ctx.service.stageBillsPc.getSumTotalPrice(lastStage);
  309. tp.contract_tp = tpData.contract_tp;
  310. tp.qc_tp = tpData.qc_tp;
  311. tp.positive_qc_tp = tpData.positive_qc_tp;
  312. tp.negative_qc_tp = tpData.negative_qc_tp;
  313. tp.contract_pc_tp = pcSum.contract_pc_tp;
  314. tp.qc_pc_tp = pcSum.qc_pc_tp;
  315. tp.pc_tp = pcSum.pc_tp;
  316. tp.positive_qc_pc_tp = pcSum.positive_qc_pc_tp;
  317. tp.negative_qc_pc_tp = pcSum.negative_qc_pc_tp;
  318. const payTp = await this.ctx.service.stagePay.getSpecialTotalPrice(lastStage);
  319. tp.yf_tp = payTp.yf;
  320. tp.sf_tp = payTp.sf;
  321. };
  322. async _refreshStageRela(tender, data) {
  323. if (tender.ledger_status === auditConst.ledger.status.checked) {
  324. const lastStage = await this.ctx.service.stage.getLastestStage(tender.id, true);
  325. if (!lastStage) return;
  326. data.stage_count = lastStage.order;
  327. data.stage_complete_count = lastStage.status === auditConst.stage.status.checked ? lastStage.order : lastStage.order - 1;
  328. data.stage_status = lastStage.status;
  329. const tp = {
  330. 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,
  331. 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,
  332. yf_tp: lastStage.yf_tp, sf_tp: lastStage.sf_tp,
  333. pre_contract_tp: lastStage.pre_contract_tp, pre_qc_tp: lastStage.pre_qc_tp,
  334. pre_positive_qc_tp: lastStage.pre_positive_qc_tp, pre_negative_qc_tp: lastStage.pre_positive_qc_tp,
  335. pre_yf_tp: lastStage.pre_yf_tp, pre_sf_tp: lastStage.pre_sf_tp,
  336. };
  337. const preStage = lastStage.order > 1 ? await this.ctx.service.stage.getDataByCondition({ tid: lastStage.tid, order: lastStage.order - 1}) : null;
  338. if (lastStage.status === auditConst.stage.status.uncheck) {
  339. if (preStage) {
  340. const preAuditor = await this.ctx.service.stageAudit.getLastestAuditor(preStage.id, preStage.times, preStage.status);
  341. data.stage_flow_pre_uid = preAuditor.aid;
  342. data.stage_flow_pre_info = JSON.stringify({
  343. order: preAuditor.order, status: preAuditor.status, time: preAuditor.end_time,
  344. name: preAuditor.name, company: preAuditor.company, role: preAuditor.role, mobile: preAuditor.mobile, telephone: preAuditor.telephone,
  345. });
  346. data.stage_flow_pre_tp = JSON.stringify({
  347. 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,
  348. 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,
  349. yf_tp: preStage.yf_tp, sf_tp: preStage.sf_tp,
  350. pre_contract_tp: preStage.pre_contract_tp, pre_qc_tp: preStage.pre_qc_tp,
  351. pre_positive_qc_tp: preStage.pre_positive_qc_tp, pre_negative_qc_tp: preStage.pre_positive_qc_tp,
  352. pre_yf_tp: preStage.pre_yf_tp, pre_sf_tp: preStage.pre_sf_tp,
  353. });
  354. }
  355. lastStage.curOrder = 0;
  356. await this._calcTp(lastStage, tp);
  357. data.stage_flow_cur_uid = lastStage.user_id;
  358. const curInfo = await this.ctx.service.projectAccount.getAccountCacheData(lastStage.user_id, { order: 0, status: lastStage.status });
  359. data.stage_flow_cur_info = JSON.stringify(curInfo);
  360. data.stage_flow_cur_tp = JSON.stringify(tp);
  361. } else if (lastStage.status === auditConst.stage.status.checked) {
  362. const preAuditor = await this.ctx.service.stageAudit.getLastestAuditor(lastStage.id, lastStage.times, lastStage.status);
  363. lastStage.curOrder = preAuditor.order;
  364. await this._calcTp(lastStage, tp);
  365. data.stage_flow_pre_uid = preAuditor.aid;
  366. data.stage_flow_pre_info = JSON.stringify({
  367. order: preAuditor.order, status: preAuditor.status, time: preAuditor.end_time,
  368. name: preAuditor.name, company: preAuditor.company, role: preAuditor.role, mobile: preAuditor.mobile, telephone: preAuditor.telephone,
  369. });
  370. data.stage_flow_cur_tp = JSON.stringify(tp);
  371. data.stage_flow_pre_tp = JSON.stringify(tp);
  372. } else if (lastStage.status === auditConst.stage.status.checkNo) {
  373. lastStage.curOrder = 0;
  374. await this._calcTp(lastStage, tp);
  375. data.stage_flow_cur_uid = lastStage.user_id;
  376. const curInfo = await this.ctx.service.projectAccount.getAccountCacheData(lastStage.user_id, { order: 0, status: auditConst.stage.status.uncheck });
  377. data.stage_flow_cur_info = JSON.stringify(curInfo);
  378. data.stage_flow_cur_tp = JSON.stringify(tp);
  379. const preAuditor = await this.ctx.service.stageAudit.getLastestAuditor(lastStage.id, lastStage.times - 1, lastStage.status);
  380. data.stage_flow_pre_uid = preAuditor.aid;
  381. data.stage_flow_pre_info = JSON.stringify({
  382. order: preAuditor.order, status: preAuditor.status, time: preAuditor.end_time,
  383. name: preAuditor.name, company: preAuditor.company, role: preAuditor.role, mobile: preAuditor.mobile, telephone: preAuditor.telephone,
  384. });
  385. const his = lastStage.tp_history.find(x => { return x.times === preAuditor.times && x.order === preAuditor.order; });
  386. if (his) {
  387. tp.contract_tp = his.contract_tp;
  388. tp.qc_tp = his.qc_tp;
  389. tp.positive_qc_tp = his.positive_qc_tp;
  390. tp.negative_qc_tp = his.negative_qc_tp;
  391. tp.yf_tp = his.yf_tp;
  392. tp.sf_tp = his.sf_tp;
  393. }
  394. data.stage_flow_pre_tp = JSON.stringify(tp);
  395. } else {
  396. const curAuditor = await this.ctx.service.stageAudit.getLastestAuditor(lastStage.id, lastStage.times, lastStage.status);
  397. lastStage.curOrder = curAuditor.order;
  398. await this._calcTp(lastStage, tp);
  399. data.stage_flow_cur_uid = curAuditor.aid;
  400. data.stage_flow_cur_info = JSON.stringify({
  401. order: curAuditor.order, status: curAuditor.status,
  402. name: curAuditor.name, company: curAuditor.company, role: curAuditor.role, mobile: curAuditor.mobile, telephone: curAuditor.telephone,
  403. });
  404. data.stage_flow_cur_tp = JSON.stringify(tp);
  405. const preAuditor = curAuditor.order > 1 ? await this.ctx.service.stageAudit.getAuditorByOrder(lastStage.id, lastStage.times, curAuditor.order - 1) : null;
  406. if (preAuditor) {
  407. data.stage_flow_pre_uid = preAuditor.aid;
  408. data.stage_flow_pre_info = JSON.stringify({
  409. order: preAuditor.order, status: preAuditor.status, time: curAuditor.begin_time,
  410. name: preAuditor.name, company: preAuditor.company, role: preAuditor.role, mobile: preAuditor.mobile, telephone: preAuditor.telephone,
  411. });
  412. } else {
  413. data.stage_flow_pre_uid = lastStage.user_id;
  414. const preInfo = await this.ctx.service.projectAccount.getAccountCacheData(lastStage.user_id, { order: 0, status: auditConst.stage.status.uncheck, time: curAuditor.begin_time });
  415. data.stage_flow_pre_info = JSON.stringify(preInfo);
  416. }
  417. const his = preAuditor
  418. ? lastStage.tp_history.find(x => { return x.times === preAuditor.times && x.order === preAuditor.order; })
  419. : lastStage.tp_history.find(x => { return x.times === lastStage.times && x.order === 0; });
  420. if (his) {
  421. tp.contract_tp = his.contract_tp;
  422. tp.qc_tp = his.qc_tp;
  423. tp.positive_qc_tp = his.positive_qc_tp;
  424. tp.negative_qc_tp = his.negative_qc_tp;
  425. tp.yf_tp = his.yf_tp;
  426. tp.sf_tp = his.sf_tp;
  427. }
  428. data.stage_flow_pre_tp = JSON.stringify(tp);
  429. }
  430. }
  431. }
  432. async refreshTenderCache(tender, transaction) {
  433. const orgCache = await this.getDataById(tender.id);
  434. const data = { id: tender.id, ledger_status: 0,
  435. ledger_flow_cur_uid: 0, ledger_flow_cur_info: '', ledger_flow_pre_uid: 0, ledger_flow_pre_info: '',
  436. stage_count: 0, stage_complete_count: 0, stage_status: 0,
  437. stage_flow_cur_uid: 0, stage_flow_cur_info: '', stage_flow_cur_tp: '',
  438. stage_flow_pre_uid: 0, stage_flow_pre_info: '', stage_flow_pre_tp: '',
  439. };
  440. // 台账
  441. await this._refreshLedgerRela(tender, data);
  442. // 计量
  443. await this._refreshStageRela(tender, data);
  444. // 其他计算项
  445. const tenderInfo = await this.ctx.service.tenderInfo.getTenderInfo(tender.id, tender.project_id);
  446. data.contract_price = tenderInfo.deal_param.contractPrice || 0;
  447. data.advance_tp = await this.ctx.service.advance.getSumAdvance(tender.id);
  448. 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]);
  449. data.change_tp = changeSum.total_price || 0;
  450. const conn = transaction || this.db;
  451. if (orgCache) {
  452. conn.update(this.tableName, data);
  453. } else {
  454. conn.insert(this.tableName, data);
  455. }
  456. }
  457. }
  458. return TenderCache;
  459. };