change_project_audit.js 80 KB

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