stage_audit.js 72 KB

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