stage_audit.js 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163
  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 auditors = await this.getAuditGroupByListWithOwner(stageId, times)
  294. const records = []
  295. auditors.forEach(audit => {
  296. records.push({
  297. pid,
  298. type: pushType.stage,
  299. uid: audit.aid,
  300. status: auditConst.status.checked,
  301. content: noticeContent
  302. })
  303. })
  304. await transaction.insert('zh_notice', records)
  305. await transaction.update(this.tableName, {
  306. id: audit.id,
  307. status: checkData.checkType,
  308. opinion: checkData.opinion,
  309. end_time: time
  310. })
  311. // 计算并合同支付最终数据
  312. const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction)
  313. this.ctx.stage.tp_history.push({
  314. times,
  315. order: audit.order,
  316. contract_tp: tpData.contract_tp,
  317. qc_tp: tpData.qc_tp,
  318. yf_tp: yfPay.tp,
  319. sf_tp: sfPay.tp
  320. })
  321. // 无下一审核人表示,审核结束
  322. if (nextAudit) {
  323. // 复制一份下一审核人数据
  324. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, nextAudit.order, transaction)
  325. await this.ctx.service.stageJgcl.updateHistory(this.ctx.stage, transaction)
  326. await this.ctx.service.stageBonus.updateHistory(this.ctx.stage, transaction)
  327. await this.ctx.service.stageOther.updateHistory(this.ctx.stage, transaction)
  328. // 流程至下一审批人
  329. await transaction.update(this.tableName, {
  330. id: nextAudit.id,
  331. status: auditConst.status.checking,
  332. begin_time: time
  333. })
  334. // 同步 期信息
  335. await transaction.update(this.ctx.service.stage.tableName, {
  336. id: stageId,
  337. status: auditConst.status.checking,
  338. contract_tp: tpData.contract_tp,
  339. qc_tp: tpData.qc_tp,
  340. yf_tp: yfPay.tp,
  341. sf_tp: sfPay.tp,
  342. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  343. cache_time_r: this.ctx.stage.cache_time_l
  344. })
  345. // 添加短信通知-需要审批提醒功能
  346. // const smsUser = await this.ctx.service.projectAccount.getDataById(nextAudit.aid);
  347. // if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
  348. // const smsType = JSON.parse(smsUser.sms_type);
  349. // if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  350. // const tenderInfo = await this.ctx.service.tender.getDataById(nextAudit.tid);
  351. // const stageInfo = await this.ctx.service.stage.getDataById(nextAudit.sid);
  352. // const sms = new SMS(this.ctx);
  353. // const tenderName = await sms.contentChange(tenderInfo.name);
  354. // const projectName = await sms.contentChange(this.ctx.tender.info.deal_info.buildName);
  355. // const result = await this.ctx.helper.urlToShort('http://' + this.ctx.request.header.host + '/wap/tender/' + this.ctx.tender.id + '/stage/' + stageInfo.order);
  356. // // const result = '';
  357. // const ptmsg = projectName !== '' ? '项目「' + projectName + '」标段「' + tenderName + '」' : tenderName;
  358. // const content = '【纵横计量支付】' + ptmsg + '第' + stageInfo.order + '期,需要您审批。' + result;
  359. // sms.send(smsUser.auth_mobile, content);
  360. // }
  361. // }
  362. const stageInfo = await this.ctx.service.stage.getDataById(nextAudit.sid)
  363. const shenpiUrl = await this.ctx.helper.urlToShort('http://' + this.ctx.request.header.host + '/wap/tender/' + this.ctx.tender.id + '/stage/' + stageInfo.order)
  364. await this.ctx.helper.sendAliSms(nextAudit.aid, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), SmsAliConst.template.stage_check, {
  365. qi: stageInfo.order,
  366. code: shenpiUrl
  367. })
  368. } else {
  369. // 本期结束
  370. // 生成截止本期数据 final数据
  371. await this.ctx.service.stageBillsFinal.generateFinalData(transaction, this.ctx.tender, this.ctx.stage)
  372. await this.ctx.service.stagePosFinal.generateFinalData(transaction, this.ctx.tender, this.ctx.stage)
  373. // 同步 期信息
  374. await transaction.update(this.ctx.service.stage.tableName, {
  375. id: stageId,
  376. status: checkData.checkType,
  377. contract_tp: tpData.contract_tp,
  378. qc_tp: tpData.qc_tp,
  379. yf_tp: yfPay.tp,
  380. sf_tp: sfPay.tp,
  381. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  382. cache_time_r: this.ctx.stage.cache_time_l
  383. })
  384. // 添加短信通知-审批通过提醒功能
  385. // const mobile_array = [];
  386. const stageInfo = await this.ctx.service.stage.getDataById(stageId)
  387. const auditList = await this.getAuditors(stageId, stageInfo.times)
  388. // const smsUser1 = await this.ctx.service.projectAccount.getDataById(stageInfo.user_id);
  389. // if (smsUser1.auth_mobile !== undefined && smsUser1.sms_type !== '' && smsUser1.sms_type !== null) {
  390. // const smsType = JSON.parse(smsUser1.sms_type);
  391. // if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  392. // mobile_array.push(smsUser1.auth_mobile);
  393. // }
  394. // }
  395. // for (const user of auditList) {
  396. // const smsUser = await this.ctx.service.projectAccount.getDataById(user.aid);
  397. // if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
  398. // const smsType = JSON.parse(smsUser.sms_type);
  399. // if (mobile_array.indexOf(smsUser.auth_mobile) === -1 && smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  400. // mobile_array.push(smsUser.auth_mobile);
  401. // }
  402. // }
  403. // }
  404. // if (mobile_array.length > 0) {
  405. // const tenderInfo = await this.ctx.service.tender.getDataById(stageInfo.tid);
  406. // const sms = new SMS(this.ctx);
  407. // const tenderName = await sms.contentChange(tenderInfo.name);
  408. // const projectName = await sms.contentChange(this.ctx.tender.info.deal_info.buildName);
  409. // const ptmsg = projectName !== '' ? '项目「' + projectName + '」标段「' + tenderName + '」' : tenderName;
  410. // const content = '【纵横计量支付】' + ptmsg + '第' + stageInfo.order + '期,审批通过。';
  411. // sms.send(mobile_array, content);
  412. // }
  413. const users = this._.pull(this._.map(auditList, 'aid'), stageInfo.user_id)
  414. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.JL, smsTypeConst.judge.result.toString(), SmsAliConst.template.stage_result, {
  415. qi: stageInfo.order,
  416. status: SmsAliConst.status.success
  417. })
  418. }
  419. await transaction.commit()
  420. } catch (err) {
  421. await transaction.rollback()
  422. throw err
  423. }
  424. }
  425. async _checkNo(pid, stageId, checkData, times) {
  426. const time = new Date()
  427. // 整理当前流程审核人状态更新
  428. const audit = await this.getDataByCondition({ sid: stageId, times, status: auditConst.status.checking })
  429. if (!audit) {
  430. throw '审核数据错误'
  431. }
  432. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage)
  433. const sql = 'SELECT `tid`, `sid`, `aid`, `order` FROM ?? WHERE `sid` = ? and `times` = ? GROUP BY `aid` ORDER BY `id` ASC'
  434. const sqlParam = [this.tableName, stageId, times]
  435. const auditors = await this.db.query(sql, sqlParam)
  436. let order = 1
  437. for (const a of auditors) {
  438. a.times = times + 1
  439. a.order = order
  440. a.status = auditConst.status.uncheck
  441. order++
  442. }
  443. const transaction = await this.db.beginTransaction()
  444. try {
  445. // 添加推送
  446. const noticeContent = await this.getNoticeContent(pid, audit.tid, stageId, audit.aid)
  447. const records = [
  448. {
  449. pid,
  450. type: pushType.stage,
  451. uid: this.ctx.stage.user_id,
  452. status: auditConst.status.checkNo,
  453. content: noticeContent
  454. }
  455. ]
  456. auditors.forEach(audit => {
  457. records.push({
  458. pid,
  459. type: pushType.stage,
  460. uid: audit.aid,
  461. status: auditConst.status.checkNo,
  462. content: noticeContent
  463. })
  464. })
  465. await transaction.insert('zh_notice', records)
  466. // 计算并合同支付最终数据
  467. const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction)
  468. this.ctx.stage.tp_history.push({
  469. times,
  470. order: audit.order,
  471. contract_tp: tpData.contract_tp,
  472. qc_tp: tpData.qc_tp,
  473. yf_tp: yfPay.tp,
  474. sf_tp: sfPay.tp
  475. })
  476. await transaction.update(this.tableName, {
  477. id: audit.id,
  478. status: checkData.checkType,
  479. opinion: checkData.opinion,
  480. end_time: time
  481. })
  482. // 同步 期信息
  483. await transaction.update(this.ctx.service.stage.tableName, {
  484. id: stageId,
  485. status: checkData.checkType,
  486. contract_tp: tpData.contract_tp,
  487. qc_tp: tpData.qc_tp,
  488. times: times + 1,
  489. yf_tp: yfPay.tp,
  490. sf_tp: sfPay.tp,
  491. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  492. cache_time_r: this.ctx.stage.cache_time_l
  493. })
  494. // 拷贝新一次审核流程列表
  495. await transaction.insert(this.tableName, auditors)
  496. // 计算该审批人最终数据
  497. await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction)
  498. // 复制一份最新数据给原报
  499. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times + 1, 0, transaction)
  500. await this.ctx.service.stageJgcl.updateHistory(this.ctx.stage, transaction)
  501. await this.ctx.service.stageBonus.updateHistory(this.ctx.stage, transaction)
  502. // 添加短信通知-审批退回提醒功能
  503. // const mobile_array = [];
  504. const stageInfo = await this.ctx.service.stage.getDataById(stageId)
  505. const auditList = await this.getAuditors(stageId, stageInfo.times)
  506. // const smsUser1 = await this.ctx.service.projectAccount.getDataById(stageInfo.user_id);
  507. // if (smsUser1.auth_mobile !== '' && smsUser1.auth_mobile !== undefined && smsUser1.sms_type !== '' && smsUser1.sms_type !== null) {
  508. // const smsType = JSON.parse(smsUser1.sms_type);
  509. // if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  510. // mobile_array.push(smsUser1.auth_mobile);
  511. // }
  512. // }
  513. // for (const user of auditList) {
  514. // const smsUser = await this.ctx.service.projectAccount.getDataById(user.aid);
  515. // if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
  516. // const smsType = JSON.parse(smsUser.sms_type);
  517. // if (mobile_array.indexOf(smsUser.auth_mobile) === -1 && smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  518. // mobile_array.push(smsUser.auth_mobile);
  519. // }
  520. // }
  521. // }
  522. // if (mobile_array.length > 0) {
  523. // const tenderInfo = await this.ctx.service.tender.getDataById(stageInfo.tid);
  524. // const sms = new SMS(this.ctx);
  525. // const tenderName = await sms.contentChange(tenderInfo.name);
  526. // const projectName = await sms.contentChange(this.ctx.tender.info.deal_info.buildName);
  527. // const ptmsg = projectName !== '' ? '项目「' + projectName + '」标段「' + tenderName + '」' : tenderName;
  528. // const content = '【纵横计量支付】' + ptmsg + '第' + stageInfo.order + '期,审批退回。';
  529. // sms.send(mobile_array, content);
  530. // }
  531. const users = this._.pull(this._.map(auditList, 'aid'), stageInfo.user_id)
  532. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.JL, smsTypeConst.judge.result.toString(), SmsAliConst.template.stage_result, {
  533. qi: stageInfo.order,
  534. status: SmsAliConst.status.back
  535. })
  536. await transaction.commit()
  537. } catch (err) {
  538. await transaction.rollback()
  539. throw err
  540. }
  541. }
  542. async _checkNoPre(pid, stageId, checkData, times) {
  543. const time = new Date()
  544. // 整理当前流程审核人状态更新
  545. const audit = await this.getDataByCondition({ sid: stageId, times, status: auditConst.status.checking })
  546. if (!audit || audit.order <= 1) {
  547. throw '审核数据错误'
  548. }
  549. // 添加重新审批后,不能用order-1,取groupby值里的上一个才对
  550. // const preAuditor = await this.getDataByCondition({sid: stageId, times: times, order: audit.order - 1});
  551. const auditors2 = await this.getAuditGroupByList(stageId, times)
  552. const auditorIndex = await auditors2.findIndex(function (item) {
  553. return item.aid === audit.aid
  554. })
  555. const preAuditor = auditors2[auditorIndex - 1]
  556. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage)
  557. const transaction = await this.db.beginTransaction()
  558. try {
  559. // 添加推送
  560. const noticeContent = await this.getNoticeContent(pid, audit.tid, stageId, audit.aid)
  561. const records = [
  562. {
  563. pid,
  564. type: pushType.stage,
  565. uid: this.ctx.stage.user_id,
  566. status: auditConst.status.checkNoPre,
  567. content: noticeContent
  568. }
  569. ]
  570. auditors2.forEach(audit => {
  571. records.push({
  572. pid,
  573. type: pushType.stage,
  574. uid: audit.aid,
  575. status: auditConst.status.checkNoPre,
  576. content: noticeContent
  577. })
  578. })
  579. await transaction.insert('zh_notice', records)
  580. // 计算并合同支付最终数据
  581. const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction)
  582. this.ctx.stage.tp_history.push({
  583. times,
  584. order: audit.order,
  585. contract_tp: tpData.contract_tp,
  586. qc_tp: tpData.qc_tp,
  587. yf_tp: yfPay.tp,
  588. sf_tp: sfPay.tp
  589. })
  590. // 同步 期信息
  591. await transaction.update(this.ctx.service.stage.tableName, {
  592. id: stageId,
  593. contract_tp: tpData.contract_tp,
  594. qc_tp: tpData.qc_tp,
  595. times,
  596. yf_tp: yfPay.tp,
  597. sf_tp: sfPay.tp,
  598. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  599. cache_time_r: this.ctx.stage.cache_time_l
  600. })
  601. await transaction.update(this.tableName, {
  602. id: audit.id,
  603. status: checkData.checkType,
  604. opinion: checkData.opinion,
  605. end_time: time
  606. })
  607. // 顺移气候审核人流程顺序
  608. this.initSqlBuilder()
  609. this.sqlBuilder.setAndWhere('sid', { value: this.ctx.stage.id, operate: '=' })
  610. this.sqlBuilder.setAndWhere('order', { value: audit.order, operate: '>' })
  611. this.sqlBuilder.setUpdateData('order', { value: 2, selfOperate: '+' })
  612. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update')
  613. const data = await transaction.query(sql, sqlParam)
  614. // 上一审批人,当前审批人 再次添加至流程
  615. const newAuditors = []
  616. newAuditors.push({
  617. tid: audit.tid,
  618. sid: audit.sid,
  619. aid: preAuditor.aid,
  620. times: audit.times,
  621. order: audit.order + 1,
  622. status: auditConst.status.checking,
  623. begin_time: time
  624. })
  625. newAuditors.push({
  626. tid: audit.tid,
  627. sid: audit.sid,
  628. aid: audit.aid,
  629. times: audit.times,
  630. order: audit.order + 2,
  631. status: auditConst.status.uncheck
  632. })
  633. await transaction.insert(this.tableName, newAuditors)
  634. // 计算该审批人最终数据
  635. await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction)
  636. // 复制一份最新数据给下一人
  637. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, audit.order + 1, transaction)
  638. await this.ctx.service.stageJgcl.updateHistory(this.ctx.stage, transaction)
  639. await this.ctx.service.stageBonus.updateHistory(this.ctx.stage, transaction)
  640. await this.ctx.service.stageOther.updateHistory(this.ctx.stage, transaction)
  641. // 同步 期信息
  642. await transaction.update(this.ctx.service.stage.tableName, {
  643. id: stageId,
  644. status: checkData.checkType,
  645. cache_time_r: this.ctx.stage.cache_time_l
  646. })
  647. // 添加短信通知-需要审批提醒功能
  648. // const smsUser = await this.ctx.service.projectAccount.getDataById(preAuditor.aid);
  649. // if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
  650. // const smsType = JSON.parse(smsUser.sms_type);
  651. // if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  652. // const tenderInfo = await this.ctx.service.tender.getDataById(audit.tid);
  653. // const stageInfo = await this.ctx.service.stage.getDataById(audit.sid);
  654. // const sms = new SMS(this.ctx);
  655. // const tenderName = await sms.contentChange(tenderInfo.name);
  656. // const projectName = await sms.contentChange(this.ctx.tender.info.deal_info.buildName);
  657. // const ptmsg = projectName !== '' ? '项目「' + projectName + '」标段「' + tenderName + '」' : tenderName;
  658. // const result = await this.ctx.helper.urlToShort('http://' + this.ctx.request.header.host + '/wap/tender/' + this.ctx.tender.id + '/stage/' + stageInfo.order);
  659. // // const result = '';
  660. // const content = '【纵横计量支付】' + ptmsg + '第' + stageInfo.order + '期,需要您审批。' + result;
  661. // sms.send(smsUser.auth_mobile, content);
  662. // }
  663. // }
  664. const stageInfo = await this.ctx.service.stage.getDataById(audit.sid)
  665. const shenpiUrl = await this.ctx.helper.urlToShort('http://' + this.ctx.request.header.host + '/wap/tender/' + this.ctx.tender.id + '/stage/' + stageInfo.order)
  666. await this.ctx.helper.sendAliSms(preAuditor.aid, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), SmsAliConst.template.stage_check, {
  667. qi: stageInfo.order,
  668. code: shenpiUrl
  669. })
  670. await transaction.commit()
  671. } catch (err) {
  672. await transaction.rollback()
  673. throw err
  674. }
  675. }
  676. /**
  677. * 审批
  678. * @param {Number} stageId - 标段id
  679. * @param {auditConst.status.checked|auditConst.status.checkNo} checkType - 审批结果
  680. * @param {Number} times - 第几次审批
  681. * @return {Promise<void>}
  682. */
  683. async check(stageId, checkData, times = 1) {
  684. if (checkData.checkType !== auditConst.status.checked && checkData.checkType !== auditConst.status.checkNo && checkData.checkType !== auditConst.status.checkNoPre) {
  685. throw '提交数据错误'
  686. }
  687. // // 整理当前流程审核人状态更新
  688. // const audit = await this.getDataByCondition({sid: stageId, times: times, status: auditConst.status.checking});
  689. // if (!audit) {
  690. // throw '审核数据错误';
  691. // }
  692. // const time = new Date();
  693. const pid = this.ctx.session.sessionProject.id
  694. switch (checkData.checkType) {
  695. case auditConst.status.checked:
  696. await this._checked(pid, stageId, checkData, times)
  697. break
  698. case auditConst.status.checkNo:
  699. await this._checkNo(pid, stageId, checkData, times)
  700. break
  701. case auditConst.status.checkNoPre:
  702. await this._checkNoPre(pid, stageId, checkData, times)
  703. break
  704. default:
  705. throw '无效审批操作'
  706. }
  707. }
  708. /**
  709. * 审批
  710. * @param {Number} stageId - 标段id
  711. * @param {Number} times - 第几次审批
  712. * @return {Promise<void>}
  713. */
  714. async checkAgain(stageId, times = 1) {
  715. const time = new Date()
  716. // 整理当前流程审核人状态更新
  717. const audit = (
  718. await this.getAllDataByCondition({
  719. where: { sid: stageId, times },
  720. orders: [['order', 'desc']],
  721. limit: 1,
  722. offset: 0
  723. })
  724. )[0]
  725. if (!audit || audit.order < 1) {
  726. throw '审核数据错误'
  727. }
  728. const transaction = await this.db.beginTransaction()
  729. try {
  730. // 当前审批人2次添加至流程中
  731. const newAuditors = []
  732. newAuditors.push({
  733. tid: audit.tid,
  734. sid: audit.sid,
  735. aid: audit.aid,
  736. times: audit.times,
  737. order: audit.order + 1,
  738. status: auditConst.status.checkAgain,
  739. begin_time: time,
  740. end_time: time,
  741. opinion: ''
  742. })
  743. newAuditors.push({
  744. tid: audit.tid,
  745. sid: audit.sid,
  746. aid: audit.aid,
  747. times: audit.times,
  748. order: audit.order + 2,
  749. status: auditConst.status.checking,
  750. begin_time: time
  751. })
  752. await transaction.insert(this.tableName, newAuditors)
  753. // 复制一份最新数据给下一人
  754. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, audit.order + 1, transaction)
  755. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, audit.order + 2, transaction)
  756. await this.ctx.service.stageJgcl.updateHistory(this.ctx.stage, transaction)
  757. await this.ctx.service.stageBonus.updateHistory(this.ctx.stage, transaction)
  758. await this.ctx.service.stageOther.updateHistory(this.ctx.stage, transaction)
  759. // 本期结束
  760. // 生成截止本期数据 final数据
  761. await this.ctx.service.stageBillsFinal.delGenerateFinalData(transaction, this.ctx.tender, this.ctx.stage)
  762. await this.ctx.service.stagePosFinal.delGenerateFinalData(transaction, this.ctx.tender, this.ctx.stage)
  763. // 同步 期信息
  764. await transaction.update(this.ctx.service.stage.tableName, {
  765. id: stageId,
  766. status: auditConst.status.checking,
  767. cache_time_r: this.ctx.stage.cache_time_l
  768. })
  769. // 添加短信通知-需要审批提醒功能
  770. // const smsUser = await this.ctx.service.projectAccount.getDataById(audit.aid);
  771. // if (smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
  772. // const smsType = JSON.parse(smsUser.sms_type);
  773. // if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  774. // const tenderInfo = await this.ctx.service.tender.getDataById(audit.tid);
  775. // const stageInfo = await this.ctx.service.stage.getDataById(audit.sid);
  776. // const sms = new SMS(this.ctx);
  777. // const tenderName = await sms.contentChange(tenderInfo.name);
  778. // const projectName = await sms.contentChange(this.ctx.tender.info.deal_info.buildName);
  779. // const ptmsg = projectName !== '' ? '项目「' + projectName + '」标段「' + tenderName + '」' : tenderName;
  780. // const result = await this.ctx.helper.urlToShort('http://' + this.ctx.request.header.host + '/wap/tender/' + this.ctx.tender.id + '/stage/' + stageInfo.order);
  781. // const content = '【纵横计量支付】' + ptmsg + '第' + stageInfo.order + '期,需要您审批。' + result;
  782. // sms.send(smsUser.auth_mobile, content);
  783. // }
  784. // }
  785. const stageInfo = await this.ctx.service.stage.getDataById(audit.sid)
  786. const shenpiUrl = await this.ctx.helper.urlToShort('http://' + this.ctx.request.header.host + '/wap/tender/' + this.ctx.tender.id + '/stage/' + stageInfo.order)
  787. await this.ctx.helper.sendAliSms(audit.aid, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), SmsAliConst.template.stage_check, {
  788. qi: stageInfo.order,
  789. code: shenpiUrl
  790. })
  791. await transaction.commit()
  792. } catch (err) {
  793. await transaction.rollback()
  794. throw err
  795. }
  796. }
  797. /**
  798. * 获取审核人需要审核的期列表
  799. *
  800. * @param auditorId
  801. * @return {Promise<*>}
  802. */
  803. async getAuditStage(auditorId) {
  804. const sql =
  805. 'SELECT sa.`aid`, sa.`times`, sa.`order`, sa.`begin_time`, sa.`end_time`, sa.`tid`, sa.`sid`,' +
  806. ' s.`order` As `sorder`, s.`status` As `sstatus`,' +
  807. ' t.`name`, t.`project_id`, t.`type`, t.`user_id` ' +
  808. ' FROM ?? AS sa, ?? AS s, ?? As t ' +
  809. ' WHERE ((sa.`aid` = ? and sa.`status` = ?) OR (s.`user_id` = ? and sa.`status` = ? and s.`status` = ? and sa.`times` = (s.`times`-1)))' +
  810. ' and sa.`sid` = s.`id` and sa.`tid` = t.`id`'
  811. const sqlParam = [
  812. this.tableName,
  813. this.ctx.service.stage.tableName,
  814. this.ctx.service.tender.tableName,
  815. auditorId,
  816. auditConst.status.checking,
  817. auditorId,
  818. auditConst.status.checkNo,
  819. auditConst.status.checkNo
  820. ]
  821. return await this.db.query(sql, sqlParam)
  822. }
  823. /**
  824. * 获取 某时间后 审批进度 更新的期
  825. * @param {Number} pid - 查询标段
  826. * @param {Number} uid - 查询人
  827. * @param {Date} time - 查询时间
  828. * @return {Promise<*>}
  829. */
  830. async getNoticeStage(pid, uid, time) {
  831. // const sql = 'SELECT * FROM (SELECT t.`name`, t.`project_id`, t.`type`, t.`user_id`, ' +
  832. // ' s.`order` As `s_order`, s.`status` As `s_status`, ' +
  833. // ' sa.`aid`, sa.`times`, sa.`order`, sa.`end_time`, sa.`tid`, sa.`sid`, sa.`status`, ' +
  834. // ' pa.`name` As `su_name`, pa.role As `su_role`, pa.company As `su_company`' +
  835. // ' FROM (SELECT * FROM ?? WHERE `user_id` = ? OR `id` in (SELECT `tid` FROM ?? WHERE `aid` = ? GROUP BY `tid`)) As t' +
  836. // ' LEFT JOIN ?? As s On t.`id` = s.`tid`' +
  837. // ' LEFT JOIN ?? As sa ON s.`id` = sa.`sid`' +
  838. // ' LEFT JOIN ?? As pa ON sa.`aid` = pa.`id`' +
  839. // ' WHERE sa.`end_time` > ? and t.`project_id` = ?' +
  840. // ' ORDER By sa.`end_time` DESC LIMIT 1000) as new_t GROUP BY new_t.`tid`' +
  841. // ' ORDER By new_t.`end_time`';
  842. // const sqlParam = [this.ctx.service.tender.tableName, uid, this.tableName, uid, this.ctx.service.stage.tableName, this.tableName,
  843. // this.ctx.service.projectAccount.tableName, time, pid];
  844. // return await this.db.query(sql, sqlParam);
  845. let notice = await this.db.select('zh_notice', {
  846. where: { pid, type: pushType.stage, uid },
  847. orders: [['create_time', 'desc']],
  848. limit: 10,
  849. offset: 0
  850. })
  851. notice = notice.map(v => {
  852. const extra = JSON.parse(v.content)
  853. delete v.content
  854. return { ...v, ...extra }
  855. })
  856. return notice
  857. }
  858. /**
  859. * 用于添加推送所需的content内容
  860. * @param {Number} pid 项目id
  861. * @param {Number} tid 台账id
  862. * @param {Number} sid 期id
  863. * @param {Number} uid 审核人id
  864. */
  865. async getNoticeContent(pid, tid, sid, uid) {
  866. const noticeSql =
  867. 'SELECT * FROM (SELECT ' +
  868. ' t.`id` As `tid`, t.`name`, s.`order`, pa.`name` As `su_name`, pa.role As `su_role`' +
  869. ' FROM (SELECT * FROM ?? WHERE `id` = ? ) As t' +
  870. ' LEFT JOIN ?? As s On s.`id` = ?' +
  871. ' LEFT JOIN ?? As pa ON pa.`id` = ?' +
  872. ' WHERE t.`project_id` = ? ) as new_t GROUP BY new_t.`tid`'
  873. const noticeSqlParam = [this.ctx.service.tender.tableName, tid, this.ctx.service.stage.tableName, sid, this.ctx.service.projectAccount.tableName, uid, pid]
  874. const content = await this.db.query(noticeSql, noticeSqlParam)
  875. return content.length ? JSON.stringify(content[0]) : ''
  876. }
  877. /**
  878. * 获取审核人流程列表
  879. *
  880. * @param auditorId
  881. * @return {Promise<*>}
  882. */
  883. async getAuditGroupByList(stageId, times) {
  884. const sql =
  885. 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`aid`, la.`order` ' +
  886. 'FROM ?? AS la, ?? AS pa ' +
  887. 'WHERE la.`sid` = ? and la.`times` = ? and la.`aid` = pa.`id` GROUP BY la.`aid` ORDER BY la.`order`'
  888. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, times]
  889. return await this.db.query(sql, sqlParam)
  890. // const sql = 'SELECT `tid`, `sid`, `aid`, `order` FROM ?? WHERE `sid` = ? and `times` = ? GROUP BY `aid`';
  891. // const sqlParam = [this.tableName, stageId, times];
  892. // return await this.db.query(sql, sqlParam);
  893. }
  894. /**
  895. * 获取审核人流程列表
  896. *
  897. * @param auditorId
  898. * @return {Promise<*>}
  899. */
  900. async getAuditGroupByListWithOwner(stageId, times) {
  901. const result = await this.getAuditGroupByList(stageId, times)
  902. const sql =
  903. 'SELECT pa.`id` As aid, pa.`name`, pa.`company`, pa.`role`, ? As times, ? As sid, 0 As `order`' +
  904. ' FROM ' +
  905. this.ctx.service.stage.tableName +
  906. ' As s' +
  907. ' LEFT JOIN ' +
  908. this.ctx.service.projectAccount.tableName +
  909. ' As pa' +
  910. ' ON s.user_id = pa.id' +
  911. ' WHERE s.id = ?'
  912. const sqlParam = [times, stageId, stageId]
  913. const user = await this.db.queryOne(sql, sqlParam)
  914. result.unshift(user)
  915. return result
  916. }
  917. /**
  918. * 复制上一期的审批人列表给最新一期
  919. *
  920. * @param transaction - 新增一期的事务
  921. * @param {Object} preStage - 上一期
  922. * @param {Object} newStage - 最新一期
  923. * @return {Promise<*>}
  924. */
  925. async copyPreStageAuditors(transaction, preStage, newStage) {
  926. const auditors = await this.getAuditGroupByList(preStage.id, preStage.times)
  927. const newAuditors = []
  928. for (const a of auditors) {
  929. const na = {
  930. tid: preStage.tid,
  931. sid: newStage.id,
  932. aid: a.aid,
  933. times: newStage.times,
  934. order: newAuditors.length + 1,
  935. status: auditConst.status.uncheck
  936. }
  937. newAuditors.push(na)
  938. }
  939. const result = await transaction.insert(this.tableName, newAuditors)
  940. return (result.effectRows = auditors.length)
  941. }
  942. /**
  943. * 移除审核人
  944. *
  945. * @param {Number} stageId - 期id
  946. * @param {Number} status - 期状态
  947. * @param {Number} status - 期次数
  948. * @return {Promise<boolean>}
  949. */
  950. async getAuditorByStatus(stageId, status, times = 1) {
  951. let auditor = null
  952. let sql = ''
  953. let sqlParam = ''
  954. switch (status) {
  955. case auditConst.status.checking:
  956. case auditConst.status.checked:
  957. case auditConst.status.checkNoPre:
  958. sql =
  959. 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`order` ' +
  960. 'FROM ?? AS la, ?? AS pa ' +
  961. 'WHERE la.`sid` = ? and la.`status` = ? and la.`aid` = pa.`id` order by la.`times` desc, la.`order` desc'
  962. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, status]
  963. auditor = await this.db.queryOne(sql, sqlParam)
  964. break
  965. case auditConst.status.checkNo:
  966. sql =
  967. 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`order` ' +
  968. 'FROM ?? AS la, ?? AS pa ' +
  969. 'WHERE la.`sid` = ? and la.`status` = ? and la.`times` = ? and la.`aid` = pa.`id` order by la.`times` desc, la.`order` desc'
  970. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, auditConst.status.checkNo, parseInt(times) - 1]
  971. auditor = await this.db.queryOne(sql, sqlParam)
  972. break
  973. case auditConst.status.uncheck:
  974. default:
  975. break
  976. }
  977. return auditor
  978. }
  979. /**
  980. * 取某一期已批准审核信息(报表用)
  981. *
  982. * @param {Number} stageId - 期id
  983. * @param {Number} times - 期次数
  984. * @return {Promise<boolean>}
  985. */
  986. async getStageAudit(stageId, times = 1) {
  987. 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'
  988. const sqlParam = [this.tableName, stageId, times]
  989. const rst = await this.db.query(sql, sqlParam)
  990. return rst
  991. }
  992. /**
  993. * 取待审批期列表(wap用)
  994. *
  995. * @param auditorId
  996. * @return {Promise<*>}
  997. */
  998. async getAuditStageByWap(auditorId) {
  999. const sql =
  1000. 'SELECT sa.`aid`, sa.`times`, sa.`begin_time`, sa.`end_time`, sa.`tid`, sa.`sid`,' +
  1001. // ' 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`, ' +
  1002. ' s.*,' +
  1003. ' t.`name`, t.`project_id`, t.`type`, t.`user_id`,' +
  1004. ' ti.`deal_info` ' +
  1005. ' FROM ?? AS sa, ?? AS s, ?? As t, ?? AS ti ' +
  1006. ' WHERE sa.`aid` = ? and sa.`status` = ?' +
  1007. ' and sa.`sid` = s.`id` and sa.`tid` = t.`id` and ti.`tid` = t.`id`'
  1008. const sqlParam = [
  1009. this.tableName,
  1010. this.ctx.service.stage.tableName,
  1011. this.ctx.service.tender.tableName,
  1012. this.ctx.service.tenderInfo.tableName,
  1013. auditorId,
  1014. auditConst.status.checking
  1015. ]
  1016. return await this.db.query(sql, sqlParam)
  1017. }
  1018. /**
  1019. * 删除 某期 某次 全审批流程
  1020. * 私有,不做判断,不补全最新一轮审批人数据,不计算缓存
  1021. * @param {Number} sid - 标段id
  1022. * @param {Number} times - 第几次审批
  1023. * @param transaction - 删除事务
  1024. * @return {Promise<void>}
  1025. */
  1026. async _timesDelete(sid, times, transaction) {
  1027. // 审批流程
  1028. await transaction.delete(this.tableName, { sid, times })
  1029. await transaction.delete(this.ctx.service.pos.tableName, { add_stage: sid, add_times: times })
  1030. await transaction.delete(this.ctx.service.stageBills.tableName, { sid, times })
  1031. await transaction.delete(this.ctx.service.stagePos.tableName, { sid, times })
  1032. await transaction.delete(this.ctx.service.stageDetail.tableName, { sid, times })
  1033. await transaction.delete(this.ctx.service.stageChange.tableName, { sid, stimes: times })
  1034. await transaction.delete(this.ctx.service.stagePay.tableName, { sid, stimes: times })
  1035. await transaction.delete(this.ctx.service.pay.tableName, { csid: sid, cstimes: times })
  1036. // 其他台账
  1037. await this.ctx.service.stageJgcl.deleteStageTimesData(sid, times, transaction)
  1038. await this.ctx.service.stageOther.deleteStageTimesData(sid, times, transaction)
  1039. await this.ctx.service.stageBonus.deleteStageTimesData(sid, times, transaction)
  1040. }
  1041. /**
  1042. * 删除本次审批流程
  1043. * @param {Number} stageId - 标段id
  1044. * @param {Number} times - 第几次审批
  1045. * @return {Promise<void>}
  1046. */
  1047. async timesDelete() {
  1048. const transaction = await this.db.beginTransaction()
  1049. try {
  1050. // 删除最新一次数据
  1051. await this._timesDelete(this.ctx.stage.id, this.ctx.stage.times, transaction)
  1052. // 审批退回,未重新上报时,需删除最新两次数据
  1053. const isCheckNo = this.ctx.stage.status === auditConst.status.checkNo
  1054. const nowTimes = isCheckNo ? this.ctx.stage.times - 1 : this.ctx.stage.times
  1055. if (isCheckNo) {
  1056. await this._timesDelete(this.ctx.stage.id, nowTimes, transaction)
  1057. }
  1058. // 添加上一次审批人
  1059. const sql = 'SELECT `tid`, `sid`, `aid`, `order` FROM ?? WHERE `sid` = ? and `times` = ? GROUP BY `aid` ORDER BY `id` ASC'
  1060. const sqlParam = [this.tableName, this.ctx.stage.id, nowTimes - 1]
  1061. const auditors = await this.db.query(sql, sqlParam)
  1062. let order = 1
  1063. for (const a of auditors) {
  1064. a.times = nowTimes
  1065. a.order = order
  1066. a.status = auditConst.status.uncheck
  1067. order++
  1068. }
  1069. // 拷贝新一次审核流程列表
  1070. await transaction.insert(this.tableName, auditors)
  1071. // 计算缓存
  1072. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage)
  1073. // 计算并合同支付最终数据
  1074. const lastAudit = await this.getDataByCondition({
  1075. sid: this.ctx.stage.id,
  1076. times: nowTimes - 1,
  1077. status: auditConst.status.checkNo
  1078. })
  1079. if (!lastAudit) throw '审批数据错误'
  1080. await this.ctx.service.stagePay.copyStagePays4DeleteTimes(this.ctx.stage, nowTimes, 0, lastAudit.times, lastAudit.order, transaction)
  1081. const stagePay = await this.ctx.service.stagePay.getAuditorStageData(this.ctx.stage.id, lastAudit.times, lastAudit.order)
  1082. const yfPay = stagePay.find(function (x) {
  1083. return x.ptype === payConst.payType.yf
  1084. })
  1085. const sfPay = stagePay.find(function (x) {
  1086. return x.ptype === payConst.payType.sf
  1087. })
  1088. // 同步 期信息
  1089. const time = new Date()
  1090. await transaction.update(this.ctx.service.stage.tableName, {
  1091. id: this.ctx.stage.id,
  1092. status: auditConst.status.checkNo,
  1093. contract_tp: tpData.contract_tp,
  1094. qc_tp: tpData.qc_tp,
  1095. times: nowTimes,
  1096. yf_tp: yfPay.tp,
  1097. sf_tp: sfPay.tp,
  1098. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  1099. cache_time_l: time,
  1100. cache_time_r: time
  1101. })
  1102. await transaction.commit()
  1103. } catch (err) {
  1104. await transaction.rollback()
  1105. throw err
  1106. }
  1107. }
  1108. }
  1109. return StageAudit
  1110. }