stage_audit.js 71 KB

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