stage_audit.js 89 KB

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