change_audit.js 70 KB

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