change_audit.js 75 KB

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