stage.js 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  1. 'use strict';
  2. /**
  3. * 期计量 数据模型
  4. *
  5. * @author Mai
  6. * @date 2018/8/13
  7. * @version
  8. */
  9. const auditConst = require('../const/audit');
  10. const payConst = require('../const/deal_pay.js');
  11. const roleRelSvr = require('./role_rpt_rel');
  12. const fs = require('fs');
  13. const path = require('path');
  14. const _ = require('lodash');
  15. const projectLogConst = require('../const/project_log');
  16. const syncApiConst = require('../const/sync_api');
  17. const RevisePrice = require('../lib/revise_price');
  18. module.exports = app => {
  19. class Stage extends app.BaseService {
  20. /**
  21. * 构造函数
  22. *
  23. * @param {Object} ctx - egg全局变量
  24. * @return {void}
  25. */
  26. constructor(ctx) {
  27. super(ctx);
  28. this.tableName = 'stage';
  29. }
  30. /**
  31. * 根据id查找数据
  32. *
  33. * @param {Number} id - 数据库中的id
  34. * @return {Object} - 返回单条数据
  35. */
  36. async getDataById(id) {
  37. const result = await this.db.get(this.tableName, { id });
  38. if (result) { result.tp_history = result.tp_history ? JSON.parse(result.tp_history) : []; }
  39. return result;
  40. }
  41. /**
  42. * 根据条件查找单条数据
  43. *
  44. * @param {Object} condition - 筛选条件
  45. * @return {Object} - 返回单条数据
  46. */
  47. async getDataByCondition(condition) {
  48. const result = await this.db.get(this.tableName, condition);
  49. if (result) { result.tp_history = result.tp_history ? JSON.parse(result.tp_history) : []; }
  50. return result;
  51. }
  52. /**
  53. * 根据条件查找列表数据
  54. *
  55. * @param {Object} condition - 筛选条件
  56. * @return {Array} - 返回数据
  57. */
  58. async getAllDataByCondition(condition) {
  59. const result = await this.db.select(this.tableName, condition);
  60. for (const r of result) {
  61. r.tp_history = r.tp_history ? JSON.parse(r.tp_history) : [];
  62. }
  63. return result;
  64. }
  65. async loadStageUser(stage) {
  66. const status = auditConst.stage.status;
  67. const accountId = this.ctx.session.sessionUser.accountId;
  68. stage.user = await this.ctx.service.projectAccount.getAccountInfoById(stage.user_id);
  69. stage.auditors = await this.ctx.service.stageAudit.getAuditors(stage.id, stage.times); // 全部参与的审批人
  70. stage.auditorIds = this._.map(stage.auditors, 'aid');
  71. stage.curAuditors = stage.auditors.filter(x => { return x.status === status.checking; }); // 当前流程中审批中的审批人
  72. stage.curAuditorIds = this._.map(stage.curAuditors, 'aid');
  73. stage.flowAuditors = stage.curAuditors.length > 0 ? stage.auditors.filter(x => { return x.order === stage.curAuditors[0].order; }) : []; // 当前流程中参与的审批人(包含会签时,审批通过的人)
  74. stage.flowAuditorIds = this._.map(stage.flowAuditors, 'aid');
  75. stage.nextAuditors = stage.curAuditors.length > 0 ? stage.auditors.filter(x => { return x.order === stage.curAuditors[0].order + 1; }) : [];
  76. stage.nextAuditorIds = this._.map(stage.nextAuditors, 'aid');
  77. stage.auditorGroups = this.ctx.helper.groupAuditors(stage.auditors);
  78. stage.userGroups = this.ctx.helper.groupAuditorsUniq(stage.auditorGroups);
  79. stage.userGroups.unshift([{
  80. aid: stage.user.id, order: 0, times: stage.times, audit_order: 0, audit_type: auditConst.auditType.key.common,
  81. name: stage.user.name, role: stage.user.role, company: stage.user.company
  82. }]);
  83. stage.finalAuditorIds = stage.userGroups[stage.userGroups.length - 1].map(x => { return x.aid; });
  84. stage.assists = await this.service.stageAuditAss.getData(stage); // 全部协同人
  85. stage.assists = stage.assists.filter(x => {
  86. return x.user_id === stage.user_id || stage.auditorIds.indexOf(x.user_id) >= 0;
  87. }); // 过滤无效协同人
  88. stage.userAssists = stage.assists.filter(x => { return x.user_id === stage.user_id; }); // 原报协同人
  89. stage.userAssistIds = this._.map(stage.userAssists, 'ass_user_id');
  90. stage.auditAssists = stage.assists.filter(x => { return x.user_id !== stage.user_id; }); // 审批协同人
  91. stage.auditAssistIds = this._.map(stage.auditAssists, 'ass_user_id');
  92. stage.relaAssists = stage.assists.filter(x => { return x.user_id === accountId }); // 登录人的协同人
  93. stage.userIds = stage.status === status.uncheck // 当前流程下全部参与人id
  94. ? [stage.user_id, ...stage.userAssistIds]
  95. : [stage.user_id, ...stage.userAssistIds, ...stage.auditorIds, ...stage.auditAssistIds];
  96. }
  97. async loadStageAuditViewData(stage) {
  98. const times = stage.status === auditConst.stage.status.checkNo ? stage.times - 1 : stage.times;
  99. if (!stage.user) stage.user = await this.ctx.service.projectAccount.getAccountInfoById(stage.user_id);
  100. stage.auditHistory = await this.ctx.service.stageAudit.getAuditorHistory(stage.id, times);
  101. // 获取审批流程中左边列表
  102. if (stage.status === auditConst.stage.status.checkNo && stage.user_id !== this.ctx.session.sessionUser.accountId) {
  103. const auditors = await this.ctx.service.stageAudit.getAuditors(stage.id, times); // 全部参与的审批人
  104. const auditorGroups = this.ctx.helper.groupAuditors(auditors);
  105. stage.auditors2 = this.ctx.helper.groupAuditorsUniq(auditorGroups);
  106. stage.auditors2.unshift([{
  107. aid: stage.user.id, order: 0, times: stage.times - 1, audit_order: 0, audit_type: auditConst.auditType.key.common,
  108. name: stage.user.name, role: stage.user.role, company: stage.user.company
  109. }]);
  110. } else {
  111. stage.auditors2 = stage.userGroups;
  112. }
  113. if (stage.status === auditConst.stage.status.uncheck || stage.status === auditConst.stage.status.checkNo) {
  114. stage.auditorList = await this.ctx.service.stageAudit.getAuditors(stage.id, stage.times);
  115. }
  116. }
  117. async loadPreCheckedStage(stage) {
  118. if (stage.order > 1) {
  119. const preCheckedStages = await this.ctx.service.stage.getAllDataByCondition({
  120. where: { tid: stage.tid, status: auditConst.stage.status.checked },
  121. orders: [['order', 'desc']],
  122. });
  123. stage.preCheckedStage = preCheckedStages[0];
  124. } else {
  125. stage.preCheckedStage = undefined;
  126. }
  127. stage.isCheckFirst = stage.order > 1 ? (stage.preCheckedStage ? stage.preCheckedStage.order === stage.order - 1 : false) : true;
  128. }
  129. async doCheckStage(stage, force = false) {
  130. const status = auditConst.stage.status;
  131. await this.loadStageUser(stage);
  132. await this.loadPreCheckedStage(stage);
  133. const accountId = this.ctx.session.sessionUser.accountId, shareIds = [];
  134. const isTenderTourist = await this.service.tenderTourist.getDataByCondition({ tid: stage.tid, user_id: accountId });
  135. const permission = this.ctx.session.sessionUser.permission;
  136. if (stage.status === status.uncheck) {
  137. stage.readOnly = accountId !== stage.user_id && stage.userAssistIds.indexOf(accountId) < 0;
  138. if (!stage.readOnly) {
  139. stage.assist = stage.userAssists.find(x => { return x.ass_user_id === accountId; });
  140. }
  141. stage.curTimes = stage.times;
  142. stage.curOrder = 0;
  143. } else if (stage.status === status.checkNo) {
  144. stage.readOnly = accountId !== stage.user_id && stage.userAssistIds.indexOf(accountId) < 0;
  145. const checkNoAudit = await this.service.stageAudit.getDataByCondition({
  146. sid: stage.id, times: stage.times - 1, status: status.checkNo,
  147. });
  148. if (!stage.readOnly) {
  149. stage.assist = stage.userAssists.find(x => { return x.ass_user_id === accountId; });
  150. stage.curTimes = stage.times;
  151. stage.curOrder = 0;
  152. } else {
  153. stage.curTimes = stage.times - 1;
  154. stage.curOrder = checkNoAudit.order;
  155. }
  156. } else if (stage.status === status.checked) {
  157. stage.readOnly = true;
  158. stage.curTimes = stage.times;
  159. stage.curOrder = _.max(_.map(stage.auditors, 'order'));
  160. } else {
  161. const ass = stage.auditAssists.find(x => { return stage.flowAuditorIds.indexOf(x.user_id) >= 0 && x.ass_user_id === accountId; });
  162. stage.readOnly = stage.flowAuditorIds.indexOf(accountId) < 0 && !ass;
  163. stage.curTimes = stage.times;
  164. if (!stage.readOnly) {
  165. stage.assist = ass;
  166. stage.curOrder = stage.curAuditors[0].order;
  167. } else {
  168. stage.curOrder = stage.curAuditors[0].order - 1;
  169. }
  170. // 会签,会签人审批通过时,只读,但是curOrder需按原来的取值
  171. if (!stage.readOnly) {
  172. stage.readOnly = !_.isEqual(stage.flowAuditorIds, stage.curAuditorIds);
  173. stage.canCheck = true;
  174. }
  175. }
  176. if (stage.readOnly) {
  177. stage.assist = accountId === stage.user_id || stage.auditorIds.indexOf(accountId) >= 0
  178. ? null
  179. : stage.assists.find(x => { return x.ass_user_id === accountId});
  180. }
  181. if (stage.userIds.indexOf(accountId) >= 0) {
  182. stage.filePermission = true;
  183. } else if (!!isTenderTourist || force) {
  184. stage.filePermission = this.tender && this.tender.touristPermission ? this.tender.touristPermission.file : false;
  185. } else {
  186. stage.filePermission = false;
  187. }
  188. let time = stage.readOnly ? stage.cache_time_r : stage.cache_time_l;
  189. if (!time) time = stage.in_time ? stage.in_time : new Date();
  190. stage.cacheTime = time.getTime();
  191. // 历史台账
  192. if (stage.status === status.checked) {
  193. stage.ledgerHis = await this.service.ledgerHistory.getDataById(stage.his_id);
  194. }
  195. // 是否台账修订中
  196. const lastRevise = await this.service.ledgerRevise.getLastestRevise(stage.tid);
  197. stage.revising = (lastRevise && lastRevise.status !== auditConst.revise.status.checked) || false;
  198. return stage;
  199. }
  200. async doCheckStageCanCancel(stage) {
  201. // 获取当前审批人的上一个审批人,判断是否是当前登录人,并赋予撤回功能,(当审批人存在有审批过时,上一人不允许再撤回)
  202. const status = auditConst.stage.status;
  203. const accountId = this.ctx.session.sessionUser.accountId;
  204. stage.cancancel = 0;
  205. if (stage.status !== status.checked && stage.status !== status.uncheck) {
  206. if (stage.status !== status.checkNo) {
  207. // 找出当前操作人上一个审批人,包括审批完成的和退回上一个审批人的,同时当前操作人为第一人时,就是则为原报
  208. if (stage.flowAuditors.find(x => { return x.status !== auditConst.stage.status.checking}) && stage.flowAuditorIds.indexOf(accountId) < 0) return; // 当前流程存在审批人审批通过时,不可撤回
  209. const flowAssists = stage.auditAssists.filter(x => { return stage.flowAuditorIds.indexOf(x.user_id) >= 0; });
  210. if (flowAssists.find(x => { return x.confirm; })) return; //当前流程存在协审人确认时,不可撤回
  211. if (stage.curAuditorIds.indexOf(accountId) < 0 && stage.flowAuditorIds.indexOf(accountId) >= 0) {
  212. stage.cancancel = 5; // 会签未全部审批通过时,审批人撤回审批通过
  213. return;
  214. }
  215. const preAuditors = stage.curAuditors[0].order !== 1 ? stage.auditors.filter(x => { return x.order === stage.curAuditors[0].order - 1; }) : [];
  216. const preAuditorCheckAgain = preAuditors.find(pa => { return pa.status === status.checkAgain; });
  217. const preAuditorCheckCancel = preAuditors.find(pa => { return pa.status === status.checkCancel; });
  218. const preAuditorHasOld = preAuditors.find(pa => { return pa.is_old === 1; });
  219. const preAuditorIds = (preAuditorCheckAgain ? [] : preAuditors.map(x => { return x.aid })); // 重审不可撤回
  220. if ((this._.isEqual(stage.flowAuditorIds, preAuditorIds) && preAuditorCheckCancel) || preAuditorHasOld) {
  221. return; // 不可以多次撤回
  222. }
  223. const preAuditChecked = preAuditors.find(pa => { return pa.status === status.checked && pa.aid === accountId; });
  224. const preAuditCheckNoPre = preAuditors.find(pa => { return pa.status === status.checkNoPre && pa.aid === accountId; });
  225. if (preAuditorIds.indexOf(accountId) >= 0) {
  226. if (preAuditChecked) {
  227. stage.cancancel = 2;// 审批人撤回审批通过
  228. } else if (preAuditCheckNoPre) {
  229. stage.cancancel = 3;// 审批人撤回审批退回上一人
  230. }
  231. stage.preAuditors = preAuditors;
  232. } else if (preAuditors.length === 0 && accountId === stage.user_id) {
  233. stage.cancancel = 1;// 原报撤回
  234. }
  235. } else {
  236. const lastAuditors = await this.service.stageAudit.getAuditors(stage.id, stage.times - 1);
  237. const onAuditor = _.findLast(lastAuditors, { status: status.checkNo });
  238. if (onAuditor.aid === accountId) {
  239. stage.cancancel = 4;// 审批人撤回退回原报
  240. stage.preAuditors = lastAuditors.filter(x => { return x.order === onAuditor.order });
  241. }
  242. }
  243. }
  244. }
  245. async checkStage(sid) {
  246. if (!this.ctx.stage) {
  247. const stage = await this.ctx.service.stage.getDataById(sid);
  248. if (!stage) throw '校验的期数据不存在';
  249. await this.doCheckStage(stage);
  250. this.ctx.stage = stage;
  251. }
  252. }
  253. /**
  254. * 获取 最新一期 期计量
  255. * @param tenderId
  256. * @param includeUnCheck
  257. * @return {Promise<*>}
  258. */
  259. async getLastestStage(tenderId, includeUnCheck = false) {
  260. this.initSqlBuilder();
  261. this.sqlBuilder.setAndWhere('tid', {
  262. value: tenderId,
  263. operate: '=',
  264. });
  265. if (!includeUnCheck) {
  266. this.sqlBuilder.setAndWhere('status', {
  267. value: auditConst.stage.status.uncheck,
  268. operate: '!=',
  269. });
  270. }
  271. this.sqlBuilder.orderBy = [['order', 'desc']];
  272. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  273. const stage = await this.db.queryOne(sql, sqlParam);
  274. if (stage) stage.tp_history = stage.tp_history ? JSON.parse(stage.tp_history) : [];
  275. return stage;
  276. }
  277. /**
  278. * 获取 最新一期 审批完成的 期计量
  279. * @param tenderId
  280. * @return {Promise<*>}
  281. */
  282. async getLastestCompleteStage(tenderId) {
  283. this.initSqlBuilder();
  284. this.sqlBuilder.setAndWhere('tid', {
  285. value: tenderId,
  286. operate: '=',
  287. });
  288. this.sqlBuilder.setAndWhere('status', {
  289. value: auditConst.stage.status.checked,
  290. operate: '=',
  291. });
  292. this.sqlBuilder.orderBy = [['order', 'desc']];
  293. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  294. const stage = await this.db.queryOne(sql, sqlParam);
  295. if (stage) stage.tp_history = stage.tp_history ? JSON.parse(stage.tp_history) : [];
  296. return stage;
  297. }
  298. /**
  299. * 获取标段下的期列表(报表选择用,只需要一些key信息,不需要计算详细数据,也懒得一个个字段写了,用*来处理)
  300. * @param tenderId
  301. * @return {Promise<void>}
  302. */
  303. async getValidStagesShort(tenderId) {
  304. const sql = 'select * from zh_stage where tid = ? order by zh_stage.order';
  305. const sqlParam = [tenderId];
  306. return await this.db.query(sql, sqlParam);
  307. }
  308. /**
  309. * 获取某一期信息(报表用)
  310. * @param stageId
  311. * @return {Promise<void>}
  312. */
  313. async getStageById(stageId) {
  314. const sql = 'select * from zh_stage where id = ? order by zh_stage.order';
  315. const sqlParam = [stageId];
  316. return await this.db.query(sql, sqlParam);
  317. }
  318. async checkStageGatherData(stage, force = false) {
  319. // 最新一期计量(未审批完成),当前操作人的期详细数据,应实时计算
  320. if (stage.status !== auditConst.stage.status.checked) {
  321. await this.doCheckStage(stage, force);
  322. if (!stage.readOnly && stage.check_calc) {
  323. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(stage);
  324. const pcSum = await this.ctx.service.stageBillsPc.getSumTotalPrice(stage);
  325. stage.contract_tp = tpData.contract_tp;
  326. stage.qc_tp = tpData.qc_tp;
  327. stage.positive_qc_tp = tpData.positive_qc_tp;
  328. stage.negative_qc_tp = tpData.negative_qc_tp;
  329. stage.contract_pc_tp = pcSum.contract_pc_tp;
  330. stage.qc_pc_tp = pcSum.qc_pc_tp;
  331. stage.pc_tp = pcSum.pc_tp;
  332. stage.positive_qc_pc_tp = pcSum.positive_qc_pc_tp;
  333. stage.negative_qc_pc_tp = pcSum.negative_qc_pc_tp;
  334. stage.tp = this.ctx.helper.sum([stage.contract_tp, stage.qc_tp, stage.pc_tp]);
  335. const tp = await this.ctx.service.stagePay.getSpecialTotalPrice(stage);
  336. stage.yf_tp = tp.yf;
  337. stage.sf_tp = tp.sf;
  338. stage.end_tp = this.ctx.helper.add(stage.pre_tp, stage.tp);
  339. await this.update({
  340. check_calc: false,
  341. contract_tp: stage.contract_tp, qc_tp: stage.qc_tp,
  342. positive_qc_tp: stage.positive_qc_tp, negative_qc_tp: stage.negative_qc_tp,
  343. contract_pc_tp: stage.contract_pc_tp || 0, qc_pc_tp: stage.qc_pc_tp || 0, pc_tp: stage.pc_tp || 0,
  344. positive_qc_pc_tp: stage.positive_qc_pc_tp || 0, negative_qc_pc_tp: stage.negative_qc_pc_tp || 0,
  345. yf_tp: stage.yf_tp, sf_tp: stage.sf_tp,
  346. }, { id: stage.id });
  347. } else if (stage.tp_history) {
  348. const his = this.ctx.helper._.find(stage.tp_history, { times: stage.curTimes, order: stage.curOrder });
  349. if (his) {
  350. stage.contract_tp = his.contract_tp;
  351. stage.qc_tp = his.qc_tp;
  352. stage.positive_qc_tp = his.positive_qc_tp;
  353. stage.negative_qc_tp = his.negative_qc_tp;
  354. stage.yf_tp = his.yf_tp;
  355. stage.sf_tp = his.sf_tp;
  356. stage.tp = this.ctx.helper.sum([stage.contract_tp, stage.qc_tp, stage.pc_tp]);
  357. stage.end_tp = this.ctx.helper.add(stage.pre_tp, stage.tp);
  358. }
  359. }
  360. }
  361. }
  362. /**
  363. * 获取标段下的全部计量期,按倒序
  364. * @param tenderId
  365. * @return {Promise<void>}
  366. */
  367. async getValidStages(tenderId) {
  368. const stages = await this.db.select(this.tableName, {
  369. where: { tid: tenderId },
  370. orders: [['order', 'desc']],
  371. });
  372. for (const s of stages) {
  373. s.tp_history = s.tp_history ? JSON.parse(s.tp_history) : [];
  374. }
  375. if (stages.length !== 0 && !this.ctx.session.sessionUser.is_admin) {
  376. const lastStage = stages[0];
  377. if (lastStage.status === auditConst.stage.status.uncheck && !this.ctx.tender.isTourist) {
  378. const assist = await this.ctx.service.auditAss.getAllDataByCondition({ where: { tid: tenderId, user_id: lastStage.user_id } });
  379. const assistId = assist.map(x => { return x.ass_user_id });
  380. if (lastStage.user_id !== this.ctx.session.sessionUser.accountId && assistId.indexOf(this.ctx.session.sessionUser.accountId) < 0) {
  381. stages.splice(0, 1);
  382. }
  383. }
  384. }
  385. // 最新一期计量(未审批完成),当前操作人的期详细数据,应实时计算
  386. if (stages.length === 0) return stages;
  387. await this.checkStageGatherData(stages[0], true);
  388. for (const s of stages) {
  389. s.tp = this.ctx.helper.sum([s.contract_tp, s.qc_tp, s.pc_tp]);
  390. s.pre_tp = this.ctx.helper.add(s.pre_contract_tp, s.pre_qc_tp);
  391. s.end_tp = this.ctx.helper.add(s.pre_tp, s.tp);
  392. if (s.yf_tp && s.sf_tp === 0) {
  393. const sf = await this.ctx.service.stagePay.getHistorySf(s);
  394. if (sf && s.readOnly) {
  395. await this.ctx.service.stage.update({ sf_tp: sf.tp, pre_sf_tp: sf.pre_tp }, { id: s.id });
  396. }
  397. s.sf_tp = sf ? sf.tp : 0;
  398. }
  399. }
  400. return stages;
  401. }
  402. async getNextStages(tenderId, order) {
  403. const sql = 'SELECT * FROM ?? WHERE tid = ? AND `order` > ?';
  404. return await this.db.query(sql, [this.tableName, tenderId, order]);
  405. }
  406. async _getSumTp(condition, ...field) {
  407. const fieldSql = [];
  408. for (const f of field) {
  409. fieldSql.push('SUM(`' + f + '`) as ' + '`' + f + '`');
  410. }
  411. const sql = 'SELECT ' + fieldSql.join(', ') + ' FROM ' + this.tableName + ' ' + this.ctx.helper.whereSql(condition);
  412. return await this.db.queryOne(sql);
  413. }
  414. /**
  415. *
  416. * @param tenderId - 标段id
  417. * @param date - 计量年月
  418. * @param period - 开始-截止日期
  419. * @return {Promise<void>}
  420. */
  421. async addStage(tenderId, date, period) {
  422. const stages = await this.getAllDataByCondition({
  423. where: { tid: tenderId },
  424. order: [['order', 'asc']],
  425. });
  426. const preStage = stages[stages.length - 1];
  427. const preCheckedStage = stages.find(x => { return x.status === auditConst.stage.status.checked; });
  428. const order = stages.length + 1;
  429. const newStage = {
  430. sid: this.uuid.v4(),
  431. tid: tenderId,
  432. order,
  433. in_time: new Date(),
  434. s_time: date,
  435. period,
  436. times: 1,
  437. status: auditConst.stage.status.uncheck,
  438. user_id: this.ctx.session.sessionUser.accountId,
  439. check_calc: false,
  440. };
  441. newStage.cache_time_l = newStage.in_time;
  442. newStage.cache_time_r = newStage.in_time;
  443. if (preStage) {
  444. newStage.im_type = preStage.im_type;
  445. newStage.im_pre = preStage.im_pre;
  446. newStage.im_gather = preStage.im_gather;
  447. newStage.im_gather_node = preStage.im_gather_node;
  448. if (preCheckedStage) {
  449. newStage.pre_contract_tp = this.ctx.helper.sum([preCheckedStage.pre_contract_tp, preCheckedStage.contract_tp, preCheckedStage.contract_pc_tp]);
  450. newStage.pre_qc_tp = this.ctx.helper.sum([preCheckedStage.pre_qc_tp, preStage.qc_tp, preCheckedStage.qc_pc_tp]);
  451. newStage.pre_positive_qc_tp = this.ctx.helper.sum([preCheckedStage.pre_positive_qc_tp, preCheckedStage.positive_qc_tp, preCheckedStage.positive_qc_pc_tp]);
  452. newStage.pre_negative_qc_tp = this.ctx.helper.sum([preCheckedStage.pre_negative_qc_tp, preCheckedStage.negative_qc_tp, preCheckedStage.negative_qc_pc_tp]);
  453. newStage.pre_yf_tp = this.ctx.helper.add(preCheckedStage.pre_yf_tp, preStage.yf_tp);
  454. if (preCheckedStage.order === 1 || preCheckedStage.pre_sf_tp) {
  455. newStage.pre_sf_tp = this.ctx.helper.add(preCheckedStage.pre_sf_tp, preCheckedStage.sf_tp);
  456. } else {
  457. const sumTp = await this._getSumTp({tid: preCheckedStage.tid}, 'sf_tp');
  458. newStage.pre_sf_tp = sumTp.sf_tp || 0;
  459. }
  460. }
  461. } else {
  462. const projFunRela = await this.ctx.service.project.getFunRela(this.ctx.session.sessionProject.id);
  463. newStage.im_type = projFunRela.imType;
  464. }
  465. const transaction = await this.db.beginTransaction();
  466. try {
  467. // 新增期记录
  468. const result = await transaction.insert(this.tableName, newStage);
  469. if (result.affectedRows === 1) {
  470. newStage.id = result.insertId;
  471. } else {
  472. throw '新增期数据失败';
  473. }
  474. // 存在上一期时,复制上一期审批流程
  475. if (preStage) {
  476. const auditResult = await this.ctx.service.stageAudit.copyPreStageAuditors(transaction, preStage, newStage);
  477. if (!auditResult) {
  478. throw '复制上一期审批流程失败';
  479. }
  480. }
  481. // 新增期合同支付数据
  482. const dealResult = await this.ctx.service.stagePay.addInitialStageData(newStage, transaction);
  483. if (!dealResult) {
  484. throw '新增期合同支付数据失败';
  485. }
  486. // 新增期其他台账数据
  487. let pcTp = { contract_pc_tp: 0, qc_pc_tp: 0, pc_tp: 0, positive_qc_pc_tp: 0, negative_qc_pc_tp: 0 };
  488. if (preCheckedStage) {
  489. const jgclResult = await this.ctx.service.stageJgcl.addInitialStageData(newStage, preCheckedStage, transaction);
  490. if (!jgclResult) throw '初始化甲供材料数据失败';
  491. const otherResult = await this.ctx.service.stageOther.addInitialStageData(newStage, preCheckedStage, transaction);
  492. if (!otherResult) throw '初始化其他台账数据失败';
  493. const safeResult = await this.ctx.service.stageSafeProd.addInitialStageData(newStage, preCheckedStage, transaction);
  494. if (!safeResult) throw '初始化其他台账数据失败';
  495. const tempResult = await this.ctx.service.stageTempLand.addInitialStageData(newStage, preCheckedStage, transaction);
  496. if (!tempResult) throw '初始化其他台账数据失败';
  497. }
  498. if (preStage && preCheckedStage && preStage.order === preCheckedStage.order) {
  499. const priceCalc = new RevisePrice(this.ctx);
  500. pcTp = await priceCalc.newStagePriceChange(newStage, preStage, transaction);
  501. }
  502. await this.ctx.service.tenderCache.updateStageCache4Add(transaction, newStage, pcTp);
  503. // 新增期拷贝报表相关配置/签名角色 等
  504. if (preStage) {
  505. const rptResult = await this.ctx.service.rptCustomDefine.addInitialStageData(newStage, preStage, transaction);
  506. await this.ctx.service.roleRptRel.addInitialStageData(tenderId, newStage, preStage);
  507. }
  508. await transaction.commit();
  509. // 通知发送 - 第三方更新
  510. if (this.ctx.session.sessionProject.custom && syncApiConst.notice_type.indexOf(this.ctx.session.sessionProject.customType) !== -1) {
  511. const base_data = {
  512. tid: tenderId,
  513. sid: result.insertId,
  514. op: 'insert',
  515. };
  516. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  517. // 存在上一期时
  518. base_data.op = preStage ? 'update' : 'insert';
  519. base_data.sid = -1;
  520. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  521. }
  522. return newStage;
  523. } catch (err) {
  524. await transaction.rollback();
  525. throw err;
  526. }
  527. }
  528. /**
  529. * 编辑计量期
  530. *
  531. * @param {Number} tenderId - 标段Id
  532. * @param {Number} order - 第N期
  533. * @param {String} date - 计量年月
  534. * @param {String} period - 开始-截止时间
  535. * @return {Promise<void>}
  536. */
  537. async saveStage(tenderId, order, date, period) {
  538. await this.db.update(this.tableName, {
  539. s_time: date,
  540. period,
  541. }, { where: { tid: tenderId, order } });
  542. }
  543. /**
  544. * 设置 中间计量 生成规则,并生成数据
  545. * @param {Number} tenderId - 标段id
  546. * @param {Number} order - 期序号
  547. * @param {Number} data - 中间计量生成规则
  548. * @return {Promise<void>}
  549. */
  550. async buildDetailData(tenderId, order, data) {
  551. const conn = await this.db.beginTransaction();
  552. try {
  553. await conn.update(this.tableName, { im_type: data.im_type, im_pre: data.im_pre , im_start_num: data.im_start_num }, { where: { tid: tenderId, order } });
  554. // to do 生成中间计量数据
  555. await conn.commit();
  556. } catch (err) {
  557. await conn.rollback();
  558. throw err;
  559. }
  560. }
  561. async getChangeSubtotal(stage) {
  562. const result = {};
  563. const bg = await this.ctx.service.stageChange.getSubtotal(stage);
  564. const importBg = await this.ctx.service.stageImportChange.getSubtotal(stage);
  565. result.common = this.ctx.helper.add(bg.common, importBg.common);
  566. result.great = this.ctx.helper.add(bg.great, importBg.great);
  567. result.more = this.ctx.helper.add(bg.more, importBg.more);
  568. return result;
  569. }
  570. /**
  571. * 获取 当期的 计算基数
  572. * @return {Promise<any>}
  573. */
  574. async getStagePayCalcBase(stage, tenderInfo) {
  575. const calcBase = JSON.parse(JSON.stringify(payConst.calcBase));
  576. const param = tenderInfo.deal_param;
  577. const sum = await this.ctx.service.stageBills.getSumTotalPrice(stage);
  578. const pcSum = await this.ctx.service.stageBillsPc.getSumTotalPrice(stage);
  579. const bg = await this.getChangeSubtotal(stage);
  580. for (const cb of calcBase) {
  581. switch (cb.code) {
  582. case 'htj':
  583. cb.value = param.contractPrice;
  584. break;
  585. case 'zlje':
  586. cb.value = param.zanLiePrice;
  587. break;
  588. case 'htjszl':
  589. cb.value = this.ctx.helper.sub(param.contractPrice, param.zanLiePrice);
  590. break;
  591. case 'kgyfk':
  592. cb.value = param.startAdvance;
  593. break;
  594. case 'clyfk':
  595. cb.value = param.materialAdvance;
  596. break;
  597. case 'bqwc':
  598. cb.value = this.ctx.helper.sum([sum.contract_tp, sum.qc_tp, pcSum.pc_tp]);
  599. break;
  600. case 'bqht':
  601. cb.value = sum.contract_tp; //this.ctx.helper.add(sum.contract_tp, pcSum.contract_pc_tp);
  602. break;
  603. case 'bqbg':
  604. cb.value = sum.qc_tp; //this.ctx.helper.add(sum.qc_tp, pcSum.qc_pc_tp);
  605. break;
  606. case 'ybbqwc':
  607. const sumGcl = await this.ctx.service.stageBills.getSumTotalPriceGcl(stage, '^[^0-9]*1[0-9]{2}(-|$)');
  608. const sumPc = await this.ctx.service.stageBillsPc.getSumTotalPriceGcl(stage, '^[^0-9]*1[0-9]{2}(-|$)');
  609. cb.value = this.ctx.helper.sum([sumGcl.contract_tp, sumGcl.qc_tp, sumPc.pc_tp]);
  610. break;
  611. case 'ybbqbg':
  612. cb.value = bg.common;
  613. break;
  614. case 'jdbqbg':
  615. cb.value = bg.more;
  616. break;
  617. case 'zdbqbg':
  618. cb.value = bg.great;
  619. break;
  620. default:
  621. cb.value = 0;
  622. }
  623. }
  624. return calcBase;
  625. }
  626. async updateCheckCalcFlag(stage, check) {
  627. const result = await this.db.update(this.tableName, { id: stage.id, check_calc: check });
  628. return result.affectedRows === 1;
  629. }
  630. async updateCacheTime(sid) {
  631. const result = await this.db.update(this.tableName, { id: sid, cache_time_l: new Date() });
  632. return result.affectedRows === 1;
  633. }
  634. /**
  635. * 删除计量期
  636. *
  637. * @param {Number} id - 期Id
  638. * @return {Promise<void>}
  639. */
  640. async deleteStage(id) {
  641. const transaction = await this.db.beginTransaction();
  642. try {
  643. const stageInfo = await this.getDataById(id);
  644. // 通知发送 - 第三方更新
  645. // if (this.ctx.session.sessionProject.custom && syncApiConst.notice_type.indexOf(this.ctx.session.sessionProject.customType) !== -1) {
  646. // const base_data = {
  647. // tid: this.ctx.tender.id,
  648. // sid: id,
  649. // op: 'delete',
  650. // };
  651. // await this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  652. // // 是否还存在其他期
  653. // base_data.op = stageInfo.order === 1 ? 'delete' : 'update';
  654. // base_data.sid = -1;
  655. // await this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  656. // }
  657. await transaction.delete(this.tableName, { id });
  658. await this.ctx.service.tenderCache.updateStageCache4Del(transaction, stageInfo);
  659. await transaction.delete(this.ctx.service.pos.tableName, { add_stage: id });
  660. await transaction.delete(this.ctx.service.stageAudit.tableName, { sid: id });
  661. await transaction.delete(this.ctx.service.stageBills.tableName, { sid: id });
  662. await transaction.delete(this.ctx.service.stageChange.tableName, { sid: id });
  663. await transaction.delete(this.ctx.service.stageChangeFinal.tableName, { sid: id });
  664. await transaction.delete(this.ctx.service.stageImportChange.tableName, { sid: id });
  665. await transaction.delete(this.ctx.service.stagePos.tableName, { sid: id });
  666. await transaction.delete(this.ctx.service.stageDetail.tableName, { sid: id });
  667. await transaction.delete(this.ctx.service.stagePosFinal.tableName, { sid: id });
  668. await transaction.delete(this.ctx.service.stageBillsFinal.tableName, { sid: id });
  669. await transaction.delete(this.ctx.service.stageRela.tableName, { sid: id });
  670. await transaction.delete(this.ctx.service.stageRelaBills.tableName, { sid: id });
  671. await transaction.delete(this.ctx.service.stageRelaBillsFinal.tableName, { sid: id });
  672. await transaction.delete(this.ctx.service.stageRelaIm.tableName, { sid: id });
  673. await transaction.delete(this.ctx.service.stageRelaImBills.tableName, { sid: id });
  674. await transaction.delete(this.ctx.service.stageAuditAss.tableName, { sid: id });
  675. // 删除计量合同支付附件
  676. const payList = await this.ctx.service.stagePay.getAllDataByCondition({ where: { sid: id } });
  677. if (payList) {
  678. for (const pt of payList) {
  679. if (pt.attachment !== null && pt.attachment !== '') {
  680. const payAttList = JSON.parse(pt.attachment);
  681. for (const pat of payAttList) {
  682. if (fs.existsSync(path.join(this.app.baseDir, pat.filepath))) {
  683. await fs.unlinkSync(path.join(this.app.baseDir, pat.filepath));
  684. }
  685. }
  686. }
  687. }
  688. }
  689. await transaction.delete(this.ctx.service.stagePay.tableName, { sid: id });
  690. await this.ctx.service.pay.doDeleteStage(stageInfo, transaction);
  691. // 删除计量附件文件
  692. const attList = await this.ctx.service.stageAtt.getAllDataByCondition({ where: { tid: stageInfo.tid, sid: stageInfo.order } });
  693. if (attList.length !== 0) {
  694. for (const att of attList) {
  695. if (fs.existsSync(path.join(this.app.baseDir, att.filepath))) {
  696. await fs.unlinkSync(path.join(this.app.baseDir, att.filepath));
  697. }
  698. }
  699. }
  700. await transaction.delete(this.ctx.service.stageAtt.tableName, { tid: stageInfo.tid, sid: stageInfo.order });
  701. // 其他台账
  702. await transaction.delete(this.ctx.service.stageJgcl.tableName, { sid: id });
  703. const bonus = await this.ctx.service.stageBonus.getStageData(id);
  704. if (bonus && bonus.length > 0) {
  705. for (const b of bonus) {
  706. for (const f of b.proof_file) {
  707. if (fs.existsSync(path.join(this.app.baseDir, f.filepath))) {
  708. await fs.unlinkSync(path.join(this.app.baseDir, f.filepath));
  709. }
  710. }
  711. }
  712. }
  713. await transaction.delete(this.ctx.service.stageBonus.tableName, { sid: id });
  714. await transaction.delete(this.ctx.service.stageOther.tableName, { sid: id });
  715. // 同步删除进度里所选的期
  716. await transaction.delete(this.ctx.service.scheduleStage.tableName, { tid: stageInfo.tid, order: stageInfo.order });
  717. const detailAtt = await this.ctx.service.stageDetailAtt.getAllDataByCondition({ where: { sid: id } });
  718. if (detailAtt && detailAtt.length > 0) {
  719. for (const da of detailAtt) {
  720. da.attachment = da.attachment ? JSON.parse(da.attachment) : [];
  721. for (const daa of da.attachment) {
  722. if (fs.existsSync(path.join(this.app.baseDir, daa.filepath))) {
  723. await fs.unlinkSync(path.join(this.app.baseDir, daa.filepath));
  724. }
  725. }
  726. }
  727. }
  728. await transaction.delete(this.ctx.service.stageDetailAtt.tableName, { sid: id });
  729. // 重算进度计量总金额
  730. await this.ctx.service.scheduleStage.calcStageSjTp(transaction, stageInfo.tid);
  731. // 删除收方单及附件
  732. const shoufangAttList = await this.ctx.service.stageShoufangAtt.getAllDataByCondition({ where: { sid: id } });
  733. if (shoufangAttList.length !== 0) {
  734. for (const att of shoufangAttList) {
  735. if (fs.existsSync(path.join(this.app.baseDir, att.filepath))) {
  736. await fs.unlinkSync(path.join(this.app.baseDir, att.filepath));
  737. }
  738. }
  739. }
  740. await transaction.delete(this.ctx.service.stageShoufangAtt.tableName, { sid: id });
  741. const shoufangList = await this.ctx.service.stageShoufang.getAllDataByCondition({ where: { sid: id } });
  742. if (shoufangList.length !== 0) {
  743. for (const att of shoufangList) {
  744. if (fs.existsSync(path.join(this.app.baseDir, 'app/' + att.qrcode))) {
  745. await fs.unlinkSync(path.join(this.app.baseDir, 'app/' + att.qrcode));
  746. }
  747. }
  748. }
  749. await transaction.delete(this.ctx.service.stageShoufang.tableName, { sid: id });
  750. await transaction.delete(this.ctx.service.cooperationConfirm.tableName, { sid: id });
  751. // 记录删除日志
  752. await this.ctx.service.projectLog.addProjectLog(transaction, projectLogConst.type.stage, projectLogConst.status.delete, '第' + stageInfo.order + '期');
  753. await transaction.commit();
  754. return true;
  755. } catch (err) {
  756. await transaction.rollback();
  757. throw err;
  758. }
  759. }
  760. /**
  761. * 获取 多期的 计算基数 -(材料调差调用)
  762. * @return {Promise<any>}
  763. */
  764. async getMaterialCalcBase(stage_list, tenderInfo) {
  765. const calcBase = JSON.parse(JSON.stringify(payConst.materialCalcBase));
  766. const param = tenderInfo.deal_param;
  767. const sum = await this.ctx.service.stageBills.getSumTotalPriceByMaterial(stage_list);
  768. const pcSum = await this.ctx.service.stageBillsPc.getSumTotalPriceByMaterial(stage_list);
  769. for (const cb of calcBase) {
  770. switch (cb.code) {
  771. case 'htj':
  772. cb.value = param.contractPrice;
  773. break;
  774. case 'zlje':
  775. cb.value = param.zanLiePrice;
  776. break;
  777. case 'htjszl':
  778. cb.value = this.ctx.helper.sub(param.contractPrice, param.zanLiePrice);
  779. break;
  780. case 'kgyfk':
  781. cb.value = param.startAdvance;
  782. break;
  783. case 'clyfk':
  784. cb.value = param.materialAdvance;
  785. break;
  786. case 'bqwc':
  787. cb.value = this.ctx.helper.sum([sum.contract_tp, sum.qc_tp, pcSum.pc_tp]);
  788. break;
  789. case 'bqht':
  790. cb.value = sum.contract_tp;
  791. break;
  792. case 'bqbg':
  793. cb.value = sum.qc_tp;
  794. break;
  795. case 'yib':
  796. case 'erb':
  797. case 'sanb':
  798. case 'sib':
  799. case 'wub':
  800. case 'liub':
  801. case 'qib':
  802. case 'bab':
  803. case 'jiub':
  804. const sumGcl = await this.ctx.service.stageBills.getSumTotalPriceGclByMaterial(stage_list, cb.filter);
  805. const sumPc = await this.ctx.service.stageBillsPc.getSumTotalPriceGclByMaterial(stage_list, cb.filter);
  806. cb.value = this.ctx.helper.sum([sumGcl.contract_tp, sumGcl.qc_tp, sumPc.pc_tp]);
  807. break;
  808. case 'bqyf':
  809. cb.value = this.ctx.helper.roundNum(this._.sumBy(stage_list, 'yf_tp'), 2);
  810. break;
  811. default:
  812. cb.value = 0;
  813. }
  814. }
  815. return calcBase;
  816. }
  817. /**
  818. * 获取必要的stage信息调用curTimes, curOrder, id , times, curAuditor(材料调差)
  819. * @param stage_id_list
  820. * @return {Promise<void>}
  821. */
  822. async getStageMsgByStageId(stage_id_list) {
  823. const list = [];
  824. stage_id_list = stage_id_list.toString().split(',');
  825. for (const sid of stage_id_list) {
  826. const stage = await this.getDataById(sid);
  827. stage.auditors = await this.service.stageAudit.getAuditors(stage.id, stage.times);
  828. // todo 不确定是否使用,暂时注释,待测试验证
  829. // stage.curAuditor = await this.service.stageAudit.getCurAuditor(stage.id, stage.times);
  830. stage.curOrder = _.max(_.map(stage.auditors, 'order'));
  831. stage.curTimes = stage.times;
  832. list.push(stage);
  833. }
  834. return list;
  835. }
  836. async getStageByDataCollect(tenderId, stage_tp) {
  837. const stages = await this.db.select(this.tableName, {
  838. columns: ['id', 'user_id', 'times', 'status', 's_time', 'contract_tp', 'qc_tp', 'pc_tp', 'pre_contract_tp', 'pre_qc_tp', 'tp_history'],
  839. where: { tid: tenderId },
  840. orders: [['order', 'desc']],
  841. });
  842. if (stages.length > 0 && stages[0].status === auditConst.stage.status.uncheck) {
  843. stages.splice(0, 1);
  844. }
  845. // 最新一期计量(未审批完成),取上一个人的期详细数据,应实时计算
  846. const stage = stages[0];
  847. if (stages.length === 0) return stages;
  848. // await this.checkStageGatherDataByDataCollect(stage);
  849. if (stage.status !== auditConst.stage.status.checked) {
  850. // 批量把stage_tp的值赋值给stage
  851. _.forEach(stage_tp, function(value, key) {
  852. stage[key] = stage_tp[key] ? stage_tp[key] : null;
  853. });
  854. }
  855. for (const s of stages) {
  856. s.tp = this.ctx.helper.sum([s.contract_tp, s.qc_tp, s.pc_tp]);
  857. s.pre_tp = this.ctx.helper.add(s.pre_contract_tp, s.pre_qc_tp);
  858. s.end_tp = this.ctx.helper.add(s.pre_tp, s.tp);
  859. }
  860. return stages;
  861. }
  862. async doCheckStageByDataCollect(stage) {
  863. const status = auditConst.stage.status;
  864. await this.loadStageUser(stage);
  865. if (stage.status === status.checkNo) {
  866. stage.readOnly = false;
  867. const checkNoAudit = await this.service.stageAudit.getDataByCondition({
  868. sid: stage.id, times: stage.times - 1, status: status.checkNo,
  869. });
  870. stage.curTimes = stage.times - 1;
  871. stage.curOrder = checkNoAudit.order;
  872. } else if (stage.status === status.checked) {
  873. stage.readOnly = true;
  874. stage.curTimes = stage.times;
  875. stage.curOrder = _.max(_.map(stage.auditors, 'order'));
  876. } else {
  877. stage.readOnly = false;
  878. stage.curTimes = stage.times;
  879. stage.curOrder = stage.curAuditors[0].order - 1;
  880. }
  881. return stage;
  882. }
  883. async checkStageGatherDataByDataCollect(stage) {
  884. // 最新一期计量(未审批完成),当前操作人的期详细数据,应实时计算
  885. if (stage.status !== auditConst.stage.status.checked) {
  886. await this.doCheckStageByDataCollect(stage);
  887. if (!stage.readOnly && stage.check_calc) {
  888. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(stage);
  889. const pcSum = await this.ctx.service.stageBillsPc.getSumTotalPrice(stage);
  890. stage.contract_tp = tpData.contract_tp;
  891. stage.qc_tp = tpData.qc_tp;
  892. stage.positive_qc_tp = tpData.positive_qc_tp;
  893. stage.negative_qc_tp = tpData.negative_qc_tp;
  894. stage.contract_pc_tp = pcSum.contract_pc_tp;
  895. stage.qc_pc_tp = pcSum.qc_pc_tp;
  896. stage.pc_tp = pcSum.pc_tp;
  897. stage.positive_qc_pc_tp = pcSum.positive_qc_pc_tp;
  898. stage.negative_qc_pc_tp = pcSum.negative_qc_pc_tp;
  899. stage.tp = this.ctx.helper.sum([stage.contract_tp, stage.qc_tp, stage.pc_tp]);
  900. const tp = await this.ctx.service.stagePay.getSpecialTotalPrice(stage);
  901. stage.yf_tp = tp.yf;
  902. stage.sf_tp = tp.sf;
  903. stage.end_tp = this.ctx.helper.add(stage.pre_tp, stage.tp);
  904. } else if (stage.tp_history) {
  905. const his = this.ctx.helper._.find(stage.tp_history, { times: stage.curTimes, order: stage.curOrder });
  906. if (his) {
  907. stage.contract_tp = his.contract_tp;
  908. stage.qc_tp = his.qc_tp;
  909. stage.positive_qc_tp = his.positive_qc_tp;
  910. stage.negative_qc_tp = his.negative_qc_tp;
  911. stage.yf_tp = his.yf_tp;
  912. stage.sf_tp = his.sf_tp;
  913. stage.tp = this.ctx.helper.sum([stage.contract_tp, stage.qc_tp, stage.pc_tp]);
  914. stage.end_tp = this.ctx.helper.add(stage.pre_tp, stage.tp);
  915. }
  916. }
  917. }
  918. }
  919. async isLastStage(tid, sid) {
  920. const lastStage = await this.ctx.service.stage.getLastestStage(tid, true);
  921. return lastStage ? lastStage.id === sid : false;
  922. }
  923. }
  924. return Stage;
  925. };