stage_audit.js 71 KB

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