stage_audit.js 75 KB

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