change_project_audit.js 79 KB

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