stage_audit.js 68 KB

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