tender_cache.js 35 KB

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