change_project_audit.js 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499
  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 wechatData = {
  339. type: 'project',
  340. status: wxConst.status.check,
  341. tips: wxConst.tips.check,
  342. code: this.ctx.session.sessionProject.code,
  343. c_name: this.ctx.change.name,
  344. };
  345. await this.ctx.helper.sendWechat(this._.map(audits, 'aid'), smsTypeConst.const.BG, smsTypeConst.judge.approval.toString(), wxConst.template.change, wechatData);
  346. for (const audit of audits) {
  347. await this.ctx.service.noticeAgain.addNoticeAgain(transaction, smsTypeConst.const.BG, {
  348. pid: this.ctx.session.sessionProject.id,
  349. tid: this.ctx.tender.id,
  350. uid: audit.aid,
  351. sp_type: 'change',
  352. sp_id: audit.id,
  353. table_name: this.tableName,
  354. template: wxConst.template.change,
  355. wx_data: wechatData,
  356. });
  357. }
  358. await transaction.delete(this.ctx.service.changeProjectHistory.tableName, { cpid: cpId });
  359. // todo 更新标段tender状态 ?
  360. // 检查三方特殊推送
  361. await this.ctx.service.specMsg.addChangeProjectMsg(transaction, this.ctx.session.sessionProject.id, this.ctx.change, pushOperate.change_project.flow);
  362. await transaction.commit();
  363. } catch (err) {
  364. await transaction.rollback();
  365. throw err;
  366. }
  367. return true;
  368. }
  369. /**
  370. * 获取审核人需要审核的期列表
  371. *
  372. * @param auditorId
  373. * @return {Promise<*>}
  374. */
  375. async getAuditChangeProject(auditorId) {
  376. const sql = 'SELECT ma.`aid`, ma.`times`, ma.`order`, ma.`begin_time`, ma.`end_time`, ma.`tid`, ma.`cpid`,' +
  377. ' m.`status` As `mstatus`, m.`code` As `mcode`,' +
  378. ' t.`name`, t.`project_id`, t.`type`, t.`user_id` ' +
  379. ' FROM ?? AS ma LEFT JOIN ?? AS m ON ma.`cpid` = m.`id` LEFT JOIN ?? As t ON ma.`tid` = t.`id`' +
  380. ' WHERE ((ma.`aid` = ? and ma.`status` = ?) OR (m.`uid` = ? and ma.`status` = ? and m.`status` = ? and ma.`times` = (m.`times`-1)))' +
  381. ' ORDER BY ma.`begin_time` DESC';
  382. 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];
  383. const result = await this.db.query(sql, sqlParam);
  384. // 过滤result中存在重复cpid的值, 保留最新的一条
  385. const filterResult = [];
  386. const cpidArr = [];
  387. for (const r of result) {
  388. if (cpidArr.indexOf(r.cpid) === -1) {
  389. filterResult.push(r);
  390. cpidArr.push(r.cpid);
  391. }
  392. }
  393. return filterResult;
  394. }
  395. /**
  396. * 获取审核人审核的次数
  397. *
  398. * @param auditorId
  399. * @return {Promise<*>}
  400. */
  401. async getCountByChecked(auditorId) {
  402. return await this.db.count(this.tableName, { aid: auditorId, status: [auditConst.status.checked, auditConst.status.back, auditConst.status.checkNo] });
  403. }
  404. /**
  405. * 获取最近一次审批结束时间
  406. *
  407. * @param auditorId
  408. * @return {Promise<*>}
  409. */
  410. async getLastEndTimeByChecked(auditorId) {
  411. const sql = 'SELECT `end_time` FROM ?? WHERE `aid` = ? ' +
  412. 'AND `status` in (' + this.ctx.helper.getInArrStrSqlFilter([auditConst.status.checked, auditConst.status.back, auditConst.status.checkNo]) + ') ORDER BY `end_time` DESC';
  413. const sqlParam = [this.tableName, auditorId];
  414. const result = await this.db.queryOne(sql, sqlParam);
  415. return result ? result.end_time : null;
  416. }
  417. /**
  418. * 用于添加推送所需的content内容
  419. * @param {Number} pid 项目id
  420. * @param {Number} tid 台账id
  421. * @param {Number} cpId 立项书id
  422. * @param {Number} uid 审批人id
  423. */
  424. async getNoticeContent(pid, tid, cpId, uid, opinion = '') {
  425. const noticeSql = 'SELECT * FROM (SELECT ' +
  426. ' t.`id` As `tid`, ma.`cpid`, m.`code` as `c_code`, t.`name`, pa.`name` As `su_name`, pa.role As `su_role`' +
  427. ' FROM (SELECT * FROM ?? WHERE `id` = ? ) As t' +
  428. ' LEFT JOIN ?? As m On t.`id` = m.`tid` AND m.`id` = ?' +
  429. ' LEFT JOIN ?? As ma ON m.`id` = ma.`cpid`' +
  430. ' LEFT JOIN ?? As pa ON pa.`id` = ?' +
  431. ' WHERE t.`project_id` = ? ) as new_t GROUP BY new_t.`tid`';
  432. const noticeSqlParam = [this.ctx.service.tender.tableName, tid, this.ctx.service.changeProject.tableName, cpId, this.tableName, this.ctx.service.projectAccount.tableName, uid, pid];
  433. const content = await this.db.query(noticeSql, noticeSqlParam);
  434. if (content.length) {
  435. content[0].opinion = opinion;
  436. }
  437. return content.length ? JSON.stringify(content[0]) : '';
  438. }
  439. /**
  440. * 审批
  441. * @param {Number} cpId - 立项书id
  442. * @param {auditConst.status.checked|auditConst.status.checkNo} checkType - 审批结果
  443. * @param {Number} times - 第几次审批
  444. * @return {Promise<void>}
  445. */
  446. async check(cpId, checkData, times = 1) {
  447. if (checkData.checkType !== auditConst.status.checked && checkData.checkType !== auditConst.status.checkNo && checkData.checkType !== auditConst.status.back) {
  448. throw '提交数据错误';
  449. }
  450. const pid = this.ctx.session.sessionProject.id;
  451. switch (checkData.checkType) {
  452. case auditConst.status.checked:
  453. await this._checked(pid, cpId, checkData, times);
  454. break;
  455. case auditConst.status.back:
  456. await this._back(pid, cpId, checkData, times);
  457. break;
  458. case auditConst.status.checkNo:
  459. await this._checkNo(pid, cpId, checkData, times);
  460. break;
  461. default:
  462. throw '无效审批操作';
  463. }
  464. }
  465. async _checked(pid, cpId, checkData, times) {
  466. const accountId = this.ctx.session.sessionUser.accountId;
  467. const time = new Date();
  468. // 整理当前流程审核人状态更新
  469. const audits = await this.getAllDataByCondition({ where: { cpid: cpId, times, status: auditConst.status.checking } });
  470. if (audits.length === 0) throw '审核数据错误';
  471. const selfAudit = audits.find(x => { return x.aid === accountId; });
  472. if (!selfAudit) throw '当前标段您无权审批';
  473. const flowAudits = await this.getAllDataByCondition({ where: { cpid: cpId, times, order: selfAudit.order } });
  474. const nextAudits = await this.getAllDataByCondition({ where: { cpid: cpId, times, order: selfAudit.order + 1 } });
  475. // 获取审核人列表
  476. // const sql = 'SELECT `tid`, `cpid`, `aid`, `order` FROM ?? WHERE `cpid` = ? and `times` = ? GROUP BY `aid` ORDER BY `id` ASC';
  477. // const sqlParam = [this.tableName, cpId, times];
  478. // const auditors = await this.db.query(sql, sqlParam);
  479. //
  480. // const nextAudit = await this.getDataByCondition({ cpid: cpId, times, order: audit.order + 1 });
  481. const transaction = await this.db.beginTransaction();
  482. try {
  483. // 更新本人审批状态
  484. await transaction.update(this.tableName, {
  485. id: selfAudit.id,
  486. status: checkData.checkType,
  487. opinion: checkData.opinion,
  488. end_time: time,
  489. });
  490. await this.ctx.service.noticeAgain.stopNoticeAgain(transaction, this.tableName, selfAudit.id);
  491. // 获取推送必要信息
  492. const noticeContent = await this.getNoticeContent(pid, selfAudit.tid, cpId, selfAudit.aid, checkData.opinion);
  493. // 添加推送
  494. // const records = [{ pid, type: pushType.changeProject, uid: this.ctx.change.uid, status: auditConst.status.checked, content: noticeContent }];
  495. const records = [];
  496. const auditors = await this.getAuditorsWithOwner(cpId, times);
  497. auditors.forEach(audit => {
  498. records.push({ pid, type: pushType.changeProject, uid: audit.aid, status: auditConst.status.checked, content: noticeContent });
  499. });
  500. await transaction.insert('zh_notice', records);
  501. if (audits.length === 1 || selfAudit.audit_type !== auditType.key.and) {
  502. // 或签更新他人审批状态
  503. if (selfAudit.audit_type === auditType.key.or) {
  504. const updateOther = [];
  505. for (const audit of audits) {
  506. if (audit.aid === selfAudit.aid) continue;
  507. updateOther.push({
  508. id: audit.id,
  509. status: auditConst.status.checkSkip,
  510. opinion: '',
  511. end_time: time,
  512. });
  513. await this.ctx.service.noticeAgain.stopNoticeAgain(transaction, this.tableName, audit.id);
  514. }
  515. if (updateOther.length > 0) transaction.updateRows(this.tableName, updateOther);
  516. }
  517. // 无下一审核人表示,审核结束
  518. if (nextAudits.length > 0) {
  519. // 流程至下一审批人
  520. const updateData = nextAudits.map(x => { return { id: x.id, status: auditConst.status.checking, begin_time: time }; });
  521. await transaction.updateRows(this.tableName, updateData);
  522. // 同步 期信息
  523. await transaction.update(this.ctx.service.changeProject.tableName, {
  524. id: cpId, status: auditConst.status.checking,
  525. });
  526. // 微信模板通知
  527. const wechatData = {
  528. type: 'project',
  529. status: wxConst.status.check,
  530. tips: wxConst.tips.check,
  531. code: this.ctx.session.sessionProject.code,
  532. c_name: this.ctx.change.name,
  533. };
  534. await this.ctx.helper.sendWechat(this._.map(nextAudits, 'aid'), smsTypeConst.const.BG, smsTypeConst.judge.approval.toString(), wxConst.template.change, wechatData);
  535. for (const nextAudit of nextAudits) {
  536. await this.ctx.service.noticeAgain.addNoticeAgain(transaction, smsTypeConst.const.BG, {
  537. pid: this.ctx.session.sessionProject.id,
  538. tid: this.ctx.tender.id,
  539. uid: nextAudit.aid,
  540. sp_type: 'change',
  541. sp_id: nextAudit.id,
  542. table_name: this.tableName,
  543. template: wxConst.template.change,
  544. wx_data: wechatData,
  545. });
  546. }
  547. // 检查三方特殊推送
  548. await this.ctx.service.specMsg.addChangeProjectMsg(transaction, pid, this.ctx.change, pushOperate.change_project.flow);
  549. } else {
  550. // 本期结束
  551. // 生成截止本期数据 final数据
  552. // 同步 期信息
  553. await transaction.update(this.ctx.service.changeProject.tableName, {
  554. id: cpId, status: checkData.checkType,
  555. });
  556. // 微信模板通知
  557. const users = this._.uniq(this._.map(auditors, 'aid'));
  558. const wechatData = {
  559. type: 'project',
  560. status: wxConst.status.success,
  561. tips: wxConst.tips.success,
  562. code: this.ctx.session.sessionProject.code,
  563. c_name: this.ctx.change.name,
  564. };
  565. await this.ctx.helper.sendWechat(users, smsTypeConst.const.BG, smsTypeConst.judge.result.toString(), wxConst.template.change, wechatData);
  566. // 检查三方特殊推送
  567. await this.ctx.service.specMsg.addChangeProjectMsg(transaction, pid, this.ctx.change, pushOperate.change_project.flow);
  568. await this.ctx.service.specMsg.addChangeProjectMsg(transaction, pid, this.ctx.change, pushOperate.change_project.checked);
  569. }
  570. } else {
  571. // 同步 期信息
  572. await transaction.update(this.ctx.service.changeProject.tableName, {
  573. id: cpId,
  574. status: auditConst.status.checking,
  575. });
  576. }
  577. await transaction.commit();
  578. } catch (err) {
  579. await transaction.rollback();
  580. throw err;
  581. }
  582. }
  583. async _back(pid, cpId, checkData, times) {
  584. const accountId = this.ctx.session.sessionUser.accountId;
  585. const time = new Date();
  586. const changeData = await this.ctx.service.changeProject.getDataById(cpId);
  587. // 整理当前流程审核人状态更新
  588. const audits = await this.getAllDataByCondition({ where: { cpid: cpId, times, status: auditConst.status.checking } });
  589. if (!audits) throw '审核数据错误';
  590. const selfAudit = audits.find(x => { return x.aid === accountId; });
  591. if (!selfAudit) throw '当前标段您无权审批';
  592. // const flowAudits = await this.getAllDataByCondition({ where: { cpid: cpId, times: selfAudit.times, order: selfAudit.order } });
  593. const auditors = await this.getUniqAuditor(cpId, times); // 全部参与的审批人
  594. const newAuditors = auditors.map(x => {
  595. return {
  596. aid: x.aid, tid: selfAudit.tid, cpid: selfAudit.cpid,
  597. times: times + 1, order: x.audit_order, status: auditConst.status.uncheck,
  598. audit_type: x.audit_type, audit_order: x.audit_order,
  599. };
  600. });
  601. const transaction = await this.db.beginTransaction();
  602. try {
  603. const updateData = [];
  604. audits.forEach(x => {
  605. updateData.push({
  606. id: x.id,
  607. status: x.aid === selfAudit.aid ? checkData.checkType : auditConst.status.checkSkip,
  608. opinion: x.aid === selfAudit.aid ? checkData.opinion : '',
  609. end_time: x.aid === selfAudit.aid ? time : null,
  610. });
  611. });
  612. await transaction.updateRows(this.tableName, updateData);
  613. await this.ctx.service.noticeAgain.stopNoticeAgain(transaction, this.tableName, this._.map(updateData, 'id'));
  614. // 添加到消息推送表
  615. const noticeContent = await this.getNoticeContent(pid, selfAudit.tid, cpId, selfAudit.aid, checkData.opinion);
  616. const records = [{ pid, type: pushType.changeProject, uid: this.ctx.change.uid, status: auditConst.status.back, content: noticeContent }];
  617. auditors.forEach(audit => {
  618. records.push({ pid, type: pushType.changeProject, uid: audit.aid, status: auditConst.status.back, content: noticeContent });
  619. });
  620. await transaction.insert(this.ctx.service.noticePush.tableName, records);
  621. // 同步期信息
  622. await transaction.update(this.ctx.service.changeProject.tableName, {
  623. id: cpId, status: checkData.checkType,
  624. times: times + 1,
  625. });
  626. // 拷贝新一次审核流程列表
  627. await transaction.insert(this.tableName, newAuditors);
  628. // 微信模板通知
  629. const users = this._.uniq(this._.concat(this._.map(auditors, 'aid'), this.ctx.change.uid));
  630. const wechatData = {
  631. type: 'project',
  632. status: wxConst.status.back,
  633. tips: wxConst.tips.back,
  634. code: this.ctx.session.sessionProject.code,
  635. c_name: this.ctx.change.name,
  636. };
  637. await this.ctx.helper.sendWechat(users, smsTypeConst.const.BG, smsTypeConst.judge.result.toString(), wxConst.template.change, wechatData);
  638. // 生成内容保存表至zh_change_project_history中,用于撤回
  639. await this.ctx.service.changeProjectHistory.saveHistory(transaction, changeData);
  640. // 检查三方特殊推送
  641. await this.ctx.service.specMsg.addChangeProjectMsg(transaction, pid, this.ctx.change, pushOperate.change_project.flow);
  642. await transaction.commit();
  643. } catch (err) {
  644. await transaction.rollback();
  645. throw err;
  646. }
  647. }
  648. async _checkNo(pid, cpId, checkData, times) {
  649. const accountId = this.ctx.session.sessionUser.accountId;
  650. const time = new Date();
  651. // 整理当前流程审核人状态更新
  652. const audits = await this.getAllDataByCondition({ where: { cpid: cpId, times, status: auditConst.status.checking } });
  653. if (!audits) throw '审核数据错误';
  654. const selfAudit = audits.find(x => { return x.aid === accountId; });
  655. if (!selfAudit) throw '当前标段您无权终止';
  656. const auditors = await this.getUniqAuditor(cpId, times); // 全部参与的审批人
  657. // 获取审核人列表
  658. // const sql = 'SELECT `tid`, `cpid`, `aid`, `order` FROM ?? WHERE `cpid` = ? and `times` = ? GROUP BY `aid` ORDER BY `id` ASC';
  659. // const sqlParam = [this.tableName, cpId, times];
  660. // const auditors = await this.db.query(sql, sqlParam);
  661. const transaction = await this.db.beginTransaction();
  662. try {
  663. const updateData = [];
  664. audits.forEach(x => {
  665. updateData.push({
  666. id: x.id,
  667. status: x.aid === selfAudit.aid ? checkData.checkType : auditConst.status.checkSkip,
  668. opinion: x.aid === selfAudit.aid ? checkData.opinion : '',
  669. end_time: x.aid === selfAudit.aid ? time : null,
  670. });
  671. });
  672. await transaction.updateRows(this.tableName, updateData);
  673. await this.ctx.service.noticeAgain.stopNoticeAgain(transaction, this.tableName, this._.map(updateData, 'id'));
  674. // 获取推送必要信息
  675. const noticeContent = await this.getNoticeContent(pid, selfAudit.tid, cpId, selfAudit.aid, checkData.opinion);
  676. // 添加推送
  677. const records = [{ pid, type: pushType.changeProject, uid: this.ctx.change.uid, status: auditConst.status.checkNo, content: noticeContent }];
  678. auditors.forEach(audit => {
  679. records.push({ pid, type: pushType.changeProject, uid: audit.aid, status: auditConst.status.checkNo, content: noticeContent });
  680. });
  681. await transaction.insert('zh_notice', records);
  682. // 本期结束
  683. // 生成截止本期数据 final数据
  684. // 同步 期信息
  685. await transaction.update(this.ctx.service.changeProject.tableName, {
  686. id: cpId, status: checkData.checkType,
  687. });
  688. // 微信模板通知
  689. const users = this._.uniq(this._.concat(this._.map(auditors, 'aid'), this.ctx.change.uid));
  690. const wechatData = {
  691. type: 'project',
  692. status: wxConst.status.stop,
  693. tips: wxConst.tips.stop,
  694. code: this.ctx.session.sessionProject.code,
  695. c_name: this.ctx.change.name,
  696. };
  697. await this.ctx.helper.sendWechat(users, smsTypeConst.const.BG, smsTypeConst.judge.result.toString(), wxConst.template.change, wechatData);
  698. // 检查三方特殊推送
  699. await this.ctx.service.specMsg.addChangeProjectMsg(transaction, pid, this.ctx.change, pushOperate.change_project.flow);
  700. await transaction.commit();
  701. } catch (err) {
  702. await transaction.rollback();
  703. throw err;
  704. }
  705. }
  706. /**
  707. * 复制上一期的审批人列表给最新一期
  708. *
  709. * @param transaction - 新增一期的事务
  710. * @param {Object} preMaterial - 上一期
  711. * @param {Object} newaMaterial - 最新一期
  712. * @return {Promise<*>}
  713. */
  714. async copyPreChangeProjectAuditors(transaction, preChange, newChange) {
  715. // 再获取非原报审批人
  716. const auditList = await this.getUniqAuditor(preChange.id, preChange.times); // 全部参与的审批人
  717. const newAuditors = [];
  718. for (const x of auditList) {
  719. if (x.audit_order !== 0) {
  720. newAuditors.push({
  721. tid: preChange.tid, cpid: newChange.id, aid: x.aid,
  722. times: 1, order: x.audit_order, status: auditConst.status.uncheck,
  723. audit_type: x.audit_type, audit_order: x.audit_order,
  724. });
  725. }
  726. }
  727. const result = newAuditors.length > 0 ? await transaction.insert(this.tableName, newAuditors) : null;
  728. return result ? result.affectedRows === newAuditors.length : true;
  729. }
  730. async getAllAuditors(tenderId) {
  731. const sql = 'SELECT ma.aid, ma.tid FROM ' + this.tableName + ' ma' +
  732. ' LEFT JOIN ' + this.ctx.service.tender.tableName + ' t On ma.tid = t.id' +
  733. ' WHERE t.id = ?' +
  734. ' GROUP BY ma.aid';
  735. const sqlParam = [tenderId];
  736. return this.db.query(sql, sqlParam);
  737. }
  738. /**
  739. * 审批撤回
  740. * @param {Number} stageId - 标段id
  741. * @param {Number} times - 第几次审批
  742. * @return {Promise<void>}
  743. */
  744. async checkCancel(change) {
  745. // 分5种情况,根据ctx.cancancel值判断:
  746. // 1.原报发起撤回,当前流程删除,并回到待上报
  747. // 2.审批人撤回审批通过,增加流程,并回到它审批中
  748. // 3.审批人撤回审批终止,增加流程,并回到它审批中,并更新立项状态为审批中
  749. // 4.审批人撤回退回原报操作,删除新增的审批流,增加流程,回滚到它审批中
  750. // 5.会签审批人撤回审批通过(还有其他审批人未审批通过),仅修改本人流程状态
  751. switch (change.cancancel) {
  752. case 1: await this._userCheckCancel(change); break;
  753. case 2: await this._auditCheckCancel(change); break;
  754. case 3: await this._auditCheckCancelStop(change); break;
  755. case 4: await this._auditCheckCancelNo(change); break;
  756. case 5: await this._auditCheckCancelAnd(change); break;
  757. default: throw '不可撤回,请刷新页面重试';
  758. }
  759. }
  760. /**
  761. * 原报撤回,直接改动审批人状态
  762. * 如果存在审批人数据,将其改为原报流程数据,但保留原提交人
  763. *
  764. * 一审 1 A checking -> A uncheck status改 pay/jl:删0(jl为增量数据,只删重复部分) 1->0 删1
  765. * ...
  766. *
  767. * @param stage
  768. * @returns {Promise<void>}
  769. * @private
  770. */
  771. async _userCheckCancel(change) {
  772. const transaction = await this.db.beginTransaction();
  773. try {
  774. // 整理当前流程审核人状态更新
  775. // 审批人变成待审批状态
  776. const updateData = change.curAuditors.map(x => {
  777. return {
  778. id: x.id,
  779. status: auditConst.status.uncheck,
  780. begin_time: null, opinion: null,
  781. };
  782. });
  783. await transaction.updateRows(this.tableName, updateData);
  784. await this.ctx.service.noticeAgain.deleteNoticeAgain(transaction, this.tableName, this._.map(updateData, 'id'));
  785. // 变成待上报状态
  786. await transaction.update(this.ctx.service.changeProject.tableName, {
  787. id: change.id,
  788. status: change.times === 1 ? auditConst.status.uncheck : auditConst.status.back,
  789. });
  790. await transaction.commit();
  791. } catch (err) {
  792. await transaction.rollback();
  793. throw err;
  794. }
  795. }
  796. /**
  797. * 审批人撤回审批通过,插入两条数据
  798. *
  799. * 一审 1 A checked 一审 1 A checked
  800. * 二审 2 B checked pre -> 二审 2 B checked
  801. * 三审 3 C checking cur 二审 3 B checkCancel 增 增extra_his 增tp_his
  802. * 四审 4 D uncheck 二审 4 B checking 增 增pay_cur
  803. * 三审 5 C uncheck order、status改
  804. * 四审 6 D uncheck order改
  805. *
  806. * @param stage
  807. * @returns {Promise<void>}
  808. * @private
  809. */
  810. async _auditCheckCancel(change) {
  811. if (change.curAuditors.length === 0 || change.curAuditors[0].order <= 1) {
  812. throw '撤回用户数据错误';
  813. }
  814. const accountId = this.ctx.session.sessionUser.accountId;
  815. const selfAuditor = change.preAuditors.find(x => { return x.aid === accountId; });
  816. if (!selfAuditor) throw '撤回用户数据错误';
  817. const time = new Date();
  818. const transaction = await this.db.beginTransaction();
  819. try {
  820. // 顺移其后审核人流程顺序
  821. const sql = 'UPDATE ' + this.tableName + ' SET `order` = `order` + 2 WHERE cpid = ? AND times = ? AND `order` > ?';
  822. await transaction.query(sql, [change.id, selfAuditor.times, change.curAuditors[0].order]);
  823. // 当前审批人2次添加至流程中
  824. const checkCancelAuditors = [],
  825. checkingAuditors = [];
  826. change.preAuditors.forEach(x => {
  827. checkCancelAuditors.push({
  828. tid: change.tid, cpid: change.id, aid: x.aid,
  829. times: x.times, order: x.order + 1,
  830. status: x.aid === selfAuditor.aid ? auditConst.status.checkCancel : auditConst.status.checkSkip,
  831. begin_time: time, end_time: time, opinion: '',
  832. audit_type: x.audit_type, audit_order: x.audit_order,
  833. });
  834. });
  835. change.preAuditors.forEach(x => {
  836. checkingAuditors.push({
  837. tid: change.tid, cpid: change.id, aid: x.aid,
  838. times: x.times, order: x.order + 2,
  839. status: auditConst.status.checking,
  840. begin_time: time, end_time: time, opinion: '',
  841. audit_type: x.audit_type, audit_order: x.audit_order,
  842. });
  843. });
  844. await transaction.insert(this.tableName, [...checkCancelAuditors, ...checkingAuditors]);
  845. // 当前审批人变成待审批
  846. await transaction.updateRows(this.tableName, change.curAuditors.map(x => {
  847. return { id: x.id, begin_time: null, status: auditConst.status.uncheck, order: x.order + 2 };
  848. }));
  849. await this.ctx.service.noticeAgain.deleteNoticeAgain(transaction, this.tableName, this._.map(change.curAuditors, 'id'));
  850. await transaction.commit();
  851. } catch (err) {
  852. await transaction.rollback();
  853. throw err;
  854. }
  855. }
  856. /**
  857. * 审批人撤回审批终止,插入两条数据
  858. *
  859. * @param stage
  860. * @returns {Promise<void>}
  861. * @private
  862. */
  863. async _auditCheckCancelStop(change) {
  864. const accountId = this.ctx.session.sessionUser.accountId;
  865. const selfAuditor = change.preAuditors.find(x => { return x.aid === accountId && x.status === auditConst.status.checkNo; });
  866. if (!selfAuditor) throw '该变更立项由他人审批终止,您不可撤回';
  867. const time = new Date();
  868. const transaction = await this.db.beginTransaction();
  869. try {
  870. // 顺移其后审核人流程顺序
  871. const sql = 'UPDATE ' + this.tableName + ' SET `order` = `order` + 2 WHERE cpid = ? AND times = ? AND `order` > ?';
  872. await transaction.query(sql, [change.id, change.times, selfAuditor.order]);
  873. // 当前审批人2次添加至流程中
  874. const checkCancelAuditors = [],
  875. checkingAuditors = [];
  876. change.preAuditors.forEach(x => {
  877. checkCancelAuditors.push({
  878. tid: change.tid, cpid: change.id, aid: x.aid,
  879. times: x.times, order: x.order + 1,
  880. status: x.aid === selfAuditor.aid ? auditConst.status.checkCancel : auditConst.status.checkSkip,
  881. begin_time: time, end_time: time, opinion: '',
  882. audit_type: x.audit_type, audit_order: x.audit_order,
  883. });
  884. });
  885. change.preAuditors.forEach(x => {
  886. checkingAuditors.push({
  887. tid: change.tid, cpid: change.id, aid: x.aid,
  888. times: x.times, order: x.order + 2,
  889. status: auditConst.status.checking,
  890. begin_time: time, end_time: time, opinion: '',
  891. audit_type: x.audit_type, audit_order: x.audit_order,
  892. });
  893. });
  894. await transaction.insert(this.tableName, [...checkCancelAuditors, ...checkingAuditors]);
  895. await transaction.update(this.ctx.service.changeProject.tableName, {
  896. id: change.id,
  897. status: auditConst.status.checking,
  898. });
  899. await transaction.commit();
  900. } catch (err) {
  901. await transaction.rollback();
  902. throw err;
  903. }
  904. }
  905. /**
  906. * 审批人撤回审批退回原报
  907. *
  908. * 1# 一审 1 A checked 1# 一审 1 A checked
  909. * 二审 2 B checkNo pre -> 二审 2 B checkNo
  910. * 三审 3 C uncheck 二审 3 B checkCancel 增 pay: 2#0 -> 1#3 jl: 2#0 -> 1#3 增tp_his 增extra_his
  911. * 二审 4 B checking 增 pay: 2#0 -> 1#4
  912. * 三审 5 C uncheck order改
  913. *
  914. * 2# 一审 1 A uncheck 2# 删 pay: 2#0删 jl: 2#0删
  915. * 二审 2 B uncheck
  916. * 三审 3 C uncheck
  917. *
  918. * @param stage
  919. * @returns {Promise<void>}
  920. * @private
  921. */
  922. async _auditCheckCancelNo(change) {
  923. const accountId = this.ctx.session.sessionUser.accountId;
  924. const selfAuditor = change.preAuditors.find(x => { return x.aid === accountId && x.status === auditConst.status.back; });
  925. if (!selfAuditor) throw '该标段由他人审批退回,您不可撤回';
  926. const time = new Date();
  927. const transaction = await this.db.beginTransaction();
  928. try {
  929. // 整理上一个流程审核人状态更新
  930. // 顺移其后审核人流程顺序
  931. const sql = 'UPDATE ' + this.tableName + ' SET `order` = `order` + 2 WHERE cpid = ? AND times = ? AND `order` > ?';
  932. await transaction.query(sql, [change.id, selfAuditor.times, selfAuditor.order]);
  933. // 当前审批人2次添加至流程中
  934. const checkCancelAuditors = [],
  935. checkingAuditors = [];
  936. change.preAuditors.forEach(x => {
  937. checkCancelAuditors.push({
  938. tid: change.tid, cpid: change.id, aid: x.aid,
  939. times: x.times, order: x.order + 1,
  940. status: x.aid === selfAuditor.aid ? auditConst.status.checkCancel : auditConst.status.checkSkip,
  941. begin_time: time, end_time: time, opinion: '',
  942. audit_type: x.audit_type, audit_order: x.audit_order,
  943. });
  944. });
  945. change.preAuditors.forEach(x => {
  946. checkingAuditors.push({
  947. tid: change.tid, cpid: change.id, aid: x.aid,
  948. times: x.times, order: x.order + 2,
  949. status: auditConst.status.checking,
  950. begin_time: time, end_time: time, opinion: '',
  951. audit_type: x.audit_type, audit_order: x.audit_order,
  952. });
  953. });
  954. await transaction.insert(this.tableName, [...checkCancelAuditors, ...checkingAuditors]);
  955. // 删除当前次审批流
  956. await transaction.delete(this.tableName, { cpid: change.id, times: change.times });
  957. // 回退数据
  958. await this.ctx.service.changeProjectHistory.returnHistory(transaction, change.id);
  959. await transaction.delete(this.ctx.service.changeProjectHistory.tableName, { cpid: change.id });
  960. // // 设置变更立项为审批中
  961. // await transaction.update(this.ctx.service.changeProject.tableName, {
  962. // id: change.id,
  963. // time: change.times - 1,
  964. // status: auditConst.status.checking,
  965. // });
  966. await transaction.commit();
  967. } catch (err) {
  968. await transaction.rollback();
  969. throw err;
  970. }
  971. }
  972. /**
  973. * 会签未全部审批通过时,撤回仅修改本人状态
  974. *
  975. * @param stage
  976. * @returns {Promise<void>}
  977. * @private
  978. */
  979. async _auditCheckCancelAnd(change) {
  980. const accountId = this.ctx.session.sessionUser.accountId;
  981. const selfAuditor = change.flowAuditors.find(x => { return x.aid === accountId; });
  982. if (!selfAuditor || selfAuditor.status !== auditConst.status.checked) throw '不可撤回';
  983. const transaction = await this.db.beginTransaction();
  984. try {
  985. await transaction.update(this.tableName, {
  986. id: selfAuditor.id, status: auditConst.status.checking, opinion: '', end_time: null,
  987. });
  988. await transaction.commit();
  989. } catch (err) {
  990. await transaction.rollback();
  991. throw err;
  992. }
  993. }
  994. /**
  995. * 重新审批变更令
  996. * @param { string } cid - 查询的清单
  997. * @return {Promise<*>} - 可用的变更令列表
  998. */
  999. async checkRevise(change) {
  1000. const accountId = this.ctx.session.sessionUser.accountId;
  1001. const time = new Date();
  1002. // 初始化事务
  1003. const transaction = await this.db.beginTransaction();
  1004. let result = false;
  1005. try {
  1006. const pid = this.ctx.session.sessionProject.id;
  1007. // 获取审核人列表
  1008. const auditors = await this.getUniqAuditor(change.id, change.times); // 全部参与的审批人
  1009. // const sql = 'SELECT `tid`, `cpid`, `aid`, `order` FROM ?? WHERE `cpid` = ? and `times` = ? GROUP BY `aid` ORDER BY `id` ASC';
  1010. // const sqlParam = [this.tableName, change.id, change.times];
  1011. // const auditors = await this.db.query(sql, sqlParam);
  1012. // 添加到消息推送表
  1013. const noticeContent = await this.getNoticeContent(pid, change.tid, change.id, this.ctx.session.sessionUser.accountId, '发起修订');
  1014. const records = [{ pid, type: pushType.changeProject, uid: change.uid, status: auditConst.status.revise, content: noticeContent }];
  1015. auditors.forEach(auditor => {
  1016. records.push({
  1017. pid,
  1018. type: pushType.changeProject,
  1019. uid: auditor.aid,
  1020. status: auditConst.status.revise,
  1021. content: noticeContent,
  1022. });
  1023. });
  1024. await transaction.insert('zh_notice', records);
  1025. const order = change.auditors.length > 0 ? change.auditors.reduce((rst, a) => { return Math.max(rst, a.order); }, 0) + 1 : 1; // 最大值 + 1
  1026. // 当前审批人添加至流程中
  1027. const checkReviseAuditor = {
  1028. tid: change.tid, cpid: change.id, aid: change.uid,
  1029. times: change.times, order,
  1030. status: auditConst.status.revise,
  1031. begin_time: time, end_time: time, opinion: '',
  1032. audit_type: auditType.key.common, audit_order: 0,
  1033. };
  1034. await transaction.insert(this.tableName, checkReviseAuditor);
  1035. // 修订人加入审批流程
  1036. const newAuditors = auditors.map(x => {
  1037. return {
  1038. aid: x.aid, tid: change.tid, cpid: change.id,
  1039. times: change.times + 1, order: x.audit_order, status: auditConst.status.uncheck,
  1040. audit_type: x.audit_type, audit_order: x.audit_order,
  1041. };
  1042. });
  1043. await transaction.insert(this.tableName, newAuditors);
  1044. // 生成内容保存表至zh_change_history中,用于撤销修订回退
  1045. const changeData = await transaction.get(this.ctx.service.changeProject.tableName, { id: change.id });
  1046. await this.ctx.service.changeProjectHistory.saveHistory(transaction, changeData);
  1047. // 设置变更立项修订状态
  1048. await transaction.update(this.ctx.service.changeProject.tableName, {
  1049. id: change.id,
  1050. status: auditConst.status.revise,
  1051. times: change.times + 1,
  1052. final_auditor_str: '',
  1053. });
  1054. await transaction.commit();
  1055. result = true;
  1056. } catch (error) {
  1057. console.log(error);
  1058. await transaction.rollback();
  1059. result = false;
  1060. }
  1061. return result;
  1062. }
  1063. /**
  1064. * 撤销修订变更令
  1065. * @param { string } cid - 查询的清单
  1066. * @return {Promise<*>} - 可用的变更令列表
  1067. */
  1068. async cancelRevise(change) {
  1069. const time = new Date();
  1070. // 初始化事务
  1071. const transaction = await this.db.beginTransaction();
  1072. let result = false;
  1073. try {
  1074. const pid = this.ctx.session.sessionProject.id;
  1075. // 获取审核人列表
  1076. const auditors = await this.getUniqAuditor(change.id, change.times - 1); // 全部参与的审批人
  1077. // 添加到消息推送表
  1078. const noticeContent = await this.getNoticeContent(pid, change.tid, change.id, this.ctx.session.sessionUser.accountId, '撤销修订');
  1079. const records = [{ pid, type: pushType.changeProject, uid: change.uid, status: auditConst.status.cancelRevise, content: noticeContent }];
  1080. auditors.forEach(auditor => {
  1081. records.push({
  1082. pid,
  1083. type: pushType.changeProject,
  1084. uid: auditor.aid,
  1085. status: auditConst.status.cancelRevise,
  1086. content: noticeContent,
  1087. });
  1088. });
  1089. await transaction.insert('zh_notice', records);
  1090. const lastAudit = await this.getLastAudit(change.id, change.times - 1);
  1091. // 新增一个撤销修订状态到审批流程中
  1092. const revise_audit = {
  1093. tid: change.tid, cpid: change.id, aid: this.ctx.session.sessionUser.accountId,
  1094. times: change.times - 1, order: lastAudit.order + 1, status: auditConst.status.cancelRevise,
  1095. begin_time: time, end_time: time, opinion: '',
  1096. audit_type: lastAudit.audit_type, audit_order: lastAudit.audit_order,
  1097. };
  1098. await transaction.insert(this.tableName, revise_audit);
  1099. await transaction.delete(this.tableName, { cpid: change.id, times: change.times });
  1100. await this.ctx.service.changeProjectHistory.returnHistory(transaction, change.id);
  1101. await transaction.delete(this.ctx.service.changeProjectHistory.tableName, { cpid: change.id });
  1102. await transaction.commit();
  1103. result = true;
  1104. } catch (error) {
  1105. console.log(error);
  1106. await transaction.rollback();
  1107. result = false;
  1108. }
  1109. return result;
  1110. }
  1111. /**
  1112. * 重新审批变更立项
  1113. * @param { string } cid - 查询的清单
  1114. * @return {Promise<*>} - 可用的变更令列表
  1115. */
  1116. async checkAgain(change) {
  1117. const accountId = this.ctx.session.sessionUser.accountId;
  1118. // 初始化事务
  1119. const time = new Date();
  1120. const transaction = await this.db.beginTransaction();
  1121. let result = false;
  1122. try {
  1123. const noYbAuditors = change.auditors.filter(x => { return x.audit_order !== 0; });
  1124. const noYbMaxOrder = noYbAuditors[noYbAuditors.length - 1].order;
  1125. const maxOrder = change.auditors[change.auditors.length - 1].order;
  1126. const audits = change.auditors.filter(x => { return x.order === noYbMaxOrder; });
  1127. if (!audits || audits.length === 0 || maxOrder < 1) throw '审核数据错误';
  1128. const selfAudit = audits.find(x => { return x.aid === accountId; });
  1129. if (!selfAudit) throw '当前标段您无权审批';
  1130. // 当前审批人2次添加至流程中
  1131. const checkAgainAuditors = [];
  1132. audits.forEach(x => {
  1133. checkAgainAuditors.push({
  1134. tid: change.tid, cpid: change.id, aid: x.aid,
  1135. times: x.times, order: maxOrder + 1,
  1136. status: !selfAudit || x.aid === selfAudit.aid ? auditConst.status.checkAgain : auditConst.status.checkSkip,
  1137. begin_time: time, end_time: time, opinion: '',
  1138. audit_type: x.audit_type, audit_order: x.audit_order,
  1139. });
  1140. });
  1141. const checkingAuditors = [];
  1142. audits.forEach(x => {
  1143. checkingAuditors.push({
  1144. tid: change.tid, cpid: change.id, aid: x.aid,
  1145. times: x.times, order: maxOrder + 2,
  1146. status: auditConst.status.checking,
  1147. begin_time: time,
  1148. audit_type: x.audit_type, audit_order: x.audit_order,
  1149. });
  1150. });
  1151. await transaction.insert(this.tableName, checkAgainAuditors);
  1152. const checkingAuditors_result = await transaction.insert(this.tableName, checkingAuditors);
  1153. // 获取刚批量添加的所有list
  1154. // for (let j = 0; j < checkingAuditors.length; j++) {
  1155. // checkingAuditors[j].id = checkingAuditors_result.insertId + j;
  1156. // }
  1157. // 设置变更令审批中
  1158. await transaction.update(this.ctx.service.changeProject.tableName, {
  1159. id: change.id,
  1160. status: auditConst.status.checking,
  1161. final_auditor_str: '',
  1162. });
  1163. // 检查三方特殊推送
  1164. await this.ctx.service.specMsg.addChangeProjectMsg(transaction, this.ctx.session.sessionProject.id, this.ctx.change, pushOperate.change_project.flow);
  1165. await transaction.commit();
  1166. result = true;
  1167. } catch (error) {
  1168. console.log(error);
  1169. await transaction.rollback();
  1170. result = false;
  1171. }
  1172. return result;
  1173. }
  1174. async getAuditorGroup(cpId, times) {
  1175. const auditors = await this.getAuditors(cpId, times); // 全部参与的审批人
  1176. return this.ctx.helper.groupAuditors(auditors, 'order', true);
  1177. }
  1178. async getUserGroup(cpId, times) {
  1179. const group = await this.getAuditorGroup(cpId, times);
  1180. const sql =
  1181. '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' +
  1182. ' FROM ' + this.ctx.service.changeProject.tableName + ' As s' +
  1183. ' LEFT JOIN ' + this.ctx.service.projectAccount.tableName + ' As pa' +
  1184. ' ON s.uid = pa.id' +
  1185. ' WHERE s.id = ?';
  1186. const sqlParam = [times, cpId, cpId];
  1187. const user = await this.db.queryOne(sql, sqlParam);
  1188. user.audit_order = 0;
  1189. group.unshift([ user ]);
  1190. return group;
  1191. }
  1192. async getUniqUserGroup(cpId, times) {
  1193. const group = await this.getAuditorGroup(cpId, times);
  1194. const sql =
  1195. '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' +
  1196. ' FROM ' + this.ctx.service.changeProject.tableName + ' As s' +
  1197. ' LEFT JOIN ' + this.ctx.service.projectAccount.tableName + ' As pa' +
  1198. ' ON s.uid = pa.id' +
  1199. ' WHERE s.id = ?';
  1200. const sqlParam = [times, cpId, cpId];
  1201. const user = await this.db.queryOne(sql, sqlParam);
  1202. user.audit_order = 0;
  1203. group.unshift([ user ]);
  1204. return this.ctx.helper.groupAuditorsUniq(group);
  1205. }
  1206. async getAuditorHistory(cpId, times, reverse = false) {
  1207. const history = [];
  1208. if (times >= 1) {
  1209. for (let i = 1; i <= times; i++) {
  1210. const auditors = await this.getAuditors(cpId, i);
  1211. const group = this.ctx.helper.groupAuditors(auditors);
  1212. const historyGroup = [];
  1213. // 找出group里audit_order最大值
  1214. const max_info = group.length > 0 ? this._.maxBy(group, function(item) {
  1215. return item && item[0] && item[0].audit_order;
  1216. }) : null;
  1217. const max_order = max_info ? max_info[0].audit_order : -1;
  1218. for (const g of group) {
  1219. const his = {
  1220. beginYear: '', beginDate: '', beginTime: '', endYear: '', endDate: '', endTime: '', begin_time: null, end_time: null,
  1221. audit_type: g[0].audit_type, audit_order: g[0].audit_order,
  1222. auditors: g,
  1223. };
  1224. if (his.audit_type === auditType.key.common) {
  1225. his.name = g[0].name;
  1226. } else {
  1227. his.name = this.ctx.helper.transFormToChinese(his.audit_order) + '审';
  1228. }
  1229. his.is_final = his.audit_order === max_order;
  1230. if (g[0].begin_time) {
  1231. his.begin_time = g[0].begin_time;
  1232. const beginTime = this.ctx.moment(g[0].begin_time);
  1233. his.beginYear = beginTime.format('YYYY');
  1234. his.beginDate = beginTime.format('MM-DD');
  1235. his.beginTime = beginTime.format('HH:mm:ss');
  1236. }
  1237. let end_time;
  1238. g.forEach(x => {
  1239. if (x.status === auditConst.status.checkSkip) return;
  1240. if (!his.status || x.status === auditConst.status.checking) his.status = x.status;
  1241. if (x.end_time && (!end_time || x.end_time > end_time)) {
  1242. end_time = x.end_time;
  1243. if (his.status !== auditConst.status.checking) his.status = x.status;
  1244. }
  1245. });
  1246. if (end_time) {
  1247. his.end_time = end_time;
  1248. const endTime = this.ctx.moment(end_time);
  1249. his.endYear = endTime.format('YYYY');
  1250. his.endDate = endTime.format('MM-DD');
  1251. his.endTime = endTime.format('HH:mm:ss');
  1252. }
  1253. historyGroup.push(his);
  1254. }
  1255. if (reverse) {
  1256. history.push(historyGroup.reverse());
  1257. } else {
  1258. history.push(historyGroup);
  1259. }
  1260. }
  1261. }
  1262. return history;
  1263. }
  1264. async getUniqAuditor(cpId, times) {
  1265. const auditors = await this.getAuditors(cpId, times); // 全部参与的审批人
  1266. const result = [];
  1267. auditors.forEach(x => {
  1268. if (result.findIndex(r => { return x.aid === r.aid && x.audit_order === r.audit_order; }) < 0) {
  1269. result.push(x);
  1270. }
  1271. });
  1272. return result;
  1273. }
  1274. async saveAudit(cpId, times, sp_group, data) {
  1275. const transaction = await this.db.beginTransaction();
  1276. try {
  1277. const auditors = await this.getAuditGroupByList(cpId, times);
  1278. const now_audit = this._.find(auditors, { aid: data.old_aid });
  1279. if (data.operate !== 'del') {
  1280. const exist = await this.getDataByCondition({ cpid: cpId, times, aid: data.new_aid });
  1281. if (exist) throw '该审核人已存在,请勿重复添加';
  1282. }
  1283. if (data.operate === 'add') {
  1284. if (now_audit.status !== auditConst.status.uncheck && now_audit.status !== auditConst.status.checking) {
  1285. throw '当前人下无法操作新增';
  1286. }
  1287. const newAudit = {
  1288. tid: this.ctx.tender.id, cpid: cpId, aid: data.new_aid,
  1289. order: now_audit.order + 1,
  1290. audit_order: now_audit.audit_order + 1, audit_type: auditType.key.common,
  1291. times, status: auditConst.status.uncheck,
  1292. };
  1293. // order+1
  1294. await this._syncOrderByDelete(transaction, cpId, now_audit.order + 1, times, '+');
  1295. await transaction.insert(this.tableName, newAudit);
  1296. // 更新审批流程页数据,如果存在
  1297. } else if (data.operate === 'add-sibling') {
  1298. if (now_audit.status !== auditConst.status.uncheck && now_audit.status !== auditConst.status.checking) {
  1299. throw '当前人下无法操作新增';
  1300. }
  1301. const newAudit = {
  1302. tid: this.ctx.tender.id, cpid: cpId, aid: data.new_aid,
  1303. order: now_audit.order,
  1304. audit_order: now_audit.audit_order, audit_type: now_audit.audit_type,
  1305. times, status: auditConst.status.uncheck,
  1306. };
  1307. await transaction.insert(this.tableName, newAudit);
  1308. } else if (data.operate === 'del') {
  1309. if (now_audit.status !== auditConst.status.uncheck) {
  1310. throw '当前人无法操作删除';
  1311. }
  1312. const flowAuditors = auditors.filter(x => { return x.audit_order === now_audit.audit_order; });
  1313. await transaction.delete(this.tableName, { cpid: cpId, times, aid: now_audit.aid, order: now_audit.order });
  1314. if (flowAuditors.length === 1) await this._syncOrderByDelete(transaction, cpId, now_audit.order, times);
  1315. // 旧的更新为is_old为1
  1316. // await transaction.update(this.tableName, { is_old: 1 }, {
  1317. // where: {
  1318. // sid: stageId,
  1319. // times,
  1320. // aid: data.old_aid,
  1321. // }
  1322. // });
  1323. } else if (data.operate === 'change') {
  1324. const nowAudit = await this.getDataByCondition({ cpid: cpId, times, aid: now_audit.aid, order: now_audit.order });
  1325. if (now_audit.status !== auditConst.status.uncheck || !nowAudit) {
  1326. throw '当前人无法操作替换';
  1327. }
  1328. nowAudit.aid = data.new_aid;
  1329. await transaction.update(this.tableName, nowAudit);
  1330. // 旧的更新为is_old为1
  1331. // await transaction.update(this.tableName, { is_old: 1 }, {
  1332. // where: {
  1333. // sid: stageId,
  1334. // times,
  1335. // aid: data.old_aid,
  1336. // }
  1337. // });
  1338. }
  1339. if (this.ctx.tender.info.shenpi.change === shenpiConst.sp_status.gdspl) {
  1340. const newAuditors = await transaction.select(this.tableName, { where: { cpid: cpId, times }, orders: [['order', 'asc']] });
  1341. const newAuditorGroup = this.ctx.helper.groupAuditors(newAuditors, 'order', true);
  1342. const uniqNewAuditorGroup = this.ctx.helper.groupAuditorsUniq(newAuditorGroup);
  1343. // await this.ctx.service.shenpiAudit.updateAuditListWithAuditType(transaction, this.ctx.tender.id, this.ctx.tender.info.shenpi.change, shenpiConst.sp_type.change, uniqNewAuditorGroup, sp_group);
  1344. } else if (this.ctx.tender.info.shenpi.change === shenpiConst.sp_status.gdzs) {
  1345. const newAuditors = await this.getAuditGroupByList(cpId, times, transaction);
  1346. 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'));
  1347. }
  1348. // 更新到审批流程方法
  1349. await transaction.commit();
  1350. } catch (err) {
  1351. await transaction.rollback();
  1352. throw err;
  1353. }
  1354. }
  1355. async changeSpGroup(change, sp_group) {
  1356. const transaction = await this.db.beginTransaction();
  1357. try {
  1358. const group = await this.ctx.service.shenpiGroup.getDataById(sp_group);
  1359. if (!group) {
  1360. throw '该固定审批组不存在,请刷新页面重新获取';
  1361. }
  1362. 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 } });
  1363. await this.ctx.service.shenpiAudit.noYbShenpiList(change.uid, shenpiList);
  1364. await this.updateNewAuditors(change, shenpiList, transaction);
  1365. await transaction.update(this.ctx.service.changeProject.tableName, { id: change.id, sp_group: group.id });
  1366. await transaction.commit();
  1367. } catch (err) {
  1368. await transaction.rollback();
  1369. throw err;
  1370. }
  1371. return true;
  1372. }
  1373. async updateNewAuditList(change, newList) {
  1374. const transaction = await this.db.beginTransaction();
  1375. try {
  1376. await this.updateNewAuditors(change, newList, transaction);
  1377. await transaction.commit();
  1378. } catch (err) {
  1379. await transaction.rollback();
  1380. throw err;
  1381. }
  1382. }
  1383. async updateNewAuditors(change, newList, transaction) {
  1384. // 先删除旧的审批流,再添加新的
  1385. await transaction.delete(this.tableName, { cpid: change.id, times: change.times });
  1386. const newAuditors = [];
  1387. for (const auditor of newList) {
  1388. newAuditors.push({
  1389. tid: change.tid, cpid: change.id, aid: auditor.audit_id,
  1390. times: change.times, order: auditor.audit_order, status: auditConst.status.uncheck,
  1391. audit_type: auditor.audit_type, audit_order: auditor.audit_order,
  1392. });
  1393. }
  1394. if (newAuditors.length > 0) await transaction.insert(this.tableName, newAuditors);
  1395. }
  1396. async updateLastAudit(change, auditList, lastId) {
  1397. const transaction = await this.db.beginTransaction();
  1398. try {
  1399. // 先判断auditList里的aid是否与lastId相同,相同则删除并重新更新order
  1400. const existAudit = auditList.find(x => { return x.aid === lastId; });
  1401. let order = auditList.length > 0 ? auditList.reduce((rst, a) => { return Math.max(rst, a.order); }, 0) + 1 : 1; // 最大值 + 1
  1402. if (existAudit) {
  1403. await transaction.delete(this.tableName, { cpid: change.id, times: change.times, aid: lastId });
  1404. const sameOrder = auditList.filter(x => { return x.order === existAudit.order; });
  1405. if (sameOrder.length === 1) {
  1406. const updateData = [];
  1407. auditList.forEach(x => {
  1408. if (x.order <= existAudit.order) return;
  1409. updateData.push({ id: x.id, order: x.order - 1, audit_order: x.audit_order - 1 });
  1410. });
  1411. if (updateData.length > 0) {
  1412. await transaction.updateRows(updateData);
  1413. }
  1414. order = order - 1;
  1415. }
  1416. }
  1417. // 添加终审
  1418. const newAuditor = {
  1419. tid: change.tid, cpid: change.id, aid: lastId,
  1420. times: change.times, order, status: auditConst.status.uncheck,
  1421. audit_type: auditType.key.common, audit_order: order,
  1422. };
  1423. await transaction.insert(this.tableName, newAuditor);
  1424. await transaction.commit();
  1425. } catch (err) {
  1426. await transaction.rollback();
  1427. throw err;
  1428. }
  1429. }
  1430. }
  1431. return ChangeProjectAudit;
  1432. };