stage_audit.js 54 KB

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