change_project_audit.js 81 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date 2018/8/14
  7. * @version
  8. */
  9. const auditConst = require('../const/audit').changeProject;
  10. const pushType = require('../const/audit').pushType;
  11. const pushOperate = require('../const/spec_3f').pushOperate;
  12. const shenpiConst = require('../const/shenpi');
  13. const smsTypeConst = require('../const/sms_type');
  14. const SMS = require('../lib/sms');
  15. const SmsAliConst = require('../const/sms_alitemplate');
  16. const wxConst = require('../const/wechat_template');
  17. const auditType = require('../const/audit').auditType;
  18. module.exports = app => {
  19. class ChangeProjectAudit extends app.BaseService {
  20. /**
  21. * 构造函数
  22. *
  23. * @param {Object} ctx - egg全局变量
  24. * @return {void}
  25. */
  26. constructor(ctx) {
  27. super(ctx);
  28. this.tableName = 'change_project_audit';
  29. }
  30. /**
  31. * 获取 审核列表信息
  32. *
  33. * @param {Number} cpId - 变更立项id
  34. * @param {Number} times - 第几次审批
  35. * @return {Promise<*>}
  36. */
  37. async getAuditors(cpId, times = 1, order_sort = 'asc', noYB = false) {
  38. // const sql = '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`, g.`sort` ' +
  39. // 'FROM ?? AS la, ?? AS pa, (SELECT t1.`aid`,(@i:=@i+1) as `sort` FROM (SELECT t.`aid`, t.`order` FROM (select `aid`, `order` from ?? WHERE `cpid` = ? AND `times` = ? ORDER BY `order` LIMIT 200) t GROUP BY t.`aid` ORDER BY t.`order`) t1, (select @i:=0) as it) as g ' +
  40. // 'WHERE la.`cpid` = ? and la.`times` = ? and la.`aid` = pa.`id` and g.`aid` = la.`aid` order by la.`order`';
  41. // const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, this.tableName, cpId, times, cpId, times];
  42. // const result = await this.db.query(sql, sqlParam);
  43. // const sql2 = 'SELECT COUNT(a.`aid`) as num FROM (SELECT `aid` FROM ?? WHERE `cpid` = ? AND `times` = ? GROUP BY `aid`) as a';
  44. // const sqlParam2 = [this.tableName, cpId, times];
  45. // const count = await this.db.queryOne(sql2, sqlParam2);
  46. // for (const i in result) {
  47. // result[i].max_sort = count.num;
  48. // }
  49. const ybSql = noYB ? ' AND la.audit_order != 0' : '';
  50. const sql = 'SELECT la.id, la.aid, la.times, la.order, la.status, la.opinion, la.begin_time, la.end_time, la.audit_type, la.audit_order,' +
  51. ' pa.name, pa.company, pa.role, pa.mobile, pa.telephone' +
  52. ` FROM ${this.tableName} la LEFT JOIN ${this.ctx.service.projectAccount.tableName} pa ON la.aid = pa.id` +
  53. ' WHERE la.cpid = ? AND la.times = ?' + ybSql +
  54. ' ORDER BY la.order ' + order_sort;
  55. const sqlParam = [cpId, times];
  56. const result = await this.db.query(sql, sqlParam);
  57. const max_sort = this._.max(result.map(x => { return x.audit_order; }));
  58. for (const i in result) {
  59. result[i].max_sort = max_sort;
  60. }
  61. return result;
  62. }
  63. /**
  64. * 获取 当前审核人
  65. *
  66. * @param {Number} cpId - 变更立项id
  67. * @param {Number} times - 第几次审批
  68. * @return {Promise<*>}
  69. */
  70. async getCurAuditor(cpId, times = 1) {
  71. const sql = '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`, la.audit_type, la.audit_order ' +
  72. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  73. ' WHERE la.`cpid` = ? and la.`status` = ? and la.`times` = ?';
  74. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, cpId, auditConst.status.checking, times];
  75. return await this.db.queryOne(sql, sqlParam);
  76. }
  77. async getCurAuditors(cpId, times = 1) {
  78. const sql =
  79. '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`, la.audit_type, la.audit_order ' +
  80. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id`' +
  81. ' WHERE la.`cpid` = ? and la.`status` = ? and la.`times` = ?';
  82. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, cpId, auditConst.status.checking, times];
  83. return await this.db.query(sql, sqlParam);
  84. }
  85. /**
  86. * 获取审核人流程列表
  87. *
  88. * @param auditorId
  89. * @return {Promise<*>}
  90. */
  91. async getAuditGroupByList(changeId, times, transaction = false) {
  92. const sql = 'SELECT pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`cpid`, la.`aid`, la.`order`, la.`status`, la.audit_type, la.audit_order ' +
  93. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id`' +
  94. ' WHERE la.`cpid` = ? and la.`times` = ? and la.`status` != ? and la.`status` != ? GROUP BY la.`aid` ORDER BY la.`order`';
  95. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, changeId, times, auditConst.status.revise, auditConst.status.cancelRevise];
  96. // const sql =
  97. // 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`cpid`, la.`order`, la.`status`, la.audit_type, la.audit_order' +
  98. // ' FROM (SELECT `aid`, max(`order`) `order` FROM ?? WHERE `cpid` = ? and `times` = ? and `is_old` = ? GROUP BY aid) sa' +
  99. // ' LEFT JOIN ?? la ON sa.`aid` = la.`aid` AND sa.`order` = la.`order`' +
  100. // ' Left JOIN ?? AS pa On la.`aid` = pa.`id` WHERE la.`cpid` = ? and la.`times` = ? and la.`is_old` = ? order BY la.`order`';
  101. // const sqlParam = [this.tableName, changeId, times, 0, this.tableName, this.ctx.service.projectAccount.tableName, changeId, times, 0];
  102. return transaction !== false ? await transaction.query(sql, sqlParam) : await this.db.query(sql, sqlParam);
  103. }
  104. /**
  105. * 移除审核人
  106. *
  107. * @param {Number} materialId - 材料调差期id
  108. * @param {Number} status - 期状态
  109. * @param {Number} status - 期次数
  110. * @return {Promise<boolean>}
  111. */
  112. async getAuditorByStatus(cpId, status, times = 1) {
  113. let auditor = null;
  114. let sql = '';
  115. let sqlParam = '';
  116. switch (status) {
  117. case auditConst.status.checking :
  118. case auditConst.status.checked :
  119. case auditConst.status.checkNo :
  120. case auditConst.status.revise :
  121. case auditConst.status.cancelRevise :
  122. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`cpid`, la.`aid`, la.`order` ' +
  123. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  124. ' WHERE la.`cpid` = ? and la.`status` = ? ' +
  125. ' ORDER BY la.`times` desc, la.`order` desc';
  126. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, cpId, status];
  127. auditor = await this.db.queryOne(sql, sqlParam);
  128. break;
  129. case auditConst.status.back :
  130. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`cpid`, la.`aid`, la.`order` ' +
  131. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id`' +
  132. ' WHERE la.`cpid` = ? and la.`status` = ? and la.`times` = ?' +
  133. ' ORDER BY la.`times` desc, la.`order` desc';
  134. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, cpId, auditConst.status.back, parseInt(times) - 1];
  135. auditor = await this.db.queryOne(sql, sqlParam);
  136. break;
  137. case auditConst.status.uncheck :
  138. break;
  139. default:break;
  140. }
  141. return auditor;
  142. }
  143. async getAuditorsByStatus(cpId, status, times = 1) {
  144. let auditor = [];
  145. let sql = '';
  146. let sqlParam = '';
  147. let cur;
  148. switch (status) {
  149. case auditConst.status.checking :
  150. case auditConst.status.checked :
  151. case auditConst.status.checkNo :
  152. case auditConst.status.revise :
  153. case auditConst.status.cancelRevise :
  154. cur = await this.db.queryOne('SELECT * From ?? where cpid = ? AND times = ? AND status = ? ORDER By times DESC, `order` DESC', [this.tableName, cpId, times, status]);
  155. if (!cur) return [];
  156. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`cpid`, la.`order`, la.`status`, la.`audit_order`, la.`audit_type` ' +
  157. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  158. ' WHERE la.`cpid` = ? and la.`order` = ? and times = ?';
  159. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, cpId, cur.order, times];
  160. auditor = await this.db.query(sql, sqlParam);
  161. break;
  162. case auditConst.status.back :
  163. cur = await this.db.queryOne('SELECT * From ?? where cpid = ? AND times = ? AND status = ? ORDER By times DESC, `order` DESC', [this.tableName, cpId, parseInt(times) - 1, status]);
  164. if (!cur) return [];
  165. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`cpid`, la.`order`, la.`status`, la.`audit_order`, la.`audit_type` ' +
  166. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  167. ' WHERE la.`cpid` = ? and la.`order` = ? and la.`times` = ?';
  168. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, cpId, cur.order, parseInt(times) - 1];
  169. auditor = await this.db.query(sql, sqlParam);
  170. break;
  171. case auditConst.status.uncheck:
  172. default:
  173. break;
  174. }
  175. return auditor;
  176. }
  177. async getLastAudit(cpid, times, transaction = null) {
  178. const sql = 'SELECT * FROM ?? WHERE `cpid` = ? AND `times` = ? ORDER BY `order` DESC';
  179. const sqlParam = [this.tableName, cpid, times];
  180. return transaction ? await transaction.queryOne(sql, sqlParam) : await this.db.queryOne(sql, sqlParam);
  181. }
  182. /**
  183. * 获取审核人流程列表(包括原报)
  184. * @param {Number} materialId 调差id
  185. * @param {Number} times 审核次数
  186. * @return {Promise<Array>} 查询结果集(包括原报)
  187. */
  188. async getAuditorsWithOwner(cpId, times = 1) {
  189. const result = await this.getAuditGroupByList(cpId, times);
  190. const sql =
  191. 'SELECT pa.`id` As aid, pa.`name`, pa.`company`, pa.`role`, ? As times, ? As cpid, 0 As `order`' +
  192. ' FROM ' +
  193. this.ctx.service.changeProject.tableName +
  194. ' As s' +
  195. ' LEFT JOIN ' +
  196. this.ctx.service.projectAccount.tableName +
  197. ' As pa' +
  198. ' ON s.uid = pa.id' +
  199. ' WHERE s.id = ?';
  200. const sqlParam = [times, cpId, cpId];
  201. const user = await this.db.queryOne(sql, sqlParam);
  202. result.unshift(user);
  203. return result;
  204. }
  205. /**
  206. * 新增审核人
  207. *
  208. * @param {Number} cpId - 立项书id
  209. * @param {Number} auditorId - 审核人id
  210. * @param {Number} times - 第几次审批
  211. * @return {Promise<number>}
  212. */
  213. async addAuditor(cpId, auditorId, times = 1, is_gdzs = 0) {
  214. const transaction = await this.db.beginTransaction();
  215. let flag = false;
  216. try {
  217. let [newOrder, newAuditOrder] = await this.getNewOrder(cpId, times);
  218. // 判断是否存在固定终审,存在则newOrder - 1并使终审order+1
  219. newOrder = is_gdzs === 1 ? newOrder - 1 : newOrder;
  220. newAuditOrder = is_gdzs === 1 ? newAuditOrder - 1 : newAuditOrder;
  221. if (is_gdzs) await this._syncOrderByDelete(transaction, cpId, newOrder, times, '+');
  222. const data = {
  223. tid: this.ctx.tender.id,
  224. cpid: cpId,
  225. aid: auditorId,
  226. times,
  227. order: newOrder,
  228. status: auditConst.status.uncheck,
  229. audit_order: newAuditOrder,
  230. };
  231. const result = await transaction.insert(this.tableName, data);
  232. await transaction.commit();
  233. flag = result.effectRows = 1;
  234. } catch (err) {
  235. await transaction.rollback();
  236. throw err;
  237. }
  238. return flag;
  239. }
  240. /**
  241. * 获取 最新审核顺序
  242. *
  243. * @param {Number} cpId - 立项书id
  244. * @param {Number} times - 第几次审批
  245. * @return {Promise<number>}
  246. */
  247. async getNewOrder(cpId, times = 1) {
  248. const sql = 'SELECT Max(`order`) As max_order, Max(audit_order) As max_audit_order FROM ?? Where `cpid` = ? and `times` = ?';
  249. const sqlParam = [this.tableName, cpId, times];
  250. const result = await this.db.queryOne(sql, sqlParam);
  251. return result && result.max_order ? [result.max_order + 1, result.max_audit_order + 1] : [1, 1];
  252. }
  253. /**
  254. * 移除审核人
  255. *
  256. * @param {Number} cpId - 变更立项书id
  257. * @param {Number} auditorId - 审核人id
  258. * @param {Number} times - 第几次审批
  259. * @return {Promise<boolean>}
  260. */
  261. async deleteAuditor(cpId, auditorId, times = 1) {
  262. const transaction = await this.db.beginTransaction();
  263. try {
  264. const condition = { cpid: cpId, aid: auditorId, times };
  265. const auditor = await this.getDataByCondition(condition);
  266. if (!auditor) {
  267. throw '该审核人不存在';
  268. }
  269. // 移除整个流程的人
  270. await transaction.delete(this.tableName, { cpid: cpId, order: auditor.order, times });
  271. await this._syncOrderByDelete(transaction, cpId, auditor.order, times);
  272. await transaction.commit();
  273. } catch (err) {
  274. await transaction.rollback();
  275. throw err;
  276. }
  277. return true;
  278. }
  279. /**
  280. * 移除审核人时,同步其后审核人order
  281. * @param transaction - 事务
  282. * @param {Number} cpId - 变更立项书id
  283. * @param {Number} auditorId - 审核人id
  284. * @param {Number} times - 第几次审批
  285. * @return {Promise<*>}
  286. * @private
  287. */
  288. async _syncOrderByDelete(transaction, cpId, order, times, selfOperate = '-') {
  289. this.initSqlBuilder();
  290. this.sqlBuilder.setAndWhere('cpid', {
  291. value: cpId,
  292. operate: '=',
  293. });
  294. this.sqlBuilder.setAndWhere('order', {
  295. value: order,
  296. operate: '>=',
  297. });
  298. this.sqlBuilder.setAndWhere('times', {
  299. value: times,
  300. operate: '=',
  301. });
  302. this.sqlBuilder.setUpdateData('order', {
  303. value: 1,
  304. selfOperate,
  305. });
  306. this.sqlBuilder.setUpdateData('audit_order', {
  307. value: 1,
  308. selfOperate,
  309. });
  310. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  311. const data = await transaction.query(sql, sqlParam);
  312. return data;
  313. }
  314. /**
  315. * 开始审批
  316. * @param {Number} cpId - 立项书id
  317. * @param {Number} times - 第几次审批
  318. * @return {Promise<boolean>}
  319. */
  320. async start(cpId, times = 1) {
  321. const audits = await this.getAllDataByCondition({ where: { cpid: cpId, times, order: 1 } });
  322. if (audits.length === 0) {
  323. if (this.ctx.tender.info.shenpi.change === shenpiConst.sp_status.gdspl) {
  324. throw '请联系管理员添加审批人';
  325. } else {
  326. throw '请先选择审批人,再上报数据';
  327. }
  328. }
  329. const transaction = await this.db.beginTransaction();
  330. try {
  331. const begin_time = new Date();
  332. const updateData = audits.map(x => { return { id: x.id, status: auditConst.status.checking, begin_time }; });
  333. await transaction.updateRows(this.tableName, updateData);
  334. await transaction.update(this.ctx.service.changeProject.tableName, {
  335. id: cpId, status: auditConst.status.checking,
  336. });
  337. // 微信模板通知
  338. const shenpiUrl = await this.ctx.helper.urlToShort(
  339. this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/change/project/' + cpId + '/information#shenpi'
  340. );
  341. const wechatData = {
  342. type: 'project',
  343. wap_url: shenpiUrl,
  344. status: wxConst.status.check,
  345. tips: wxConst.tips.check,
  346. code: this.ctx.session.sessionProject.code,
  347. c_name: this.ctx.change.name,
  348. };
  349. await this.ctx.helper.sendWechat(this._.map(audits, 'aid'), smsTypeConst.const.BG, smsTypeConst.judge.approval.toString(), wxConst.template.change, wechatData);
  350. for (const audit of audits) {
  351. await this.ctx.service.noticeAgain.addNoticeAgain(transaction, smsTypeConst.const.BG, {
  352. pid: this.ctx.session.sessionProject.id,
  353. tid: this.ctx.tender.id,
  354. uid: audit.aid,
  355. sp_type: 'change',
  356. sp_id: audit.id,
  357. table_name: this.tableName,
  358. template: wxConst.template.change,
  359. wx_data: wechatData,
  360. });
  361. }
  362. await transaction.delete(this.ctx.service.changeProjectHistory.tableName, { cpid: cpId });
  363. // todo 更新标段tender状态 ?
  364. // 检查三方特殊推送
  365. await this.ctx.service.specMsg.addChangeProjectMsg(transaction, this.ctx.session.sessionProject.id, this.ctx.change, pushOperate.change_project.flow);
  366. await transaction.commit();
  367. } catch (err) {
  368. await transaction.rollback();
  369. throw err;
  370. }
  371. return true;
  372. }
  373. /**
  374. * 获取审核人需要审核的期列表
  375. *
  376. * @param auditorId
  377. * @return {Promise<*>}
  378. */
  379. async getAuditChangeProject(auditorId, spid = '') {
  380. const spSql = spid ? ' and t.`spid` = "' + spid + '"' : '';
  381. const sql = 'SELECT ma.`aid`, ma.`times`, ma.`order`, ma.`begin_time`, ma.`end_time`, ma.`tid`, ma.`cpid`,' +
  382. ' m.`status` As `mstatus`, m.`code` As `mcode`,' +
  383. ' t.`name`, t.`project_id`, t.`type`, t.`user_id` ' +
  384. ' FROM ?? AS ma LEFT JOIN ?? AS m ON ma.`cpid` = m.`id` LEFT JOIN ?? As t ON ma.`tid` = t.`id`' +
  385. ' WHERE ((ma.`aid` = ? and ma.`status` = ?) OR (m.`uid` = ? and ma.`status` = ? and m.`status` = ? and ma.`times` = (m.`times`-1)))' + spSql +
  386. ' ORDER BY ma.`begin_time` DESC';
  387. const sqlParam = [this.tableName, this.ctx.service.changeProject.tableName, this.ctx.service.tender.tableName, auditorId, auditConst.status.checking, auditorId, auditConst.status.back, auditConst.status.back];
  388. const result = await this.db.query(sql, sqlParam);
  389. // 过滤result中存在重复cpid的值, 保留最新的一条
  390. const filterResult = [];
  391. const cpidArr = [];
  392. for (const r of result) {
  393. if (cpidArr.indexOf(r.cpid) === -1) {
  394. filterResult.push(r);
  395. cpidArr.push(r.cpid);
  396. }
  397. }
  398. return filterResult;
  399. }
  400. /**
  401. * 获取审核人审核的次数
  402. *
  403. * @param auditorId
  404. * @return {Promise<*>}
  405. */
  406. async getCountByChecked(auditorId, spid = '') {
  407. if (spid) {
  408. const sql = 'SELECT count(*) AS count FROM ?? AS a LEFT JOIN ?? AS t ON a.tid = t.id WHERE a.`aid` = ? AND a.`status` in (' + this.ctx.helper.getInArrStrSqlFilter([auditConst.status.checked, auditConst.status.back, auditConst.status.checkNo]) + ') AND t.`spid` = ?';
  409. const sqlParam = [this.tableName, this.ctx.service.tender.tableName, auditorId, spid];
  410. const result = await this.db.queryOne(sql, sqlParam);
  411. return result.count ? result.count : 0;
  412. }
  413. return await this.db.count(this.tableName, { aid: auditorId, status: [auditConst.status.checked, auditConst.status.back, auditConst.status.checkNo] });
  414. }
  415. /**
  416. * 获取最近一次审批结束时间
  417. *
  418. * @param auditorId
  419. * @return {Promise<*>}
  420. */
  421. async getLastEndTimeByChecked(auditorId, spid = '') {
  422. const sqSql = spid ? ' AND t.`spid` = "' + spid + '"' : '';
  423. const sql = 'SELECT a.`end_time` FROM ?? AS a LEFT JOIN ?? AS t ON a.`tid` = t.`id` WHERE a.`aid` = ? ' +
  424. 'AND a.`status` in (' + this.ctx.helper.getInArrStrSqlFilter([auditConst.status.checked, auditConst.status.back, auditConst.status.checkNo]) + ')' + sqSql +
  425. ' ORDER BY a.`end_time` DESC';
  426. const sqlParam = [this.tableName, this.ctx.service.tender.tableName, auditorId];
  427. const result = await this.db.queryOne(sql, sqlParam);
  428. return result ? result.end_time : null;
  429. }
  430. /**
  431. * 用于添加推送所需的content内容
  432. * @param {Number} pid 项目id
  433. * @param {Number} tid 台账id
  434. * @param {Number} cpId 立项书id
  435. * @param {Number} uid 审批人id
  436. */
  437. async getNoticeContent(pid, tid, cpId, uid, opinion = '') {
  438. const noticeSql = 'SELECT * FROM (SELECT ' +
  439. ' t.`id` As `tid`, ma.`cpid`, m.`code` as `c_code`, t.`name`, pa.`name` As `su_name`, pa.role As `su_role`' +
  440. ' FROM (SELECT * FROM ?? WHERE `id` = ? ) As t' +
  441. ' LEFT JOIN ?? As m On t.`id` = m.`tid` AND m.`id` = ?' +
  442. ' LEFT JOIN ?? As ma ON m.`id` = ma.`cpid`' +
  443. ' LEFT JOIN ?? As pa ON pa.`id` = ?' +
  444. ' WHERE t.`project_id` = ? ) as new_t GROUP BY new_t.`tid`';
  445. const noticeSqlParam = [this.ctx.service.tender.tableName, tid, this.ctx.service.changeProject.tableName, cpId, this.tableName, this.ctx.service.projectAccount.tableName, uid, pid];
  446. const content = await this.db.query(noticeSql, noticeSqlParam);
  447. if (content.length) {
  448. content[0].opinion = opinion;
  449. }
  450. return content.length ? JSON.stringify(content[0]) : '';
  451. }
  452. /**
  453. * 审批
  454. * @param {Number} cpId - 立项书id
  455. * @param {auditConst.status.checked|auditConst.status.checkNo} checkType - 审批结果
  456. * @param {Number} times - 第几次审批
  457. * @return {Promise<void>}
  458. */
  459. async check(cpId, checkData, times = 1) {
  460. if (checkData.checkType !== auditConst.status.checked && checkData.checkType !== auditConst.status.checkNo && checkData.checkType !== auditConst.status.back) {
  461. throw '提交数据错误';
  462. }
  463. const pid = this.ctx.session.sessionProject.id;
  464. switch (checkData.checkType) {
  465. case auditConst.status.checked:
  466. await this._checked(pid, cpId, checkData, times);
  467. break;
  468. case auditConst.status.back:
  469. await this._back(pid, cpId, checkData, times);
  470. break;
  471. case auditConst.status.checkNo:
  472. await this._checkNo(pid, cpId, checkData, times);
  473. break;
  474. default:
  475. throw '无效审批操作';
  476. }
  477. }
  478. async _checked(pid, cpId, checkData, times) {
  479. const accountId = this.ctx.session.sessionUser.accountId;
  480. const time = new Date();
  481. // 整理当前流程审核人状态更新
  482. const audits = await this.getAllDataByCondition({ where: { cpid: cpId, times, status: auditConst.status.checking } });
  483. if (audits.length === 0) throw '审核数据错误';
  484. const selfAudit = audits.find(x => { return x.aid === accountId; });
  485. if (!selfAudit) throw '当前标段您无权审批';
  486. const flowAudits = await this.getAllDataByCondition({ where: { cpid: cpId, times, order: selfAudit.order } });
  487. const nextAudits = await this.getAllDataByCondition({ where: { cpid: cpId, times, order: selfAudit.order + 1 } });
  488. // 获取审核人列表
  489. // const sql = 'SELECT `tid`, `cpid`, `aid`, `order` FROM ?? WHERE `cpid` = ? and `times` = ? GROUP BY `aid` ORDER BY `id` ASC';
  490. // const sqlParam = [this.tableName, cpId, times];
  491. // const auditors = await this.db.query(sql, sqlParam);
  492. //
  493. // const nextAudit = await this.getDataByCondition({ cpid: cpId, times, order: audit.order + 1 });
  494. const transaction = await this.db.beginTransaction();
  495. try {
  496. // 更新本人审批状态
  497. await transaction.update(this.tableName, {
  498. id: selfAudit.id,
  499. status: checkData.checkType,
  500. opinion: checkData.opinion,
  501. end_time: time,
  502. });
  503. await this.ctx.service.noticeAgain.stopNoticeAgain(transaction, this.tableName, selfAudit.id);
  504. // 获取推送必要信息
  505. const noticeContent = await this.getNoticeContent(pid, selfAudit.tid, cpId, selfAudit.aid, checkData.opinion);
  506. // 添加推送
  507. // const records = [{ pid, type: pushType.changeProject, uid: this.ctx.change.uid, status: auditConst.status.checked, content: noticeContent }];
  508. const records = [];
  509. const auditors = await this.getAuditorsWithOwner(cpId, times);
  510. auditors.forEach(audit => {
  511. records.push({ pid, tid: selfAudit.tid, type: pushType.changeProject, uid: audit.aid, status: auditConst.status.checked, content: noticeContent });
  512. });
  513. await transaction.insert('zh_notice', records);
  514. if (audits.length === 1 || selfAudit.audit_type !== auditType.key.and) {
  515. // 或签更新他人审批状态
  516. if (selfAudit.audit_type === auditType.key.or) {
  517. const updateOther = [];
  518. for (const audit of audits) {
  519. if (audit.aid === selfAudit.aid) continue;
  520. updateOther.push({
  521. id: audit.id,
  522. status: auditConst.status.checkSkip,
  523. opinion: '',
  524. end_time: time,
  525. });
  526. await this.ctx.service.noticeAgain.stopNoticeAgain(transaction, this.tableName, audit.id);
  527. }
  528. if (updateOther.length > 0) transaction.updateRows(this.tableName, updateOther);
  529. }
  530. // 无下一审核人表示,审核结束
  531. if (nextAudits.length > 0) {
  532. // 流程至下一审批人
  533. const updateData = nextAudits.map(x => { return { id: x.id, status: auditConst.status.checking, begin_time: time }; });
  534. await transaction.updateRows(this.tableName, updateData);
  535. // 同步 期信息
  536. await transaction.update(this.ctx.service.changeProject.tableName, {
  537. id: cpId, status: auditConst.status.checking,
  538. });
  539. // 微信模板通知
  540. const shenpiUrl = await this.ctx.helper.urlToShort(
  541. this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/change/project/' + cpId + '/information#shenpi'
  542. );
  543. const wechatData = {
  544. type: 'project',
  545. wap_url: shenpiUrl,
  546. status: wxConst.status.check,
  547. tips: wxConst.tips.check,
  548. code: this.ctx.session.sessionProject.code,
  549. c_name: this.ctx.change.name,
  550. };
  551. await this.ctx.helper.sendWechat(this._.map(nextAudits, 'aid'), smsTypeConst.const.BG, smsTypeConst.judge.approval.toString(), wxConst.template.change, wechatData);
  552. for (const nextAudit of nextAudits) {
  553. await this.ctx.service.noticeAgain.addNoticeAgain(transaction, smsTypeConst.const.BG, {
  554. pid: this.ctx.session.sessionProject.id,
  555. tid: this.ctx.tender.id,
  556. uid: nextAudit.aid,
  557. sp_type: 'change',
  558. sp_id: nextAudit.id,
  559. table_name: this.tableName,
  560. template: wxConst.template.change,
  561. wx_data: wechatData,
  562. });
  563. }
  564. // 检查三方特殊推送
  565. await this.ctx.service.specMsg.addChangeProjectMsg(transaction, pid, this.ctx.change, pushOperate.change_project.flow);
  566. } else {
  567. // 本期结束
  568. // 生成截止本期数据 final数据
  569. // 同步 期信息
  570. await transaction.update(this.ctx.service.changeProject.tableName, {
  571. id: cpId, status: checkData.checkType,
  572. });
  573. // 微信模板通知
  574. const users = this._.uniq(this._.map(auditors, 'aid'));
  575. const shenpiUrl = await this.ctx.helper.urlToShort(
  576. this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/change/project/' + cpId + '/information#shenpi'
  577. );
  578. const wechatData = {
  579. type: 'project',
  580. wap_url: shenpiUrl,
  581. status: wxConst.status.success,
  582. tips: wxConst.tips.success,
  583. code: this.ctx.session.sessionProject.code,
  584. c_name: this.ctx.change.name,
  585. };
  586. await this.ctx.helper.sendWechat(users, smsTypeConst.const.BG, smsTypeConst.judge.result.toString(), wxConst.template.change, wechatData);
  587. // 检查三方特殊推送
  588. await this.ctx.service.specMsg.addChangeProjectMsg(transaction, pid, this.ctx.change, pushOperate.change_project.flow);
  589. await this.ctx.service.specMsg.addChangeProjectMsg(transaction, pid, this.ctx.change, pushOperate.change_project.checked);
  590. }
  591. } else {
  592. // 同步 期信息
  593. await transaction.update(this.ctx.service.changeProject.tableName, {
  594. id: cpId,
  595. status: auditConst.status.checking,
  596. });
  597. }
  598. await transaction.commit();
  599. } catch (err) {
  600. await transaction.rollback();
  601. throw err;
  602. }
  603. }
  604. async _back(pid, cpId, checkData, times) {
  605. const accountId = this.ctx.session.sessionUser.accountId;
  606. const time = new Date();
  607. const changeData = await this.ctx.service.changeProject.getDataById(cpId);
  608. // 整理当前流程审核人状态更新
  609. const audits = await this.getAllDataByCondition({ where: { cpid: cpId, times, status: auditConst.status.checking } });
  610. if (!audits) throw '审核数据错误';
  611. const selfAudit = audits.find(x => { return x.aid === accountId; });
  612. if (!selfAudit) throw '当前标段您无权审批';
  613. // const flowAudits = await this.getAllDataByCondition({ where: { cpid: cpId, times: selfAudit.times, order: selfAudit.order } });
  614. const auditors = await this.getUniqAuditor(cpId, times); // 全部参与的审批人
  615. const newAuditors = auditors.map(x => {
  616. return {
  617. aid: x.aid, tid: selfAudit.tid, cpid: selfAudit.cpid,
  618. times: times + 1, order: x.audit_order, status: auditConst.status.uncheck,
  619. audit_type: x.audit_type, audit_order: x.audit_order,
  620. };
  621. });
  622. const transaction = await this.db.beginTransaction();
  623. try {
  624. const updateData = [];
  625. audits.forEach(x => {
  626. updateData.push({
  627. id: x.id,
  628. status: x.aid === selfAudit.aid ? checkData.checkType : auditConst.status.checkSkip,
  629. opinion: x.aid === selfAudit.aid ? checkData.opinion : '',
  630. end_time: x.aid === selfAudit.aid ? time : null,
  631. });
  632. });
  633. await transaction.updateRows(this.tableName, updateData);
  634. await this.ctx.service.noticeAgain.stopNoticeAgain(transaction, this.tableName, this._.map(updateData, 'id'));
  635. // 添加到消息推送表
  636. const noticeContent = await this.getNoticeContent(pid, selfAudit.tid, cpId, selfAudit.aid, checkData.opinion);
  637. const records = [{ pid, tid: selfAudit.tid, type: pushType.changeProject, uid: this.ctx.change.uid, status: auditConst.status.back, content: noticeContent }];
  638. auditors.forEach(audit => {
  639. records.push({ pid, tid: selfAudit.tid, type: pushType.changeProject, uid: audit.aid, status: auditConst.status.back, content: noticeContent });
  640. });
  641. await transaction.insert(this.ctx.service.noticePush.tableName, records);
  642. // 同步期信息
  643. await transaction.update(this.ctx.service.changeProject.tableName, {
  644. id: cpId, status: checkData.checkType,
  645. times: times + 1,
  646. });
  647. // 拷贝新一次审核流程列表
  648. await transaction.insert(this.tableName, newAuditors);
  649. // 微信模板通知
  650. const users = this._.uniq(this._.concat(this._.map(auditors, 'aid'), this.ctx.change.uid));
  651. const shenpiUrl = await this.ctx.helper.urlToShort(
  652. this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/change/project/' + cpId + '/information#shenpi'
  653. );
  654. const wechatData = {
  655. type: 'project',
  656. wap_url: shenpiUrl,
  657. status: wxConst.status.back,
  658. tips: wxConst.tips.back,
  659. code: this.ctx.session.sessionProject.code,
  660. c_name: this.ctx.change.name,
  661. };
  662. await this.ctx.helper.sendWechat(users, smsTypeConst.const.BG, smsTypeConst.judge.result.toString(), wxConst.template.change, wechatData);
  663. // 生成内容保存表至zh_change_project_history中,用于撤回
  664. await this.ctx.service.changeProjectHistory.saveHistory(transaction, changeData);
  665. // 检查三方特殊推送
  666. await this.ctx.service.specMsg.addChangeProjectMsg(transaction, pid, this.ctx.change, pushOperate.change_project.flow);
  667. await transaction.commit();
  668. } catch (err) {
  669. await transaction.rollback();
  670. throw err;
  671. }
  672. }
  673. async _checkNo(pid, cpId, checkData, times) {
  674. const accountId = this.ctx.session.sessionUser.accountId;
  675. const time = new Date();
  676. // 整理当前流程审核人状态更新
  677. const audits = await this.getAllDataByCondition({ where: { cpid: cpId, times, status: auditConst.status.checking } });
  678. if (!audits) throw '审核数据错误';
  679. const selfAudit = audits.find(x => { return x.aid === accountId; });
  680. if (!selfAudit) throw '当前标段您无权终止';
  681. const auditors = await this.getUniqAuditor(cpId, times); // 全部参与的审批人
  682. // 获取审核人列表
  683. // const sql = 'SELECT `tid`, `cpid`, `aid`, `order` FROM ?? WHERE `cpid` = ? and `times` = ? GROUP BY `aid` ORDER BY `id` ASC';
  684. // const sqlParam = [this.tableName, cpId, times];
  685. // const auditors = await this.db.query(sql, sqlParam);
  686. const transaction = await this.db.beginTransaction();
  687. try {
  688. const updateData = [];
  689. audits.forEach(x => {
  690. updateData.push({
  691. id: x.id,
  692. status: x.aid === selfAudit.aid ? checkData.checkType : auditConst.status.checkSkip,
  693. opinion: x.aid === selfAudit.aid ? checkData.opinion : '',
  694. end_time: x.aid === selfAudit.aid ? time : null,
  695. });
  696. });
  697. await transaction.updateRows(this.tableName, updateData);
  698. await this.ctx.service.noticeAgain.stopNoticeAgain(transaction, this.tableName, this._.map(updateData, 'id'));
  699. // 获取推送必要信息
  700. const noticeContent = await this.getNoticeContent(pid, selfAudit.tid, cpId, selfAudit.aid, checkData.opinion);
  701. // 添加推送
  702. const records = [{ pid, tid: selfAudit.tid, type: pushType.changeProject, uid: this.ctx.change.uid, status: auditConst.status.checkNo, content: noticeContent }];
  703. auditors.forEach(audit => {
  704. records.push({ pid, tid: selfAudit.tid, type: pushType.changeProject, uid: audit.aid, status: auditConst.status.checkNo, content: noticeContent });
  705. });
  706. await transaction.insert('zh_notice', records);
  707. // 本期结束
  708. // 生成截止本期数据 final数据
  709. // 同步 期信息
  710. await transaction.update(this.ctx.service.changeProject.tableName, {
  711. id: cpId, status: checkData.checkType,
  712. });
  713. // 微信模板通知
  714. const users = this._.uniq(this._.concat(this._.map(auditors, 'aid'), this.ctx.change.uid));
  715. const shenpiUrl = await this.ctx.helper.urlToShort(
  716. this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/change/project/' + cpId + '/information#shenpi'
  717. );
  718. const wechatData = {
  719. type: 'project',
  720. wap_url: shenpiUrl,
  721. status: wxConst.status.stop,
  722. tips: wxConst.tips.stop,
  723. code: this.ctx.session.sessionProject.code,
  724. c_name: this.ctx.change.name,
  725. };
  726. await this.ctx.helper.sendWechat(users, smsTypeConst.const.BG, smsTypeConst.judge.result.toString(), wxConst.template.change, wechatData);
  727. // 检查三方特殊推送
  728. await this.ctx.service.specMsg.addChangeProjectMsg(transaction, pid, this.ctx.change, pushOperate.change_project.flow);
  729. await transaction.commit();
  730. } catch (err) {
  731. await transaction.rollback();
  732. throw err;
  733. }
  734. }
  735. /**
  736. * 复制上一期的审批人列表给最新一期
  737. *
  738. * @param transaction - 新增一期的事务
  739. * @param {Object} preMaterial - 上一期
  740. * @param {Object} newaMaterial - 最新一期
  741. * @return {Promise<*>}
  742. */
  743. async copyPreChangeProjectAuditors(transaction, preChange, newChange) {
  744. // 再获取非原报审批人
  745. const auditList = await this.getUniqAuditor(preChange.id, preChange.times); // 全部参与的审批人
  746. const newAuditors = [];
  747. for (const x of auditList) {
  748. if (x.audit_order !== 0) {
  749. newAuditors.push({
  750. tid: preChange.tid, cpid: newChange.id, aid: x.aid,
  751. times: 1, order: x.audit_order, status: auditConst.status.uncheck,
  752. audit_type: x.audit_type, audit_order: x.audit_order,
  753. });
  754. }
  755. }
  756. const result = newAuditors.length > 0 ? await transaction.insert(this.tableName, newAuditors) : null;
  757. return result ? result.affectedRows === newAuditors.length : true;
  758. }
  759. async getAllAuditors(tenderId) {
  760. const sql = 'SELECT ma.aid, ma.tid FROM ' + this.tableName + ' ma' +
  761. ' LEFT JOIN ' + this.ctx.service.tender.tableName + ' t On ma.tid = t.id' +
  762. ' WHERE t.id = ?' +
  763. ' GROUP BY ma.aid';
  764. const sqlParam = [tenderId];
  765. return this.db.query(sql, sqlParam);
  766. }
  767. /**
  768. * 审批撤回
  769. * @param {Number} stageId - 标段id
  770. * @param {Number} times - 第几次审批
  771. * @return {Promise<void>}
  772. */
  773. async checkCancel(change) {
  774. // 分5种情况,根据ctx.cancancel值判断:
  775. // 1.原报发起撤回,当前流程删除,并回到待上报
  776. // 2.审批人撤回审批通过,增加流程,并回到它审批中
  777. // 3.审批人撤回审批终止,增加流程,并回到它审批中,并更新立项状态为审批中
  778. // 4.审批人撤回退回原报操作,删除新增的审批流,增加流程,回滚到它审批中
  779. // 5.会签审批人撤回审批通过(还有其他审批人未审批通过),仅修改本人流程状态
  780. switch (change.cancancel) {
  781. case 1: await this._userCheckCancel(change); break;
  782. case 2: await this._auditCheckCancel(change); break;
  783. case 3: await this._auditCheckCancelStop(change); break;
  784. case 4: await this._auditCheckCancelNo(change); break;
  785. case 5: await this._auditCheckCancelAnd(change); break;
  786. default: throw '不可撤回,请刷新页面重试';
  787. }
  788. }
  789. /**
  790. * 原报撤回,直接改动审批人状态
  791. * 如果存在审批人数据,将其改为原报流程数据,但保留原提交人
  792. *
  793. * 一审 1 A checking -> A uncheck status改 pay/jl:删0(jl为增量数据,只删重复部分) 1->0 删1
  794. * ...
  795. *
  796. * @param stage
  797. * @returns {Promise<void>}
  798. * @private
  799. */
  800. async _userCheckCancel(change) {
  801. const transaction = await this.db.beginTransaction();
  802. try {
  803. // 整理当前流程审核人状态更新
  804. // 审批人变成待审批状态
  805. const updateData = change.curAuditors.map(x => {
  806. return {
  807. id: x.id,
  808. status: auditConst.status.uncheck,
  809. begin_time: null, opinion: null,
  810. };
  811. });
  812. await transaction.updateRows(this.tableName, updateData);
  813. await this.ctx.service.noticeAgain.deleteNoticeAgain(transaction, this.tableName, this._.map(updateData, 'id'));
  814. // 变成待上报状态
  815. await transaction.update(this.ctx.service.changeProject.tableName, {
  816. id: change.id,
  817. status: change.times === 1 ? auditConst.status.uncheck : auditConst.status.back,
  818. });
  819. await transaction.commit();
  820. } catch (err) {
  821. await transaction.rollback();
  822. throw err;
  823. }
  824. }
  825. /**
  826. * 审批人撤回审批通过,插入两条数据
  827. *
  828. * 一审 1 A checked 一审 1 A checked
  829. * 二审 2 B checked pre -> 二审 2 B checked
  830. * 三审 3 C checking cur 二审 3 B checkCancel 增 增extra_his 增tp_his
  831. * 四审 4 D uncheck 二审 4 B checking 增 增pay_cur
  832. * 三审 5 C uncheck order、status改
  833. * 四审 6 D uncheck order改
  834. *
  835. * @param stage
  836. * @returns {Promise<void>}
  837. * @private
  838. */
  839. async _auditCheckCancel(change) {
  840. if (change.curAuditors.length === 0 || change.curAuditors[0].order <= 1) {
  841. throw '撤回用户数据错误';
  842. }
  843. const accountId = this.ctx.session.sessionUser.accountId;
  844. const selfAuditor = change.preAuditors.find(x => { return x.aid === accountId; });
  845. if (!selfAuditor) throw '撤回用户数据错误';
  846. const time = new Date();
  847. const transaction = await this.db.beginTransaction();
  848. try {
  849. // 顺移其后审核人流程顺序
  850. const sql = 'UPDATE ' + this.tableName + ' SET `order` = `order` + 2 WHERE cpid = ? AND times = ? AND `order` > ?';
  851. await transaction.query(sql, [change.id, selfAuditor.times, change.curAuditors[0].order]);
  852. // 当前审批人2次添加至流程中
  853. const checkCancelAuditors = [],
  854. checkingAuditors = [];
  855. change.preAuditors.forEach(x => {
  856. checkCancelAuditors.push({
  857. tid: change.tid, cpid: change.id, aid: x.aid,
  858. times: x.times, order: x.order + 1,
  859. status: x.aid === selfAuditor.aid ? auditConst.status.checkCancel : auditConst.status.checkSkip,
  860. begin_time: time, end_time: time, opinion: '',
  861. audit_type: x.audit_type, audit_order: x.audit_order,
  862. });
  863. });
  864. change.preAuditors.forEach(x => {
  865. checkingAuditors.push({
  866. tid: change.tid, cpid: change.id, aid: x.aid,
  867. times: x.times, order: x.order + 2,
  868. status: auditConst.status.checking,
  869. begin_time: time, end_time: time, opinion: '',
  870. audit_type: x.audit_type, audit_order: x.audit_order,
  871. });
  872. });
  873. await transaction.insert(this.tableName, [...checkCancelAuditors, ...checkingAuditors]);
  874. // 当前审批人变成待审批
  875. await transaction.updateRows(this.tableName, change.curAuditors.map(x => {
  876. return { id: x.id, begin_time: null, status: auditConst.status.uncheck, order: x.order + 2 };
  877. }));
  878. await this.ctx.service.noticeAgain.deleteNoticeAgain(transaction, this.tableName, this._.map(change.curAuditors, 'id'));
  879. await transaction.commit();
  880. } catch (err) {
  881. await transaction.rollback();
  882. throw err;
  883. }
  884. }
  885. /**
  886. * 审批人撤回审批终止,插入两条数据
  887. *
  888. * @param stage
  889. * @returns {Promise<void>}
  890. * @private
  891. */
  892. async _auditCheckCancelStop(change) {
  893. const accountId = this.ctx.session.sessionUser.accountId;
  894. const selfAuditor = change.preAuditors.find(x => { return x.aid === accountId && x.status === auditConst.status.checkNo; });
  895. if (!selfAuditor) throw '该变更立项由他人审批终止,您不可撤回';
  896. const time = new Date();
  897. const transaction = await this.db.beginTransaction();
  898. try {
  899. // 顺移其后审核人流程顺序
  900. const sql = 'UPDATE ' + this.tableName + ' SET `order` = `order` + 2 WHERE cpid = ? AND times = ? AND `order` > ?';
  901. await transaction.query(sql, [change.id, change.times, selfAuditor.order]);
  902. // 当前审批人2次添加至流程中
  903. const checkCancelAuditors = [],
  904. checkingAuditors = [];
  905. change.preAuditors.forEach(x => {
  906. checkCancelAuditors.push({
  907. tid: change.tid, cpid: change.id, aid: x.aid,
  908. times: x.times, order: x.order + 1,
  909. status: x.aid === selfAuditor.aid ? auditConst.status.checkCancel : auditConst.status.checkSkip,
  910. begin_time: time, end_time: time, opinion: '',
  911. audit_type: x.audit_type, audit_order: x.audit_order,
  912. });
  913. });
  914. change.preAuditors.forEach(x => {
  915. checkingAuditors.push({
  916. tid: change.tid, cpid: change.id, aid: x.aid,
  917. times: x.times, order: x.order + 2,
  918. status: auditConst.status.checking,
  919. begin_time: time, end_time: time, opinion: '',
  920. audit_type: x.audit_type, audit_order: x.audit_order,
  921. });
  922. });
  923. await transaction.insert(this.tableName, [...checkCancelAuditors, ...checkingAuditors]);
  924. await transaction.update(this.ctx.service.changeProject.tableName, {
  925. id: change.id,
  926. status: auditConst.status.checking,
  927. });
  928. await transaction.commit();
  929. } catch (err) {
  930. await transaction.rollback();
  931. throw err;
  932. }
  933. }
  934. /**
  935. * 审批人撤回审批退回原报
  936. *
  937. * 1# 一审 1 A checked 1# 一审 1 A checked
  938. * 二审 2 B checkNo pre -> 二审 2 B checkNo
  939. * 三审 3 C uncheck 二审 3 B checkCancel 增 pay: 2#0 -> 1#3 jl: 2#0 -> 1#3 增tp_his 增extra_his
  940. * 二审 4 B checking 增 pay: 2#0 -> 1#4
  941. * 三审 5 C uncheck order改
  942. *
  943. * 2# 一审 1 A uncheck 2# 删 pay: 2#0删 jl: 2#0删
  944. * 二审 2 B uncheck
  945. * 三审 3 C uncheck
  946. *
  947. * @param stage
  948. * @returns {Promise<void>}
  949. * @private
  950. */
  951. async _auditCheckCancelNo(change) {
  952. const accountId = this.ctx.session.sessionUser.accountId;
  953. const selfAuditor = change.preAuditors.find(x => { return x.aid === accountId && x.status === auditConst.status.back; });
  954. if (!selfAuditor) throw '该标段由他人审批退回,您不可撤回';
  955. const time = new Date();
  956. const transaction = await this.db.beginTransaction();
  957. try {
  958. // 整理上一个流程审核人状态更新
  959. // 顺移其后审核人流程顺序
  960. const sql = 'UPDATE ' + this.tableName + ' SET `order` = `order` + 2 WHERE cpid = ? AND times = ? AND `order` > ?';
  961. await transaction.query(sql, [change.id, selfAuditor.times, selfAuditor.order]);
  962. // 当前审批人2次添加至流程中
  963. const checkCancelAuditors = [],
  964. checkingAuditors = [];
  965. change.preAuditors.forEach(x => {
  966. checkCancelAuditors.push({
  967. tid: change.tid, cpid: change.id, aid: x.aid,
  968. times: x.times, order: x.order + 1,
  969. status: x.aid === selfAuditor.aid ? auditConst.status.checkCancel : auditConst.status.checkSkip,
  970. begin_time: time, end_time: time, opinion: '',
  971. audit_type: x.audit_type, audit_order: x.audit_order,
  972. });
  973. });
  974. change.preAuditors.forEach(x => {
  975. checkingAuditors.push({
  976. tid: change.tid, cpid: change.id, aid: x.aid,
  977. times: x.times, order: x.order + 2,
  978. status: auditConst.status.checking,
  979. begin_time: time, end_time: time, opinion: '',
  980. audit_type: x.audit_type, audit_order: x.audit_order,
  981. });
  982. });
  983. await transaction.insert(this.tableName, [...checkCancelAuditors, ...checkingAuditors]);
  984. // 删除当前次审批流
  985. await transaction.delete(this.tableName, { cpid: change.id, times: change.times });
  986. // 回退数据
  987. await this.ctx.service.changeProjectHistory.returnHistory(transaction, change.id);
  988. await transaction.delete(this.ctx.service.changeProjectHistory.tableName, { cpid: change.id });
  989. // // 设置变更立项为审批中
  990. // await transaction.update(this.ctx.service.changeProject.tableName, {
  991. // id: change.id,
  992. // time: change.times - 1,
  993. // status: auditConst.status.checking,
  994. // });
  995. await transaction.commit();
  996. } catch (err) {
  997. await transaction.rollback();
  998. throw err;
  999. }
  1000. }
  1001. /**
  1002. * 会签未全部审批通过时,撤回仅修改本人状态
  1003. *
  1004. * @param stage
  1005. * @returns {Promise<void>}
  1006. * @private
  1007. */
  1008. async _auditCheckCancelAnd(change) {
  1009. const accountId = this.ctx.session.sessionUser.accountId;
  1010. const selfAuditor = change.flowAuditors.find(x => { return x.aid === accountId; });
  1011. if (!selfAuditor || selfAuditor.status !== auditConst.status.checked) throw '不可撤回';
  1012. const transaction = await this.db.beginTransaction();
  1013. try {
  1014. await transaction.update(this.tableName, {
  1015. id: selfAuditor.id, status: auditConst.status.checking, opinion: '', end_time: null,
  1016. });
  1017. await transaction.commit();
  1018. } catch (err) {
  1019. await transaction.rollback();
  1020. throw err;
  1021. }
  1022. }
  1023. /**
  1024. * 重新审批变更令
  1025. * @param { string } cid - 查询的清单
  1026. * @return {Promise<*>} - 可用的变更令列表
  1027. */
  1028. async checkRevise(change) {
  1029. const accountId = this.ctx.session.sessionUser.accountId;
  1030. const time = new Date();
  1031. // 初始化事务
  1032. const transaction = await this.db.beginTransaction();
  1033. let result = false;
  1034. try {
  1035. const pid = this.ctx.session.sessionProject.id;
  1036. // 获取审核人列表
  1037. const auditors = await this.getUniqAuditor(change.id, change.times); // 全部参与的审批人
  1038. // const sql = 'SELECT `tid`, `cpid`, `aid`, `order` FROM ?? WHERE `cpid` = ? and `times` = ? GROUP BY `aid` ORDER BY `id` ASC';
  1039. // const sqlParam = [this.tableName, change.id, change.times];
  1040. // const auditors = await this.db.query(sql, sqlParam);
  1041. // 添加到消息推送表
  1042. const noticeContent = await this.getNoticeContent(pid, change.tid, change.id, this.ctx.session.sessionUser.accountId, '发起修订');
  1043. const records = [{ pid, tid: change.tid, type: pushType.changeProject, uid: change.uid, status: auditConst.status.revise, content: noticeContent }];
  1044. auditors.forEach(auditor => {
  1045. records.push({
  1046. pid,
  1047. tid: change.tid,
  1048. type: pushType.changeProject,
  1049. uid: auditor.aid,
  1050. status: auditConst.status.revise,
  1051. content: noticeContent,
  1052. });
  1053. });
  1054. await transaction.insert('zh_notice', records);
  1055. const order = change.auditors.length > 0 ? change.auditors.reduce((rst, a) => { return Math.max(rst, a.order); }, 0) + 1 : 1; // 最大值 + 1
  1056. // 当前审批人添加至流程中
  1057. const checkReviseAuditor = {
  1058. tid: change.tid, cpid: change.id, aid: change.uid,
  1059. times: change.times, order,
  1060. status: auditConst.status.revise,
  1061. begin_time: time, end_time: time, opinion: '',
  1062. audit_type: auditType.key.common, audit_order: 0,
  1063. };
  1064. await transaction.insert(this.tableName, checkReviseAuditor);
  1065. // 修订人加入审批流程
  1066. const newAuditors = auditors.map(x => {
  1067. return {
  1068. aid: x.aid, tid: change.tid, cpid: change.id,
  1069. times: change.times + 1, order: x.audit_order, status: auditConst.status.uncheck,
  1070. audit_type: x.audit_type, audit_order: x.audit_order,
  1071. };
  1072. });
  1073. await transaction.insert(this.tableName, newAuditors);
  1074. // 生成内容保存表至zh_change_history中,用于撤销修订回退
  1075. const changeData = await transaction.get(this.ctx.service.changeProject.tableName, { id: change.id });
  1076. await this.ctx.service.changeProjectHistory.saveHistory(transaction, changeData);
  1077. // 设置变更立项修订状态
  1078. await transaction.update(this.ctx.service.changeProject.tableName, {
  1079. id: change.id,
  1080. status: auditConst.status.revise,
  1081. times: change.times + 1,
  1082. final_auditor_str: '',
  1083. });
  1084. await transaction.commit();
  1085. result = true;
  1086. } catch (error) {
  1087. console.log(error);
  1088. await transaction.rollback();
  1089. result = false;
  1090. }
  1091. return result;
  1092. }
  1093. /**
  1094. * 撤销修订变更令
  1095. * @param { string } cid - 查询的清单
  1096. * @return {Promise<*>} - 可用的变更令列表
  1097. */
  1098. async cancelRevise(change) {
  1099. const time = new Date();
  1100. // 初始化事务
  1101. const transaction = await this.db.beginTransaction();
  1102. let result = false;
  1103. try {
  1104. const pid = this.ctx.session.sessionProject.id;
  1105. // 获取审核人列表
  1106. const auditors = await this.getUniqAuditor(change.id, change.times - 1); // 全部参与的审批人
  1107. // 添加到消息推送表
  1108. const noticeContent = await this.getNoticeContent(pid, change.tid, change.id, this.ctx.session.sessionUser.accountId, '撤销修订');
  1109. const records = [{ pid, tid: change.tid, type: pushType.changeProject, uid: change.uid, status: auditConst.status.cancelRevise, content: noticeContent }];
  1110. auditors.forEach(auditor => {
  1111. records.push({
  1112. pid,
  1113. tid: change.tid,
  1114. type: pushType.changeProject,
  1115. uid: auditor.aid,
  1116. status: auditConst.status.cancelRevise,
  1117. content: noticeContent,
  1118. });
  1119. });
  1120. await transaction.insert('zh_notice', records);
  1121. const lastAudit = await this.getLastAudit(change.id, change.times - 1);
  1122. // 新增一个撤销修订状态到审批流程中
  1123. const revise_audit = {
  1124. tid: change.tid, cpid: change.id, aid: this.ctx.session.sessionUser.accountId,
  1125. times: change.times - 1, order: lastAudit.order + 1, status: auditConst.status.cancelRevise,
  1126. begin_time: time, end_time: time, opinion: '',
  1127. audit_type: lastAudit.audit_type, audit_order: lastAudit.audit_order,
  1128. };
  1129. await transaction.insert(this.tableName, revise_audit);
  1130. await transaction.delete(this.tableName, { cpid: change.id, times: change.times });
  1131. await this.ctx.service.changeProjectHistory.returnHistory(transaction, change.id);
  1132. await transaction.delete(this.ctx.service.changeProjectHistory.tableName, { cpid: change.id });
  1133. await transaction.commit();
  1134. result = true;
  1135. } catch (error) {
  1136. console.log(error);
  1137. await transaction.rollback();
  1138. result = false;
  1139. }
  1140. return result;
  1141. }
  1142. /**
  1143. * 重新审批变更立项
  1144. * @param { string } cid - 查询的清单
  1145. * @return {Promise<*>} - 可用的变更令列表
  1146. */
  1147. async checkAgain(change) {
  1148. const accountId = this.ctx.session.sessionUser.accountId;
  1149. // 初始化事务
  1150. const time = new Date();
  1151. const transaction = await this.db.beginTransaction();
  1152. let result = false;
  1153. try {
  1154. const noYbAuditors = change.auditors.filter(x => { return x.audit_order !== 0; });
  1155. const noYbMaxOrder = noYbAuditors[noYbAuditors.length - 1].order;
  1156. const maxOrder = change.auditors[change.auditors.length - 1].order;
  1157. const audits = change.auditors.filter(x => { return x.order === noYbMaxOrder; });
  1158. if (!audits || audits.length === 0 || maxOrder < 1) throw '审核数据错误';
  1159. const selfAudit = audits.find(x => { return x.aid === accountId; });
  1160. if (!selfAudit) throw '当前标段您无权审批';
  1161. // 当前审批人2次添加至流程中
  1162. const checkAgainAuditors = [];
  1163. audits.forEach(x => {
  1164. checkAgainAuditors.push({
  1165. tid: change.tid, cpid: change.id, aid: x.aid,
  1166. times: x.times, order: maxOrder + 1,
  1167. status: !selfAudit || x.aid === selfAudit.aid ? auditConst.status.checkAgain : auditConst.status.checkSkip,
  1168. begin_time: time, end_time: time, opinion: '',
  1169. audit_type: x.audit_type, audit_order: x.audit_order,
  1170. });
  1171. });
  1172. const checkingAuditors = [];
  1173. audits.forEach(x => {
  1174. checkingAuditors.push({
  1175. tid: change.tid, cpid: change.id, aid: x.aid,
  1176. times: x.times, order: maxOrder + 2,
  1177. status: auditConst.status.checking,
  1178. begin_time: time,
  1179. audit_type: x.audit_type, audit_order: x.audit_order,
  1180. });
  1181. });
  1182. await transaction.insert(this.tableName, checkAgainAuditors);
  1183. const checkingAuditors_result = await transaction.insert(this.tableName, checkingAuditors);
  1184. // 获取刚批量添加的所有list
  1185. // for (let j = 0; j < checkingAuditors.length; j++) {
  1186. // checkingAuditors[j].id = checkingAuditors_result.insertId + j;
  1187. // }
  1188. // 设置变更令审批中
  1189. await transaction.update(this.ctx.service.changeProject.tableName, {
  1190. id: change.id,
  1191. status: auditConst.status.checking,
  1192. final_auditor_str: '',
  1193. });
  1194. // 检查三方特殊推送
  1195. await this.ctx.service.specMsg.addChangeProjectMsg(transaction, this.ctx.session.sessionProject.id, this.ctx.change, pushOperate.change_project.flow);
  1196. await transaction.commit();
  1197. result = true;
  1198. } catch (error) {
  1199. console.log(error);
  1200. await transaction.rollback();
  1201. result = false;
  1202. }
  1203. return result;
  1204. }
  1205. async getAuditorGroup(cpId, times) {
  1206. const auditors = await this.getAuditors(cpId, times); // 全部参与的审批人
  1207. return this.ctx.helper.groupAuditors(auditors, 'order', true);
  1208. }
  1209. async getUserGroup(cpId, times) {
  1210. const group = await this.getAuditorGroup(cpId, times);
  1211. const sql =
  1212. 'SELECT pa.`id` As aid, pa.`name`, pa.`company`, pa.`role`, ? As times, ? As cpid, 0 As `order`, 1 As audit_type, 0 As audit_order' +
  1213. ' FROM ' + this.ctx.service.changeProject.tableName + ' As s' +
  1214. ' LEFT JOIN ' + this.ctx.service.projectAccount.tableName + ' As pa' +
  1215. ' ON s.uid = pa.id' +
  1216. ' WHERE s.id = ?';
  1217. const sqlParam = [times, cpId, cpId];
  1218. const user = await this.db.queryOne(sql, sqlParam);
  1219. user.audit_order = 0;
  1220. group.unshift([ user ]);
  1221. return group;
  1222. }
  1223. async getUniqUserGroup(cpId, times) {
  1224. const group = await this.getAuditorGroup(cpId, times);
  1225. const sql =
  1226. 'SELECT pa.`id` As aid, pa.`name`, pa.`company`, pa.`role`, ? As times, ? As cpid, 0 As `order`, 1 As audit_type, 0 As audit_order' +
  1227. ' FROM ' + this.ctx.service.changeProject.tableName + ' As s' +
  1228. ' LEFT JOIN ' + this.ctx.service.projectAccount.tableName + ' As pa' +
  1229. ' ON s.uid = pa.id' +
  1230. ' WHERE s.id = ?';
  1231. const sqlParam = [times, cpId, cpId];
  1232. const user = await this.db.queryOne(sql, sqlParam);
  1233. user.audit_order = 0;
  1234. group.unshift([ user ]);
  1235. return this.ctx.helper.groupAuditorsUniq(group);
  1236. }
  1237. async getAuditorHistory(cpId, times, reverse = false) {
  1238. const history = [];
  1239. if (times >= 1) {
  1240. for (let i = 1; i <= times; i++) {
  1241. const auditors = await this.getAuditors(cpId, i);
  1242. const group = this.ctx.helper.groupAuditors(auditors);
  1243. const historyGroup = [];
  1244. // 找出group里audit_order最大值
  1245. const max_info = group.length > 0 ? this._.maxBy(group, function(item) {
  1246. return item && item[0] && item[0].audit_order;
  1247. }) : null;
  1248. const max_order = max_info ? max_info[0].audit_order : -1;
  1249. for (const g of group) {
  1250. const his = {
  1251. beginYear: '', beginDate: '', beginTime: '', endYear: '', endDate: '', endTime: '', begin_time: null, end_time: null,
  1252. audit_type: g[0].audit_type, audit_order: g[0].audit_order,
  1253. auditors: g,
  1254. };
  1255. if (his.audit_type === auditType.key.common) {
  1256. his.name = g[0].name;
  1257. } else {
  1258. his.name = this.ctx.helper.transFormToChinese(his.audit_order) + '审';
  1259. }
  1260. his.is_final = his.audit_order === max_order;
  1261. if (g[0].begin_time) {
  1262. his.begin_time = g[0].begin_time;
  1263. const beginTime = this.ctx.moment(g[0].begin_time);
  1264. his.beginYear = beginTime.format('YYYY');
  1265. his.beginDate = beginTime.format('MM-DD');
  1266. his.beginTime = beginTime.format('HH:mm:ss');
  1267. }
  1268. let end_time;
  1269. g.forEach(x => {
  1270. if (x.status === auditConst.status.checkSkip) return;
  1271. if (!his.status || x.status === auditConst.status.checking) his.status = x.status;
  1272. if (x.end_time && (!end_time || x.end_time > end_time)) {
  1273. end_time = x.end_time;
  1274. if (his.status !== auditConst.status.checking) his.status = x.status;
  1275. }
  1276. });
  1277. if (end_time) {
  1278. his.end_time = end_time;
  1279. const endTime = this.ctx.moment(end_time);
  1280. his.endYear = endTime.format('YYYY');
  1281. his.endDate = endTime.format('MM-DD');
  1282. his.endTime = endTime.format('HH:mm:ss');
  1283. }
  1284. historyGroup.push(his);
  1285. }
  1286. if (reverse) {
  1287. history.push(historyGroup.reverse());
  1288. } else {
  1289. history.push(historyGroup);
  1290. }
  1291. }
  1292. }
  1293. return history;
  1294. }
  1295. async getUniqAuditor(cpId, times) {
  1296. const auditors = await this.getAuditors(cpId, times); // 全部参与的审批人
  1297. const result = [];
  1298. auditors.forEach(x => {
  1299. if (result.findIndex(r => { return x.aid === r.aid && x.audit_order === r.audit_order; }) < 0) {
  1300. result.push(x);
  1301. }
  1302. });
  1303. return result;
  1304. }
  1305. async saveAudit(cpId, times, sp_group, data) {
  1306. const transaction = await this.db.beginTransaction();
  1307. try {
  1308. const auditors = await this.getAuditGroupByList(cpId, times);
  1309. const now_audit = this._.find(auditors, { aid: data.old_aid });
  1310. if (data.operate !== 'del') {
  1311. const exist = await this.getDataByCondition({ cpid: cpId, times, aid: data.new_aid });
  1312. if (exist) throw '该审核人已存在,请勿重复添加';
  1313. }
  1314. if (data.operate === 'add') {
  1315. if (now_audit.status !== auditConst.status.uncheck && now_audit.status !== auditConst.status.checking) {
  1316. throw '当前人下无法操作新增';
  1317. }
  1318. const newAudit = {
  1319. tid: this.ctx.tender.id, cpid: cpId, aid: data.new_aid,
  1320. order: now_audit.order + 1,
  1321. audit_order: now_audit.audit_order + 1, audit_type: auditType.key.common,
  1322. times, status: auditConst.status.uncheck,
  1323. };
  1324. // order+1
  1325. await this._syncOrderByDelete(transaction, cpId, now_audit.order + 1, times, '+');
  1326. await transaction.insert(this.tableName, newAudit);
  1327. // 更新审批流程页数据,如果存在
  1328. } else if (data.operate === 'add-sibling') {
  1329. if (now_audit.status !== auditConst.status.uncheck && now_audit.status !== auditConst.status.checking) {
  1330. throw '当前人下无法操作新增';
  1331. }
  1332. const newAudit = {
  1333. tid: this.ctx.tender.id, cpid: cpId, aid: data.new_aid,
  1334. order: now_audit.order,
  1335. audit_order: now_audit.audit_order, audit_type: now_audit.audit_type,
  1336. times, status: auditConst.status.uncheck,
  1337. };
  1338. await transaction.insert(this.tableName, newAudit);
  1339. } else if (data.operate === 'del') {
  1340. if (now_audit.status !== auditConst.status.uncheck) {
  1341. throw '当前人无法操作删除';
  1342. }
  1343. const flowAuditors = auditors.filter(x => { return x.audit_order === now_audit.audit_order; });
  1344. await transaction.delete(this.tableName, { cpid: cpId, times, aid: now_audit.aid, order: now_audit.order });
  1345. if (flowAuditors.length === 1) await this._syncOrderByDelete(transaction, cpId, now_audit.order, times);
  1346. // 旧的更新为is_old为1
  1347. // await transaction.update(this.tableName, { is_old: 1 }, {
  1348. // where: {
  1349. // sid: stageId,
  1350. // times,
  1351. // aid: data.old_aid,
  1352. // }
  1353. // });
  1354. } else if (data.operate === 'change') {
  1355. const nowAudit = await this.getDataByCondition({ cpid: cpId, times, aid: now_audit.aid, order: now_audit.order });
  1356. if (now_audit.status !== auditConst.status.uncheck || !nowAudit) {
  1357. throw '当前人无法操作替换';
  1358. }
  1359. nowAudit.aid = data.new_aid;
  1360. await transaction.update(this.tableName, nowAudit);
  1361. // 旧的更新为is_old为1
  1362. // await transaction.update(this.tableName, { is_old: 1 }, {
  1363. // where: {
  1364. // sid: stageId,
  1365. // times,
  1366. // aid: data.old_aid,
  1367. // }
  1368. // });
  1369. }
  1370. if (this.ctx.tender.info.shenpi.change === shenpiConst.sp_status.gdspl) {
  1371. const newAuditors = await transaction.select(this.tableName, { where: { cpid: cpId, times }, orders: [['order', 'asc']] });
  1372. const newAuditorGroup = this.ctx.helper.groupAuditors(newAuditors, 'order', true);
  1373. const uniqNewAuditorGroup = this.ctx.helper.groupAuditorsUniq(newAuditorGroup);
  1374. // await this.ctx.service.shenpiAudit.updateAuditListWithAuditType(transaction, this.ctx.tender.id, this.ctx.tender.info.shenpi.change, shenpiConst.sp_type.change, uniqNewAuditorGroup, sp_group);
  1375. } else if (this.ctx.tender.info.shenpi.change === shenpiConst.sp_status.gdzs) {
  1376. const newAuditors = await this.getAuditGroupByList(cpId, times, transaction);
  1377. await this.ctx.service.shenpiAudit.updateAuditList(transaction, this.ctx.tender.id, this.ctx.tender.info.shenpi.change, shenpiConst.sp_type.change, this._.map(newAuditors, 'aid'));
  1378. }
  1379. // 更新到审批流程方法
  1380. await transaction.commit();
  1381. } catch (err) {
  1382. await transaction.rollback();
  1383. throw err;
  1384. }
  1385. }
  1386. async changeSpGroup(change, sp_group) {
  1387. const transaction = await this.db.beginTransaction();
  1388. try {
  1389. const group = await this.ctx.service.shenpiGroup.getDataById(sp_group);
  1390. if (!group) {
  1391. throw '该固定审批组不存在,请刷新页面重新获取';
  1392. }
  1393. const shenpiList = await this.ctx.service.shenpiAudit.getAllDataByCondition({ where: { tid: this.ctx.tender.id, sp_type: shenpiConst.sp_type.change, sp_status: shenpiConst.sp_status.gdspl, sp_group: group.id } });
  1394. await this.ctx.service.shenpiAudit.noYbShenpiList(change.uid, shenpiList);
  1395. await this.updateNewAuditors(change, shenpiList, transaction);
  1396. await transaction.update(this.ctx.service.changeProject.tableName, { id: change.id, sp_group: group.id });
  1397. await transaction.commit();
  1398. } catch (err) {
  1399. await transaction.rollback();
  1400. throw err;
  1401. }
  1402. return true;
  1403. }
  1404. async updateNewAuditList(change, newList) {
  1405. const transaction = await this.db.beginTransaction();
  1406. try {
  1407. await this.updateNewAuditors(change, newList, transaction);
  1408. await transaction.commit();
  1409. } catch (err) {
  1410. await transaction.rollback();
  1411. throw err;
  1412. }
  1413. }
  1414. async updateNewAuditors(change, newList, transaction) {
  1415. // 先删除旧的审批流,再添加新的
  1416. await transaction.delete(this.tableName, { cpid: change.id, times: change.times });
  1417. const newAuditors = [];
  1418. for (const auditor of newList) {
  1419. newAuditors.push({
  1420. tid: change.tid, cpid: change.id, aid: auditor.audit_id,
  1421. times: change.times, order: auditor.audit_order, status: auditConst.status.uncheck,
  1422. audit_type: auditor.audit_type, audit_order: auditor.audit_order,
  1423. });
  1424. }
  1425. if (newAuditors.length > 0) await transaction.insert(this.tableName, newAuditors);
  1426. }
  1427. async updateLastAudit(change, auditList, lastId) {
  1428. const transaction = await this.db.beginTransaction();
  1429. try {
  1430. // 先判断auditList里的aid是否与lastId相同,相同则删除并重新更新order
  1431. const existAudit = auditList.find(x => { return x.aid === lastId; });
  1432. let order = auditList.length > 0 ? auditList.reduce((rst, a) => { return Math.max(rst, a.order); }, 0) + 1 : 1; // 最大值 + 1
  1433. if (existAudit) {
  1434. await transaction.delete(this.tableName, { cpid: change.id, times: change.times, aid: lastId });
  1435. const sameOrder = auditList.filter(x => { return x.order === existAudit.order; });
  1436. if (sameOrder.length === 1) {
  1437. const updateData = [];
  1438. auditList.forEach(x => {
  1439. if (x.order <= existAudit.order) return;
  1440. updateData.push({ id: x.id, order: x.order - 1, audit_order: x.audit_order - 1 });
  1441. });
  1442. if (updateData.length > 0) {
  1443. await transaction.updateRows(updateData);
  1444. }
  1445. order = order - 1;
  1446. }
  1447. }
  1448. // 添加终审
  1449. const newAuditor = {
  1450. tid: change.tid, cpid: change.id, aid: lastId,
  1451. times: change.times, order, status: auditConst.status.uncheck,
  1452. audit_type: auditType.key.common, audit_order: order,
  1453. };
  1454. await transaction.insert(this.tableName, newAuditor);
  1455. await transaction.commit();
  1456. } catch (err) {
  1457. await transaction.rollback();
  1458. throw err;
  1459. }
  1460. }
  1461. /**
  1462. * 取待审批期列表(wap用)
  1463. *
  1464. * @param auditorId
  1465. * @return {Promise<*>}
  1466. */
  1467. async getAuditChangeProjectByWap(auditorId) {
  1468. const sql =
  1469. 'SELECT sa.`aid`, sa.`times`, sa.`begin_time`, sa.`end_time`, sa.`tid`, sa.`cpid`,' +
  1470. ' s.*,' +
  1471. ' t.`name` as `t_name`, t.`project_id`, t.`type`, t.`user_id`, t.`spid`,' +
  1472. ' ti.`deal_info`, ti.`decimal` ' +
  1473. ' FROM ?? AS sa' +
  1474. ' Left Join ?? AS s On sa.`cpid` = s.`id`' +
  1475. ' Left Join ?? As t On sa.`tid` = t.`id`' +
  1476. ' Left Join ?? AS ti ON ti.`tid` = t.`id`' +
  1477. ' WHERE sa.`aid` = ? and sa.`status` = ?';
  1478. const sqlParam = [
  1479. this.tableName,
  1480. this.ctx.service.changeProject.tableName,
  1481. this.ctx.service.tender.tableName,
  1482. this.ctx.service.tenderInfo.tableName,
  1483. auditorId,
  1484. auditConst.status.checking,
  1485. ];
  1486. return await this.db.query(sql, sqlParam);
  1487. }
  1488. }
  1489. return ChangeProjectAudit;
  1490. };