stage_audit.js 68 KB

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