change_apply_audit.js 69 KB

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