stage_audit.js 66 KB

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