tender_cache.js 34 KB

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