stage_audit.js 110 KB

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