stage_audit.js 63 KB

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