tender_cache.js 29 KB

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