tender_cache.js 34 KB

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