stage_audit.js 66 KB

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