stage_audit.js 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436
  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. const his_id = await this.ctx.service.ledgerHistory.backupStageLedgerHistory(this.ctx.stage);
  447. // 本期结束
  448. // 生成截止本期数据 final数据
  449. await this.ctx.service.stageBillsFinal.generateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  450. await this.ctx.service.stagePosFinal.generateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  451. await this.ctx.service.stageChangeFinal.generateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  452. // 同步 期信息
  453. await transaction.update(this.ctx.service.stage.tableName, {
  454. id: stageId,
  455. status: checkData.checkType,
  456. contract_tp: tpData.contract_tp,
  457. qc_tp: tpData.qc_tp,
  458. yf_tp: yfPay.tp,
  459. sf_tp: sfPay.tp,
  460. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  461. cache_time_r: this.ctx.stage.cache_time_l,
  462. his_id,
  463. });
  464. // 添加短信通知-审批通过提醒功能
  465. // const mobile_array = [];
  466. const stageInfo = await this.ctx.service.stage.getDataById(stageId);
  467. const auditList = await this.getAuditors(stageId, stageInfo.times);
  468. // const smsUser1 = await this.ctx.service.projectAccount.getDataById(stageInfo.user_id);
  469. // if (smsUser1.auth_mobile !== undefined && smsUser1.sms_type !== '' && smsUser1.sms_type !== null) {
  470. // const smsType = JSON.parse(smsUser1.sms_type);
  471. // if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  472. // mobile_array.push(smsUser1.auth_mobile);
  473. // }
  474. // }
  475. // for (const user of auditList) {
  476. // const smsUser = await this.ctx.service.projectAccount.getDataById(user.aid);
  477. // if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
  478. // const smsType = JSON.parse(smsUser.sms_type);
  479. // if (mobile_array.indexOf(smsUser.auth_mobile) === -1 && smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  480. // mobile_array.push(smsUser.auth_mobile);
  481. // }
  482. // }
  483. // }
  484. // if (mobile_array.length > 0) {
  485. // const tenderInfo = await this.ctx.service.tender.getDataById(stageInfo.tid);
  486. // const sms = new SMS(this.ctx);
  487. // const tenderName = await sms.contentChange(tenderInfo.name);
  488. // const projectName = await sms.contentChange(this.ctx.tender.info.deal_info.buildName);
  489. // const ptmsg = projectName !== '' ? '项目「' + projectName + '」标段「' + tenderName + '」' : tenderName;
  490. // const content = '【纵横计量支付】' + ptmsg + '第' + stageInfo.order + '期,审批通过。';
  491. // sms.send(mobile_array, content);
  492. // }
  493. const users = this._.uniq(this._.concat(this._.map(auditList, 'aid'), stageInfo.user_id));
  494. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.JL, smsTypeConst.judge.result.toString(), SmsAliConst.template.stage_result, {
  495. qi: stageInfo.order,
  496. status: SmsAliConst.status.success,
  497. });
  498. // 微信模板通知
  499. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/stage/' + stageInfo.order);
  500. const wechatData = {
  501. wap_url: shenpiUrl,
  502. qi: stageInfo.order,
  503. status: wxConst.status.success,
  504. tips: wxConst.tips.success,
  505. code: this.ctx.session.sessionProject.code,
  506. };
  507. await this.ctx.helper.sendWechat(users, smsTypeConst.const.JL, smsTypeConst.judge.result.toString(), wxConst.template.stage, wechatData);
  508. }
  509. await transaction.commit();
  510. } catch (err) {
  511. await transaction.rollback();
  512. throw err;
  513. }
  514. }
  515. async _checkNo(pid, stageId, checkData, times) {
  516. const time = new Date();
  517. // 整理当前流程审核人状态更新
  518. const audit = await this.getDataByCondition({ sid: stageId, times, status: auditConst.status.checking });
  519. if (!audit) {
  520. throw '审核数据错误';
  521. }
  522. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  523. const sql = 'SELECT `tid`, `sid`, `aid`, `order` FROM ?? WHERE `sid` = ? and `times` = ? GROUP BY `aid` ORDER BY `id` ASC';
  524. const sqlParam = [this.tableName, stageId, times];
  525. const auditors = await this.db.query(sql, sqlParam);
  526. let order = 1;
  527. for (const a of auditors) {
  528. a.times = times + 1;
  529. a.order = order;
  530. a.status = auditConst.status.uncheck;
  531. order++;
  532. }
  533. const transaction = await this.db.beginTransaction();
  534. try {
  535. await this._updateTender(transaction);
  536. // 添加推送
  537. const noticeContent = await this.getNoticeContent(pid, audit.tid, stageId, audit.aid, checkData.opinion);
  538. const records = [
  539. {
  540. pid,
  541. type: pushType.stage,
  542. uid: this.ctx.stage.user_id,
  543. status: auditConst.status.checkNo,
  544. content: noticeContent,
  545. },
  546. ];
  547. auditors.forEach(audit => {
  548. records.push({
  549. pid,
  550. type: pushType.stage,
  551. uid: audit.aid,
  552. status: auditConst.status.checkNo,
  553. content: noticeContent,
  554. });
  555. });
  556. await transaction.insert(this.ctx.service.noticePush.tableName, records);
  557. // 计算并合同支付最终数据
  558. const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  559. this.ctx.stage.tp_history.push({
  560. times,
  561. order: audit.order,
  562. contract_tp: tpData.contract_tp,
  563. qc_tp: tpData.qc_tp,
  564. yf_tp: yfPay.tp,
  565. sf_tp: sfPay.tp,
  566. });
  567. await transaction.update(this.tableName, {
  568. id: audit.id,
  569. status: checkData.checkType,
  570. opinion: checkData.opinion,
  571. end_time: time,
  572. });
  573. // 同步 期信息
  574. await transaction.update(this.ctx.service.stage.tableName, {
  575. id: stageId,
  576. status: checkData.checkType,
  577. contract_tp: tpData.contract_tp,
  578. qc_tp: tpData.qc_tp,
  579. times: times + 1,
  580. yf_tp: yfPay.tp,
  581. sf_tp: sfPay.tp,
  582. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  583. cache_time_r: this.ctx.stage.cache_time_l,
  584. });
  585. // 拷贝新一次审核流程列表
  586. await transaction.insert(this.tableName, auditors);
  587. // 计算该审批人最终数据
  588. await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  589. // 复制一份最新数据给原报
  590. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times + 1, 0, transaction);
  591. await this.ctx.service.stageJgcl.updateHistory(this.ctx.stage, transaction);
  592. await this.ctx.service.stageBonus.updateHistory(this.ctx.stage, transaction);
  593. await this.ctx.service.stageOther.updateHistory(this.ctx.stage, transaction);
  594. await this.ctx.service.stageSafeProd.updateHistory(this.ctx.stage, transaction);
  595. await this.ctx.service.stageTempLand.updateHistory(this.ctx.stage, transaction);
  596. // 添加短信通知-审批退回提醒功能
  597. // const mobile_array = [];
  598. const stageInfo = await this.ctx.service.stage.getDataById(stageId);
  599. const auditList = await this.getAuditors(stageId, stageInfo.times);
  600. // const smsUser1 = await this.ctx.service.projectAccount.getDataById(stageInfo.user_id);
  601. // if (smsUser1.auth_mobile !== '' && smsUser1.auth_mobile !== undefined && smsUser1.sms_type !== '' && smsUser1.sms_type !== null) {
  602. // const smsType = JSON.parse(smsUser1.sms_type);
  603. // if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  604. // mobile_array.push(smsUser1.auth_mobile);
  605. // }
  606. // }
  607. // for (const user of auditList) {
  608. // const smsUser = await this.ctx.service.projectAccount.getDataById(user.aid);
  609. // if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
  610. // const smsType = JSON.parse(smsUser.sms_type);
  611. // if (mobile_array.indexOf(smsUser.auth_mobile) === -1 && smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  612. // mobile_array.push(smsUser.auth_mobile);
  613. // }
  614. // }
  615. // }
  616. // if (mobile_array.length > 0) {
  617. // const tenderInfo = await this.ctx.service.tender.getDataById(stageInfo.tid);
  618. // const sms = new SMS(this.ctx);
  619. // const tenderName = await sms.contentChange(tenderInfo.name);
  620. // const projectName = await sms.contentChange(this.ctx.tender.info.deal_info.buildName);
  621. // const ptmsg = projectName !== '' ? '项目「' + projectName + '」标段「' + tenderName + '」' : tenderName;
  622. // const content = '【纵横计量支付】' + ptmsg + '第' + stageInfo.order + '期,审批退回。';
  623. // sms.send(mobile_array, content);
  624. // }
  625. const users = this._.uniq(this._.concat(this._.map(auditList, 'aid'), stageInfo.user_id));
  626. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.JL, smsTypeConst.judge.result.toString(), SmsAliConst.template.stage_result, {
  627. qi: stageInfo.order,
  628. status: SmsAliConst.status.back,
  629. });
  630. // 微信模板通知
  631. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/stage/' + stageInfo.order);
  632. const wechatData = {
  633. wap_url: shenpiUrl,
  634. qi: stageInfo.order,
  635. status: wxConst.status.back,
  636. tips: wxConst.tips.back,
  637. code: this.ctx.session.sessionProject.code,
  638. };
  639. await this.ctx.helper.sendWechat(users, smsTypeConst.const.JL, smsTypeConst.judge.result.toString(), wxConst.template.stage, wechatData);
  640. await transaction.commit();
  641. } catch (err) {
  642. await transaction.rollback();
  643. throw err;
  644. }
  645. }
  646. async _checkNoPre(pid, stageId, checkData, times) {
  647. const time = new Date();
  648. // 整理当前流程审核人状态更新
  649. const audit = await this.getDataByCondition({ sid: stageId, times, status: auditConst.status.checking });
  650. if (!audit || audit.order <= 1) {
  651. throw '审核数据错误';
  652. }
  653. // 添加重新审批后,不能用order-1,取groupby值里的上一个才对
  654. // const preAuditor = await this.getDataByCondition({sid: stageId, times: times, order: audit.order - 1});
  655. const auditors2 = await this.getAuditGroupByList(stageId, times);
  656. const auditorIndex = await auditors2.findIndex(function(item) {
  657. return item.aid === audit.aid;
  658. });
  659. const preAuditor = auditors2[auditorIndex - 1];
  660. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  661. const transaction = await this.db.beginTransaction();
  662. try {
  663. await this._updateTender(transaction);
  664. // 添加推送
  665. const noticeContent = await this.getNoticeContent(pid, audit.tid, stageId, audit.aid, checkData.opinion);
  666. const records = [
  667. {
  668. pid,
  669. type: pushType.stage,
  670. uid: this.ctx.stage.user_id,
  671. status: auditConst.status.checkNoPre,
  672. content: noticeContent,
  673. },
  674. ];
  675. auditors2.forEach(audit => {
  676. records.push({
  677. pid,
  678. type: pushType.stage,
  679. uid: audit.aid,
  680. status: auditConst.status.checkNoPre,
  681. content: noticeContent,
  682. });
  683. });
  684. await transaction.insert('zh_notice', records);
  685. // 计算并合同支付最终数据
  686. const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  687. this.ctx.stage.tp_history.push({
  688. times,
  689. order: audit.order,
  690. contract_tp: tpData.contract_tp,
  691. qc_tp: tpData.qc_tp,
  692. yf_tp: yfPay.tp,
  693. sf_tp: sfPay.tp,
  694. });
  695. // 同步 期信息
  696. await transaction.update(this.ctx.service.stage.tableName, {
  697. id: stageId,
  698. contract_tp: tpData.contract_tp,
  699. qc_tp: tpData.qc_tp,
  700. times,
  701. yf_tp: yfPay.tp,
  702. sf_tp: sfPay.tp,
  703. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  704. cache_time_r: this.ctx.stage.cache_time_l,
  705. });
  706. await transaction.update(this.tableName, {
  707. id: audit.id,
  708. status: checkData.checkType,
  709. opinion: checkData.opinion,
  710. end_time: time,
  711. });
  712. // 顺移气候审核人流程顺序
  713. this.initSqlBuilder();
  714. this.sqlBuilder.setAndWhere('sid', { value: this.ctx.stage.id, operate: '=' });
  715. this.sqlBuilder.setAndWhere('order', { value: audit.order, operate: '>' });
  716. this.sqlBuilder.setUpdateData('order', { value: 2, selfOperate: '+' });
  717. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  718. const data = await transaction.query(sql, sqlParam);
  719. // 上一审批人,当前审批人 再次添加至流程
  720. const newAuditors = [];
  721. newAuditors.push({
  722. tid: audit.tid,
  723. sid: audit.sid,
  724. aid: preAuditor.aid,
  725. times: audit.times,
  726. order: audit.order + 1,
  727. status: auditConst.status.checking,
  728. begin_time: time,
  729. });
  730. newAuditors.push({
  731. tid: audit.tid,
  732. sid: audit.sid,
  733. aid: audit.aid,
  734. times: audit.times,
  735. order: audit.order + 2,
  736. status: auditConst.status.uncheck,
  737. });
  738. await transaction.insert(this.tableName, newAuditors);
  739. // 计算该审批人最终数据
  740. await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  741. // 复制一份最新数据给下一人
  742. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, audit.order + 1, transaction);
  743. await this.ctx.service.stageJgcl.updateHistory(this.ctx.stage, transaction);
  744. await this.ctx.service.stageBonus.updateHistory(this.ctx.stage, transaction);
  745. await this.ctx.service.stageOther.updateHistory(this.ctx.stage, transaction);
  746. await this.ctx.service.stageSafeProd.updateHistory(this.ctx.stage, transaction);
  747. await this.ctx.service.stageTempLand.updateHistory(this.ctx.stage, transaction);
  748. // 多人协同数据确认删除本人和上一个审批人确认状态
  749. await this.ctx.service.cooperationConfirm.delBycheckNoPre(audit.aid, this.ctx.stage, transaction);
  750. await this.ctx.service.cooperationConfirm.delBycheckNoPre(preAuditor.aid, this.ctx.stage, transaction);
  751. // 同步 期信息
  752. await transaction.update(this.ctx.service.stage.tableName, {
  753. id: stageId,
  754. status: checkData.checkType,
  755. cache_time_r: this.ctx.stage.cache_time_l,
  756. });
  757. // 添加短信通知-需要审批提醒功能
  758. // const smsUser = await this.ctx.service.projectAccount.getDataById(preAuditor.aid);
  759. // if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
  760. // const smsType = JSON.parse(smsUser.sms_type);
  761. // if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  762. // const tenderInfo = await this.ctx.service.tender.getDataById(audit.tid);
  763. // const stageInfo = await this.ctx.service.stage.getDataById(audit.sid);
  764. // const sms = new SMS(this.ctx);
  765. // const tenderName = await sms.contentChange(tenderInfo.name);
  766. // const projectName = await sms.contentChange(this.ctx.tender.info.deal_info.buildName);
  767. // const ptmsg = projectName !== '' ? '项目「' + projectName + '」标段「' + tenderName + '」' : tenderName;
  768. // const result = await this.ctx.helper.urlToShort('http://' + this.ctx.request.header.host + '/wap/tender/' + this.ctx.tender.id + '/stage/' + stageInfo.order);
  769. // // const result = '';
  770. // const content = '【纵横计量支付】' + ptmsg + '第' + stageInfo.order + '期,需要您审批。' + result;
  771. // sms.send(smsUser.auth_mobile, content);
  772. // }
  773. // }
  774. const stageInfo = await this.ctx.service.stage.getDataById(audit.sid);
  775. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/stage/' + stageInfo.order);
  776. await this.ctx.helper.sendAliSms(preAuditor.aid, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), SmsAliConst.template.stage_check, {
  777. qi: stageInfo.order,
  778. code: shenpiUrl,
  779. });
  780. // 微信模板通知
  781. const wechatData = {
  782. wap_url: shenpiUrl,
  783. qi: stageInfo.order,
  784. status: wxConst.status.check,
  785. tips: wxConst.tips.check,
  786. code: this.ctx.session.sessionProject.code,
  787. };
  788. await this.ctx.helper.sendWechat(preAuditor.aid, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), wxConst.template.stage, wechatData);
  789. await transaction.commit();
  790. } catch (err) {
  791. await transaction.rollback();
  792. throw err;
  793. }
  794. }
  795. /**
  796. * 审批
  797. * @param {Number} stageId - 标段id
  798. * @param {auditConst.status.checked|auditConst.status.checkNo} checkType - 审批结果
  799. * @param {Number} times - 第几次审批
  800. * @return {Promise<void>}
  801. */
  802. async check(stageId, checkData, times = 1) {
  803. if (checkData.checkType !== auditConst.status.checked && checkData.checkType !== auditConst.status.checkNo && checkData.checkType !== auditConst.status.checkNoPre) {
  804. throw '提交数据错误';
  805. }
  806. // // 整理当前流程审核人状态更新
  807. // const audit = await this.getDataByCondition({sid: stageId, times: times, status: auditConst.status.checking});
  808. // if (!audit) {
  809. // throw '审核数据错误';
  810. // }
  811. // const time = new Date();
  812. const pid = this.ctx.session.sessionProject.id;
  813. switch (checkData.checkType) {
  814. case auditConst.status.checked:
  815. await this._checked(pid, stageId, checkData, times);
  816. break;
  817. case auditConst.status.checkNo:
  818. await this._checkNo(pid, stageId, checkData, times);
  819. break;
  820. case auditConst.status.checkNoPre:
  821. await this._checkNoPre(pid, stageId, checkData, times);
  822. break;
  823. default:
  824. throw '无效审批操作';
  825. }
  826. // 通知发送 - 第三方更新
  827. if (this.ctx.session.sessionProject.custom && syncApiConst.notice_type.indexOf(this.ctx.session.sessionProject.customType) !== -1) {
  828. const base_data = {
  829. tid: this.ctx.tender.id,
  830. sid: stageId,
  831. op: 'update',
  832. };
  833. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  834. base_data.op = 'update';
  835. base_data.sid = -1;
  836. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  837. }
  838. }
  839. /**
  840. * 审批
  841. * @param {Number} stageId - 标段id
  842. * @param {Number} times - 第几次审批
  843. * @return {Promise<void>}
  844. */
  845. async checkAgain(stageId, times = 1) {
  846. const time = new Date();
  847. // 整理当前流程审核人状态更新
  848. const audit = (
  849. await this.getAllDataByCondition({
  850. where: { sid: stageId, times },
  851. orders: [['order', 'desc']],
  852. limit: 1,
  853. offset: 0,
  854. })
  855. )[0];
  856. if (!audit || audit.order < 1) {
  857. throw '审核数据错误';
  858. }
  859. const transaction = await this.db.beginTransaction();
  860. try {
  861. // 当前审批人2次添加至流程中
  862. const newAuditors = [];
  863. newAuditors.push({
  864. tid: audit.tid,
  865. sid: audit.sid,
  866. aid: audit.aid,
  867. times: audit.times,
  868. order: audit.order + 1,
  869. status: auditConst.status.checkAgain,
  870. begin_time: time,
  871. end_time: time,
  872. opinion: '',
  873. });
  874. newAuditors.push({
  875. tid: audit.tid,
  876. sid: audit.sid,
  877. aid: audit.aid,
  878. times: audit.times,
  879. order: audit.order + 2,
  880. status: auditConst.status.checking,
  881. begin_time: time,
  882. });
  883. await transaction.insert(this.tableName, newAuditors);
  884. // 复制一份最新数据给下一人
  885. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, audit.order + 1, transaction);
  886. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, audit.order + 2, transaction);
  887. await this.ctx.service.stageJgcl.updateHistory4CheckAgain(this.ctx.stage, transaction);
  888. await this.ctx.service.stageBonus.updateHistory4CheckAgain(this.ctx.stage, transaction);
  889. await this.ctx.service.stageOther.updateHistory4CheckAgain(this.ctx.stage, transaction);
  890. await this.ctx.service.stageSafeProd.updateHistory4CheckAgain(this.ctx.stage, transaction);
  891. await this.ctx.service.stageTempLand.updateHistory4CheckAgain(this.ctx.stage, transaction);
  892. // 本期结束
  893. // 生成截止本期数据 final数据
  894. await this.ctx.service.stageBillsFinal.delGenerateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  895. await this.ctx.service.stagePosFinal.delGenerateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  896. await this.ctx.service.stageChangeFinal.delGenerateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  897. // 同步 期信息
  898. const his = this.ctx.stage.tp_history.find(x => { return x.times === times && x.order === audit.order });
  899. this.ctx.stage.tp_history.push({
  900. times,
  901. order: audit.order + 1,
  902. contract_tp: his.contract_tp,
  903. qc_tp: his.qc_tp,
  904. yf_tp: his.yf_tp,
  905. sf_tp: his.sf_tp,
  906. });
  907. await transaction.update(this.ctx.service.stage.tableName, {
  908. id: stageId,
  909. status: auditConst.status.checking,
  910. cache_time_r: this.ctx.stage.cache_time_l,
  911. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  912. });
  913. // 添加短信通知-需要审批提醒功能
  914. // const smsUser = await this.ctx.service.projectAccount.getDataById(audit.aid);
  915. // if (smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
  916. // const smsType = JSON.parse(smsUser.sms_type);
  917. // if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  918. // const tenderInfo = await this.ctx.service.tender.getDataById(audit.tid);
  919. // const stageInfo = await this.ctx.service.stage.getDataById(audit.sid);
  920. // const sms = new SMS(this.ctx);
  921. // const tenderName = await sms.contentChange(tenderInfo.name);
  922. // const projectName = await sms.contentChange(this.ctx.tender.info.deal_info.buildName);
  923. // const ptmsg = projectName !== '' ? '项目「' + projectName + '」标段「' + tenderName + '」' : tenderName;
  924. // const result = await this.ctx.helper.urlToShort('http://' + this.ctx.request.header.host + '/wap/tender/' + this.ctx.tender.id + '/stage/' + stageInfo.order);
  925. // const content = '【纵横计量支付】' + ptmsg + '第' + stageInfo.order + '期,需要您审批。' + result;
  926. // sms.send(smsUser.auth_mobile, content);
  927. // }
  928. // }
  929. const stageInfo = await this.ctx.service.stage.getDataById(audit.sid);
  930. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/stage/' + stageInfo.order);
  931. await this.ctx.helper.sendAliSms(audit.aid, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), SmsAliConst.template.stage_check, {
  932. qi: stageInfo.order,
  933. code: shenpiUrl,
  934. });
  935. // 微信模板通知
  936. const wechatData = {
  937. wap_url: shenpiUrl,
  938. qi: stageInfo.order,
  939. status: wxConst.status.check,
  940. tips: wxConst.tips.check,
  941. code: this.ctx.session.sessionProject.code,
  942. };
  943. await this.ctx.helper.sendWechat(audit.aid, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), wxConst.template.stage, wechatData);
  944. await transaction.commit();
  945. // 通知发送 - 第三方更新
  946. if (this.ctx.session.sessionProject.custom && syncApiConst.notice_type.indexOf(this.ctx.session.sessionProject.customType) !== -1) {
  947. const base_data = {
  948. tid: this.ctx.tender.id,
  949. sid: stageId,
  950. op: 'update',
  951. };
  952. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  953. base_data.op = 'update';
  954. base_data.sid = -1;
  955. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  956. }
  957. } catch (err) {
  958. await transaction.rollback();
  959. throw err;
  960. }
  961. }
  962. /**
  963. * 获取审核人需要审核的期列表
  964. *
  965. * @param auditorId
  966. * @return {Promise<*>}
  967. */
  968. async getAuditStage(auditorId) {
  969. const sql =
  970. 'SELECT sa.`aid`, sa.`times`, sa.`order`, sa.`begin_time`, sa.`end_time`, sa.`tid`, sa.`sid`,' +
  971. ' s.`order` As `sorder`, s.`status` As `sstatus`,' +
  972. ' t.`name`, t.`project_id`, t.`type`, t.`user_id` ' +
  973. ' FROM ?? AS sa ' +
  974. ' Left Join ?? AS s On sa.`sid` = s.`id` ' +
  975. ' Left Join ?? As t ON sa.`tid` = t.`id`' +
  976. ' WHERE ((sa.`aid` = ? and sa.`status` = ?) OR (s.`user_id` = ? and sa.`status` = ? and s.`status` = ? and sa.`times` = (s.`times`-1)))' +
  977. ' ORDER BY sa.`begin_time` DESC';
  978. const sqlParam = [
  979. this.tableName,
  980. this.ctx.service.stage.tableName,
  981. this.ctx.service.tender.tableName,
  982. auditorId,
  983. auditConst.status.checking,
  984. auditorId,
  985. auditConst.status.checkNo,
  986. auditConst.status.checkNo,
  987. ];
  988. return await this.db.query(sql, sqlParam);
  989. }
  990. /**
  991. * 获取审核人审核的次数
  992. *
  993. * @param auditorId
  994. * @return {Promise<*>}
  995. */
  996. async getCountByChecked(auditorId) {
  997. return await this.db.count(this.tableName, { aid: auditorId, status: [auditConst.status.checked, auditConst.status.checkNo, auditConst.status.checkNoPre] });
  998. }
  999. /**
  1000. * 获取最近一次审批结束时间
  1001. *
  1002. * @param auditorId
  1003. * @return {Promise<*>}
  1004. */
  1005. async getLastEndTimeByChecked(auditorId) {
  1006. const sql = 'SELECT `end_time` FROM ?? WHERE `aid` = ? ' +
  1007. 'AND `status` in (' + this.ctx.helper.getInArrStrSqlFilter([auditConst.status.checked, auditConst.status.checkNo, auditConst.status.checkNoPre]) + ') ORDER BY `end_time` DESC';
  1008. const sqlParam = [this.tableName, auditorId];
  1009. const result = await this.db.queryOne(sql, sqlParam);
  1010. return result ? result.end_time : null;
  1011. }
  1012. /**
  1013. * 获取 某时间后 审批进度 更新的期
  1014. * @param {Number} pid - 查询标段
  1015. * @param {Number} uid - 查询人
  1016. * @param {Date} time - 查询时间
  1017. * @return {Promise<*>}
  1018. */
  1019. async getNoticeStage(pid, uid, time) {
  1020. let notice = await this.db.select('zh_notice', {
  1021. where: { pid, type: pushType.stage, uid },
  1022. orders: [['create_time', 'desc']],
  1023. limit: 10,
  1024. offset: 0,
  1025. });
  1026. notice = notice.map(v => {
  1027. const extra = JSON.parse(v.content);
  1028. delete v.content;
  1029. return { ...v, ...extra };
  1030. });
  1031. return notice;
  1032. }
  1033. /**
  1034. * 用于添加推送所需的content内容
  1035. * @param {Number} pid 项目id
  1036. * @param {Number} tid 台账id
  1037. * @param {Number} sid 期id
  1038. * @param {Number} uid 审核人id
  1039. */
  1040. async getNoticeContent(pid, tid, sid, uid, opinion = '') {
  1041. const noticeSql =
  1042. 'SELECT * FROM (SELECT ' +
  1043. ' t.`id` As `tid`, t.`name`, s.`order`, pa.`name` As `su_name`, pa.role As `su_role`' +
  1044. ' FROM (SELECT * FROM ?? WHERE `id` = ? ) As t' +
  1045. ' LEFT JOIN ?? As s On s.`id` = ?' +
  1046. ' LEFT JOIN ?? As pa ON pa.`id` = ?' +
  1047. ' WHERE t.`project_id` = ? ) as new_t GROUP BY new_t.`tid`';
  1048. const noticeSqlParam = [this.ctx.service.tender.tableName, tid, this.ctx.service.stage.tableName, sid, this.ctx.service.projectAccount.tableName, uid, pid];
  1049. const content = await this.db.query(noticeSql, noticeSqlParam);
  1050. if (content.length) {
  1051. content[0].opinion = opinion;
  1052. }
  1053. return content.length ? JSON.stringify(content[0]) : '';
  1054. }
  1055. /**
  1056. * 获取审核人流程列表
  1057. *
  1058. * @param auditorId
  1059. * @return {Promise<*>}
  1060. */
  1061. async getAuditGroupByList(stageId, times) {
  1062. const sql =
  1063. 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`aid`, la.`order` ' +
  1064. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  1065. ' WHERE la.`sid` = ? and la.`times` = ? GROUP BY la.`aid` ORDER BY la.`order`';
  1066. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, times];
  1067. return await this.db.query(sql, sqlParam);
  1068. }
  1069. /**
  1070. * 获取审核人流程列表
  1071. *
  1072. * @param auditorId
  1073. * @return {Promise<*>}
  1074. */
  1075. async getAuditGroupByListWithOwner(stageId, times) {
  1076. const result = await this.getAuditGroupByList(stageId, times);
  1077. const sql =
  1078. 'SELECT pa.`id` As aid, pa.`name`, pa.`company`, pa.`role`, ? As times, ? As sid, 0 As `order`' +
  1079. ' FROM ' + this.ctx.service.stage.tableName + ' As s' +
  1080. ' LEFT JOIN ' + this.ctx.service.projectAccount.tableName + ' As pa' +
  1081. ' ON s.user_id = pa.id' +
  1082. ' WHERE s.id = ?';
  1083. const sqlParam = [times, stageId, stageId];
  1084. const user = await this.db.queryOne(sql, sqlParam);
  1085. result.unshift(user);
  1086. return result;
  1087. }
  1088. /**
  1089. * 复制上一期的审批人列表给最新一期
  1090. *
  1091. * @param transaction - 新增一期的事务
  1092. * @param {Object} preStage - 上一期
  1093. * @param {Object} newStage - 最新一期
  1094. * @return {Promise<*>}
  1095. */
  1096. async copyPreStageAuditors(transaction, preStage, newStage) {
  1097. const auditors = await this.getAuditGroupByList(preStage.id, preStage.times);
  1098. const newAuditors = [];
  1099. for (const a of auditors) {
  1100. const na = {
  1101. tid: preStage.tid,
  1102. sid: newStage.id,
  1103. aid: a.aid,
  1104. times: newStage.times,
  1105. order: newAuditors.length + 1,
  1106. status: auditConst.status.uncheck,
  1107. };
  1108. newAuditors.push(na);
  1109. }
  1110. const result = await transaction.insert(this.tableName, newAuditors);
  1111. return (result.effectRows = auditors.length);
  1112. }
  1113. /**
  1114. * 移除审核人
  1115. *
  1116. * @param {Number} stageId - 期id
  1117. * @param {Number} status - 期状态
  1118. * @param {Number} status - 期次数
  1119. * @return {Promise<boolean>}
  1120. */
  1121. async getAuditorByStatus(stageId, status, times = 1) {
  1122. let auditor = null;
  1123. let sql = '';
  1124. let sqlParam = '';
  1125. switch (status) {
  1126. case auditConst.status.checking:
  1127. case auditConst.status.checked:
  1128. case auditConst.status.checkNoPre:
  1129. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`order` ' +
  1130. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  1131. ' WHERE la.`sid` = ? and la.`status` = ? order by la.`times` desc, la.`order` desc';
  1132. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, status];
  1133. auditor = await this.db.queryOne(sql, sqlParam);
  1134. break;
  1135. case auditConst.status.checkNo:
  1136. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`order` ' +
  1137. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  1138. ' WHERE la.`sid` = ? and la.`status` = ? and la.`times` = ? order by la.`times` desc, la.`order` desc';
  1139. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, auditConst.status.checkNo, parseInt(times) - 1];
  1140. auditor = await this.db.queryOne(sql, sqlParam);
  1141. break;
  1142. case auditConst.status.uncheck:
  1143. default:
  1144. break;
  1145. }
  1146. return auditor;
  1147. }
  1148. /**
  1149. * 取某一期已批准审核信息(报表用)
  1150. *
  1151. * @param {Number} stageId - 期id
  1152. * @param {Number} times - 期次数
  1153. * @return {Promise<boolean>}
  1154. */
  1155. async getStageAudit(stageId, times = 1) {
  1156. 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';
  1157. const sqlParam = [this.tableName, stageId, times];
  1158. const rst = await this.db.query(sql, sqlParam);
  1159. return rst;
  1160. }
  1161. /**
  1162. * 取待审批期列表(wap用)
  1163. *
  1164. * @param auditorId
  1165. * @return {Promise<*>}
  1166. */
  1167. async getAuditStageByWap(auditorId) {
  1168. const sql =
  1169. 'SELECT sa.`aid`, sa.`times`, sa.`begin_time`, sa.`end_time`, sa.`tid`, sa.`sid`,' +
  1170. // ' 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`, ' +
  1171. ' s.*,' +
  1172. ' t.`name`, t.`project_id`, t.`type`, t.`user_id`,' +
  1173. ' ti.`deal_info` ' +
  1174. ' FROM ?? AS sa' +
  1175. ' Left Join ?? AS s On sa.`sid` = s.`id`' +
  1176. ' Left Join ?? As t On sa.`tid` = t.`id`' +
  1177. ' Left Join ?? AS ti ON ti.`tid` = t.`id`' +
  1178. ' WHERE sa.`aid` = ? and sa.`status` = ?';
  1179. const sqlParam = [
  1180. this.tableName,
  1181. this.ctx.service.stage.tableName,
  1182. this.ctx.service.tender.tableName,
  1183. this.ctx.service.tenderInfo.tableName,
  1184. auditorId,
  1185. auditConst.status.checking,
  1186. ];
  1187. return await this.db.query(sql, sqlParam);
  1188. }
  1189. /**
  1190. * 删除 某期 某次 全审批流程
  1191. * 私有,不做判断,不补全最新一轮审批人数据,不计算缓存
  1192. * @param {Number} sid - 标段id
  1193. * @param {Number} times - 第几次审批
  1194. * @param transaction - 删除事务
  1195. * @return {Promise<void>}
  1196. */
  1197. async _timesDelete(sid, times, transaction) {
  1198. // 审批流程
  1199. await transaction.delete(this.tableName, { sid, times });
  1200. await transaction.delete(this.ctx.service.pos.tableName, { add_stage: sid, add_times: times });
  1201. await transaction.delete(this.ctx.service.stageBills.tableName, { sid, times });
  1202. await transaction.delete(this.ctx.service.stagePos.tableName, { sid, times });
  1203. await transaction.delete(this.ctx.service.stageDetail.tableName, { sid, times });
  1204. await transaction.delete(this.ctx.service.stageChange.tableName, { sid, stimes: times });
  1205. await transaction.delete(this.ctx.service.stagePay.tableName, { sid, stimes: times });
  1206. await transaction.delete(this.ctx.service.pay.tableName, { csid: sid, cstimes: times });
  1207. // 其他台账
  1208. await this.ctx.service.stageJgcl.deleteStageTimesData(sid, times, transaction);
  1209. await this.ctx.service.stageOther.deleteStageTimesData(sid, times, transaction);
  1210. await this.ctx.service.stageBonus.deleteStageTimesData(sid, times, transaction);
  1211. await this.ctx.service.stageSafeProd.deleteStageTimesData(sid, times, transaction);
  1212. await this.ctx.service.stageTempLand.deleteStageTimesData(sid, times, transaction);
  1213. }
  1214. /**
  1215. * 删除本次审批流程
  1216. * @param {Number} stageId - 标段id
  1217. * @param {Number} times - 第几次审批
  1218. * @return {Promise<void>}
  1219. */
  1220. async timesDelete() {
  1221. const transaction = await this.db.beginTransaction();
  1222. try {
  1223. // 删除最新一次数据
  1224. await this._timesDelete(this.ctx.stage.id, this.ctx.stage.times, transaction);
  1225. // 审批退回,未重新上报时,需删除最新两次数据
  1226. const isCheckNo = this.ctx.stage.status === auditConst.status.checkNo;
  1227. const nowTimes = isCheckNo ? this.ctx.stage.times - 1 : this.ctx.stage.times;
  1228. if (isCheckNo) {
  1229. await this._timesDelete(this.ctx.stage.id, nowTimes, transaction);
  1230. }
  1231. // 添加上一次审批人
  1232. const sql = 'SELECT `tid`, `sid`, `aid`, `order` FROM ?? WHERE `sid` = ? and `times` = ? GROUP BY `aid` ORDER BY `id` ASC';
  1233. const sqlParam = [this.tableName, this.ctx.stage.id, nowTimes - 1];
  1234. const auditors = await this.db.query(sql, sqlParam);
  1235. let order = 1;
  1236. for (const a of auditors) {
  1237. a.times = nowTimes;
  1238. a.order = order;
  1239. a.status = auditConst.status.uncheck;
  1240. order++;
  1241. }
  1242. await this._updateTender(transaction);
  1243. // 拷贝新一次审核流程列表
  1244. await transaction.insert(this.tableName, auditors);
  1245. // 计算缓存
  1246. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  1247. // 计算并合同支付最终数据
  1248. const lastAudit = await this.getDataByCondition({
  1249. sid: this.ctx.stage.id,
  1250. times: nowTimes - 1,
  1251. status: auditConst.status.checkNo,
  1252. });
  1253. if (!lastAudit) throw '审批数据错误';
  1254. await this.ctx.service.stagePay.copyStagePays4DeleteTimes(this.ctx.stage, nowTimes, 0, lastAudit.times, lastAudit.order, transaction);
  1255. const stagePay = await this.ctx.service.stagePay.getAuditorStageData(this.ctx.stage.id, lastAudit.times, lastAudit.order);
  1256. const yfPay = stagePay.find(function(x) {
  1257. return x.ptype === payConst.payType.yf;
  1258. });
  1259. const sfPay = stagePay.find(function(x) {
  1260. return x.ptype === payConst.payType.sf;
  1261. });
  1262. // 同步 期信息
  1263. const time = new Date();
  1264. await transaction.update(this.ctx.service.stage.tableName, {
  1265. id: this.ctx.stage.id,
  1266. status: auditConst.status.checkNo,
  1267. contract_tp: tpData.contract_tp,
  1268. qc_tp: tpData.qc_tp,
  1269. times: nowTimes,
  1270. yf_tp: yfPay ? yfPay.tp : null,
  1271. sf_tp: sfPay ? sfPay.tp : null,
  1272. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  1273. cache_time_l: time,
  1274. cache_time_r: time,
  1275. });
  1276. await transaction.commit();
  1277. // 通知发送 - 第三方更新
  1278. if (this.ctx.session.sessionProject.custom && syncApiConst.notice_type.indexOf(this.ctx.session.sessionProject.customType) !== -1) {
  1279. const base_data = {
  1280. tid: this.ctx.tender.id,
  1281. sid: this.ctx.stage.id,
  1282. op: 'update',
  1283. };
  1284. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  1285. base_data.op = 'update';
  1286. base_data.sid = -1;
  1287. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  1288. }
  1289. } catch (err) {
  1290. await transaction.rollback();
  1291. throw err;
  1292. }
  1293. }
  1294. // 固定审批流-更新
  1295. async updateNewAuditList(stage, newIdList) {
  1296. const transaction = await this.db.beginTransaction();
  1297. try {
  1298. // 先删除旧的审批流,再添加新的
  1299. await transaction.delete(this.tableName, { sid: stage.id, times: stage.times });
  1300. const newAuditors = [];
  1301. let order = 1;
  1302. for (const aid of newIdList) {
  1303. newAuditors.push({
  1304. tid: stage.tid, sid: stage.id, aid,
  1305. times: stage.times, order, status: auditConst.status.uncheck,
  1306. });
  1307. order++;
  1308. }
  1309. if(newAuditors.length > 0) await transaction.insert(this.tableName, newAuditors);
  1310. await transaction.commit();
  1311. } catch (err) {
  1312. await transaction.rollback();
  1313. throw err;
  1314. }
  1315. }
  1316. // 固定终审-更新
  1317. async updateLastAudit(stage, auditList, lastId) {
  1318. const transaction = await this.db.beginTransaction();
  1319. try {
  1320. // 先判断auditList里的aid是否与lastId相同,相同则删除并重新更新order
  1321. const idList = this._.map(auditList, 'aid');
  1322. let order = idList.length + 1;
  1323. if (idList.indexOf(lastId) !== -1) {
  1324. await transaction.delete(this.tableName, { sid: stage.id, times: stage.times, aid: lastId });
  1325. const audit = this._.find(auditList, { 'aid': lastId });
  1326. // 顺移之后审核人流程顺序
  1327. await this._syncOrderByDelete(transaction, stage.id, audit.order, stage.times);
  1328. order = order - 1;
  1329. }
  1330. // 添加终审
  1331. const newAuditor = {
  1332. tid: stage.tid, sid: stage.id, aid: lastId,
  1333. times: stage.times, order, status: auditConst.status.uncheck,
  1334. };
  1335. await transaction.insert(this.tableName, newAuditor);
  1336. await transaction.commit();
  1337. } catch (err) {
  1338. await transaction.rollback();
  1339. throw err;
  1340. }
  1341. }
  1342. async getFinalAuditGroup(stageId, times) {
  1343. const sql =
  1344. '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 ' +
  1345. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id`' +
  1346. ' WHERE la.`sid` = ? and la.`times` = ? GROUP BY la.`aid` ORDER BY la.`order`';
  1347. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, times];
  1348. const result = await this.db.query(sql, sqlParam);
  1349. for (const r of result) {
  1350. const auditor = await this.getDataByCondition({sid: stageId, times: r.times, order: r.max_order});
  1351. r.status = auditor.status;
  1352. r.opinion = auditor.opinion;
  1353. r.begin_time = auditor.begin_time;
  1354. r.end_time = auditor.end_time;
  1355. }
  1356. return result;
  1357. }
  1358. async getNumByMonth(tid, startMonth, endMonth) {
  1359. 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 ?';
  1360. const sqlParam = [this.tableName, this.tableName, tid, auditConst.status.checked, startMonth, endMonth];
  1361. const result = await this.db.queryOne(sql, sqlParam);
  1362. return result ? result.num : 0;
  1363. }
  1364. }
  1365. return StageAudit;
  1366. };