stage_audit.js 52 KB

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