stage_audit.js 111 KB

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