stage_audit.js 58 KB

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