stage_audit.js 110 KB

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