change_project_audit.js 82 KB

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