stage_audit.js 121 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date 2019/2/27
  7. * @version
  8. */
  9. const auditConst = require('../const/audit').stage;
  10. const auditType = require('../const/audit').auditType;
  11. const smsTypeConst = require('../const/sms_type');
  12. const SMS = require('../lib/sms');
  13. const SmsAliConst = require('../const/sms_alitemplate');
  14. const wxConst = require('../const/wechat_template');
  15. const shenpiConst = require('../const/shenpi');
  16. const payConst = require('../const/deal_pay');
  17. const pushType = require('../const/audit').pushType;
  18. const syncApiConst = require('../const/sync_api');
  19. const measureType = require('../const/tender').measureType;
  20. const RevisePrice = require('../lib/revise_price');
  21. const pushOperate = require('../const/spec_3f').pushOperate;
  22. module.exports = app => {
  23. class StageAudit extends app.BaseService {
  24. /**
  25. * 构造函数
  26. *
  27. * @param {Object} ctx - egg全局变量
  28. * @return {void}
  29. */
  30. constructor(ctx) {
  31. super(ctx);
  32. this.tableName = 'stage_audit';
  33. }
  34. /**
  35. * 获取 审核人信息
  36. *
  37. * @param {Number} stageId - 期id
  38. * @param {Number} auditorId - 审核人id
  39. * @param {Number} times - 第几次审批
  40. * @return {Promise<*>}
  41. */
  42. async getAuditor(stageId, auditorId, times = 1) {
  43. const sql =
  44. 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, la.`times`, la.`order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time` ' +
  45. 'FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id`' +
  46. 'WHERE la.`sid` = ? and la.`aid` = ? and la.`times` = ?';
  47. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, auditorId, times];
  48. return await this.db.queryOne(sql, sqlParam);
  49. }
  50. async getAuditorByOrder(stageId, order, times) {
  51. const sql =
  52. 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`,' +
  53. ' la.`times`, la.`order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time` ' +
  54. ' FROM ' + this.tableName + ' AS la' +
  55. ' Left Join ' + this.ctx.service.projectAccount.tableName + ' AS pa ON la.`aid` = pa.`id`' +
  56. ' WHERE la.`sid` = ? and la.`order` = ? and la.`times` = ?' +
  57. ' ORDER BY `order` DESC';
  58. const sqlParam = [stageId, order, times ? times: 1];
  59. return await this.db.queryOne(sql, sqlParam);
  60. }
  61. async getAuditorsByOrder(stageId, order, times) {
  62. const sql =
  63. 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`,' +
  64. ' la.`times`, la.`order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time` ' +
  65. ' FROM ' + this.tableName + ' AS la' +
  66. ' Left Join ' + this.ctx.service.projectAccount.tableName + ' AS pa ON la.`aid` = pa.`id`' +
  67. ' WHERE la.`sid` = ? and la.`order` = ? and la.`times` = ?' +
  68. ' ORDER BY `order` DESC';
  69. const sqlParam = [stageId, order, times ? times: 1];
  70. return await this.db.query(sql, sqlParam);
  71. }
  72. async getLastestAuditor(stageId, times, status) {
  73. const sql =
  74. 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, la.audit_type, la.audit_order,' +
  75. ' la.`times`, la.`order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time` ' +
  76. ' FROM ' + this.tableName + ' AS la' +
  77. ' Left Join ' + this.ctx.service.projectAccount.tableName + ' AS pa ON la.`aid` = pa.`id`' +
  78. ' WHERE la.`sid` = ? and la.`status` = ? and la.`times` = ?' +
  79. ' ORDER BY `order` DESC';
  80. const sqlParam = [stageId, status, times ? times: 1];
  81. return await this.db.queryOne(sql, sqlParam);
  82. }
  83. async getLastestAuditors(stageId, times, status) {
  84. const sql =
  85. 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, la.audit_type, la.audit_order,' +
  86. ' la.`times`, la.`order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time` ' +
  87. ' FROM ' + this.tableName + ' AS la' +
  88. ' Left Join ' + this.ctx.service.projectAccount.tableName + ' AS pa ON la.`aid` = pa.`id`' +
  89. ' WHERE la.`sid` = ? and la.`status` = ? and la.`times` = ?' +
  90. ' ORDER BY `order` DESC';
  91. const sqlParam = [stageId, status, times ? times: 1];
  92. const result = await this.db.query(sql, sqlParam);
  93. if (result.length === 0) return [];
  94. return result.filter(x => { return x.order === result[0].order });
  95. }
  96. /**
  97. * 获取 审核列表信息
  98. *
  99. * @param {Number} stageId - 期id
  100. * @param {Number} times - 第几次审批
  101. * @param {Number} order_sort - 列表排序方式
  102. * @return {Promise<*>}
  103. */
  104. async getAuditors(stageId, times = 1, order_sort = 'asc') {
  105. const sql = 'SELECT la.id, la.aid, la.times, la.order, la.status, la.opinion, la.begin_time, la.end_time, la.audit_type, la.audit_order,' +
  106. ' pa.name, pa.company, pa.role, pa.mobile, pa.telephone, pa.sign_path' +
  107. ` FROM ${this.tableName} la LEFT JOIN ${this.ctx.service.projectAccount.tableName} pa ON la.aid = pa.id` +
  108. ' WHERE la.sid = ? AND la.times = ?' +
  109. ' ORDER BY la.order ' + order_sort;
  110. const sqlParam = [stageId, times];
  111. const result = await this.db.query(sql, sqlParam);
  112. const max_sort = this._.max(result.map(x => { return x.audit_order; }));
  113. for (const i in result) {
  114. result[i].max_sort = max_sort;
  115. }
  116. return result;
  117. }
  118. async getAllAuditors(tenderId) {
  119. const sql =
  120. 'SELECT sa.aid, sa.tid FROM ' + this.tableName + ' sa' +
  121. ' LEFT JOIN ' + this.ctx.service.tender.tableName + ' t On sa.tid = t.id' +
  122. ' WHERE t.id = ?' +
  123. ' GROUP BY sa.aid';
  124. const sqlParam = [tenderId];
  125. return this.db.query(sql, sqlParam);
  126. }
  127. /**
  128. * 获取标段审核人最后一位的名称
  129. *
  130. * @param {Number} tenderId - 标段id
  131. * @param {Number} auditorId - 审核人id
  132. * @param {Number} times - 第几次审批
  133. * @return {Promise<*>}
  134. */
  135. async getStatusName(stageId) {
  136. const sql = 'SELECT pa.`name` FROM ?? AS sa Left Join ?? AS pa On sa.`aid` = pa.`id`' +
  137. ' WHERE sa.`sid` = ? and sa.`status` != ? ORDER BY sa.`times` DESC, sa.`order` DESC';
  138. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, auditConst.status.uncheck];
  139. return await this.db.queryOne(sql, sqlParam);
  140. }
  141. /**
  142. * 获取 当前审核人
  143. *
  144. * @param {Number} stageId - 期id
  145. * @param {Number} times - 第几次审批
  146. * @return {Promise<*>}
  147. */
  148. async getCurAuditor(stageId, times = 1) {
  149. const sql =
  150. 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, la.`times`, la.`order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time`, la.audit_type, la.audit_order ' +
  151. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id`' +
  152. ' WHERE la.`sid` = ? and la.`status` = ? and la.`times` = ?';
  153. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, auditConst.status.checking, times];
  154. return await this.db.queryOne(sql, sqlParam);
  155. }
  156. async getCurAuditors(stageId, times = 1) {
  157. const sql =
  158. 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, la.`times`, la.`order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time`, la.audit_type, la.audit_order ' +
  159. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id`' +
  160. ' WHERE la.`sid` = ? and la.`status` = ? and la.`times` = ?';
  161. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, auditConst.status.checking, times];
  162. return await this.db.query(sql, sqlParam);
  163. }
  164. /**
  165. * 获取 最新审核顺序
  166. *
  167. * @param {Number} stageId - 期id
  168. * @param {Number} times - 第几次审批
  169. * @return {Promise<number>}
  170. */
  171. async getNewOrder(stageId, times = 1) {
  172. const sql = 'SELECT Max(`order`) As max_order, Max(audit_order) As max_audit_order FROM ' + this.tableName + ' Where `sid` = ? and `times` = ?';
  173. const sqlParam = [stageId, times];
  174. const result = await this.db.queryOne(sql, sqlParam);
  175. return result && result.max_order ? [result.max_order + 1, result.max_audit_order + 1] : [1, 1];
  176. }
  177. /**
  178. * 新增审核人
  179. *
  180. * @param {Number} stageId - 期id
  181. * @param {Number} auditorId - 审核人id
  182. * @param {Number} times - 第几次审批
  183. * @return {Promise<number>}
  184. */
  185. async addAuditor(stageId, auditorId, times = 1, is_gdzs = 0) {
  186. const transaction = await this.db.beginTransaction();
  187. try {
  188. let [newOrder, newAuditOrder] = await this.getNewOrder(stageId, times);
  189. // 判断是否存在固定终审,存在则newOrder - 1并使终审order+1
  190. newOrder = is_gdzs === 1 ? newOrder - 1 : newOrder;
  191. newAuditOrder = is_gdzs === 1 ? newAuditOrder - 1 : newAuditOrder;
  192. if (is_gdzs) await this._syncOrderByDelete(transaction, stageId, newOrder, times, '+');
  193. const data = {
  194. tid: this.ctx.tender.id,
  195. sid: stageId,
  196. aid: auditorId,
  197. times,
  198. order: newOrder,
  199. status: auditConst.status.uncheck,
  200. audit_order: newAuditOrder,
  201. };
  202. const result = await transaction.insert(this.tableName, data);
  203. await transaction.commit();
  204. return result.effectRows = 1;
  205. } catch (err) {
  206. await transaction.rollback();
  207. throw err;
  208. }
  209. return false;
  210. }
  211. /**
  212. * 移除审核人时,同步其后审核人order
  213. * @param transaction - 事务
  214. * @param {Number} stageId - 标段id
  215. * @param {Number} auditorId - 审核人id
  216. * @param {Number} times - 第几次审批
  217. * @return {Promise<*>}
  218. * @private
  219. */
  220. async _syncOrderByDelete(transaction, stageId, order, times, selfOperate = '-') {
  221. this.initSqlBuilder();
  222. this.sqlBuilder.setAndWhere('sid', {
  223. value: stageId,
  224. operate: '=',
  225. });
  226. this.sqlBuilder.setAndWhere('order', {
  227. value: order,
  228. operate: '>=',
  229. });
  230. this.sqlBuilder.setAndWhere('times', {
  231. value: times,
  232. operate: '=',
  233. });
  234. this.sqlBuilder.setUpdateData('order', {
  235. value: 1,
  236. selfOperate: selfOperate,
  237. });
  238. this.sqlBuilder.setUpdateData('audit_order', {
  239. value: 1,
  240. selfOperate: selfOperate,
  241. });
  242. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  243. const data = await transaction.query(sql, sqlParam);
  244. return data;
  245. }
  246. /**
  247. * 移除审核人
  248. *
  249. * @param {Number} stageId - 期id
  250. * @param {Number} auditorId - 审核人id
  251. * @param {Number} times - 第几次审批
  252. * @return {Promise<boolean>}
  253. */
  254. async deleteAuditor(stageId, auditorId, times = 1) {
  255. const transaction = await this.db.beginTransaction();
  256. try {
  257. const condition = { sid: stageId, aid: auditorId, times };
  258. const auditor = await this.getDataByCondition(condition);
  259. if (!auditor) {
  260. throw '该审核人不存在';
  261. }
  262. // 移除整个流程的人
  263. await transaction.delete(this.tableName, { sid: stageId, order: auditor.order, times});
  264. await this._syncOrderByDelete(transaction, stageId, auditor.order, times);
  265. await transaction.commit();
  266. } catch (err) {
  267. await transaction.rollback();
  268. throw err;
  269. }
  270. return true;
  271. }
  272. async _updateTender(transaction) {
  273. if (!this.ctx.tender) return;
  274. if (this.ctx.tender.data.measure_type !== measureType.gcl.value) return;
  275. const sum = await this.ctx.service.ledger.addUp({ tender_id: this.ctx.tender.id/* , is_leaf: true*/ });
  276. await transaction.update(this.ctx.service.tender.tableName, {
  277. id: this.ctx.tender.id, total_price: sum.total_price, deal_tp: sum.deal_tp
  278. });
  279. return sum;
  280. }
  281. /**
  282. * 开始审批
  283. *
  284. * @param {Number} stageId - 期id
  285. * @param {Number} times - 第几次审批
  286. * @return {Promise<boolean>}
  287. */
  288. async start(stageId, times = 1) {
  289. const audits = await this.getAllDataByCondition({ where: { sid: stageId, times, order: 1 } });
  290. if (audits.length === 0) {
  291. if(this.ctx.tender.info.shenpi.stage === shenpiConst.sp_status.gdspl) {
  292. throw '请联系管理员添加审批人';
  293. } else {
  294. throw '请先选择审批人,再上报数据';
  295. }
  296. }
  297. const transaction = await this.db.beginTransaction();
  298. try {
  299. const ledgerTp = await this._updateTender(transaction);
  300. const begin_time = new Date();
  301. const updateData = audits.map(x => { return { id: x.id, status: auditConst.status.checking, begin_time } });
  302. await transaction.updateRows(this.tableName, updateData);
  303. // 计算原报最终数据
  304. const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  305. // 复制一份下一审核人数据
  306. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, 1, transaction);
  307. await this.ctx.service.stageJgcl.updateHistory(this.ctx.stage, transaction);
  308. await this.ctx.service.stageBonus.updateHistory(this.ctx.stage, transaction);
  309. await this.ctx.service.stageOther.updateHistory(this.ctx.stage, transaction);
  310. await this.ctx.service.stageSafeProd.updateHistory(this.ctx.stage, transaction);
  311. await this.ctx.service.stageTempLand.updateHistory(this.ctx.stage, transaction);
  312. // 更新期数据
  313. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  314. const stageTp = {
  315. contract_tp: tpData.contract_tp,
  316. qc_tp: tpData.qc_tp,
  317. positive_qc_tp: tpData.positive_qc_tp,
  318. negative_qc_tp: tpData.negative_qc_tp,
  319. yf_tp: yfPay.tp,
  320. sf_tp: sfPay.tp,
  321. };
  322. this.ctx.stage.tp_history.push({ times: this.ctx.stage.curTimes, order: 0, ...stageTp });
  323. await this.ctx.service.tenderCache.updateStageCache4Start(transaction, this.ctx.stage, auditConst.status.checking, audits, ledgerTp, stageTp);
  324. await transaction.update(this.ctx.service.stage.tableName, {
  325. id: stageId,
  326. status: auditConst.status.checking,
  327. ...stageTp,
  328. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  329. cache_time_r: this.ctx.stage.cache_time_l,
  330. });
  331. // 多人协同,取消下一审批人存在的锁定
  332. await this.ctx.service.stageAuditAss.cancelLock(this.ctx.stage, audits.map(x => { return x.aid; }), transaction);
  333. // 添加短信通知-需要审批提醒功能
  334. const stageInfo = await this.ctx.service.stage.getDataById(stageId);
  335. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/measure/stage/' + stageInfo.order);
  336. const auditIds = this._.map(audits, 'aid');
  337. const users = this._.map(this.ctx.stage.auditAssists.filter(x => { return auditIds.indexOf(x.user_id) >= 0; }), 'ass_user_id');
  338. users.push(...auditIds);
  339. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), SmsAliConst.template.stage_check, {
  340. qi: stageInfo.order,
  341. code: shenpiUrl,
  342. });
  343. // 微信模板通知
  344. const wechatData = {
  345. wap_url: shenpiUrl,
  346. qi: stageInfo.order,
  347. status: wxConst.status.check,
  348. tips: wxConst.tips.check,
  349. code: this.ctx.session.sessionProject.code,
  350. };
  351. await this.ctx.helper.sendWechat(users, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), wxConst.template.stage, wechatData);
  352. // 重新发送配置
  353. for (const a of audits) {
  354. await this.ctx.service.noticeAgain.addNoticeAgain(transaction, smsTypeConst.const.JL, {
  355. pid: this.ctx.session.sessionProject.id,
  356. tid: this.ctx.tender.id,
  357. uid: a.aid,
  358. sp_type: 'stage',
  359. sp_id: a.id,
  360. table_name: this.tableName,
  361. template: wxConst.template.stage,
  362. wx_data: wechatData,
  363. });
  364. }
  365. // 上报/审批 - 检查三方特殊推送
  366. await this.ctx.service.specMsg.addStageMsg(transaction, this.ctx.session.sessionProject.id, this.ctx.stage, pushOperate.stage.flow);
  367. await transaction.commit();
  368. // 通知发送 - 第三方更新
  369. if (this.ctx.session.sessionProject.custom && syncApiConst.notice_type.indexOf(this.ctx.session.sessionProject.customType) !== -1) {
  370. const base_data = {
  371. tid: this.ctx.tender.id,
  372. sid: stageId,
  373. op: 'update',
  374. };
  375. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  376. base_data.op = 'update';
  377. base_data.sid = -1;
  378. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  379. }
  380. } catch (err) {
  381. await transaction.rollback();
  382. throw err;
  383. }
  384. return true;
  385. }
  386. async _checked(pid, stageId, checkData, times) {
  387. const accountId = this.ctx.session.sessionUser.accountId;
  388. const time = new Date();
  389. // 整理当前流程审核人状态更新
  390. const audits = await this.getAllDataByCondition({ where: { sid: stageId, times, status: auditConst.status.checking } });
  391. if (audits.length === 0) throw '审核数据错误';
  392. const selfAudit = audits.find(x => { return x.aid === accountId; });
  393. if (!selfAudit) throw '当前标段您无权审批';
  394. const flowAudits = await this.getAllDataByCondition({ where: { sid: stageId, times, order: selfAudit.order } });
  395. const nextAudits = await this.getAllDataByCondition({ where: { sid: stageId, times, order: selfAudit.order + 1 } });
  396. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  397. const stageInfo = await this.ctx.service.stage.getDataById(stageId);
  398. // 计算并合同支付最终数据
  399. const [yfPay, sfPay, stagePays] = await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage);
  400. const transaction = await this.db.beginTransaction();
  401. try {
  402. const ledgerTp = await this._updateTender(transaction);
  403. // 添加通知
  404. const noticeContent = await this.getNoticeContent(pid, stageInfo.tid, stageId, accountId, checkData.opinion);
  405. const auditors = await this.getAuditGroupByListWithOwner(stageId, times);
  406. const defaultNoticeRecord = {
  407. pid,
  408. type: pushType.stage,
  409. status: auditConst.status.checked,
  410. content: noticeContent,
  411. };
  412. const records = [];
  413. auditors.forEach(audit => {
  414. records.push({
  415. uid: audit.aid,
  416. ...defaultNoticeRecord,
  417. });
  418. });
  419. this.ctx.stage.userAssists.forEach(u => {
  420. records.push({ uid: u.ass_user_id, ...defaultNoticeRecord});
  421. });
  422. this.ctx.stage.auditAssists.forEach(u => {
  423. records.push({ uid: u.ass_user_id, ...defaultNoticeRecord});
  424. });
  425. await transaction.insert('zh_notice', records);
  426. // 更新本人审批状态
  427. await transaction.update(this.tableName, {
  428. id: selfAudit.id,
  429. status: checkData.checkType,
  430. opinion: checkData.opinion,
  431. end_time: time,
  432. });
  433. await this.ctx.service.noticeAgain.stopNoticeAgain(transaction, this.tableName, selfAudit.id);
  434. const stageTp = {
  435. contract_tp: tpData.contract_tp,
  436. qc_tp: tpData.qc_tp,
  437. positive_qc_tp: tpData.positive_qc_tp,
  438. negative_qc_tp: tpData.negative_qc_tp,
  439. yf_tp: yfPay.tp,
  440. sf_tp: sfPay.tp,
  441. };
  442. this.ctx.stage.tp_history.push({ times, order: selfAudit.order, ...stageTp });
  443. if (audits.length === 1 || selfAudit.audit_type !== auditType.key.and) {
  444. // 或签更新他人审批状态
  445. if (selfAudit.audit_type === auditType.key.or) {
  446. const updateOther = [];
  447. for (const audit of audits) {
  448. if (audit.aid === selfAudit.aid) continue;
  449. updateOther.push({
  450. id: audit.id,
  451. status: auditConst.status.checkSkip,
  452. opinion: '',
  453. end_time: time,
  454. });
  455. await this.ctx.service.noticeAgain.stopNoticeAgain(transaction, this.tableName, audit.id);
  456. }
  457. if (updateOther.length > 0) transaction.updateRows(this.tableName, updateOther);
  458. }
  459. // 无下一审核人表示,审核结束
  460. if (nextAudits.length > 0) {
  461. // 复制一份下一审核人数据
  462. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, nextAudits[0].order, transaction);
  463. await this.ctx.service.stageJgcl.updateHistory(this.ctx.stage, transaction);
  464. await this.ctx.service.stageBonus.updateHistory(this.ctx.stage, transaction);
  465. await this.ctx.service.stageOther.updateHistory(this.ctx.stage, transaction);
  466. await this.ctx.service.stageSafeProd.updateHistory(this.ctx.stage, transaction);
  467. await this.ctx.service.stageTempLand.updateHistory(this.ctx.stage, transaction);
  468. // 流程至下一审批人
  469. const updateData = nextAudits.map(x => { return { id: x.id, status: auditConst.status.checking, begin_time: time }; });
  470. await transaction.updateRows(this.tableName, updateData);
  471. // 同步 期信息
  472. await transaction.update(this.ctx.service.stage.tableName, {
  473. id: stageId,
  474. status: auditConst.status.checking,
  475. ...stageTp,
  476. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  477. cache_time_r: this.ctx.stage.cache_time_l,
  478. });
  479. await this.ctx.service.tenderCache.updateStageCache4Flow(transaction, this.ctx.stage, auditConst.status.checking, nextAudits, flowAudits, ledgerTp, stageTp);
  480. // 多人协同,取消下一审批人存在的锁定
  481. await this.ctx.service.stageAuditAss.cancelLock(this.ctx.stage, nextAudits.map(x => { return x.aid; }), transaction);
  482. // 添加短信通知-需要审批提醒功能
  483. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/measure/stage/' + stageInfo.order);
  484. const nextAuditIds = this._.map(nextAudits, 'aid');
  485. const users = this._.map(this._.filter(this.ctx.auditAssists, function (x) { return nextAuditIds.indexOf(x.user_id) >= 0; }), 'ass_user_id');
  486. users.push(...nextAuditIds);
  487. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), SmsAliConst.template.stage_check, {
  488. qi: stageInfo.order,
  489. code: shenpiUrl,
  490. });
  491. // 微信模板通知
  492. const wechatData = {
  493. wap_url: shenpiUrl,
  494. qi: stageInfo.order,
  495. status: wxConst.status.check,
  496. tips: wxConst.tips.check,
  497. code: this.ctx.session.sessionProject.code,
  498. };
  499. await this.ctx.helper.sendWechat(users, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), wxConst.template.stage, wechatData);
  500. // 重新发送配置
  501. for (const a of nextAudits) {
  502. await this.ctx.service.noticeAgain.addNoticeAgain(transaction, smsTypeConst.const.JL, {
  503. pid: this.ctx.session.sessionProject.id,
  504. tid: this.ctx.tender.id,
  505. uid: a.aid,
  506. sp_type: 'stage',
  507. sp_id: a.id,
  508. table_name: this.tableName,
  509. template: wxConst.template.stage,
  510. wx_data: wechatData,
  511. });
  512. }
  513. } else {
  514. await this.ctx.service.revisePrice.doPriceUsed(this.ctx.stage, transaction);
  515. await this.ctx.service.tenderTag.saveTenderTag(this.ctx.tender.id, {stage_time: new Date()}, transaction);
  516. const his_id = await this.ctx.service.ledgerHistory.checkBackupLedgerHistory(this.ctx.stage.tid, this.ctx.stage.id);
  517. // 本期结束
  518. // 生成截止本期数据 final数据
  519. await this.ctx.service.stageBillsFinal.generateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  520. await this.ctx.service.stagePosFinal.generateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  521. await this.ctx.service.stageChangeFinal.generateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  522. // 同步 期信息
  523. await transaction.update(this.ctx.service.stage.tableName, {
  524. id: stageId,
  525. status: checkData.checkType,
  526. ...stageTp,
  527. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  528. cache_time_r: this.ctx.stage.cache_time_l,
  529. his_id,
  530. });
  531. await this.ctx.service.tenderCache.updateStageCache4Flow(transaction, this.ctx.stage, checkData.checkType, nextAudits, flowAudits, ledgerTp, stageTp);
  532. await this.ctx.service.stagePay.cacheOrder(this.ctx.stage, transaction);
  533. // 当前期不是最新一起时
  534. if (this.ctx.stage.highOrder !== this.ctx.stage.order) {
  535. const nextStages = await this.ctx.service.stage.getNextStages(this.ctx.stage.tid, this.ctx.stage.order);
  536. for (const ns of nextStages) {
  537. await this.ctx.service.stagePay.reInitialStageData(this.ctx.stage, ns, transaction);
  538. await this.ctx.service.stageOther.addInitialStageData(ns, this.ctx.stage, transaction);
  539. await this.ctx.service.stageSafeProd.addInitialStageData(ns, this.ctx.stage, transaction);
  540. await this.ctx.service.stageJgcl.addInitialStageData(ns, this.ctx.stage, transaction);
  541. await this.ctx.service.stageTempLand.addInitialStageData(ns, this.ctx.stage, transaction);
  542. }
  543. const preStageTp = {
  544. pre_contract_tp: this.ctx.helper.sum([this.ctx.stage.pre_contract_tp, stageTp.contract_tp, this.ctx.stage.contract_pc_tp]),
  545. pre_qc_tp: this.ctx.helper.sum([this.ctx.stage.pre_qc_tp, stageTp.qc_tp, this.ctx.stage.qc_pc_tp]),
  546. pre_positive_qc_tp: this.ctx.helper.sum([this.ctx.stage.pre_positive_qc_tp, stageTp.positive_qc_tp, this.ctx.stage.positive_qc_pc_tp]),
  547. pre_negative_qc_tp: this.ctx.helper.sum([this.ctx.stage.pre_negative_qc_tp, stageTp.negative_qc_tp, this.ctx.stage.negative_qc_pc_tp]),
  548. pre_yf_tp: this.ctx.helper.add(this.ctx.stage.pre_yf_tp, stageTp.yf_tp),
  549. pre_sf_tp: this.ctx.helper.add(this.ctx.stage.pre_sf_tp, stageTp.sf_tp),
  550. };
  551. await transaction.updateRows(this.ctx.service.stage.tableName, nextStages.map(x => { return { id: x.id, ...preStageTp }; }));
  552. }
  553. // 添加短信通知-审批通过提醒功能
  554. const auditList = await this.getAuditors(stageId, stageInfo.times);
  555. const users = this._.uniq(this._.concat(this._.map(auditList, 'aid'), stageInfo.user_id, this._.map(this.ctx.stage.userAssists, 'ass_user_id'), this._.map(this.ctx.stage.auditAssists, 'ass_user_id')));
  556. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.JL, smsTypeConst.judge.result.toString(), SmsAliConst.template.stage_result, {
  557. qi: stageInfo.order,
  558. status: SmsAliConst.status.success,
  559. });
  560. // 微信模板通知
  561. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/measure/stage/' + stageInfo.order);
  562. const wechatData = {
  563. wap_url: shenpiUrl,
  564. qi: stageInfo.order,
  565. status: wxConst.status.success,
  566. tips: wxConst.tips.success,
  567. code: this.ctx.session.sessionProject.code,
  568. };
  569. await this.ctx.helper.sendWechat(users, smsTypeConst.const.JL, smsTypeConst.judge.result.toString(), wxConst.template.stage, wechatData);
  570. // 期审批完成,检查角色签名数据
  571. await this.ctx.service.roleRptRel.checkSignDate(transaction, this.ctx.stage, selfAudit, time);
  572. // 审批通过 - 检查三方特殊推送
  573. await this.ctx.service.specMsg.addStageMsg(transaction, pid, this.ctx.stage, pushOperate.stage.checked);
  574. }
  575. // 上报/审批 - 检查三方特殊推送
  576. await this.ctx.service.specMsg.addStageMsg(transaction, pid, this.ctx.stage, pushOperate.stage.flow);
  577. } else {
  578. // 同步 期信息
  579. await transaction.update(this.ctx.service.stage.tableName, {
  580. id: stageId,
  581. status: auditConst.status.checking,
  582. ...stageTp,
  583. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  584. cache_time_r: this.ctx.stage.cache_time_l,
  585. });
  586. }
  587. await transaction.commit();
  588. } catch (err) {
  589. await transaction.rollback();
  590. throw err;
  591. }
  592. }
  593. async _checkNo(pid, stageId, checkData, times) {
  594. const accountId = this.ctx.session.sessionUser.accountId;
  595. const time = new Date();
  596. // 整理当前流程审核人状态更新
  597. const audits = await this.getAllDataByCondition({ where: { sid: stageId, times, status: auditConst.status.checking } });
  598. if (!audits) throw '审核数据错误';
  599. const selfAudit = audits.find(x => { return x.aid === accountId; });
  600. if (!selfAudit) throw '当前标段您无权审批';
  601. const flowAudits = await this.getAllDataByCondition({ where: { sid: stageId, times: selfAudit.times, order: selfAudit.order }});
  602. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  603. const auditors = await this.getUniqAuditor(stageId, times); // 全部参与的审批人
  604. const newAuditors = auditors.map(x => {
  605. return {
  606. aid: x.aid, tid: selfAudit.tid, sid: selfAudit.sid,
  607. times: times + 1, order: x.audit_order, status: auditConst.status.uncheck,
  608. audit_type: x.audit_type, audit_order: x.audit_order,
  609. }
  610. });
  611. const transaction = await this.db.beginTransaction();
  612. try {
  613. const ledgerTp = await this._updateTender(transaction);
  614. // 添加推送
  615. const noticeContent = await this.getNoticeContent(pid, selfAudit.tid, stageId, selfAudit.aid, checkData.opinion);
  616. const defaultNoticeRecord = {
  617. pid,
  618. type: pushType.stage,
  619. status: auditConst.status.checkNo,
  620. content: noticeContent,
  621. };
  622. const records = [
  623. {
  624. uid: this.ctx.stage.user_id,
  625. ...defaultNoticeRecord,
  626. },
  627. ];
  628. auditors.forEach(audit => {
  629. records.push({
  630. uid: audit.aid,
  631. ...defaultNoticeRecord,
  632. });
  633. });
  634. this.ctx.stage.userAssists.forEach(u => {
  635. records.push({ uid: u.ass_user_id, ...defaultNoticeRecord});
  636. });
  637. this.ctx.stage.auditAssists.forEach(u => {
  638. records.push({ uid: u.ass_user_id, ...defaultNoticeRecord});
  639. });
  640. await transaction.insert(this.ctx.service.noticePush.tableName, records);
  641. // 计算并合同支付最终数据
  642. const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  643. const stageTp = {
  644. contract_tp: tpData.contract_tp,
  645. qc_tp: tpData.qc_tp,
  646. positive_qc_tp: tpData.positive_qc_tp,
  647. negative_qc_tp: tpData.negative_qc_tp,
  648. yf_tp: yfPay.tp,
  649. sf_tp: sfPay.tp,
  650. };
  651. const orgHisIndex = this.ctx.stage.tp_history.findIndex(x => { return x.times === times && x.order === selfAudit.order; });
  652. if (orgHisIndex) this.ctx.stage.tp_history.splice(orgHisIndex, 1);
  653. this.ctx.stage.tp_history.push({ times, order: selfAudit.order, ...stageTp });
  654. const updateData = [];
  655. audits.forEach(x => {
  656. updateData.push({
  657. id: x.id,
  658. status: x.aid === selfAudit.aid ? checkData.checkType : auditConst.status.checkSkip,
  659. opinion: x.aid === selfAudit.aid ? checkData.opinion : '',
  660. end_time: x.aid === selfAudit.aid ? time : null,
  661. });
  662. });
  663. await transaction.updateRows(this.tableName, updateData);
  664. await this.ctx.service.noticeAgain.stopNoticeAgain(transaction, this.tableName, this._.map(updateData, 'id'));
  665. // 同步 期信息
  666. await transaction.update(this.ctx.service.stage.tableName, {
  667. id: stageId,
  668. status: checkData.checkType,
  669. ...stageTp,
  670. times: times + 1,
  671. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  672. cache_time_r: this.ctx.stage.cache_time_l,
  673. });
  674. await this.ctx.service.tenderCache.updateStageCache4Flow(transaction, this.ctx.stage, checkData.checkType,
  675. [{ aid: this.ctx.stage.user_id, audit_order: 0, audit_type: auditType.key.common, status: auditConst.status.uncheck }], flowAudits, ledgerTp, stageTp);
  676. // 拷贝新一次审核流程列表
  677. await transaction.insert(this.tableName, newAuditors);
  678. // 计算该审批人最终数据
  679. await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  680. // 复制一份最新数据给原报
  681. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times + 1, 0, transaction);
  682. await this.ctx.service.stageJgcl.updateHistory(this.ctx.stage, transaction);
  683. await this.ctx.service.stageBonus.updateHistory(this.ctx.stage, transaction);
  684. await this.ctx.service.stageOther.updateHistory(this.ctx.stage, transaction);
  685. await this.ctx.service.stageSafeProd.updateHistory(this.ctx.stage, transaction);
  686. await this.ctx.service.stageTempLand.updateHistory(this.ctx.stage, transaction);
  687. // 锁定本人数据,保留锁定数据相关确认状态
  688. await this.ctx.service.stageAuditAss.lockConfirm4CheckNo(this.ctx.stage, audits.map(x => { return x.aid; }), auditors, transaction);
  689. // 添加短信通知-审批退回提醒功能
  690. // const mobile_array = [];
  691. const stageInfo = await this.ctx.service.stage.getDataById(stageId);
  692. const auditList = await this.getAuditors(stageId, stageInfo.times);
  693. const users = this._.uniq(this._.concat(this._.map(auditList, 'aid'), stageInfo.user_id, this._.map(this.ctx.stage.userAssists, 'ass_user_id'), this._.map(this.ctx.stage.auditAssists, 'ass_user_id')));
  694. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.JL, smsTypeConst.judge.result.toString(), SmsAliConst.template.stage_result, {
  695. qi: stageInfo.order,
  696. status: SmsAliConst.status.back,
  697. });
  698. // 微信模板通知
  699. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/measure/stage/' + stageInfo.order);
  700. const wechatData = {
  701. wap_url: shenpiUrl,
  702. qi: stageInfo.order,
  703. status: wxConst.status.back,
  704. tips: wxConst.tips.back,
  705. code: this.ctx.session.sessionProject.code,
  706. };
  707. await this.ctx.helper.sendWechat(users, smsTypeConst.const.JL, smsTypeConst.judge.result.toString(), wxConst.template.stage, wechatData);
  708. // 上报/审批 - 检查三方特殊推送
  709. await this.ctx.service.specMsg.addStageMsg(transaction, pid, this.ctx.stage, pushOperate.stage.flow);
  710. await transaction.commit();
  711. } catch (err) {
  712. await transaction.rollback();
  713. throw err;
  714. }
  715. }
  716. async _checkNoPre(pid, stageId, checkData, times) {
  717. const accountId = this.ctx.session.sessionUser.accountId;
  718. const time = new Date();
  719. // 整理当前流程审核人状态更新
  720. const audits = await this.getAllDataByCondition({ where: { sid: stageId, times, status: auditConst.status.checking } });
  721. if (audits.length === 0 || audits[0].order <= 1) throw '审核数据错误';
  722. const selfAudit = audits.find(x => { return x.aid === accountId; });
  723. if (!selfAudit) throw '当前标段您无权审批';
  724. const flowAudits = await this.getAllDataByCondition({ where: { sid: stageId, times: selfAudit.times, order: selfAudit.order }});
  725. // 添加重新审批后,不能用order-1,取groupby值里的上一个才对
  726. // const preAuditor = await this.getDataByCondition({sid: stageId, times: times, order: audit.order - 1});
  727. const auditors2 = await this.getAuditGroupByList(stageId, times);
  728. const preAuditors = auditors2.filter(x => { return x.audit_order === selfAudit.audit_order - 1});
  729. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  730. const transaction = await this.db.beginTransaction();
  731. try {
  732. const ledgerTp = await this._updateTender(transaction);
  733. // 添加通知
  734. const noticeContent = await this.getNoticeContent(pid, selfAudit.tid, stageId, selfAudit.aid, checkData.opinion);
  735. const defaultNoticeRecord = {
  736. pid,
  737. type: pushType.stage,
  738. status: auditConst.status.checkNoPre,
  739. content: noticeContent,
  740. };
  741. const records = [
  742. {
  743. uid: this.ctx.stage.user_id,
  744. ...defaultNoticeRecord
  745. },
  746. ];
  747. auditors2.forEach(audit => {
  748. records.push({
  749. uid: audit.aid,
  750. ...defaultNoticeRecord
  751. });
  752. });
  753. this.ctx.stage.userAssists.forEach(u => {
  754. records.push({ uid: u.ass_user_id, ...defaultNoticeRecord});
  755. });
  756. this.ctx.stage.auditAssists.forEach(u => {
  757. records.push({ uid: u.ass_user_id, ...defaultNoticeRecord});
  758. });
  759. await transaction.insert('zh_notice', records);
  760. // 计算并合同支付最终数据
  761. const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  762. const stageTp = {
  763. contract_tp: tpData.contract_tp,
  764. qc_tp: tpData.qc_tp,
  765. positive_qc_tp: tpData.positive_qc_tp,
  766. negative_qc_tp: tpData.negative_qc_tp,
  767. yf_tp: yfPay.tp,
  768. sf_tp: sfPay.tp,
  769. };
  770. this.ctx.stage.tp_history.push({ times, order: selfAudit.order, ...stageTp });
  771. // 同步 期信息
  772. await transaction.update(this.ctx.service.stage.tableName, {
  773. id: stageId,
  774. times,
  775. ...stageTp,
  776. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  777. cache_time_r: this.ctx.stage.cache_time_l,
  778. });
  779. await this.ctx.service.tenderCache.updateStageCache4Flow(transaction, this.ctx.stage, checkData.checkType, preAuditors, flowAudits, ledgerTp, stageTp);
  780. // 更新同一流程所有审批人状态
  781. const updateData = [];
  782. for (const audit of audits) {
  783. if (audit.aid === selfAudit.aid) {
  784. updateData.push({
  785. id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time,
  786. });
  787. } else {
  788. updateData.push({
  789. id: audit.id, status: auditConst.status.checkSkip, opinion: '', end_time: null,
  790. });
  791. }
  792. }
  793. await transaction.updateRows(this.tableName, updateData);
  794. await this.ctx.service.noticeAgain.stopNoticeAgain(transaction, this.tableName, this._.map(updateData, 'id'));
  795. // 顺移其后审核人流程顺序
  796. const sql = 'UPDATE ' + this.tableName + ' SET `order` = `order` + 2 WHERE sid = ? AND times = ? AND `order` > ?';
  797. await transaction.query(sql, [stageId, selfAudit.times, selfAudit.order]);
  798. // 上一审批人,当前审批人 再次添加至流程
  799. const newAuditors = [];
  800. preAuditors.forEach(x => {
  801. newAuditors.push({
  802. tid: this.ctx.stage.tid, sid: this.ctx.stage.id, aid: x.aid,
  803. times: x.times, order: selfAudit.order + 1,
  804. status: auditConst.status.checking, begin_time: time,
  805. audit_type: x.audit_type, audit_order: x.audit_order,
  806. });
  807. });
  808. // 获取ids值
  809. const newAuditors_result = await transaction.insert(this.tableName, newAuditors);
  810. // 获取刚批量添加的所有list
  811. for (let j = 0; j < preAuditors.length; j++) {
  812. newAuditors[j].id = newAuditors_result.insertId + j;
  813. }
  814. const newFlowAuditors = [];
  815. flowAudits.forEach(x => {
  816. newFlowAuditors.push({
  817. tid: this.ctx.stage.tid, sid: this.ctx.stage.id, aid: x.aid,
  818. times: x.times, order: selfAudit.order + 2,
  819. status: auditConst.status.uncheck,
  820. audit_type: x.audit_type, audit_order: x.audit_order,
  821. });
  822. });
  823. await transaction.insert(this.tableName, newFlowAuditors);
  824. // 计算该审批人最终数据
  825. await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  826. // 复制一份最新数据给下一人
  827. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, selfAudit.order + 1, transaction);
  828. await this.ctx.service.stageJgcl.updateHistory(this.ctx.stage, transaction);
  829. await this.ctx.service.stageBonus.updateHistory(this.ctx.stage, transaction);
  830. await this.ctx.service.stageOther.updateHistory(this.ctx.stage, transaction);
  831. await this.ctx.service.stageSafeProd.updateHistory(this.ctx.stage, transaction);
  832. await this.ctx.service.stageTempLand.updateHistory(this.ctx.stage, transaction);
  833. const preAuditorIds = preAuditors.map(x => { return x.aid; });
  834. // 锁定本人数据,保留锁定数据相关确认状态
  835. await this.ctx.service.stageAuditAss.lockConfirm4CheckNoPre(this.ctx.stage, audits.map(x => { return x.aid; }), preAuditorIds, transaction);
  836. // 同步 期信息
  837. await transaction.update(this.ctx.service.stage.tableName, {
  838. id: stageId,
  839. status: checkData.checkType,
  840. cache_time_r: this.ctx.stage.cache_time_l,
  841. });
  842. const stageInfo = await this.ctx.service.stage.getDataById(stageId);
  843. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/measure/stage/' + stageInfo.order);
  844. const users = this._.map(this.ctx.stage.auditAssists.filter(x => {return preAuditorIds.indexOf(x.user_id) >= 0 }), 'ass_user_id');
  845. users.push(...preAuditorIds);
  846. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), SmsAliConst.template.stage_check, {
  847. qi: stageInfo.order,
  848. code: shenpiUrl,
  849. });
  850. // 微信模板通知
  851. const wechatData = {
  852. wap_url: shenpiUrl,
  853. qi: stageInfo.order,
  854. status: wxConst.status.check,
  855. tips: wxConst.tips.check,
  856. code: this.ctx.session.sessionProject.code,
  857. };
  858. await this.ctx.helper.sendWechat(users, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), wxConst.template.stage, wechatData);
  859. // 重新发送配置
  860. for (const a of newAuditors) {
  861. await this.ctx.service.noticeAgain.addNoticeAgain(transaction, smsTypeConst.const.JL, {
  862. pid: this.ctx.session.sessionProject.id,
  863. tid: this.ctx.tender.id,
  864. uid: a.aid,
  865. sp_type: 'stage',
  866. sp_id: a.id,
  867. table_name: this.tableName,
  868. template: wxConst.template.stage,
  869. wx_data: wechatData,
  870. });
  871. }
  872. // 上报/审批 - 检查三方特殊推送
  873. await this.ctx.service.specMsg.addStageMsg(transaction, pid, this.ctx.stage, pushOperate.stage.flow);
  874. await transaction.commit();
  875. } catch (err) {
  876. await transaction.rollback();
  877. throw err;
  878. }
  879. }
  880. /**
  881. * 审批
  882. * @param {Number} stageId - 标段id
  883. * @param {auditConst.status.checked|auditConst.status.checkNo} checkType - 审批结果
  884. * @param {Number} times - 第几次审批
  885. * @return {Promise<void>}
  886. */
  887. async check(stageId, checkData, times = 1) {
  888. if (checkData.checkType !== auditConst.status.checked && checkData.checkType !== auditConst.status.checkNo && checkData.checkType !== auditConst.status.checkNoPre) {
  889. throw '提交数据错误';
  890. }
  891. // // 整理当前流程审核人状态更新
  892. // const audit = await this.getDataByCondition({sid: stageId, times: times, status: auditConst.status.checking});
  893. // if (!audit) {
  894. // throw '审核数据错误';
  895. // }
  896. // const time = new Date();
  897. const pid = this.ctx.session.sessionProject.id;
  898. switch (checkData.checkType) {
  899. case auditConst.status.checked:
  900. await this._checked(pid, stageId, checkData, times);
  901. break;
  902. case auditConst.status.checkNo:
  903. await this._checkNo(pid, stageId, checkData, times);
  904. break;
  905. case auditConst.status.checkNoPre:
  906. await this._checkNoPre(pid, stageId, checkData, times);
  907. break;
  908. default:
  909. throw '无效审批操作';
  910. }
  911. // 通知发送 - 第三方更新
  912. if (this.ctx.session.sessionProject.custom && syncApiConst.notice_type.indexOf(this.ctx.session.sessionProject.customType) !== -1) {
  913. const base_data = {
  914. tid: this.ctx.tender.id,
  915. sid: stageId,
  916. op: 'update',
  917. };
  918. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  919. base_data.op = 'update';
  920. base_data.sid = -1;
  921. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  922. }
  923. }
  924. /**
  925. * 审批
  926. * @param {Number} stageId - 标段id
  927. * @param {Number} times - 第几次审批
  928. * @return {Promise<void>}
  929. */
  930. async checkAgain(stageId, times = 1, force) {
  931. const accountId = this.ctx.session.sessionUser.accountId;
  932. const time = new Date();
  933. // 整理当前流程审核人状态更新
  934. const auditors = await this.getAllDataByCondition({
  935. where: { sid: stageId, times },
  936. orders: [['order', 'asc']],
  937. });
  938. const maxOrder = auditors[auditors.length - 1].order;
  939. const audits = auditors.filter(x => { return x.order === maxOrder});
  940. if (!audits || audits.length === 0 || maxOrder < 1) throw '审核数据错误';
  941. const selfAudit = audits.find(x => { return x.aid === accountId; });
  942. if (!selfAudit && !force) throw '当前标段您无权审批';
  943. const finalAudit = selfAudit || audits[0];
  944. const stageInfo = await this.ctx.service.stage.getDataById(stageId);
  945. const transaction = await this.db.beginTransaction();
  946. try {
  947. // 当前审批人2次添加至流程中
  948. const checkAgainAuditors = [], checkingAuditors = [];
  949. audits.forEach(x => {
  950. checkAgainAuditors.push({
  951. tid: x.tid, sid: x.sid, aid: x.aid,
  952. times: x.times, order: x.order + 1,
  953. status: !selfAudit || x.aid === selfAudit.aid ? auditConst.status.checkAgain : auditConst.status.checkSkip,
  954. begin_time: time, end_time: time, opinion: '',
  955. audit_type: x.audit_type, audit_order: x.audit_order,
  956. });
  957. });
  958. audits.forEach(x => {
  959. checkingAuditors.push({
  960. tid: x.tid, sid: x.sid, aid: x.aid,
  961. times: x.times, order: x.order + 2,
  962. status: auditConst.status.checking,
  963. begin_time: time, end_time: time, opinion: '',
  964. audit_type: x.audit_type, audit_order: x.audit_order,
  965. });
  966. });
  967. await transaction.insert(this.tableName, checkAgainAuditors);
  968. const checkingAuditors_result = await transaction.insert(this.tableName, checkingAuditors);
  969. // 获取刚批量添加的所有list
  970. for (let j = 0; j < checkingAuditors.length; j++) {
  971. checkingAuditors[j].id = checkingAuditors_result.insertId + j;
  972. }
  973. // 复制一份最新数据给下一人
  974. await this.ctx.service.stagePay.clearCacheOrder(this.ctx.stage, transaction);
  975. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, finalAudit.order + 1, transaction);
  976. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, finalAudit.order + 2, transaction);
  977. await this.ctx.service.stageJgcl.updateHistory4CheckAgain(this.ctx.stage, transaction);
  978. await this.ctx.service.stageBonus.updateHistory4CheckAgain(this.ctx.stage, transaction);
  979. await this.ctx.service.stageOther.updateHistory4CheckAgain(this.ctx.stage, transaction);
  980. await this.ctx.service.stageSafeProd.updateHistory4CheckAgain(this.ctx.stage, transaction);
  981. await this.ctx.service.stageTempLand.updateHistory4CheckAgain(this.ctx.stage, transaction);
  982. // 同步 期信息
  983. const his = this.ctx.stage.tp_history.find(x => { return x.times === times && x.order === finalAudit.order });
  984. const stageTp = {
  985. contract_tp: his.contract_tp,
  986. qc_tp: his.qc_tp,
  987. positive_qc_tp: his.positive_qc_tp,
  988. negative_qc_tp: his.negative_qc_tp,
  989. yf_tp: his.tp,
  990. sf_tp: his.tp,
  991. };
  992. this.ctx.stage.tp_history.push({ times, order: finalAudit.order + 1, ...stageTp });
  993. await transaction.update(this.ctx.service.stage.tableName, {
  994. id: stageId,
  995. status: auditConst.status.checking,
  996. cache_time_r: this.ctx.stage.cache_time_l,
  997. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  998. });
  999. // 已经引用到本期的单价变更,全部取消
  1000. await this.ctx.service.revisePrice.cancelPriceUsed(this.ctx.stage, transaction);
  1001. // 重算所有单价变更
  1002. const priceCalc = new RevisePrice(this.ctx);
  1003. const pcTp = await priceCalc.stageCheckAgainPriceChange(this.ctx.stage, finalAudit.order + 2, transaction);
  1004. await this.ctx.service.tenderCache.updateStageCache4Flow(transaction, this.ctx.stage, auditConst.status.checking, checkingAuditors, checkAgainAuditors, null, stageTp, pcTp);
  1005. // 添加短信通知-需要审批提醒功能
  1006. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/measure/stage/' + stageInfo.order);
  1007. const auditorIds = audits.map(x => { return x.aid; });
  1008. const users = this._.map(this.ctx.stage.auditAssists.filter(x => { return auditorIds.indexOf(x.user_id) >= 0; }), 'ass_user_id');
  1009. users.push(...auditorIds);
  1010. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), SmsAliConst.template.stage_check, {
  1011. qi: stageInfo.order,
  1012. code: shenpiUrl,
  1013. });
  1014. // 微信模板通知
  1015. const wechatData = {
  1016. wap_url: shenpiUrl,
  1017. qi: stageInfo.order,
  1018. status: wxConst.status.check,
  1019. tips: wxConst.tips.check,
  1020. code: this.ctx.session.sessionProject.code,
  1021. };
  1022. await this.ctx.helper.sendWechat(users, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), wxConst.template.stage, wechatData);
  1023. // 重新发送配置
  1024. for (const a of checkingAuditors) {
  1025. await this.ctx.service.noticeAgain.addNoticeAgain(transaction, smsTypeConst.const.JL, {
  1026. pid: this.ctx.session.sessionProject.id,
  1027. tid: this.ctx.tender.id,
  1028. uid: a.aid,
  1029. sp_type: 'stage',
  1030. sp_id: a.id,
  1031. table_name: this.tableName,
  1032. template: wxConst.template.stage,
  1033. wx_data: wechatData,
  1034. });
  1035. }
  1036. // 上报/审批 - 检查三方特殊推送
  1037. await this.ctx.service.specMsg.addStageMsg(transaction, this.ctx.session.sessionProject.id, this.ctx.stage, pushOperate.stage.flow);
  1038. await transaction.commit();
  1039. // 通知发送 - 第三方更新
  1040. if (this.ctx.session.sessionProject.custom && syncApiConst.notice_type.indexOf(this.ctx.session.sessionProject.customType) !== -1) {
  1041. const base_data = {
  1042. tid: this.ctx.tender.id,
  1043. sid: stageId,
  1044. op: 'update',
  1045. };
  1046. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  1047. base_data.op = 'update';
  1048. base_data.sid = -1;
  1049. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  1050. }
  1051. } catch (err) {
  1052. await transaction.rollback();
  1053. throw err;
  1054. }
  1055. }
  1056. /**
  1057. * 原报撤回,直接改动审批人状态
  1058. * 如果存在审批人数据,将其改为原报流程数据,但保留原提交人
  1059. *
  1060. * 一审 1 A checking -> A uncheck status改 pay/jl:删0(jl为增量数据,只删重复部分) 1->0 删1
  1061. * ...
  1062. *
  1063. * @param stage
  1064. * @returns {Promise<void>}
  1065. * @private
  1066. */
  1067. async _userCheckCancel(stage) {
  1068. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(stage);
  1069. const transaction = await this.db.beginTransaction();
  1070. try {
  1071. const ledgerTp = await this._updateTender(transaction);
  1072. // 整理当前流程审核人状态更新
  1073. // 审批人变成待审批状态
  1074. const updateData = stage.curAuditors.map(x => {
  1075. return {
  1076. id: x.id,
  1077. status: auditConst.status.uncheck,
  1078. begin_time: null,
  1079. opinion: null,
  1080. }
  1081. });
  1082. await transaction.updateRows(this.tableName, updateData);
  1083. await this.ctx.service.noticeAgain.deleteNoticeAgain(transaction, this.tableName, this._.map(updateData, 'id'));
  1084. // 计算并合同支付最终数据
  1085. const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(stage, transaction);
  1086. const stageTp = {
  1087. contract_tp: tpData.contract_tp,
  1088. qc_tp: tpData.qc_tp,
  1089. positive_qc_tp: tpData.positive_qc_tp,
  1090. negative_qc_tp: tpData.negative_qc_tp,
  1091. yf_tp: yfPay.tp,
  1092. sf_tp: sfPay.tp,
  1093. };
  1094. await transaction.update(this.ctx.service.stage.tableName, {
  1095. id: stage.id,
  1096. times: stage.times,
  1097. ...stageTp,
  1098. tp_history: JSON.stringify(this.ctx.stage.tp_history.filter(x => { return x.times < stage.times; })),
  1099. cache_time_r: this.ctx.stage.cache_time_l,
  1100. status: stage.times === 1 ? auditConst.status.uncheck : auditConst.status.checkNo,
  1101. });
  1102. // 计量明细:重复数据删除原报,新增数据修改为原报
  1103. await this.ctx.service.stageBills.updateStageBills4CheckCancel(stage.id, stage.times, 0, stage.times, 1, transaction);
  1104. await this.ctx.service.stagePos.updateStagePos4CheckCancel(stage.id, stage.times, 0, stage.times, 1, transaction);
  1105. await this.ctx.service.stageDetail.updateStageDetail4CheckCancel(stage.id, stage.times, 0, stage.times, 1, transaction);
  1106. await this.ctx.service.stageChange.updateStageChange4CheckCancel(stage.id, stage.times, 0, stage.times, 1, transaction);
  1107. // 合同支付明细:保留审批人数据,覆盖到原报中并删除该审批人合同支付数据
  1108. await this.ctx.service.stagePay.deleteAuditStagePays(stage, stage.times, 0, transaction);
  1109. await this.ctx.service.stagePay.copyAuditStagePays(stage, stage.times, 0, transaction, this.ctx.stage.times, 1);
  1110. await this.ctx.service.stagePay.deleteAuditStagePays(stage, stage.times, 1, transaction);
  1111. // 其他台账明细:不需更新历史
  1112. // 更新标段计量数据缓存
  1113. const preFlow = stage.times > 1 ? await this.getLastestAuditors(stage.id, stage.times, auditConst.status.checkNo): null;
  1114. const curFlow = [{ aid: this.ctx.stage.user_id, audit_type: auditType.key.common, audit_order: 1, status: auditConst.status.uncheck}];
  1115. await this.ctx.service.tenderCache.updateStageCache4Flow(transaction, this.ctx.stage, stage.times === 1 ? auditConst.status.uncheck : auditConst.status.checkNo,
  1116. curFlow, preFlow, ledgerTp, stageTp);
  1117. // 上报/审批 - 检查三方特殊推送
  1118. await this.ctx.service.specMsg.addStageMsg(transaction, this.ctx.session.sessionProject.id, this.ctx.stage, pushOperate.stage.flow);
  1119. await transaction.commit();
  1120. } catch(err) {
  1121. await transaction.rollback();
  1122. throw err;
  1123. }
  1124. }
  1125. /**
  1126. * 审批人撤回审批通过,插入两条数据
  1127. *
  1128. * 一审 1 A checked 一审 1 A checked
  1129. * 二审 2 B checked pre -> 二审 2 B checked
  1130. * 三审 3 C checking cur 二审 3 B checkCancel 增 增extra_his 增tp_his
  1131. * 四审 4 D uncheck 二审 4 B checking 增 增pay_cur
  1132. * 三审 5 C uncheck order、status改
  1133. * 四审 6 D uncheck order改
  1134. *
  1135. * @param stage
  1136. * @returns {Promise<void>}
  1137. * @private
  1138. */
  1139. async _auditCheckCancel(stage) {
  1140. if (stage.curAuditors.length === 0 || stage.curAuditors[0].order <= 1) {
  1141. throw '撤回用户数据错误';
  1142. }
  1143. const accountId = this.ctx.session.sessionUser.accountId;
  1144. const selfAuditor = this.ctx.stage.preAuditors.find(x => { return x.aid === accountId; });
  1145. if (!selfAuditor) throw '撤回用户数据错误';
  1146. const time = new Date();
  1147. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  1148. const transaction = await this.db.beginTransaction();
  1149. try {
  1150. const ledgerTp = await this._updateTender(transaction);
  1151. // 整理当前流程审核人状态更新
  1152. // 顺移其后审核人流程顺序
  1153. const sql = 'UPDATE ' + this.tableName + ' SET `order` = `order` + 2 WHERE sid = ? AND times = ? AND `order` > ?';
  1154. await transaction.query(sql, [stage.id, selfAuditor.times, stage.curAuditors[0].order]);
  1155. // 当前审批人2次添加至流程中
  1156. const checkCancelAuditors = [], checkingAuditors = [];
  1157. stage.preAuditors.forEach(x => {
  1158. checkCancelAuditors.push({
  1159. tid: stage.tid, sid: stage.id, aid: x.aid,
  1160. times: x.times, order: x.order + 1,
  1161. status: x.aid === selfAuditor.aid ? auditConst.status.checkCancel : auditConst.status.checkSkip,
  1162. begin_time: time, end_time: time, opinion: '',
  1163. audit_type: x.audit_type, audit_order: x.audit_order,
  1164. });
  1165. });
  1166. stage.preAuditors.forEach(x => {
  1167. checkingAuditors.push({
  1168. tid: stage.tid, sid: stage.id, aid: x.aid,
  1169. times: x.times, order: x.order + 2,
  1170. status: auditConst.status.checking,
  1171. begin_time: time, end_time: time, opinion: '',
  1172. audit_type: x.audit_type, audit_order: x.audit_order,
  1173. });
  1174. });
  1175. await transaction.insert(this.tableName, [...checkCancelAuditors, ...checkingAuditors]);
  1176. // 当前审批人变成待审批
  1177. await transaction.updateRows(this.tableName, stage.curAuditors.map(x => { return {
  1178. id: x.id, begin_time: null, status: auditConst.status.uncheck, order: x.order + 2
  1179. }}));
  1180. await this.ctx.service.noticeAgain.deleteNoticeAgain(transaction, this.tableName, this._.map(stage.curAuditors, 'id'));
  1181. // 计算并合同支付最终数据
  1182. const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  1183. const stageTp = {
  1184. contract_tp: tpData.contract_tp,
  1185. qc_tp: tpData.qc_tp,
  1186. positive_qc_tp: tpData.positive_qc_tp,
  1187. negative_qc_tp: tpData.negative_qc_tp,
  1188. yf_tp: yfPay.tp,
  1189. sf_tp: sfPay.tp,
  1190. };
  1191. this.ctx.stage.tp_history.push({ times: stage.times, order: selfAuditor.order + 1, ...stageTp });
  1192. // 同步 期信息
  1193. await transaction.update(this.ctx.service.stage.tableName, {
  1194. id: stage.id,
  1195. times: stage.times,
  1196. ...stageTp,
  1197. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  1198. cache_time_r: this.ctx.stage.cache_time_l,
  1199. });
  1200. // 计量明细:勿需修改任何数据
  1201. // 合同支付明细:复制一份最新数据给下一人
  1202. await this.ctx.service.stagePay.copyAuditStagePays(stage, stage.times, selfAuditor.order + 2, transaction, stage.times, selfAuditor.order + 1);
  1203. // 其他台账明细:更新两份历史数据
  1204. await this.ctx.service.stageJgcl.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
  1205. await this.ctx.service.stageBonus.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
  1206. await this.ctx.service.stageOther.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
  1207. await this.ctx.service.stageSafeProd.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
  1208. await this.ctx.service.stageTempLand.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
  1209. // 更新标段计量数据缓存
  1210. await this.ctx.service.tenderCache.updateStageCache4Flow(transaction, stage, stage.status, checkingAuditors, checkCancelAuditors, ledgerTp, stageTp);
  1211. // 上报/审批 - 检查三方特殊推送
  1212. await this.ctx.service.specMsg.addStageMsg(transaction, this.ctx.session.sessionProject.id, stage, pushOperate.stage.flow);
  1213. await transaction.commit();
  1214. } catch(err) {
  1215. await transaction.rollback();
  1216. throw err;
  1217. }
  1218. }
  1219. /**
  1220. * 审批人撤回审批退回上一人,插入两条数据
  1221. *
  1222. * 一审 1 A checked 一审 1 A checked
  1223. * 二审 2 B checked 二审 2 B checked
  1224. * 三审 3 C checkNoPre pre -> 三审 3 C checkNoPre
  1225. * 二审 4 B checking cur 三审 4 C checkCancel 删4B 增4C 增extra_his 增tp_his
  1226. * 三审 5 C uncheck 三审 5 C checking status改 增pay_cur
  1227. * 四审 6 D uncheck 四审 6 D uncheck
  1228. *
  1229. * @param stage
  1230. * @returns {Promise<void>}
  1231. * @private
  1232. */
  1233. async _auditCheckCancelNoPre(stage) {
  1234. if (stage.curAuditors.length === 0 || stage.curAuditors[0].order <= 1) {
  1235. throw '撤回用户数据错误';
  1236. }
  1237. const accountId = this.ctx.session.sessionUser.accountId;
  1238. const selfAuditor = this.ctx.stage.preAuditors.find(x => { return x.aid === accountId; });
  1239. if (!selfAuditor) throw '撤回用户数据错误';
  1240. const time = new Date();
  1241. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  1242. const transaction = await this.db.beginTransaction();
  1243. try {
  1244. const ledgerTp = await this._updateTender(transaction);
  1245. // 整理当前流程审核人状态更新
  1246. // 删除当前审批人
  1247. await transaction.delete(this.tableName, { id: stage.curAuditors.map(x => { return x.id; }) });
  1248. await this.ctx.service.noticeAgain.deleteNoticeAgain(transaction, this.tableName, this._.map(stage.curAuditors, 'id'));
  1249. // 添加撤回人到审批流程中
  1250. const newAuditors = [];
  1251. stage.preAuditors.forEach(x => {
  1252. newAuditors.push({
  1253. tid: stage.tid, sid: stage.id, aid: x.aid,
  1254. times: x.times, order: x.order + 1,
  1255. status: x.aid === selfAuditor.aid ? auditConst.status.checkCancel : auditConst.status.checkSkip,
  1256. begin_time: time, end_time: time, opinion: '',
  1257. audit_type: x.audit_type, audit_order: x.audit_order,
  1258. });
  1259. });
  1260. await transaction.insert(this.tableName, newAuditors);
  1261. // 更新上一个人,最新审批状态为审批中
  1262. await transaction.update(this.tableName, { begin_time: time, status: auditConst.status.checking }, {
  1263. where: { sid: stage.id, times: selfAuditor.times, order: selfAuditor.order + 2 }
  1264. });
  1265. // 计算并合同支付最终数据
  1266. const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(stage, transaction);
  1267. const stageTp = {
  1268. contract_tp: tpData.contract_tp,
  1269. qc_tp: tpData.qc_tp,
  1270. positive_qc_tp: tpData.positive_qc_tp,
  1271. negative_qc_tp: tpData.negative_qc_tp,
  1272. yf_tp: yfPay.tp,
  1273. sf_tp: sfPay.tp,
  1274. };
  1275. stage.tp_history.push({ times: stage.times, order: selfAuditor.order + 1, ...stageTp });
  1276. // 同步 期信息
  1277. await transaction.update(this.ctx.service.stage.tableName, {
  1278. id: stage.id,
  1279. times: stage.times,
  1280. ...stageTp,
  1281. status: auditConst.status.checking,
  1282. tp_history: JSON.stringify(stage.tp_history),
  1283. cache_time_r: stage.cache_time_l,
  1284. });
  1285. // 计量明细:勿需修改任何数据
  1286. // 合同支付明细:复制一份最新数据给下一人
  1287. await this.ctx.service.stagePay.copyAuditStagePays(stage, stage.times, selfAuditor.order + 2, transaction, stage.times, selfAuditor.order + 1);
  1288. // 其他台账明细:更新一份历史数据
  1289. await this.ctx.service.stageJgcl.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
  1290. await this.ctx.service.stageBonus.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
  1291. await this.ctx.service.stageOther.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
  1292. await this.ctx.service.stageSafeProd.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
  1293. await this.ctx.service.stageTempLand.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
  1294. // 更新标段计量数据缓存
  1295. await this.ctx.service.tenderCache.updateStageCache4Flow(transaction, stage, stage.status, newAuditors, stage.preAuditors, ledgerTp, stageTp);
  1296. // 上报/审批 - 检查三方特殊推送
  1297. await this.ctx.service.specMsg.addStageMsg(transaction, this.ctx.session.sessionProject.id, stage, pushOperate.stage.flow);
  1298. await transaction.commit();
  1299. } catch(err) {
  1300. await transaction.rollback();
  1301. throw err;
  1302. }
  1303. }
  1304. /**
  1305. * 审批人撤回审批退回原报
  1306. *
  1307. * 1# 一审 1 A checked 1# 一审 1 A checked
  1308. * 二审 2 B checkNo pre -> 二审 2 B checkNo
  1309. * 三审 3 C uncheck 二审 3 B checkCancel 增 pay: 2#0 -> 1#3 jl: 2#0 -> 1#3 增tp_his 增extra_his
  1310. * 二审 4 B checking 增 pay: 2#0 -> 1#4
  1311. * 三审 5 C uncheck order改
  1312. *
  1313. * 2# 一审 1 A uncheck 2# 删 pay: 2#0删 jl: 2#0删
  1314. * 二审 2 B uncheck
  1315. * 三审 3 C uncheck
  1316. *
  1317. * @param stage
  1318. * @returns {Promise<void>}
  1319. * @private
  1320. */
  1321. async _auditCheckCancelNo(stage) {
  1322. const accountId = this.ctx.session.sessionUser.accountId;
  1323. const selfAuditor = stage.preAuditors.find(x => { return x.aid === accountId && x.status === auditConst.status.checkNo; });
  1324. if (!selfAuditor) throw '该标段由他人审批退回,您不可撤回';
  1325. const time = new Date();
  1326. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(stage);
  1327. const transaction = await this.db.beginTransaction();
  1328. try {
  1329. const ledgerTp = await this._updateTender(transaction);
  1330. // 整理上一个流程审核人状态更新
  1331. // 顺移其后审核人流程顺序
  1332. const sql = 'UPDATE ' + this.tableName + ' SET `order` = `order` + 2 WHERE sid = ? AND times = ? AND `order` > ?';
  1333. await transaction.query(sql, [stage.id, selfAuditor.times, selfAuditor.order]);
  1334. // 当前审批人2次添加至流程中
  1335. const checkCancelAuditors = [], checkingAuditors = [];
  1336. stage.preAuditors.forEach(x => {
  1337. checkCancelAuditors.push({
  1338. tid: stage.tid, sid: stage.id, aid: x.aid,
  1339. times: x.times, order: x.order + 1,
  1340. status: x.aid === selfAuditor.aid ? auditConst.status.checkCancel : auditConst.status.checkSkip,
  1341. begin_time: time, end_time: time, opinion: '',
  1342. audit_type: x.audit_type, audit_order: x.audit_order,
  1343. });
  1344. });
  1345. stage.preAuditors.forEach(x => {
  1346. checkingAuditors.push({
  1347. tid: stage.tid, sid: stage.id, aid: x.aid,
  1348. times: x.times, order: x.order + 2,
  1349. status: auditConst.status.checking,
  1350. begin_time: time, end_time: time, opinion: '',
  1351. audit_type: x.audit_type, audit_order: x.audit_order,
  1352. });
  1353. });
  1354. await transaction.insert(this.tableName, [...checkCancelAuditors, ...checkingAuditors]);
  1355. // 删除当前次审批流
  1356. await transaction.delete(this.tableName, { sid: stage.id, times: stage.times });
  1357. // 计算并合同支付最终数据
  1358. const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(stage, transaction);
  1359. const stageTp = {
  1360. contract_tp: tpData.contract_tp,
  1361. qc_tp: tpData.qc_tp,
  1362. positive_qc_tp: tpData.positive_qc_tp,
  1363. negative_qc_tp: tpData.negative_qc_tp,
  1364. yf_tp: yfPay.tp,
  1365. sf_tp: sfPay.tp,
  1366. };
  1367. stage.tp_history.push({ times: selfAuditor.times, order: selfAuditor.order + 1, ...stageTp });
  1368. await transaction.update(this.ctx.service.stage.tableName, {
  1369. id: stage.id,
  1370. times: stage.times - 1,
  1371. ...stageTp,
  1372. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  1373. cache_time_r: stage.cache_time_l,
  1374. status: auditConst.status.checking,
  1375. });
  1376. // 计量明细:重复数据删除原报,新增数据修改为原报
  1377. await this.ctx.service.stageBills.updateStageBills4CheckCancel(stage.id, selfAuditor.times, selfAuditor.order + 1, stage.times, 0, transaction);
  1378. await this.ctx.service.stagePos.updateStagePos4CheckCancel(stage.id, selfAuditor.times, selfAuditor.order + 1, stage.times, 0, transaction);
  1379. await this.ctx.service.stageDetail.updateStageDetail4CheckCancel(stage.id, selfAuditor.times, selfAuditor.order + 1, stage.times, 0, transaction);
  1380. await this.ctx.service.stageChange.updateStageChange4CheckCancel(stage.id, selfAuditor.times, selfAuditor.order + 1, stage.times, 0, transaction);
  1381. // 合同支付明细:复制一份最新数据新增的2个审批流,删除最新一次审批数据
  1382. await this.ctx.service.stagePay.copyAuditStagePays(stage, stage.times - 1, selfAuditor.order + 1, transaction, stage.times, 0);
  1383. await this.ctx.service.stagePay.copyAuditStagePays(stage, stage.times - 1, selfAuditor.order + 2, transaction, stage.times, 0);
  1384. await this.ctx.service.stagePay.deleteAuditStagePays(stage, stage.times, 0, transaction);
  1385. // 其他台账明细:更新一份历史数据
  1386. await this.ctx.service.stageJgcl.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
  1387. await this.ctx.service.stageBonus.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
  1388. await this.ctx.service.stageOther.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
  1389. await this.ctx.service.stageSafeProd.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
  1390. await this.ctx.service.stageTempLand.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
  1391. // 更新标段计量数据缓存
  1392. await this.ctx.service.tenderCache.updateStageCache4Flow(transaction, stage, auditConst.status.checking, checkingAuditors, checkCancelAuditors, ledgerTp, stageTp);
  1393. // 上报/审批 - 检查三方特殊推送
  1394. await this.ctx.service.specMsg.addStageMsg(transaction, this.ctx.session.sessionProject.id, stage, pushOperate.stage.flow);
  1395. await transaction.commit();
  1396. } catch(err) {
  1397. await transaction.rollback();
  1398. throw err;
  1399. }
  1400. }
  1401. /**
  1402. * 会签未全部审批通过时,撤回仅修改本人状态
  1403. *
  1404. * @param stage
  1405. * @returns {Promise<void>}
  1406. * @private
  1407. */
  1408. async _auditCheckCancelAnd(stage) {
  1409. const accountId = this.ctx.session.sessionUser.accountId;
  1410. const selfAuditor = stage.flowAuditors.find(x => { return x.aid === accountId; });
  1411. if (!selfAuditor || selfAuditor.status !== auditConst.status.checked) throw '不可撤回';
  1412. const transaction = await this.db.beginTransaction();
  1413. try {
  1414. await transaction.update(this.tableName, {
  1415. id: selfAuditor.id, status: auditConst.status.checking, opinion: '', end_time: null,
  1416. });
  1417. await transaction.commit();
  1418. } catch(err) {
  1419. await transaction.rollback();
  1420. throw err;
  1421. }
  1422. }
  1423. /**
  1424. * 审批撤回
  1425. * @param {Number} stageId - 标段id
  1426. * @param {Number} times - 第几次审批
  1427. * @return {Promise<void>}
  1428. */
  1429. async checkCancel(stage) {
  1430. // 分5种情况,根据ctx.cancancel值判断:
  1431. // 1.原报发起撤回,当前流程删除,并回到待上报
  1432. // 2.审批人撤回审批通过,增加流程,并回到它审批中
  1433. // 3.审批人撤回审批退回上一人,并删除退回人,增加流程,并回到它审批中,并更新计量期状态为审批中
  1434. // 4.审批人撤回退回原报操作,删除新增的审批流,增加流程,回滚到它审批中
  1435. // 5.会签审批人撤回审批通过(还有其他审批人未审批通过),仅修改本人流程状态
  1436. if (stage.cancancel === 5) {
  1437. await this._auditCheckCancelAnd(stage);
  1438. } else {
  1439. switch (this.ctx.stage.cancancel) {
  1440. case 1: await this._userCheckCancel(stage); break;
  1441. case 2: await this._auditCheckCancel(stage); break;
  1442. case 3: await this._auditCheckCancelNoPre(stage); break;
  1443. case 4: await this._auditCheckCancelNo(stage); break;
  1444. default: throw '不可撤回,请刷新页面重试';
  1445. }
  1446. // 通知发送 - 第三方更新
  1447. if (this.ctx.session.sessionProject.custom && syncApiConst.notice_type.indexOf(this.ctx.session.sessionProject.customType) !== -1) {
  1448. const base_data = {
  1449. tid: stage.tid,
  1450. sid: stage.id,
  1451. op: 'update',
  1452. };
  1453. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  1454. base_data.op = 'update';
  1455. base_data.sid = -1;
  1456. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  1457. }
  1458. }
  1459. }
  1460. /**
  1461. * 获取审核人需要审核的期列表
  1462. *
  1463. * @param auditorId
  1464. * @return {Promise<*>}
  1465. */
  1466. async getAuditStage(auditorId) {
  1467. const sql =
  1468. 'SELECT sa.`aid`, sa.`times`, sa.`order`, sa.`begin_time`, sa.`end_time`, sa.`tid`, sa.`sid`,' +
  1469. ' s.`order` As `sorder`, s.`status` As `sstatus`,' +
  1470. ' t.`name`, t.`project_id`, t.`type`, t.`user_id` ' +
  1471. ' FROM ?? AS sa ' +
  1472. ' Left Join ?? AS s On sa.`sid` = s.`id` ' +
  1473. ' Left Join ?? As t ON sa.`tid` = t.`id`' +
  1474. ' WHERE ((sa.`aid` = ? and sa.`status` = ?) OR (s.`user_id` = ? and sa.`status` = ? and s.`status` = ? and sa.`times` = (s.`times`-1)))' +
  1475. ' ORDER BY sa.`begin_time` DESC';
  1476. const sqlParam = [
  1477. this.tableName,
  1478. this.ctx.service.stage.tableName,
  1479. this.ctx.service.tender.tableName,
  1480. auditorId,
  1481. auditConst.status.checking,
  1482. auditorId,
  1483. auditConst.status.checkNo,
  1484. auditConst.status.checkNo,
  1485. ];
  1486. const result = await this.db.query(sql, sqlParam);
  1487. // 过滤result中存在重复sid的值, 保留最新的一条
  1488. const filterResult = [];
  1489. const sidArr = [];
  1490. for (const r of result) {
  1491. if (sidArr.indexOf(r.sid) === -1) {
  1492. filterResult.push(r);
  1493. sidArr.push(r.sid);
  1494. }
  1495. }
  1496. return filterResult;
  1497. }
  1498. /**
  1499. * 获取审核人审核的次数
  1500. *
  1501. * @param auditorId
  1502. * @return {Promise<*>}
  1503. */
  1504. async getCountByChecked(auditorId) {
  1505. return await this.db.count(this.tableName, { aid: auditorId, status: [auditConst.status.checked, auditConst.status.checkNo, auditConst.status.checkNoPre] });
  1506. }
  1507. /**
  1508. * 获取最近一次审批结束时间
  1509. *
  1510. * @param auditorId
  1511. * @return {Promise<*>}
  1512. */
  1513. async getLastEndTimeByChecked(auditorId) {
  1514. const sql = 'SELECT `end_time` FROM ?? WHERE `aid` = ? ' +
  1515. 'AND `status` in (' + this.ctx.helper.getInArrStrSqlFilter([auditConst.status.checked, auditConst.status.checkNo, auditConst.status.checkNoPre]) + ') ORDER BY `end_time` DESC';
  1516. const sqlParam = [this.tableName, auditorId];
  1517. const result = await this.db.queryOne(sql, sqlParam);
  1518. return result ? result.end_time : null;
  1519. }
  1520. /**
  1521. * 获取 某时间后 审批进度 更新的期
  1522. * @param {Number} pid - 查询标段
  1523. * @param {Number} uid - 查询人
  1524. * @param {Date} time - 查询时间
  1525. * @return {Promise<*>}
  1526. */
  1527. async getNoticeStage(pid, uid, time) {
  1528. let notice = await this.db.select('zh_notice', {
  1529. where: { pid, type: pushType.stage, uid },
  1530. orders: [['create_time', 'desc']],
  1531. limit: 10,
  1532. offset: 0,
  1533. });
  1534. notice = notice.map(v => {
  1535. const extra = JSON.parse(v.content);
  1536. delete v.content;
  1537. return { ...v, ...extra };
  1538. });
  1539. return notice;
  1540. }
  1541. /**
  1542. * 用于添加推送所需的content内容
  1543. * @param {Number} pid 项目id
  1544. * @param {Number} tid 台账id
  1545. * @param {Number} sid 期id
  1546. * @param {Number} uid 审核人id
  1547. */
  1548. async getNoticeContent(pid, tid, sid, uid, opinion = '') {
  1549. const noticeSql =
  1550. 'SELECT * FROM (SELECT ' +
  1551. ' t.`id` As `tid`, t.`name`, s.`order`, pa.`name` As `su_name`, pa.role As `su_role`' +
  1552. ' FROM (SELECT * FROM ?? WHERE `id` = ? ) As t' +
  1553. ' LEFT JOIN ?? As s On s.`id` = ?' +
  1554. ' LEFT JOIN ?? As pa ON pa.`id` = ?' +
  1555. ' WHERE t.`project_id` = ? ) as new_t GROUP BY new_t.`tid`';
  1556. const noticeSqlParam = [this.ctx.service.tender.tableName, tid, this.ctx.service.stage.tableName, sid, this.ctx.service.projectAccount.tableName, uid, pid];
  1557. const content = await this.db.query(noticeSql, noticeSqlParam);
  1558. if (content.length) {
  1559. content[0].opinion = opinion;
  1560. }
  1561. return content.length ? JSON.stringify(content[0]) : '';
  1562. }
  1563. /**
  1564. * 获取审核人流程列表
  1565. *
  1566. * @param auditorId
  1567. * @return {Promise<*>}
  1568. */
  1569. async getAuditGroupByList(stageId, times, transaction = false) {
  1570. // const sql =
  1571. // 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`aid`, la.`order`, la.`status`' +
  1572. // ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  1573. // ' WHERE la.`sid` = ? and la.`times` = ? and la.`is_old` = 0 GROUP BY la.`aid` ORDER BY la.`order`';
  1574. // const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, times];
  1575. const sql =
  1576. 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`order`, la.`status`, la.audit_type, la.audit_order' +
  1577. ' FROM (SELECT `aid`, max(`order`) `order` FROM ?? WHERE `sid` = ? and `times` = ? and `is_old` = ? GROUP BY aid) sa' +
  1578. ' LEFT JOIN ?? la ON sa.`aid` = la.`aid` AND sa.`order` = la.`order`' +
  1579. ' Left JOIN ?? AS pa On la.`aid` = pa.`id` WHERE la.`sid` = ? and la.`times` = ? and la.`is_old` = ? order BY la.`order`';
  1580. const sqlParam = [this.tableName, stageId, times, 0, this.tableName, this.ctx.service.projectAccount.tableName, stageId, times, 0];
  1581. return transaction !== false ? await transaction.query(sql, sqlParam) : await this.db.query(sql, sqlParam);
  1582. }
  1583. /**
  1584. * 获取审核人流程列表
  1585. *
  1586. * @param auditorId
  1587. * @return {Promise<*>}
  1588. */
  1589. async getAuditGroupByListWithOwner(stageId, times) {
  1590. const result = await this.getAuditGroupByList(stageId, times);
  1591. const sql =
  1592. 'SELECT pa.`id` As aid, pa.`name`, pa.`company`, pa.`role`, ? As times, ? As sid, 0 As `order`' +
  1593. ' FROM ' + this.ctx.service.stage.tableName + ' As s' +
  1594. ' LEFT JOIN ' + this.ctx.service.projectAccount.tableName + ' As pa' +
  1595. ' ON s.user_id = pa.id' +
  1596. ' WHERE s.id = ?';
  1597. const sqlParam = [times, stageId, stageId];
  1598. const user = await this.db.queryOne(sql, sqlParam);
  1599. result.unshift(user);
  1600. return result;
  1601. }
  1602. /**
  1603. * 复制上一期的审批人列表给最新一期
  1604. *
  1605. * @param transaction - 新增一期的事务
  1606. * @param {Object} preStage - 上一期
  1607. * @param {Object} newStage - 最新一期
  1608. * @return {Promise<*>}
  1609. */
  1610. async copyPreStageAuditors(transaction, preStage, newStage) {
  1611. const auditors = await this.getAuditGroupByList(preStage.id, preStage.times);
  1612. const newAuditors = [];
  1613. for (const a of auditors) {
  1614. const na = {
  1615. tid: preStage.tid,
  1616. sid: newStage.id,
  1617. aid: a.aid,
  1618. times: newStage.times,
  1619. order: a.audit_order,
  1620. status: auditConst.status.uncheck,
  1621. audit_type: a.audit_type,
  1622. audit_order: a.audit_order,
  1623. };
  1624. newAuditors.push(na);
  1625. }
  1626. if (newAuditors.length > 0) {
  1627. const result = await transaction.insert(this.tableName, newAuditors);
  1628. return (result.effectRows = auditors.length);
  1629. } else {
  1630. return true;
  1631. }
  1632. }
  1633. /**
  1634. * 移除审核人
  1635. *
  1636. * @param {Number} stageId - 期id
  1637. * @param {Number} status - 期状态
  1638. * @param {Number} status - 期次数
  1639. * @return {Promise<boolean>}
  1640. */
  1641. async getAuditorByStatus(stageId, status, times = 1) {
  1642. let auditor = null;
  1643. let sql = '';
  1644. let sqlParam = '';
  1645. switch (status) {
  1646. case auditConst.status.checking:
  1647. case auditConst.status.checked:
  1648. case auditConst.status.checkNoPre:
  1649. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`order` ' +
  1650. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  1651. ' WHERE la.`sid` = ? and la.`status` = ? order by la.`times` desc, la.`order` desc';
  1652. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, status];
  1653. auditor = await this.db.queryOne(sql, sqlParam);
  1654. break;
  1655. case auditConst.status.checkNo:
  1656. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`order` ' +
  1657. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  1658. ' WHERE la.`sid` = ? and la.`status` = ? and la.`times` = ? order by la.`times` desc, la.`order` desc';
  1659. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, auditConst.status.checkNo, parseInt(times) - 1];
  1660. auditor = await this.db.queryOne(sql, sqlParam);
  1661. break;
  1662. case auditConst.status.uncheck:
  1663. default:
  1664. break;
  1665. }
  1666. return auditor;
  1667. }
  1668. async getAuditorsByStatus(stageId, status, times = 1) {
  1669. let auditor = [];
  1670. let sql = '';
  1671. let sqlParam = '';
  1672. let cur;
  1673. switch (status) {
  1674. case auditConst.status.checking:
  1675. case auditConst.status.checked:
  1676. case auditConst.status.checkNoPre:
  1677. cur = await this.db.queryOne(`SELECT * From ${this.tableName} where sid = ? AND times = ? AND status = ? ORDER By times DESC, ` + '`order` DESC', [stageId, times, status]);
  1678. if (!cur) return [];
  1679. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`order`, la.audit_order, la.audit_type ' +
  1680. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  1681. ' WHERE la.`sid` = ? and la.`order` = ? and times = ?';
  1682. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, cur.order, times];
  1683. auditor = await this.db.query(sql, sqlParam);
  1684. break;
  1685. case auditConst.status.checkNo:
  1686. cur = await this.db.queryOne(`SELECT * From ${this.tableName} where sid = ? AND times = ? AND status = ? ORDER By times DESC, ` + '`order` DESC', [stageId, parseInt(times) - 1, status]);
  1687. if (!cur) return [];
  1688. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`order`, la.audit_order, la.audit_type ' +
  1689. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  1690. ' WHERE la.`sid` = ? and la.`order` = ? and la.`times` = ?';
  1691. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, cur.order, parseInt(times) - 1];
  1692. auditor = await this.db.query(sql, sqlParam);
  1693. break;
  1694. case auditConst.status.uncheck:
  1695. default:
  1696. break;
  1697. }
  1698. return auditor;
  1699. }
  1700. /**
  1701. * 取某一期已批准审核信息(报表用)
  1702. *
  1703. * @param {Number} stageId - 期id
  1704. * @param {Number} times - 期次数
  1705. * @return {Promise<boolean>}
  1706. */
  1707. async getStageAudit(stageId, times = 1) {
  1708. const sql = 'SELECT a1.aid, a1.begin_time, a1.end_time, a1.status, a1.opinion ' + 'FROM ?? AS a1 ' + 'WHERE a1.`sid` = ? and a1.`times` = ? ' + 'ORDER BY a1.order';
  1709. const sqlParam = [this.tableName, stageId, times];
  1710. const rst = await this.db.query(sql, sqlParam);
  1711. return rst;
  1712. }
  1713. /**
  1714. * 取待审批期列表(wap用)
  1715. *
  1716. * @param auditorId
  1717. * @return {Promise<*>}
  1718. */
  1719. async getAuditStageByWap(auditorId) {
  1720. const sql =
  1721. 'SELECT sa.`aid`, sa.`times`, sa.`begin_time`, sa.`end_time`, sa.`tid`, sa.`sid`,' +
  1722. // ' s.`order` As `sorder`, s.`status` As `sstatus`, s.`s_time`, s.`contract_tp`, s.`qc_tp`, s.`pre_contract_tp`, s.`pre_qc_tp`, s.`yf_tp`, s.`pre_yf_tp`, ' +
  1723. ' s.*,' +
  1724. ' t.`name`, t.`project_id`, t.`type`, t.`user_id`,' +
  1725. ' ti.`deal_info` ' +
  1726. ' FROM ?? AS sa' +
  1727. ' Left Join ?? AS s On sa.`sid` = s.`id`' +
  1728. ' Left Join ?? As t On sa.`tid` = t.`id`' +
  1729. ' Left Join ?? AS ti ON ti.`tid` = t.`id`' +
  1730. ' WHERE sa.`aid` = ? and sa.`status` = ?';
  1731. const sqlParam = [
  1732. this.tableName,
  1733. this.ctx.service.stage.tableName,
  1734. this.ctx.service.tender.tableName,
  1735. this.ctx.service.tenderInfo.tableName,
  1736. auditorId,
  1737. auditConst.status.checking,
  1738. ];
  1739. return await this.db.query(sql, sqlParam);
  1740. }
  1741. /**
  1742. * 删除 某期 某次 全审批流程
  1743. * 私有,不做判断,不补全最新一轮审批人数据,不计算缓存
  1744. * @param {Number} sid - 标段id
  1745. * @param {Number} times - 第几次审批
  1746. * @param transaction - 删除事务
  1747. * @return {Promise<void>}
  1748. */
  1749. async _timesDelete(sid, times, transaction) {
  1750. // 审批流程
  1751. await transaction.delete(this.tableName, { sid, times });
  1752. await transaction.delete(this.ctx.service.pos.tableName, { add_stage: sid, add_times: times });
  1753. await transaction.delete(this.ctx.service.stageBills.tableName, { sid, times });
  1754. await transaction.delete(this.ctx.service.stagePos.tableName, { sid, times });
  1755. await transaction.delete(this.ctx.service.stageDetail.tableName, { sid, times });
  1756. await transaction.delete(this.ctx.service.stageChange.tableName, { sid, stimes: times });
  1757. await transaction.delete(this.ctx.service.stagePay.tableName, { sid, stimes: times });
  1758. await transaction.delete(this.ctx.service.pay.tableName, { csid: sid, cstimes: times });
  1759. await transaction.delete(this.ctx.service.stageAuditAss.tableName, { sid, times });
  1760. // 其他台账
  1761. await this.ctx.service.stageJgcl.deleteStageTimesData(sid, times, transaction);
  1762. await this.ctx.service.stageOther.deleteStageTimesData(sid, times, transaction);
  1763. await this.ctx.service.stageBonus.deleteStageTimesData(sid, times, transaction);
  1764. await this.ctx.service.stageSafeProd.deleteStageTimesData(sid, times, transaction);
  1765. await this.ctx.service.stageTempLand.deleteStageTimesData(sid, times, transaction);
  1766. }
  1767. /**
  1768. * 删除本次审批流程
  1769. * @param {Number} stageId - 标段id
  1770. * @param {Number} times - 第几次审批
  1771. * @return {Promise<void>}
  1772. */
  1773. async timesDelete() {
  1774. const transaction = await this.db.beginTransaction();
  1775. try {
  1776. // 删除最新一次数据
  1777. await this._timesDelete(this.ctx.stage.id, this.ctx.stage.times, transaction);
  1778. // 审批退回,未重新上报时,需删除最新两次数据
  1779. const isCheckNo = this.ctx.stage.status === auditConst.status.checkNo;
  1780. const nowTimes = isCheckNo ? this.ctx.stage.times - 1 : this.ctx.stage.times;
  1781. if (isCheckNo) {
  1782. await this._timesDelete(this.ctx.stage.id, nowTimes, transaction);
  1783. }
  1784. // 添加上一次审批人
  1785. const sql = 'SELECT `tid`, `sid`, `aid`, `order`, `audit_type`, `audit_order` FROM ?? WHERE `sid` = ? and `times` = ? GROUP BY `aid` ORDER BY `id` ASC';
  1786. const sqlParam = [this.tableName, this.ctx.stage.id, nowTimes - 1];
  1787. const auditors = await this.db.query(sql, sqlParam);
  1788. let order = 1;
  1789. for (const a of auditors) {
  1790. a.times = nowTimes;
  1791. a.order = order;
  1792. a.status = auditConst.status.uncheck;
  1793. order++;
  1794. }
  1795. const ledgerTp = await this._updateTender(transaction);
  1796. // 拷贝新一次审核流程列表
  1797. await transaction.insert(this.tableName, auditors);
  1798. // 计算缓存
  1799. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  1800. // 计算并合同支付最终数据
  1801. const lastAudit = await this.getDataByCondition({
  1802. sid: this.ctx.stage.id,
  1803. times: nowTimes - 1,
  1804. status: auditConst.status.checkNo,
  1805. });
  1806. if (!lastAudit) throw '审批数据错误';
  1807. await this.ctx.service.stagePay.copyStagePays4DeleteTimes(this.ctx.stage, nowTimes, 0, lastAudit.times, lastAudit.order, transaction);
  1808. const stagePay = await this.ctx.service.stagePay.getAuditorStageData(this.ctx.stage.id, lastAudit.times, lastAudit.order);
  1809. const yfPay = stagePay.find(function(x) {
  1810. return x.ptype === payConst.payType.yf;
  1811. });
  1812. const sfPay = stagePay.find(function(x) {
  1813. return x.ptype === payConst.payType.sf;
  1814. });
  1815. // 同步 期信息
  1816. const time = new Date();
  1817. if (this.ctx.stage.isCheckFirst) await this.ctx.service.tenderCache.updateStageCache4DelTimes(transaction, this.ctx.stage, nowTimes);
  1818. await transaction.update(this.ctx.service.stage.tableName, {
  1819. id: this.ctx.stage.id,
  1820. status: auditConst.status.checkNo,
  1821. contract_tp: tpData.contract_tp,
  1822. qc_tp: tpData.qc_tp,
  1823. positive_qc_tp: tpData.positive_qc_tp,
  1824. negative_qc_tp: tpData.negative_qc_tp,
  1825. times: nowTimes,
  1826. yf_tp: yfPay ? yfPay.tp : null,
  1827. sf_tp: sfPay ? sfPay.tp : null,
  1828. tp_history: JSON.stringify(this.ctx.stage.tp_history.filter(x => { return x.times <= nowTimes; })),
  1829. cache_time_l: time,
  1830. cache_time_r: time,
  1831. });
  1832. await transaction.commit();
  1833. // 通知发送 - 第三方更新
  1834. if (this.ctx.session.sessionProject.custom && syncApiConst.notice_type.indexOf(this.ctx.session.sessionProject.customType) !== -1) {
  1835. const base_data = {
  1836. tid: this.ctx.tender.id,
  1837. sid: this.ctx.stage.id,
  1838. op: 'update',
  1839. };
  1840. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  1841. base_data.op = 'update';
  1842. base_data.sid = -1;
  1843. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  1844. }
  1845. } catch (err) {
  1846. await transaction.rollback();
  1847. throw err;
  1848. }
  1849. }
  1850. // 固定审批流-更新
  1851. async updateNewAuditList(stage, newList) {
  1852. const transaction = await this.db.beginTransaction();
  1853. try {
  1854. // 先删除旧的审批流,再添加新的
  1855. await transaction.delete(this.tableName, { sid: stage.id, times: stage.times });
  1856. await transaction.delete(this.ctx.service.stageAuditAss.tableName, { sid: stage.id, times: stage.times });
  1857. const newAuditors = [];
  1858. for (const auditor of newList) {
  1859. newAuditors.push({
  1860. tid: stage.tid, sid: stage.id, aid: auditor.audit_id,
  1861. times: stage.times, order: auditor.audit_order, status: auditConst.status.uncheck,
  1862. audit_type: auditor.audit_type, audit_order: auditor.audit_order,
  1863. });
  1864. }
  1865. if(newAuditors.length > 0) await transaction.insert(this.tableName, newAuditors);
  1866. await transaction.commit();
  1867. } catch (err) {
  1868. await transaction.rollback();
  1869. throw err;
  1870. }
  1871. }
  1872. // 固定终审-更新
  1873. async updateLastAudit(stage, auditList, lastId) {
  1874. const transaction = await this.db.beginTransaction();
  1875. try {
  1876. // 先判断auditList里的aid是否与lastId相同,相同则删除并重新更新order
  1877. const existAudit = auditList.find(x => { return x.aid === lastId });
  1878. let order = auditList.length > 0 ? auditList.reduce((rst, a) => { return Math.max(rst, a.order)}, 0) + 1 : 1; // 最大值 + 1
  1879. if (existAudit) {
  1880. await transaction.delete(this.tableName, { sid: stage.id, times: stage.times, aid: lastId });
  1881. const sameOrder = auditList.filter(x => { return x.order === existAudit.order });
  1882. if (sameOrder.length === 1) {
  1883. const updateData = [];
  1884. auditList.forEach(x => {
  1885. if (x.order <= existAudit.order) return;
  1886. updateData.push({id: x.id, order: x.order - 1, audit_order: x.audit_order - 1});
  1887. });
  1888. if (updateData.length > 0) {
  1889. await transaction.updateRows(updateData);
  1890. }
  1891. order = order - 1;
  1892. }
  1893. }
  1894. // 添加终审
  1895. const newAuditor = {
  1896. tid: stage.tid, sid: stage.id, aid: lastId,
  1897. times: stage.times, order, status: auditConst.status.uncheck,
  1898. audit_type: auditType.key.common, audit_order: order,
  1899. };
  1900. await transaction.insert(this.tableName, newAuditor);
  1901. await transaction.commit();
  1902. } catch (err) {
  1903. await transaction.rollback();
  1904. throw err;
  1905. }
  1906. }
  1907. async getFinalAuditGroup(stageId, times) {
  1908. const sql =
  1909. 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, pa.`sign_path`, la.`times`, la.`sid`, Max(la.`order`) as max_order ' +
  1910. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id`' +
  1911. ' WHERE la.`sid` = ? and la.`times` = ? GROUP BY la.`aid` ORDER BY la.`order`';
  1912. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, times];
  1913. const result = await this.db.query(sql, sqlParam);
  1914. for (const r of result) {
  1915. const auditor = await this.getDataByCondition({sid: stageId, times: r.times, order: r.max_order});
  1916. r.status = auditor.status;
  1917. r.opinion = auditor.opinion;
  1918. r.begin_time = auditor.begin_time;
  1919. r.end_time = auditor.end_time;
  1920. }
  1921. return result;
  1922. }
  1923. async getNumByMonth(tid, startMonth, endMonth) {
  1924. // const sql = 'SELECT COUNT(*) as num FROM ?? WHERE id in (SELECT MAX(id) FROM ?? WHERE tid = ? GROUP BY sid) AND status = ? AND end_time between ? and ?';
  1925. const sql = 'SELECT COUNT(*) as num FROM ?? t1 JOIN (SELECT MAX(id) as max_id FROM ?? WHERE tid = ? GROUP BY sid) t2 ON t1.id = t2.max_id WHERE t1.status = ? AND t1.end_time between ? and ?';
  1926. const sqlParam = [this.tableName, this.tableName, tid, auditConst.status.checked, startMonth, endMonth];
  1927. const result = await this.db.queryOne(sql, sqlParam);
  1928. return result ? result.num : 0;
  1929. }
  1930. /**
  1931. * 删除本次审批流程
  1932. * @param {Number} stageId - 标段id
  1933. * @param {Number} times - 第几次审批
  1934. * @param {Object} data - 更改参数
  1935. * @return {Promise<void>}
  1936. */
  1937. async saveAudit(stageId, times, data) {
  1938. const transaction = await this.db.beginTransaction();
  1939. try {
  1940. const auditors = await this.getAuditGroupByList(stageId, times);
  1941. const now_audit = this._.find(auditors, { aid: data.old_aid });
  1942. if (data.operate !== 'del') {
  1943. const exist = await this.getDataByCondition({ sid: stageId, times, aid: data.new_aid });
  1944. if (exist) throw '该审核人已存在,请勿重复添加';
  1945. }
  1946. if (data.operate === 'add') {
  1947. if (now_audit.status !== auditConst.status.uncheck && now_audit.status !== auditConst.status.checking) {
  1948. throw '当前人下无法操作新增';
  1949. }
  1950. const newAudit = {
  1951. tid: this.ctx.tender.id,
  1952. sid: stageId,
  1953. aid: data.new_aid,
  1954. order: now_audit.order + 1,
  1955. audit_order: now_audit.audit_order + 1,
  1956. audit_type: auditType.key.common,
  1957. times: times,
  1958. status: 1
  1959. };
  1960. // order+1
  1961. await this._syncOrderByDelete(transaction, stageId, now_audit.order+1, times, '+');
  1962. await transaction.insert(this.tableName, newAudit);
  1963. // 更新审批流程页数据,如果存在
  1964. } else if (data.operate === 'add-sibling') {
  1965. if (now_audit.status !== auditConst.status.uncheck && now_audit.status !== auditConst.status.checking) {
  1966. throw '当前人下无法操作新增';
  1967. }
  1968. const newAudit = {
  1969. tid: this.ctx.tender.id,
  1970. sid: stageId,
  1971. aid: data.new_aid,
  1972. order: now_audit.order,
  1973. audit_order: now_audit.audit_order,
  1974. audit_type: now_audit.audit_type,
  1975. times: times,
  1976. status: 1
  1977. };
  1978. await transaction.insert(this.tableName, newAudit);
  1979. } else if (data.operate === 'del') {
  1980. if (now_audit.status !== auditConst.status.uncheck) {
  1981. throw '当前人无法操作删除';
  1982. }
  1983. const flowAuditors = auditors.filter(x => { return x.order === now_audit.order; });
  1984. await transaction.delete(this.tableName, { sid: stageId, times, aid: now_audit.aid, order: now_audit.order });
  1985. if (flowAuditors.length === 1) await this._syncOrderByDelete(transaction, stageId, now_audit.order, times);
  1986. // 旧的更新为is_old为1
  1987. await transaction.update(this.tableName, { is_old: 1 }, {
  1988. where: {
  1989. sid: stageId,
  1990. times,
  1991. aid: data.old_aid,
  1992. }
  1993. });
  1994. } else if (data.operate === 'change') {
  1995. const nowAudit = await this.getDataByCondition({ sid: stageId, times, aid: now_audit.aid, order: now_audit.order });
  1996. if (now_audit.status !== auditConst.status.uncheck || !nowAudit) {
  1997. throw '当前人无法操作替换';
  1998. }
  1999. nowAudit.aid = data.new_aid;
  2000. await transaction.update(this.tableName, nowAudit);
  2001. // 旧的更新为is_old为1
  2002. await transaction.update(this.tableName, { is_old: 1 }, {
  2003. where: {
  2004. sid: stageId,
  2005. times,
  2006. aid: data.old_aid,
  2007. }
  2008. });
  2009. }
  2010. if (this.ctx.tender.info.shenpi.stage === shenpiConst.sp_status.gdspl) {
  2011. const newAuditors = await transaction.select(this.tableName, { where: { sid: stageId, times } });
  2012. const newAuditorGroup = this.ctx.helper.groupAuditors(newAuditors);
  2013. const uniqNewAuditorGroup = this.ctx.helper.groupAuditorsUniq(newAuditorGroup);
  2014. await this.ctx.service.shenpiAudit.updateAuditListWithAuditType(transaction, this.ctx.tender.id, this.ctx.tender.info.shenpi.stage, shenpiConst.sp_type.stage, uniqNewAuditorGroup);
  2015. } else if (this.ctx.tender.info.shenpi.stage === shenpiConst.sp_status.gdzs) {
  2016. const newAuditors = await this.getAuditGroupByList(stageId, times, transaction);
  2017. await this.ctx.service.shenpiAudit.updateAuditList(transaction, this.ctx.tender.id, this.ctx.tender.info.shenpi.stage, shenpiConst.sp_type.stage, this._.map(newAuditors, 'aid'));
  2018. }
  2019. // 更新到审批流程方法
  2020. await transaction.commit();
  2021. } catch (err) {
  2022. await transaction.rollback();
  2023. throw err;
  2024. }
  2025. }
  2026. async getAuditorGroup(stageId, times) {
  2027. const auditors = await this.ctx.service.stageAudit.getAuditors(stageId, times); // 全部参与的审批人
  2028. return this.ctx.helper.groupAuditors(auditors);
  2029. }
  2030. async getUserGroup(stageId, times) {
  2031. const group = await this.getAuditorGroup(stageId, times);
  2032. const sql =
  2033. 'SELECT pa.`id` As aid, pa.`name`, pa.`company`, pa.`role`, ? As times, ? As sid, 0 As `order`, 1 As audit_type, 0 As audit_order' +
  2034. ' FROM ' + this.ctx.service.stage.tableName + ' As s' +
  2035. ' LEFT JOIN ' + this.ctx.service.projectAccount.tableName + ' As pa' +
  2036. ' ON s.user_id = pa.id' +
  2037. ' WHERE s.id = ?';
  2038. const sqlParam = [times, stageId, stageId];
  2039. const user = await this.db.queryOne(sql, sqlParam);
  2040. group.unshift([ user ]);
  2041. return group;
  2042. }
  2043. async getUniqUserGroup(stageId, times) {
  2044. const group = await this.getAuditorGroup(stageId, times);
  2045. const sql =
  2046. 'SELECT pa.`id` As aid, pa.`name`, pa.`company`, pa.`role`, ? As times, ? As sid, 0 As `order`, 1 As audit_type, 0 As audit_order' +
  2047. ' FROM ' + this.ctx.service.stage.tableName + ' As s' +
  2048. ' LEFT JOIN ' + this.ctx.service.projectAccount.tableName + ' As pa' +
  2049. ' ON s.user_id = pa.id' +
  2050. ' WHERE s.id = ?';
  2051. const sqlParam = [times, stageId, stageId];
  2052. const user = await this.db.queryOne(sql, sqlParam);
  2053. user.audit_order = 0;
  2054. group.unshift([ user ]);
  2055. return this.ctx.helper.groupAuditorsUniq(group);
  2056. }
  2057. async getAuditorHistory(stageId, times, reverse = false) {
  2058. const history = [];
  2059. if (times >= 1) {
  2060. for (let i = 1; i <= times; i++) {
  2061. const auditors = await this.ctx.service.stageAudit.getAuditors(stageId, i);
  2062. const group = this.ctx.helper.groupAuditors(auditors);
  2063. const historyGroup = [];
  2064. const max_order = group.length > 0 && group[group.length - 1].length > 0 ? group[group.length - 1][0].audit_order : -1;
  2065. for (const g of group) {
  2066. const his = {
  2067. beginYear: '', beginDate: '', beginTime: '', endYear: '', endDate: '', endTime: '', begin_time: null, end_time: null,
  2068. audit_type: g[0].audit_type, audit_order: g[0].audit_order,
  2069. auditors: g
  2070. };
  2071. if (his.audit_type === auditType.key.common) {
  2072. his.name = g[0].name;
  2073. } else {
  2074. his.name = this.ctx.helper.transFormToChinese(his.audit_order) + '审';
  2075. }
  2076. his.is_final = his.audit_order === max_order;
  2077. if (g[0].begin_time) {
  2078. his.begin_time = g[0].begin_time;
  2079. const beginTime = this.ctx.moment(g[0].begin_time);
  2080. his.beginYear = beginTime.format('YYYY');
  2081. his.beginDate = beginTime.format('MM-DD');
  2082. his.beginTime = beginTime.format('HH:mm:ss');
  2083. }
  2084. let end_time;
  2085. g.forEach(x => {
  2086. if (x.status === auditConst.status.checkSkip) return;
  2087. if (!his.status || x.status === auditConst.status.checking) his.status = x.status;
  2088. if (x.end_time && (!end_time || x.end_time > end_time)) {
  2089. end_time = x.end_time;
  2090. if (his.status !== auditConst.status.checking) his.status = x.status;
  2091. }
  2092. });
  2093. if (end_time) {
  2094. his.end_time = end_time;
  2095. const endTime = this.ctx.moment(end_time);
  2096. his.endYear = endTime.format('YYYY');
  2097. his.endDate = endTime.format('MM-DD');
  2098. his.endTime = endTime.format('HH:mm:ss');
  2099. }
  2100. historyGroup.push(his);
  2101. }
  2102. if (reverse) {
  2103. history.push(historyGroup.reverse());
  2104. } else {
  2105. history.push(historyGroup);
  2106. }
  2107. }
  2108. }
  2109. return history;
  2110. }
  2111. async getUniqAuditor(stageId, times) {
  2112. const auditors = await this.ctx.service.stageAudit.getAuditors(stageId, times); // 全部参与的审批人
  2113. const result = [];
  2114. auditors.forEach(x => {
  2115. if (result.findIndex(r => { return x.aid === r.aid && x.audit_order === x.audit_order; }) < 0) {
  2116. result.push(x);
  2117. }
  2118. });
  2119. return result;
  2120. }
  2121. }
  2122. return StageAudit;
  2123. };