tender_cache.js 37 KB

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