stage_audit.js 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448
  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 smsTypeConst = require('../const/sms_type');
  11. const SMS = require('../lib/sms');
  12. const SmsAliConst = require('../const/sms_alitemplate');
  13. const wxConst = require('../const/wechat_template');
  14. const shenpiConst = require('../const/shenpi');
  15. const payConst = require('../const/deal_pay');
  16. const pushType = require('../const/audit').pushType;
  17. const syncApiConst = require('../const/sync_api');
  18. const measureType = require('../const/tender').measureType;
  19. module.exports = app => {
  20. class StageAudit extends app.BaseService {
  21. /**
  22. * 构造函数
  23. *
  24. * @param {Object} ctx - egg全局变量
  25. * @return {void}
  26. */
  27. constructor(ctx) {
  28. super(ctx);
  29. this.tableName = 'stage_audit';
  30. }
  31. /**
  32. * 获取 审核人信息
  33. *
  34. * @param {Number} stageId - 期id
  35. * @param {Number} auditorId - 审核人id
  36. * @param {Number} times - 第几次审批
  37. * @return {Promise<*>}
  38. */
  39. async getAuditor(stageId, auditorId, times = 1) {
  40. const sql =
  41. '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` ' +
  42. 'FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id`' +
  43. 'WHERE la.`sid` = ? and la.`aid` = ? and la.`times` = ?';
  44. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, auditorId, times];
  45. return await this.db.queryOne(sql, sqlParam);
  46. }
  47. async getAuditorByOrder(stageId, order, times) {
  48. const sql =
  49. 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`,' +
  50. ' la.`times`, la.`order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time` ' +
  51. ' FROM ' + this.tableName + ' AS la' +
  52. ' Left Join ' + this.ctx.service.projectAccount.tableName + ' AS pa ON la.`aid` = pa.`id`' +
  53. ' WHERE la.`sid` = ? and la.`order` = ? and la.`times` = ?' +
  54. ' ORDER BY `order` DESC';
  55. const sqlParam = [stageId, order, times ? times: 1];
  56. return await this.db.queryOne(sql, sqlParam);
  57. }
  58. async getLastestAuditor(stageId, times, status) {
  59. const sql =
  60. 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`,' +
  61. ' la.`times`, la.`order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time` ' +
  62. ' FROM ' + this.tableName + ' AS la' +
  63. ' Left Join ' + this.ctx.service.projectAccount.tableName + ' AS pa ON la.`aid` = pa.`id`' +
  64. ' WHERE la.`sid` = ? and la.`status` = ? and la.`times` = ?' +
  65. ' ORDER BY `order` DESC';
  66. const sqlParam = [stageId, status, times ? times: 1];
  67. return await this.db.queryOne(sql, sqlParam);
  68. }
  69. /**
  70. * 获取 审核列表信息
  71. *
  72. * @param {Number} stageId - 期id
  73. * @param {Number} times - 第几次审批
  74. * @param {Number} order_sort - 列表排序方式
  75. * @return {Promise<*>}
  76. */
  77. async getAuditors(stageId, times = 1, order_sort = 'asc') {
  78. const sql =
  79. 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, pa.sign_path, la.`times`, la.`order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time`, g.`sort` ' +
  80. 'FROM ?? AS la, ?? AS pa, (SELECT `aid`,(@i:=@i+1) as `sort` FROM ??, (select @i:=0) as it WHERE `sid` = ? AND `times` = ? GROUP BY `aid`) as g ' +
  81. 'WHERE la.`sid` = ? and la.`times` = ? and la.`aid` = pa.`id` and g.`aid` = la.`aid` order by la.`order` ' +
  82. order_sort;
  83. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, this.tableName, stageId, times, stageId, times];
  84. const result = await this.db.query(sql, sqlParam);
  85. const sql2 = 'SELECT COUNT(a.`aid`) as num FROM (SELECT `aid` FROM ?? WHERE `sid` = ? AND `times` = ? GROUP BY `aid`) as a';
  86. const sqlParam2 = [this.tableName, stageId, times];
  87. const count = await this.db.queryOne(sql2, sqlParam2);
  88. for (const i in result) {
  89. result[i].max_sort = count.num;
  90. }
  91. return result;
  92. }
  93. async getAllAuditors(tenderId) {
  94. const sql =
  95. 'SELECT sa.aid, sa.tid FROM ' + this.tableName + ' sa' +
  96. ' LEFT JOIN ' + this.ctx.service.tender.tableName + ' t On sa.tid = t.id' +
  97. ' WHERE t.id = ?' +
  98. ' GROUP BY sa.aid';
  99. const sqlParam = [tenderId];
  100. return this.db.query(sql, sqlParam);
  101. }
  102. /**
  103. * 获取标段审核人最后一位的名称
  104. *
  105. * @param {Number} tenderId - 标段id
  106. * @param {Number} auditorId - 审核人id
  107. * @param {Number} times - 第几次审批
  108. * @return {Promise<*>}
  109. */
  110. async getStatusName(stageId) {
  111. const sql = 'SELECT pa.`name` FROM ?? AS sa Left Join ?? AS pa On sa.`aid` = pa.`id`' +
  112. ' WHERE sa.`sid` = ? and sa.`status` != ? ORDER BY sa.`times` DESC, sa.`order` DESC';
  113. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, auditConst.status.uncheck];
  114. return await this.db.queryOne(sql, sqlParam);
  115. }
  116. /**
  117. * 获取 当前审核人
  118. *
  119. * @param {Number} stageId - 期id
  120. * @param {Number} times - 第几次审批
  121. * @return {Promise<*>}
  122. */
  123. async getCurAuditor(stageId, times = 1) {
  124. const sql =
  125. '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` ' +
  126. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id`' +
  127. ' WHERE la.`sid` = ? and la.`status` = ? and la.`times` = ?';
  128. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, auditConst.status.checking, times];
  129. return await this.db.queryOne(sql, sqlParam);
  130. }
  131. /**
  132. * 获取 最新审核顺序
  133. *
  134. * @param {Number} stageId - 期id
  135. * @param {Number} times - 第几次审批
  136. * @return {Promise<number>}
  137. */
  138. async getNewOrder(stageId, times = 1) {
  139. const sql = 'SELECT Max(??) As max_order FROM ?? Where `sid` = ? and `times` = ?';
  140. const sqlParam = ['order', this.tableName, stageId, times];
  141. const result = await this.db.queryOne(sql, sqlParam);
  142. return result && result.max_order ? result.max_order + 1 : 1;
  143. }
  144. /**
  145. * 新增审核人
  146. *
  147. * @param {Number} stageId - 期id
  148. * @param {Number} auditorId - 审核人id
  149. * @param {Number} times - 第几次审批
  150. * @return {Promise<number>}
  151. */
  152. async addAuditor(stageId, auditorId, times = 1, is_gdzs = 0) {
  153. const transaction = await this.db.beginTransaction();
  154. try {
  155. let newOrder = await this.getNewOrder(stageId, times);
  156. // 判断是否存在固定终审,存在则newOrder - 1并使终审order+1
  157. newOrder = is_gdzs === 1 ? newOrder - 1 : newOrder;
  158. if (is_gdzs) await this._syncOrderByDelete(transaction, stageId, newOrder, times, '+');
  159. const data = {
  160. tid: this.ctx.tender.id,
  161. sid: stageId,
  162. aid: auditorId,
  163. times,
  164. order: newOrder,
  165. status: auditConst.status.uncheck,
  166. };
  167. const result = await transaction.insert(this.tableName, data);
  168. await transaction.commit();
  169. return result.effectRows = 1;
  170. } catch (err) {
  171. await transaction.rollback();
  172. throw err;
  173. }
  174. return false;
  175. }
  176. /**
  177. * 移除审核人时,同步其后审核人order
  178. * @param transaction - 事务
  179. * @param {Number} stageId - 标段id
  180. * @param {Number} auditorId - 审核人id
  181. * @param {Number} times - 第几次审批
  182. * @return {Promise<*>}
  183. * @private
  184. */
  185. async _syncOrderByDelete(transaction, stageId, order, times, selfOperate = '-') {
  186. this.initSqlBuilder();
  187. this.sqlBuilder.setAndWhere('sid', {
  188. value: stageId,
  189. operate: '=',
  190. });
  191. this.sqlBuilder.setAndWhere('order', {
  192. value: order,
  193. operate: '>=',
  194. });
  195. this.sqlBuilder.setAndWhere('times', {
  196. value: times,
  197. operate: '=',
  198. });
  199. this.sqlBuilder.setUpdateData('order', {
  200. value: 1,
  201. selfOperate: selfOperate,
  202. });
  203. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  204. const data = await transaction.query(sql, sqlParam);
  205. return data;
  206. }
  207. /**
  208. * 移除审核人
  209. *
  210. * @param {Number} stageId - 期id
  211. * @param {Number} auditorId - 审核人id
  212. * @param {Number} times - 第几次审批
  213. * @return {Promise<boolean>}
  214. */
  215. async deleteAuditor(stageId, auditorId, times = 1) {
  216. const transaction = await this.db.beginTransaction();
  217. try {
  218. const condition = { sid: stageId, aid: auditorId, times };
  219. const auditor = await this.getDataByCondition(condition);
  220. if (!auditor) {
  221. throw '该审核人不存在';
  222. }
  223. await this._syncOrderByDelete(transaction, stageId, auditor.order, times);
  224. await transaction.delete(this.tableName, condition);
  225. await transaction.commit();
  226. } catch (err) {
  227. await transaction.rollback();
  228. throw err;
  229. }
  230. return true;
  231. }
  232. async _updateTender(transaction) {
  233. if (!this.ctx.tender) return;
  234. if (this.ctx.tender.data.measure_type !== measureType.gcl.value) return;
  235. const sum = await this.ctx.service.ledger.addUp({ tender_id: this.ctx.tender.id/* , is_leaf: true*/ });
  236. await transaction.update(this.ctx.service.tender.tableName, {
  237. id: this.ctx.tender.id, total_price: sum.total_price, deal_tp: sum.deal_tp
  238. });
  239. }
  240. /**
  241. * 开始审批
  242. *
  243. * @param {Number} stageId - 期id
  244. * @param {Number} times - 第几次审批
  245. * @return {Promise<boolean>}
  246. */
  247. async start(stageId, times = 1) {
  248. const audit = await this.getDataByCondition({ sid: stageId, times, order: 1 });
  249. if (!audit) {
  250. if(this.ctx.tender.info.shenpi.stage === shenpiConst.sp_status.gdspl) {
  251. throw '请联系管理员添加审批人';
  252. } else {
  253. throw '请先选择审批人,再上报数据';
  254. }
  255. }
  256. const transaction = await this.db.beginTransaction();
  257. try {
  258. await this._updateTender(transaction);
  259. await transaction.update(this.tableName, {
  260. id: audit.id,
  261. status: auditConst.status.checking,
  262. begin_time: new Date(),
  263. });
  264. // 计算原报最终数据
  265. const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  266. // 复制一份下一审核人数据
  267. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, 1, transaction);
  268. await this.ctx.service.stageJgcl.updateHistory(this.ctx.stage, transaction);
  269. await this.ctx.service.stageBonus.updateHistory(this.ctx.stage, transaction);
  270. await this.ctx.service.stageOther.updateHistory(this.ctx.stage, transaction);
  271. await this.ctx.service.stageSafeProd.updateHistory(this.ctx.stage, transaction);
  272. await this.ctx.service.stageTempLand.updateHistory(this.ctx.stage, transaction);
  273. // 更新期数据
  274. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  275. this.ctx.stage.tp_history.push({
  276. times: this.ctx.stage.curTimes,
  277. order: 0,
  278. contract_tp: tpData.contract_tp,
  279. qc_tp: tpData.qc_tp,
  280. yf_tp: yfPay.tp,
  281. sf_tp: sfPay.tp,
  282. });
  283. await transaction.update(this.ctx.service.stage.tableName, {
  284. id: stageId,
  285. status: auditConst.status.checking,
  286. contract_tp: tpData.contract_tp,
  287. qc_tp: tpData.qc_tp,
  288. yf_tp: yfPay.tp,
  289. sf_tp: sfPay.tp,
  290. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  291. cache_time_r: this.ctx.stage.cache_time_l,
  292. });
  293. // 添加短信通知-需要审批提醒功能
  294. // const smsUser = await this.ctx.service.projectAccount.getDataById(audit.aid);
  295. // if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
  296. // const smsType = JSON.parse(smsUser.sms_type);
  297. // if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  298. // const tenderInfo = await this.ctx.service.tender.getDataById(audit.tid);
  299. // const stageInfo = await this.ctx.service.stage.getDataById(audit.sid);
  300. // const sms = new SMS(this.ctx);
  301. // const tenderName = await sms.contentChange(tenderInfo.name);
  302. // const projectName = await sms.contentChange(this.ctx.tender.info.deal_info.buildName);
  303. // const ptmsg = projectName !== '' ? '项目「' + projectName + '」标段「' + tenderName + '」' : tenderName;
  304. // const result = await this.ctx.helper.urlToShort('http://' + this.ctx.request.header.host + '/wap/tender/' + this.ctx.tender.id + '/stage/' + stageInfo.order);
  305. // const content = '【纵横计量支付】' + ptmsg + '第' + stageInfo.order + '期,需要您审批。' + result;
  306. // sms.send(smsUser.auth_mobile, content);
  307. // }
  308. // }
  309. const stageInfo = await this.ctx.service.stage.getDataById(audit.sid);
  310. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/stage/' + stageInfo.order);
  311. await this.ctx.helper.sendAliSms(audit.aid, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), SmsAliConst.template.stage_check, {
  312. qi: stageInfo.order,
  313. code: shenpiUrl,
  314. });
  315. // 微信模板通知
  316. const wechatData = {
  317. wap_url: shenpiUrl,
  318. qi: stageInfo.order,
  319. status: wxConst.status.check,
  320. tips: wxConst.tips.check,
  321. code: this.ctx.session.sessionProject.code,
  322. };
  323. await this.ctx.helper.sendWechat(audit.aid, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), wxConst.template.stage, wechatData);
  324. // todo 更新标段tender状态 ?
  325. await transaction.commit();
  326. // 通知发送 - 第三方更新
  327. if (this.ctx.session.sessionProject.custom && syncApiConst.notice_type.indexOf(this.ctx.session.sessionProject.customType) !== -1) {
  328. const base_data = {
  329. tid: this.ctx.tender.id,
  330. sid: stageId,
  331. op: 'update',
  332. };
  333. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  334. base_data.op = 'update';
  335. base_data.sid = -1;
  336. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  337. }
  338. } catch (err) {
  339. await transaction.rollback();
  340. throw err;
  341. }
  342. return true;
  343. }
  344. async _checked(pid, stageId, checkData, times) {
  345. const time = new Date();
  346. // 整理当前流程审核人状态更新
  347. const audit = await this.getDataByCondition({ sid: stageId, times, status: auditConst.status.checking });
  348. if (!audit) {
  349. throw '审核数据错误';
  350. }
  351. const nextAudit = await this.getDataByCondition({ sid: stageId, times, order: audit.order + 1 });
  352. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  353. const transaction = await this.db.beginTransaction();
  354. try {
  355. await this._updateTender(transaction);
  356. // 添加推送
  357. const noticeContent = await this.getNoticeContent(pid, audit.tid, stageId, audit.aid, checkData.opinion);
  358. const auditors = await this.getAuditGroupByListWithOwner(stageId, times);
  359. const records = [];
  360. auditors.forEach(audit => {
  361. records.push({
  362. pid,
  363. type: pushType.stage,
  364. uid: audit.aid,
  365. status: auditConst.status.checked,
  366. content: noticeContent,
  367. });
  368. });
  369. await transaction.insert('zh_notice', records);
  370. await transaction.update(this.tableName, {
  371. id: audit.id,
  372. status: checkData.checkType,
  373. opinion: checkData.opinion,
  374. end_time: time,
  375. });
  376. // 计算并合同支付最终数据
  377. const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  378. this.ctx.stage.tp_history.push({
  379. times,
  380. order: audit.order,
  381. contract_tp: tpData.contract_tp,
  382. qc_tp: tpData.qc_tp,
  383. yf_tp: yfPay.tp,
  384. sf_tp: sfPay.tp,
  385. });
  386. // 无下一审核人表示,审核结束
  387. if (nextAudit) {
  388. // 复制一份下一审核人数据
  389. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, nextAudit.order, transaction);
  390. await this.ctx.service.stageJgcl.updateHistory(this.ctx.stage, transaction);
  391. await this.ctx.service.stageBonus.updateHistory(this.ctx.stage, transaction);
  392. await this.ctx.service.stageOther.updateHistory(this.ctx.stage, transaction);
  393. await this.ctx.service.stageSafeProd.updateHistory(this.ctx.stage, transaction);
  394. await this.ctx.service.stageTempLand.updateHistory(this.ctx.stage, transaction);
  395. // 流程至下一审批人
  396. await transaction.update(this.tableName, {
  397. id: nextAudit.id,
  398. status: auditConst.status.checking,
  399. begin_time: time,
  400. });
  401. // 同步 期信息
  402. await transaction.update(this.ctx.service.stage.tableName, {
  403. id: stageId,
  404. status: auditConst.status.checking,
  405. contract_tp: tpData.contract_tp,
  406. qc_tp: tpData.qc_tp,
  407. yf_tp: yfPay.tp,
  408. sf_tp: sfPay.tp,
  409. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  410. cache_time_r: this.ctx.stage.cache_time_l,
  411. });
  412. // 添加短信通知-需要审批提醒功能
  413. // const smsUser = await this.ctx.service.projectAccount.getDataById(nextAudit.aid);
  414. // if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
  415. // const smsType = JSON.parse(smsUser.sms_type);
  416. // if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  417. // const tenderInfo = await this.ctx.service.tender.getDataById(nextAudit.tid);
  418. // const stageInfo = await this.ctx.service.stage.getDataById(nextAudit.sid);
  419. // const sms = new SMS(this.ctx);
  420. // const tenderName = await sms.contentChange(tenderInfo.name);
  421. // const projectName = await sms.contentChange(this.ctx.tender.info.deal_info.buildName);
  422. // const result = await this.ctx.helper.urlToShort('http://' + this.ctx.request.header.host + '/wap/tender/' + this.ctx.tender.id + '/stage/' + stageInfo.order);
  423. // // const result = '';
  424. // const ptmsg = projectName !== '' ? '项目「' + projectName + '」标段「' + tenderName + '」' : tenderName;
  425. // const content = '【纵横计量支付】' + ptmsg + '第' + stageInfo.order + '期,需要您审批。' + result;
  426. // sms.send(smsUser.auth_mobile, content);
  427. // }
  428. // }
  429. const stageInfo = await this.ctx.service.stage.getDataById(nextAudit.sid);
  430. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/stage/' + stageInfo.order);
  431. await this.ctx.helper.sendAliSms(nextAudit.aid, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), SmsAliConst.template.stage_check, {
  432. qi: stageInfo.order,
  433. code: shenpiUrl,
  434. });
  435. // 微信模板通知
  436. const wechatData = {
  437. wap_url: shenpiUrl,
  438. qi: stageInfo.order,
  439. status: wxConst.status.check,
  440. tips: wxConst.tips.check,
  441. code: this.ctx.session.sessionProject.code,
  442. };
  443. await this.ctx.helper.sendWechat(nextAudit.aid, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), wxConst.template.stage, wechatData);
  444. } else {
  445. await this.ctx.service.tenderTag.saveTenderTag(this.ctx.tender.id, {stage_time: new Date()}, transaction);
  446. let his_id;
  447. if (this.ctx.tender.measureType === measureType.tz.value) {
  448. const ledgerHis = await this.ctx.service.ledgerHistory.getLatestHistory(this.ctx.tender.id);
  449. his_id = ledgerHis.id;
  450. } else {
  451. const sbCount = await this.ctx.service.ledger.count({ tender_id: this.ctx.stage.tid });
  452. const spCount = await this.ctx.service.pos.count({ tid: this.ctx.stage.tid });
  453. const ledgerHis = await this.ctx.service.ledgerHistory.getLatestHistory(this.ctx.tender.id);
  454. if (sbCount > ledgerHis.bills_count || spCount > ledgerHis.pos_count) {
  455. his_id = await this.ctx.service.ledgerHistory.backupStageLedgerHistory(this.ctx.stage);
  456. } else {
  457. his_id = ledgerHis.id;
  458. }
  459. }
  460. // 本期结束
  461. // 生成截止本期数据 final数据
  462. await this.ctx.service.stageBillsFinal.generateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  463. await this.ctx.service.stagePosFinal.generateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  464. await this.ctx.service.stageChangeFinal.generateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  465. // 同步 期信息
  466. await transaction.update(this.ctx.service.stage.tableName, {
  467. id: stageId,
  468. status: checkData.checkType,
  469. contract_tp: tpData.contract_tp,
  470. qc_tp: tpData.qc_tp,
  471. yf_tp: yfPay.tp,
  472. sf_tp: sfPay.tp,
  473. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  474. cache_time_r: this.ctx.stage.cache_time_l,
  475. his_id,
  476. });
  477. // 添加短信通知-审批通过提醒功能
  478. // const mobile_array = [];
  479. const stageInfo = await this.ctx.service.stage.getDataById(stageId);
  480. const auditList = await this.getAuditors(stageId, stageInfo.times);
  481. // const smsUser1 = await this.ctx.service.projectAccount.getDataById(stageInfo.user_id);
  482. // if (smsUser1.auth_mobile !== undefined && smsUser1.sms_type !== '' && smsUser1.sms_type !== null) {
  483. // const smsType = JSON.parse(smsUser1.sms_type);
  484. // if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  485. // mobile_array.push(smsUser1.auth_mobile);
  486. // }
  487. // }
  488. // for (const user of auditList) {
  489. // const smsUser = await this.ctx.service.projectAccount.getDataById(user.aid);
  490. // if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
  491. // const smsType = JSON.parse(smsUser.sms_type);
  492. // if (mobile_array.indexOf(smsUser.auth_mobile) === -1 && smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  493. // mobile_array.push(smsUser.auth_mobile);
  494. // }
  495. // }
  496. // }
  497. // if (mobile_array.length > 0) {
  498. // const tenderInfo = await this.ctx.service.tender.getDataById(stageInfo.tid);
  499. // const sms = new SMS(this.ctx);
  500. // const tenderName = await sms.contentChange(tenderInfo.name);
  501. // const projectName = await sms.contentChange(this.ctx.tender.info.deal_info.buildName);
  502. // const ptmsg = projectName !== '' ? '项目「' + projectName + '」标段「' + tenderName + '」' : tenderName;
  503. // const content = '【纵横计量支付】' + ptmsg + '第' + stageInfo.order + '期,审批通过。';
  504. // sms.send(mobile_array, content);
  505. // }
  506. const users = this._.uniq(this._.concat(this._.map(auditList, 'aid'), stageInfo.user_id));
  507. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.JL, smsTypeConst.judge.result.toString(), SmsAliConst.template.stage_result, {
  508. qi: stageInfo.order,
  509. status: SmsAliConst.status.success,
  510. });
  511. // 微信模板通知
  512. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/stage/' + stageInfo.order);
  513. const wechatData = {
  514. wap_url: shenpiUrl,
  515. qi: stageInfo.order,
  516. status: wxConst.status.success,
  517. tips: wxConst.tips.success,
  518. code: this.ctx.session.sessionProject.code,
  519. };
  520. await this.ctx.helper.sendWechat(users, smsTypeConst.const.JL, smsTypeConst.judge.result.toString(), wxConst.template.stage, wechatData);
  521. }
  522. await transaction.commit();
  523. } catch (err) {
  524. await transaction.rollback();
  525. throw err;
  526. }
  527. }
  528. async _checkNo(pid, stageId, checkData, times) {
  529. const time = new Date();
  530. // 整理当前流程审核人状态更新
  531. const audit = await this.getDataByCondition({ sid: stageId, times, status: auditConst.status.checking });
  532. if (!audit) {
  533. throw '审核数据错误';
  534. }
  535. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  536. const sql = 'SELECT `tid`, `sid`, `aid`, `order` FROM ?? WHERE `sid` = ? and `times` = ? GROUP BY `aid` ORDER BY `id` ASC';
  537. const sqlParam = [this.tableName, stageId, times];
  538. const auditors = await this.db.query(sql, sqlParam);
  539. let order = 1;
  540. for (const a of auditors) {
  541. a.times = times + 1;
  542. a.order = order;
  543. a.status = auditConst.status.uncheck;
  544. order++;
  545. }
  546. const transaction = await this.db.beginTransaction();
  547. try {
  548. await this._updateTender(transaction);
  549. // 添加推送
  550. const noticeContent = await this.getNoticeContent(pid, audit.tid, stageId, audit.aid, checkData.opinion);
  551. const records = [
  552. {
  553. pid,
  554. type: pushType.stage,
  555. uid: this.ctx.stage.user_id,
  556. status: auditConst.status.checkNo,
  557. content: noticeContent,
  558. },
  559. ];
  560. auditors.forEach(audit => {
  561. records.push({
  562. pid,
  563. type: pushType.stage,
  564. uid: audit.aid,
  565. status: auditConst.status.checkNo,
  566. content: noticeContent,
  567. });
  568. });
  569. await transaction.insert(this.ctx.service.noticePush.tableName, records);
  570. // 计算并合同支付最终数据
  571. const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  572. this.ctx.stage.tp_history.push({
  573. times,
  574. order: audit.order,
  575. contract_tp: tpData.contract_tp,
  576. qc_tp: tpData.qc_tp,
  577. yf_tp: yfPay.tp,
  578. sf_tp: sfPay.tp,
  579. });
  580. await transaction.update(this.tableName, {
  581. id: audit.id,
  582. status: checkData.checkType,
  583. opinion: checkData.opinion,
  584. end_time: time,
  585. });
  586. // 同步 期信息
  587. await transaction.update(this.ctx.service.stage.tableName, {
  588. id: stageId,
  589. status: checkData.checkType,
  590. contract_tp: tpData.contract_tp,
  591. qc_tp: tpData.qc_tp,
  592. times: times + 1,
  593. yf_tp: yfPay.tp,
  594. sf_tp: sfPay.tp,
  595. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  596. cache_time_r: this.ctx.stage.cache_time_l,
  597. });
  598. // 拷贝新一次审核流程列表
  599. await transaction.insert(this.tableName, auditors);
  600. // 计算该审批人最终数据
  601. await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  602. // 复制一份最新数据给原报
  603. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times + 1, 0, transaction);
  604. await this.ctx.service.stageJgcl.updateHistory(this.ctx.stage, transaction);
  605. await this.ctx.service.stageBonus.updateHistory(this.ctx.stage, transaction);
  606. await this.ctx.service.stageOther.updateHistory(this.ctx.stage, transaction);
  607. await this.ctx.service.stageSafeProd.updateHistory(this.ctx.stage, transaction);
  608. await this.ctx.service.stageTempLand.updateHistory(this.ctx.stage, transaction);
  609. // 添加短信通知-审批退回提醒功能
  610. // const mobile_array = [];
  611. const stageInfo = await this.ctx.service.stage.getDataById(stageId);
  612. const auditList = await this.getAuditors(stageId, stageInfo.times);
  613. // const smsUser1 = await this.ctx.service.projectAccount.getDataById(stageInfo.user_id);
  614. // if (smsUser1.auth_mobile !== '' && smsUser1.auth_mobile !== undefined && smsUser1.sms_type !== '' && smsUser1.sms_type !== null) {
  615. // const smsType = JSON.parse(smsUser1.sms_type);
  616. // if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  617. // mobile_array.push(smsUser1.auth_mobile);
  618. // }
  619. // }
  620. // for (const user of auditList) {
  621. // const smsUser = await this.ctx.service.projectAccount.getDataById(user.aid);
  622. // if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
  623. // const smsType = JSON.parse(smsUser.sms_type);
  624. // if (mobile_array.indexOf(smsUser.auth_mobile) === -1 && smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  625. // mobile_array.push(smsUser.auth_mobile);
  626. // }
  627. // }
  628. // }
  629. // if (mobile_array.length > 0) {
  630. // const tenderInfo = await this.ctx.service.tender.getDataById(stageInfo.tid);
  631. // const sms = new SMS(this.ctx);
  632. // const tenderName = await sms.contentChange(tenderInfo.name);
  633. // const projectName = await sms.contentChange(this.ctx.tender.info.deal_info.buildName);
  634. // const ptmsg = projectName !== '' ? '项目「' + projectName + '」标段「' + tenderName + '」' : tenderName;
  635. // const content = '【纵横计量支付】' + ptmsg + '第' + stageInfo.order + '期,审批退回。';
  636. // sms.send(mobile_array, content);
  637. // }
  638. const users = this._.uniq(this._.concat(this._.map(auditList, 'aid'), stageInfo.user_id));
  639. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.JL, smsTypeConst.judge.result.toString(), SmsAliConst.template.stage_result, {
  640. qi: stageInfo.order,
  641. status: SmsAliConst.status.back,
  642. });
  643. // 微信模板通知
  644. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/stage/' + stageInfo.order);
  645. const wechatData = {
  646. wap_url: shenpiUrl,
  647. qi: stageInfo.order,
  648. status: wxConst.status.back,
  649. tips: wxConst.tips.back,
  650. code: this.ctx.session.sessionProject.code,
  651. };
  652. await this.ctx.helper.sendWechat(users, smsTypeConst.const.JL, smsTypeConst.judge.result.toString(), wxConst.template.stage, wechatData);
  653. await transaction.commit();
  654. } catch (err) {
  655. await transaction.rollback();
  656. throw err;
  657. }
  658. }
  659. async _checkNoPre(pid, stageId, checkData, times) {
  660. const time = new Date();
  661. // 整理当前流程审核人状态更新
  662. const audit = await this.getDataByCondition({ sid: stageId, times, status: auditConst.status.checking });
  663. if (!audit || audit.order <= 1) {
  664. throw '审核数据错误';
  665. }
  666. // 添加重新审批后,不能用order-1,取groupby值里的上一个才对
  667. // const preAuditor = await this.getDataByCondition({sid: stageId, times: times, order: audit.order - 1});
  668. const auditors2 = await this.getAuditGroupByList(stageId, times);
  669. const auditorIndex = await auditors2.findIndex(function(item) {
  670. return item.aid === audit.aid;
  671. });
  672. const preAuditor = auditors2[auditorIndex - 1];
  673. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  674. const transaction = await this.db.beginTransaction();
  675. try {
  676. await this._updateTender(transaction);
  677. // 添加推送
  678. const noticeContent = await this.getNoticeContent(pid, audit.tid, stageId, audit.aid, checkData.opinion);
  679. const records = [
  680. {
  681. pid,
  682. type: pushType.stage,
  683. uid: this.ctx.stage.user_id,
  684. status: auditConst.status.checkNoPre,
  685. content: noticeContent,
  686. },
  687. ];
  688. auditors2.forEach(audit => {
  689. records.push({
  690. pid,
  691. type: pushType.stage,
  692. uid: audit.aid,
  693. status: auditConst.status.checkNoPre,
  694. content: noticeContent,
  695. });
  696. });
  697. await transaction.insert('zh_notice', records);
  698. // 计算并合同支付最终数据
  699. const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  700. this.ctx.stage.tp_history.push({
  701. times,
  702. order: audit.order,
  703. contract_tp: tpData.contract_tp,
  704. qc_tp: tpData.qc_tp,
  705. yf_tp: yfPay.tp,
  706. sf_tp: sfPay.tp,
  707. });
  708. // 同步 期信息
  709. await transaction.update(this.ctx.service.stage.tableName, {
  710. id: stageId,
  711. contract_tp: tpData.contract_tp,
  712. qc_tp: tpData.qc_tp,
  713. times,
  714. yf_tp: yfPay.tp,
  715. sf_tp: sfPay.tp,
  716. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  717. cache_time_r: this.ctx.stage.cache_time_l,
  718. });
  719. await transaction.update(this.tableName, {
  720. id: audit.id,
  721. status: checkData.checkType,
  722. opinion: checkData.opinion,
  723. end_time: time,
  724. });
  725. // 顺移气候审核人流程顺序
  726. this.initSqlBuilder();
  727. this.sqlBuilder.setAndWhere('sid', { value: this.ctx.stage.id, operate: '=' });
  728. this.sqlBuilder.setAndWhere('order', { value: audit.order, operate: '>' });
  729. this.sqlBuilder.setUpdateData('order', { value: 2, selfOperate: '+' });
  730. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  731. const data = await transaction.query(sql, sqlParam);
  732. // 上一审批人,当前审批人 再次添加至流程
  733. const newAuditors = [];
  734. newAuditors.push({
  735. tid: audit.tid,
  736. sid: audit.sid,
  737. aid: preAuditor.aid,
  738. times: audit.times,
  739. order: audit.order + 1,
  740. status: auditConst.status.checking,
  741. begin_time: time,
  742. });
  743. newAuditors.push({
  744. tid: audit.tid,
  745. sid: audit.sid,
  746. aid: audit.aid,
  747. times: audit.times,
  748. order: audit.order + 2,
  749. status: auditConst.status.uncheck,
  750. });
  751. await transaction.insert(this.tableName, newAuditors);
  752. // 计算该审批人最终数据
  753. await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  754. // 复制一份最新数据给下一人
  755. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, audit.order + 1, transaction);
  756. await this.ctx.service.stageJgcl.updateHistory(this.ctx.stage, transaction);
  757. await this.ctx.service.stageBonus.updateHistory(this.ctx.stage, transaction);
  758. await this.ctx.service.stageOther.updateHistory(this.ctx.stage, transaction);
  759. await this.ctx.service.stageSafeProd.updateHistory(this.ctx.stage, transaction);
  760. await this.ctx.service.stageTempLand.updateHistory(this.ctx.stage, transaction);
  761. // 多人协同数据确认删除本人和上一个审批人确认状态
  762. await this.ctx.service.cooperationConfirm.delBycheckNoPre(audit.aid, this.ctx.stage, transaction);
  763. await this.ctx.service.cooperationConfirm.delBycheckNoPre(preAuditor.aid, this.ctx.stage, transaction);
  764. // 同步 期信息
  765. await transaction.update(this.ctx.service.stage.tableName, {
  766. id: stageId,
  767. status: checkData.checkType,
  768. cache_time_r: this.ctx.stage.cache_time_l,
  769. });
  770. // 添加短信通知-需要审批提醒功能
  771. // const smsUser = await this.ctx.service.projectAccount.getDataById(preAuditor.aid);
  772. // if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
  773. // const smsType = JSON.parse(smsUser.sms_type);
  774. // if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  775. // const tenderInfo = await this.ctx.service.tender.getDataById(audit.tid);
  776. // const stageInfo = await this.ctx.service.stage.getDataById(audit.sid);
  777. // const sms = new SMS(this.ctx);
  778. // const tenderName = await sms.contentChange(tenderInfo.name);
  779. // const projectName = await sms.contentChange(this.ctx.tender.info.deal_info.buildName);
  780. // const ptmsg = projectName !== '' ? '项目「' + projectName + '」标段「' + tenderName + '」' : tenderName;
  781. // const result = await this.ctx.helper.urlToShort('http://' + this.ctx.request.header.host + '/wap/tender/' + this.ctx.tender.id + '/stage/' + stageInfo.order);
  782. // // const result = '';
  783. // const content = '【纵横计量支付】' + ptmsg + '第' + stageInfo.order + '期,需要您审批。' + result;
  784. // sms.send(smsUser.auth_mobile, content);
  785. // }
  786. // }
  787. const stageInfo = await this.ctx.service.stage.getDataById(audit.sid);
  788. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/stage/' + stageInfo.order);
  789. await this.ctx.helper.sendAliSms(preAuditor.aid, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), SmsAliConst.template.stage_check, {
  790. qi: stageInfo.order,
  791. code: shenpiUrl,
  792. });
  793. // 微信模板通知
  794. const wechatData = {
  795. wap_url: shenpiUrl,
  796. qi: stageInfo.order,
  797. status: wxConst.status.check,
  798. tips: wxConst.tips.check,
  799. code: this.ctx.session.sessionProject.code,
  800. };
  801. await this.ctx.helper.sendWechat(preAuditor.aid, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), wxConst.template.stage, wechatData);
  802. await transaction.commit();
  803. } catch (err) {
  804. await transaction.rollback();
  805. throw err;
  806. }
  807. }
  808. /**
  809. * 审批
  810. * @param {Number} stageId - 标段id
  811. * @param {auditConst.status.checked|auditConst.status.checkNo} checkType - 审批结果
  812. * @param {Number} times - 第几次审批
  813. * @return {Promise<void>}
  814. */
  815. async check(stageId, checkData, times = 1) {
  816. if (checkData.checkType !== auditConst.status.checked && checkData.checkType !== auditConst.status.checkNo && checkData.checkType !== auditConst.status.checkNoPre) {
  817. throw '提交数据错误';
  818. }
  819. // // 整理当前流程审核人状态更新
  820. // const audit = await this.getDataByCondition({sid: stageId, times: times, status: auditConst.status.checking});
  821. // if (!audit) {
  822. // throw '审核数据错误';
  823. // }
  824. // const time = new Date();
  825. const pid = this.ctx.session.sessionProject.id;
  826. switch (checkData.checkType) {
  827. case auditConst.status.checked:
  828. await this._checked(pid, stageId, checkData, times);
  829. break;
  830. case auditConst.status.checkNo:
  831. await this._checkNo(pid, stageId, checkData, times);
  832. break;
  833. case auditConst.status.checkNoPre:
  834. await this._checkNoPre(pid, stageId, checkData, times);
  835. break;
  836. default:
  837. throw '无效审批操作';
  838. }
  839. // 通知发送 - 第三方更新
  840. if (this.ctx.session.sessionProject.custom && syncApiConst.notice_type.indexOf(this.ctx.session.sessionProject.customType) !== -1) {
  841. const base_data = {
  842. tid: this.ctx.tender.id,
  843. sid: stageId,
  844. op: 'update',
  845. };
  846. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  847. base_data.op = 'update';
  848. base_data.sid = -1;
  849. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  850. }
  851. }
  852. /**
  853. * 审批
  854. * @param {Number} stageId - 标段id
  855. * @param {Number} times - 第几次审批
  856. * @return {Promise<void>}
  857. */
  858. async checkAgain(stageId, times = 1) {
  859. const time = new Date();
  860. // 整理当前流程审核人状态更新
  861. const audit = (
  862. await this.getAllDataByCondition({
  863. where: { sid: stageId, times },
  864. orders: [['order', 'desc']],
  865. limit: 1,
  866. offset: 0,
  867. })
  868. )[0];
  869. if (!audit || audit.order < 1) {
  870. throw '审核数据错误';
  871. }
  872. const transaction = await this.db.beginTransaction();
  873. try {
  874. // 当前审批人2次添加至流程中
  875. const newAuditors = [];
  876. newAuditors.push({
  877. tid: audit.tid,
  878. sid: audit.sid,
  879. aid: audit.aid,
  880. times: audit.times,
  881. order: audit.order + 1,
  882. status: auditConst.status.checkAgain,
  883. begin_time: time,
  884. end_time: time,
  885. opinion: '',
  886. });
  887. newAuditors.push({
  888. tid: audit.tid,
  889. sid: audit.sid,
  890. aid: audit.aid,
  891. times: audit.times,
  892. order: audit.order + 2,
  893. status: auditConst.status.checking,
  894. begin_time: time,
  895. });
  896. await transaction.insert(this.tableName, newAuditors);
  897. // 复制一份最新数据给下一人
  898. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, audit.order + 1, transaction);
  899. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, audit.order + 2, transaction);
  900. await this.ctx.service.stageJgcl.updateHistory4CheckAgain(this.ctx.stage, transaction);
  901. await this.ctx.service.stageBonus.updateHistory4CheckAgain(this.ctx.stage, transaction);
  902. await this.ctx.service.stageOther.updateHistory4CheckAgain(this.ctx.stage, transaction);
  903. await this.ctx.service.stageSafeProd.updateHistory4CheckAgain(this.ctx.stage, transaction);
  904. await this.ctx.service.stageTempLand.updateHistory4CheckAgain(this.ctx.stage, transaction);
  905. // 本期结束
  906. // 生成截止本期数据 final数据
  907. await this.ctx.service.stageBillsFinal.delGenerateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  908. await this.ctx.service.stagePosFinal.delGenerateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  909. await this.ctx.service.stageChangeFinal.delGenerateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  910. // 同步 期信息
  911. const his = this.ctx.stage.tp_history.find(x => { return x.times === times && x.order === audit.order });
  912. this.ctx.stage.tp_history.push({
  913. times,
  914. order: audit.order + 1,
  915. contract_tp: his.contract_tp,
  916. qc_tp: his.qc_tp,
  917. yf_tp: his.yf_tp,
  918. sf_tp: his.sf_tp,
  919. });
  920. await transaction.update(this.ctx.service.stage.tableName, {
  921. id: stageId,
  922. status: auditConst.status.checking,
  923. cache_time_r: this.ctx.stage.cache_time_l,
  924. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  925. });
  926. // 添加短信通知-需要审批提醒功能
  927. // const smsUser = await this.ctx.service.projectAccount.getDataById(audit.aid);
  928. // if (smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
  929. // const smsType = JSON.parse(smsUser.sms_type);
  930. // if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  931. // const tenderInfo = await this.ctx.service.tender.getDataById(audit.tid);
  932. // const stageInfo = await this.ctx.service.stage.getDataById(audit.sid);
  933. // const sms = new SMS(this.ctx);
  934. // const tenderName = await sms.contentChange(tenderInfo.name);
  935. // const projectName = await sms.contentChange(this.ctx.tender.info.deal_info.buildName);
  936. // const ptmsg = projectName !== '' ? '项目「' + projectName + '」标段「' + tenderName + '」' : tenderName;
  937. // const result = await this.ctx.helper.urlToShort('http://' + this.ctx.request.header.host + '/wap/tender/' + this.ctx.tender.id + '/stage/' + stageInfo.order);
  938. // const content = '【纵横计量支付】' + ptmsg + '第' + stageInfo.order + '期,需要您审批。' + result;
  939. // sms.send(smsUser.auth_mobile, content);
  940. // }
  941. // }
  942. const stageInfo = await this.ctx.service.stage.getDataById(audit.sid);
  943. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/stage/' + stageInfo.order);
  944. await this.ctx.helper.sendAliSms(audit.aid, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), SmsAliConst.template.stage_check, {
  945. qi: stageInfo.order,
  946. code: shenpiUrl,
  947. });
  948. // 微信模板通知
  949. const wechatData = {
  950. wap_url: shenpiUrl,
  951. qi: stageInfo.order,
  952. status: wxConst.status.check,
  953. tips: wxConst.tips.check,
  954. code: this.ctx.session.sessionProject.code,
  955. };
  956. await this.ctx.helper.sendWechat(audit.aid, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), wxConst.template.stage, wechatData);
  957. await transaction.commit();
  958. // 通知发送 - 第三方更新
  959. if (this.ctx.session.sessionProject.custom && syncApiConst.notice_type.indexOf(this.ctx.session.sessionProject.customType) !== -1) {
  960. const base_data = {
  961. tid: this.ctx.tender.id,
  962. sid: stageId,
  963. op: 'update',
  964. };
  965. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  966. base_data.op = 'update';
  967. base_data.sid = -1;
  968. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  969. }
  970. } catch (err) {
  971. await transaction.rollback();
  972. throw err;
  973. }
  974. }
  975. /**
  976. * 获取审核人需要审核的期列表
  977. *
  978. * @param auditorId
  979. * @return {Promise<*>}
  980. */
  981. async getAuditStage(auditorId) {
  982. const sql =
  983. 'SELECT sa.`aid`, sa.`times`, sa.`order`, sa.`begin_time`, sa.`end_time`, sa.`tid`, sa.`sid`,' +
  984. ' s.`order` As `sorder`, s.`status` As `sstatus`,' +
  985. ' t.`name`, t.`project_id`, t.`type`, t.`user_id` ' +
  986. ' FROM ?? AS sa ' +
  987. ' Left Join ?? AS s On sa.`sid` = s.`id` ' +
  988. ' Left Join ?? As t ON sa.`tid` = t.`id`' +
  989. ' WHERE ((sa.`aid` = ? and sa.`status` = ?) OR (s.`user_id` = ? and sa.`status` = ? and s.`status` = ? and sa.`times` = (s.`times`-1)))' +
  990. ' ORDER BY sa.`begin_time` DESC';
  991. const sqlParam = [
  992. this.tableName,
  993. this.ctx.service.stage.tableName,
  994. this.ctx.service.tender.tableName,
  995. auditorId,
  996. auditConst.status.checking,
  997. auditorId,
  998. auditConst.status.checkNo,
  999. auditConst.status.checkNo,
  1000. ];
  1001. return await this.db.query(sql, sqlParam);
  1002. }
  1003. /**
  1004. * 获取审核人审核的次数
  1005. *
  1006. * @param auditorId
  1007. * @return {Promise<*>}
  1008. */
  1009. async getCountByChecked(auditorId) {
  1010. return await this.db.count(this.tableName, { aid: auditorId, status: [auditConst.status.checked, auditConst.status.checkNo, auditConst.status.checkNoPre] });
  1011. }
  1012. /**
  1013. * 获取最近一次审批结束时间
  1014. *
  1015. * @param auditorId
  1016. * @return {Promise<*>}
  1017. */
  1018. async getLastEndTimeByChecked(auditorId) {
  1019. const sql = 'SELECT `end_time` FROM ?? WHERE `aid` = ? ' +
  1020. 'AND `status` in (' + this.ctx.helper.getInArrStrSqlFilter([auditConst.status.checked, auditConst.status.checkNo, auditConst.status.checkNoPre]) + ') ORDER BY `end_time` DESC';
  1021. const sqlParam = [this.tableName, auditorId];
  1022. const result = await this.db.queryOne(sql, sqlParam);
  1023. return result ? result.end_time : null;
  1024. }
  1025. /**
  1026. * 获取 某时间后 审批进度 更新的期
  1027. * @param {Number} pid - 查询标段
  1028. * @param {Number} uid - 查询人
  1029. * @param {Date} time - 查询时间
  1030. * @return {Promise<*>}
  1031. */
  1032. async getNoticeStage(pid, uid, time) {
  1033. let notice = await this.db.select('zh_notice', {
  1034. where: { pid, type: pushType.stage, uid },
  1035. orders: [['create_time', 'desc']],
  1036. limit: 10,
  1037. offset: 0,
  1038. });
  1039. notice = notice.map(v => {
  1040. const extra = JSON.parse(v.content);
  1041. delete v.content;
  1042. return { ...v, ...extra };
  1043. });
  1044. return notice;
  1045. }
  1046. /**
  1047. * 用于添加推送所需的content内容
  1048. * @param {Number} pid 项目id
  1049. * @param {Number} tid 台账id
  1050. * @param {Number} sid 期id
  1051. * @param {Number} uid 审核人id
  1052. */
  1053. async getNoticeContent(pid, tid, sid, uid, opinion = '') {
  1054. const noticeSql =
  1055. 'SELECT * FROM (SELECT ' +
  1056. ' t.`id` As `tid`, t.`name`, s.`order`, pa.`name` As `su_name`, pa.role As `su_role`' +
  1057. ' FROM (SELECT * FROM ?? WHERE `id` = ? ) As t' +
  1058. ' LEFT JOIN ?? As s On s.`id` = ?' +
  1059. ' LEFT JOIN ?? As pa ON pa.`id` = ?' +
  1060. ' WHERE t.`project_id` = ? ) as new_t GROUP BY new_t.`tid`';
  1061. const noticeSqlParam = [this.ctx.service.tender.tableName, tid, this.ctx.service.stage.tableName, sid, this.ctx.service.projectAccount.tableName, uid, pid];
  1062. const content = await this.db.query(noticeSql, noticeSqlParam);
  1063. if (content.length) {
  1064. content[0].opinion = opinion;
  1065. }
  1066. return content.length ? JSON.stringify(content[0]) : '';
  1067. }
  1068. /**
  1069. * 获取审核人流程列表
  1070. *
  1071. * @param auditorId
  1072. * @return {Promise<*>}
  1073. */
  1074. async getAuditGroupByList(stageId, times) {
  1075. const sql =
  1076. 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`aid`, la.`order` ' +
  1077. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  1078. ' WHERE la.`sid` = ? and la.`times` = ? GROUP BY la.`aid` ORDER BY la.`order`';
  1079. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, times];
  1080. return await this.db.query(sql, sqlParam);
  1081. }
  1082. /**
  1083. * 获取审核人流程列表
  1084. *
  1085. * @param auditorId
  1086. * @return {Promise<*>}
  1087. */
  1088. async getAuditGroupByListWithOwner(stageId, times) {
  1089. const result = await this.getAuditGroupByList(stageId, times);
  1090. const sql =
  1091. 'SELECT pa.`id` As aid, pa.`name`, pa.`company`, pa.`role`, ? As times, ? As sid, 0 As `order`' +
  1092. ' FROM ' + this.ctx.service.stage.tableName + ' As s' +
  1093. ' LEFT JOIN ' + this.ctx.service.projectAccount.tableName + ' As pa' +
  1094. ' ON s.user_id = pa.id' +
  1095. ' WHERE s.id = ?';
  1096. const sqlParam = [times, stageId, stageId];
  1097. const user = await this.db.queryOne(sql, sqlParam);
  1098. result.unshift(user);
  1099. return result;
  1100. }
  1101. /**
  1102. * 复制上一期的审批人列表给最新一期
  1103. *
  1104. * @param transaction - 新增一期的事务
  1105. * @param {Object} preStage - 上一期
  1106. * @param {Object} newStage - 最新一期
  1107. * @return {Promise<*>}
  1108. */
  1109. async copyPreStageAuditors(transaction, preStage, newStage) {
  1110. const auditors = await this.getAuditGroupByList(preStage.id, preStage.times);
  1111. const newAuditors = [];
  1112. for (const a of auditors) {
  1113. const na = {
  1114. tid: preStage.tid,
  1115. sid: newStage.id,
  1116. aid: a.aid,
  1117. times: newStage.times,
  1118. order: newAuditors.length + 1,
  1119. status: auditConst.status.uncheck,
  1120. };
  1121. newAuditors.push(na);
  1122. }
  1123. const result = await transaction.insert(this.tableName, newAuditors);
  1124. return (result.effectRows = auditors.length);
  1125. }
  1126. /**
  1127. * 移除审核人
  1128. *
  1129. * @param {Number} stageId - 期id
  1130. * @param {Number} status - 期状态
  1131. * @param {Number} status - 期次数
  1132. * @return {Promise<boolean>}
  1133. */
  1134. async getAuditorByStatus(stageId, status, times = 1) {
  1135. let auditor = null;
  1136. let sql = '';
  1137. let sqlParam = '';
  1138. switch (status) {
  1139. case auditConst.status.checking:
  1140. case auditConst.status.checked:
  1141. case auditConst.status.checkNoPre:
  1142. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`order` ' +
  1143. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  1144. ' WHERE la.`sid` = ? and la.`status` = ? order by la.`times` desc, la.`order` desc';
  1145. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, status];
  1146. auditor = await this.db.queryOne(sql, sqlParam);
  1147. break;
  1148. case auditConst.status.checkNo:
  1149. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`order` ' +
  1150. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  1151. ' WHERE la.`sid` = ? and la.`status` = ? and la.`times` = ? order by la.`times` desc, la.`order` desc';
  1152. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, auditConst.status.checkNo, parseInt(times) - 1];
  1153. auditor = await this.db.queryOne(sql, sqlParam);
  1154. break;
  1155. case auditConst.status.uncheck:
  1156. default:
  1157. break;
  1158. }
  1159. return auditor;
  1160. }
  1161. /**
  1162. * 取某一期已批准审核信息(报表用)
  1163. *
  1164. * @param {Number} stageId - 期id
  1165. * @param {Number} times - 期次数
  1166. * @return {Promise<boolean>}
  1167. */
  1168. async getStageAudit(stageId, times = 1) {
  1169. 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';
  1170. const sqlParam = [this.tableName, stageId, times];
  1171. const rst = await this.db.query(sql, sqlParam);
  1172. return rst;
  1173. }
  1174. /**
  1175. * 取待审批期列表(wap用)
  1176. *
  1177. * @param auditorId
  1178. * @return {Promise<*>}
  1179. */
  1180. async getAuditStageByWap(auditorId) {
  1181. const sql =
  1182. 'SELECT sa.`aid`, sa.`times`, sa.`begin_time`, sa.`end_time`, sa.`tid`, sa.`sid`,' +
  1183. // ' 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`, ' +
  1184. ' s.*,' +
  1185. ' t.`name`, t.`project_id`, t.`type`, t.`user_id`,' +
  1186. ' ti.`deal_info` ' +
  1187. ' FROM ?? AS sa' +
  1188. ' Left Join ?? AS s On sa.`sid` = s.`id`' +
  1189. ' Left Join ?? As t On sa.`tid` = t.`id`' +
  1190. ' Left Join ?? AS ti ON ti.`tid` = t.`id`' +
  1191. ' WHERE sa.`aid` = ? and sa.`status` = ?';
  1192. const sqlParam = [
  1193. this.tableName,
  1194. this.ctx.service.stage.tableName,
  1195. this.ctx.service.tender.tableName,
  1196. this.ctx.service.tenderInfo.tableName,
  1197. auditorId,
  1198. auditConst.status.checking,
  1199. ];
  1200. return await this.db.query(sql, sqlParam);
  1201. }
  1202. /**
  1203. * 删除 某期 某次 全审批流程
  1204. * 私有,不做判断,不补全最新一轮审批人数据,不计算缓存
  1205. * @param {Number} sid - 标段id
  1206. * @param {Number} times - 第几次审批
  1207. * @param transaction - 删除事务
  1208. * @return {Promise<void>}
  1209. */
  1210. async _timesDelete(sid, times, transaction) {
  1211. // 审批流程
  1212. await transaction.delete(this.tableName, { sid, times });
  1213. await transaction.delete(this.ctx.service.pos.tableName, { add_stage: sid, add_times: times });
  1214. await transaction.delete(this.ctx.service.stageBills.tableName, { sid, times });
  1215. await transaction.delete(this.ctx.service.stagePos.tableName, { sid, times });
  1216. await transaction.delete(this.ctx.service.stageDetail.tableName, { sid, times });
  1217. await transaction.delete(this.ctx.service.stageChange.tableName, { sid, stimes: times });
  1218. await transaction.delete(this.ctx.service.stagePay.tableName, { sid, stimes: times });
  1219. await transaction.delete(this.ctx.service.pay.tableName, { csid: sid, cstimes: times });
  1220. // 其他台账
  1221. await this.ctx.service.stageJgcl.deleteStageTimesData(sid, times, transaction);
  1222. await this.ctx.service.stageOther.deleteStageTimesData(sid, times, transaction);
  1223. await this.ctx.service.stageBonus.deleteStageTimesData(sid, times, transaction);
  1224. await this.ctx.service.stageSafeProd.deleteStageTimesData(sid, times, transaction);
  1225. await this.ctx.service.stageTempLand.deleteStageTimesData(sid, times, transaction);
  1226. }
  1227. /**
  1228. * 删除本次审批流程
  1229. * @param {Number} stageId - 标段id
  1230. * @param {Number} times - 第几次审批
  1231. * @return {Promise<void>}
  1232. */
  1233. async timesDelete() {
  1234. const transaction = await this.db.beginTransaction();
  1235. try {
  1236. // 删除最新一次数据
  1237. await this._timesDelete(this.ctx.stage.id, this.ctx.stage.times, transaction);
  1238. // 审批退回,未重新上报时,需删除最新两次数据
  1239. const isCheckNo = this.ctx.stage.status === auditConst.status.checkNo;
  1240. const nowTimes = isCheckNo ? this.ctx.stage.times - 1 : this.ctx.stage.times;
  1241. if (isCheckNo) {
  1242. await this._timesDelete(this.ctx.stage.id, nowTimes, transaction);
  1243. }
  1244. // 添加上一次审批人
  1245. const sql = 'SELECT `tid`, `sid`, `aid`, `order` FROM ?? WHERE `sid` = ? and `times` = ? GROUP BY `aid` ORDER BY `id` ASC';
  1246. const sqlParam = [this.tableName, this.ctx.stage.id, nowTimes - 1];
  1247. const auditors = await this.db.query(sql, sqlParam);
  1248. let order = 1;
  1249. for (const a of auditors) {
  1250. a.times = nowTimes;
  1251. a.order = order;
  1252. a.status = auditConst.status.uncheck;
  1253. order++;
  1254. }
  1255. await this._updateTender(transaction);
  1256. // 拷贝新一次审核流程列表
  1257. await transaction.insert(this.tableName, auditors);
  1258. // 计算缓存
  1259. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  1260. // 计算并合同支付最终数据
  1261. const lastAudit = await this.getDataByCondition({
  1262. sid: this.ctx.stage.id,
  1263. times: nowTimes - 1,
  1264. status: auditConst.status.checkNo,
  1265. });
  1266. if (!lastAudit) throw '审批数据错误';
  1267. await this.ctx.service.stagePay.copyStagePays4DeleteTimes(this.ctx.stage, nowTimes, 0, lastAudit.times, lastAudit.order, transaction);
  1268. const stagePay = await this.ctx.service.stagePay.getAuditorStageData(this.ctx.stage.id, lastAudit.times, lastAudit.order);
  1269. const yfPay = stagePay.find(function(x) {
  1270. return x.ptype === payConst.payType.yf;
  1271. });
  1272. const sfPay = stagePay.find(function(x) {
  1273. return x.ptype === payConst.payType.sf;
  1274. });
  1275. // 同步 期信息
  1276. const time = new Date();
  1277. await transaction.update(this.ctx.service.stage.tableName, {
  1278. id: this.ctx.stage.id,
  1279. status: auditConst.status.checkNo,
  1280. contract_tp: tpData.contract_tp,
  1281. qc_tp: tpData.qc_tp,
  1282. times: nowTimes,
  1283. yf_tp: yfPay ? yfPay.tp : null,
  1284. sf_tp: sfPay ? sfPay.tp : null,
  1285. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  1286. cache_time_l: time,
  1287. cache_time_r: time,
  1288. });
  1289. await transaction.commit();
  1290. // 通知发送 - 第三方更新
  1291. if (this.ctx.session.sessionProject.custom && syncApiConst.notice_type.indexOf(this.ctx.session.sessionProject.customType) !== -1) {
  1292. const base_data = {
  1293. tid: this.ctx.tender.id,
  1294. sid: this.ctx.stage.id,
  1295. op: 'update',
  1296. };
  1297. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  1298. base_data.op = 'update';
  1299. base_data.sid = -1;
  1300. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  1301. }
  1302. } catch (err) {
  1303. await transaction.rollback();
  1304. throw err;
  1305. }
  1306. }
  1307. // 固定审批流-更新
  1308. async updateNewAuditList(stage, newIdList) {
  1309. const transaction = await this.db.beginTransaction();
  1310. try {
  1311. // 先删除旧的审批流,再添加新的
  1312. await transaction.delete(this.tableName, { sid: stage.id, times: stage.times });
  1313. const newAuditors = [];
  1314. let order = 1;
  1315. for (const aid of newIdList) {
  1316. newAuditors.push({
  1317. tid: stage.tid, sid: stage.id, aid,
  1318. times: stage.times, order, status: auditConst.status.uncheck,
  1319. });
  1320. order++;
  1321. }
  1322. if(newAuditors.length > 0) await transaction.insert(this.tableName, newAuditors);
  1323. await transaction.commit();
  1324. } catch (err) {
  1325. await transaction.rollback();
  1326. throw err;
  1327. }
  1328. }
  1329. // 固定终审-更新
  1330. async updateLastAudit(stage, auditList, lastId) {
  1331. const transaction = await this.db.beginTransaction();
  1332. try {
  1333. // 先判断auditList里的aid是否与lastId相同,相同则删除并重新更新order
  1334. const idList = this._.map(auditList, 'aid');
  1335. let order = idList.length + 1;
  1336. if (idList.indexOf(lastId) !== -1) {
  1337. await transaction.delete(this.tableName, { sid: stage.id, times: stage.times, aid: lastId });
  1338. const audit = this._.find(auditList, { 'aid': lastId });
  1339. // 顺移之后审核人流程顺序
  1340. await this._syncOrderByDelete(transaction, stage.id, audit.order, stage.times);
  1341. order = order - 1;
  1342. }
  1343. // 添加终审
  1344. const newAuditor = {
  1345. tid: stage.tid, sid: stage.id, aid: lastId,
  1346. times: stage.times, order, status: auditConst.status.uncheck,
  1347. };
  1348. await transaction.insert(this.tableName, newAuditor);
  1349. await transaction.commit();
  1350. } catch (err) {
  1351. await transaction.rollback();
  1352. throw err;
  1353. }
  1354. }
  1355. async getFinalAuditGroup(stageId, times) {
  1356. const sql =
  1357. '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 ' +
  1358. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id`' +
  1359. ' WHERE la.`sid` = ? and la.`times` = ? GROUP BY la.`aid` ORDER BY la.`order`';
  1360. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, times];
  1361. const result = await this.db.query(sql, sqlParam);
  1362. for (const r of result) {
  1363. const auditor = await this.getDataByCondition({sid: stageId, times: r.times, order: r.max_order});
  1364. r.status = auditor.status;
  1365. r.opinion = auditor.opinion;
  1366. r.begin_time = auditor.begin_time;
  1367. r.end_time = auditor.end_time;
  1368. }
  1369. return result;
  1370. }
  1371. async getNumByMonth(tid, startMonth, endMonth) {
  1372. 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 ?';
  1373. const sqlParam = [this.tableName, this.tableName, tid, auditConst.status.checked, startMonth, endMonth];
  1374. const result = await this.db.queryOne(sql, sqlParam);
  1375. return result ? result.num : 0;
  1376. }
  1377. }
  1378. return StageAudit;
  1379. };