stage_audit.js 114 KB

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