stage_audit.js 75 KB

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