change_project.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date 2018/8/14
  7. * @version
  8. */
  9. const audit = require('../const/audit').changeProject;
  10. // const smsTypeConst = require('../const/sms_type');
  11. // const SMS = require('../lib/sms');
  12. // const SmsAliConst = require('../const/sms_alitemplate');
  13. const wxConst = require('../const/wechat_template');
  14. const pushType = require('../const/audit').pushType;
  15. const projectLogConst = require('../const/project_log');
  16. const codeRuleConst = require('../const/code_rule');
  17. const changeConst = require('../const/change');
  18. module.exports = app => {
  19. class ChangeProject 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';
  29. }
  30. async add(tenderId, userId, code, name) {
  31. const type = userId === this.ctx.tender.data.user_id ? codeRuleConst.ruleType.suggestion : codeRuleConst.ruleType.will;
  32. const sql = 'SELECT COUNT(*) as count FROM ?? WHERE `tid` = ? AND `code` = ? AND type = ?';
  33. const sqlParam = [this.tableName, tenderId, code, type];
  34. const codeCount = await this.db.queryOne(sql, sqlParam);
  35. const count = codeCount.count;
  36. if (count > 0) {
  37. throw '立项书编号重复';
  38. }
  39. // 工程变更类别读取
  40. const projectData = await this.ctx.service.project.getDataById(this.ctx.session.sessionProject.id);
  41. const fun_set = await this.ctx.service.project.getFunSet(projectData.fun_set);
  42. const classInfo = this._.find(fun_set.change_class, { checked: true });
  43. // 初始化事务
  44. this.transaction = await this.db.beginTransaction();
  45. let result = false;
  46. try {
  47. const change = {
  48. tid: tenderId,
  49. uid: userId,
  50. status: audit.status.uncheck,
  51. times: 1,
  52. type,
  53. in_time: new Date(),
  54. code,
  55. name,
  56. quality: changeConst.quality.common.name,
  57. class: classInfo.new_name ? classInfo.new_name : classInfo.name,
  58. };
  59. const operate = await this.transaction.insert(this.tableName, change);
  60. if (operate.affectedRows <= 0) {
  61. throw '新建变更令数据失败';
  62. }
  63. change.id = operate.insertId;
  64. // 先找出标段最近存在的变更令审批人的变更令info
  65. const preChangeInfo = await this.getHaveAuditLastInfo(tenderId, type);
  66. if (preChangeInfo) {
  67. // 并把之前存在的变更令审批人添加到zh_change_audit
  68. const auditResult = await this.ctx.service.changeProjectAudit.copyPreChangeProjectAuditors(this.transaction, preChangeInfo, change);
  69. if (!auditResult) {
  70. throw '复制上一次审批流程失败';
  71. }
  72. }
  73. result = change;
  74. this.transaction.commit();
  75. } catch (error) {
  76. console.log(error);
  77. // 回滚
  78. await this.transaction.rollback();
  79. }
  80. return result;
  81. }
  82. async getHaveAuditLastInfo(tenderId, type) {
  83. const sql = 'SELECT a.* FROM ?? as a LEFT JOIN ?? as b ON a.`id` = b.`cpid` WHERE a.`tid` = ? AND a.type = ? ORDER BY a.`in_time` DESC';
  84. const sqlParam = [this.tableName, this.ctx.service.changeProjectAudit.tableName, tenderId, type];
  85. return await this.db.queryOne(sql, sqlParam);
  86. }
  87. /**
  88. * 获取变更立项列表
  89. * @param {int} tenderId - 标段id
  90. * @param {int} status - 状态
  91. * @param {int} hadlimit - 分页
  92. * @return {object} list - 列表
  93. */
  94. async getListByStatus(tenderId, status = 0, hadlimit = 1, sortBy = '', orderBy = '') {
  95. let sql = '';
  96. let sqlParam = '';
  97. if (this.ctx.tender.isTourist && status === 0) {
  98. sql = 'SELECT a.*, p.name as account_name FROM ?? As a LEFT JOIN ?? AS p On a.uid = p.id WHERE a.tid = ?';
  99. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, tenderId];
  100. } else {
  101. switch (status) {
  102. case 0: // 包含你的所有变更立项
  103. sql =
  104. 'SELECT a.*, p.name as account_name FROM ?? AS a LEFT JOIN ?? AS p On a.uid = p.id WHERE a.tid = ? AND' +
  105. ' (a.uid = ? OR (a.status != ? AND (a.id IN (SELECT b.cpid FROM ?? AS b WHERE b.aid = ? AND a.times = b.times GROUP BY b.cpid)' +
  106. ' OR a.id IN (SELECT c.cpid FROM ?? AS c WHERE c.aid = ? AND c.tid = ?)))' +
  107. ' OR a.status = ? )';
  108. sqlParam = [
  109. this.tableName,
  110. this.ctx.service.projectAccount.tableName,
  111. tenderId,
  112. this.ctx.session.sessionUser.accountId,
  113. audit.status.uncheck,
  114. this.ctx.service.changeProjectAudit.tableName,
  115. this.ctx.session.sessionUser.accountId,
  116. this.ctx.service.changeProjectXsAudit.tableName,
  117. this.ctx.session.sessionUser.accountId,
  118. tenderId,
  119. audit.status.checked,
  120. ];
  121. break;
  122. case 1: // 待处理(你的)
  123. sql = 'SELECT a.*, p.name as account_name FROM ?? as a LEFT JOIN ?? AS p On a.uid = p.id WHERE' +
  124. ' a.tid = ? AND (a.id in(SELECT b.cpid FROM ?? as b WHERE b.tid = ? AND b.aid = ? AND b.status = ?)' +
  125. ' OR (a.uid = ? AND (a.status = ? OR a.status = ? OR a.status = ?)))';
  126. sqlParam = [this.tableName,
  127. this.ctx.service.projectAccount.tableName,
  128. tenderId, this.ctx.service.changeProjectAudit.tableName,
  129. tenderId, this.ctx.session.sessionUser.accountId,
  130. audit.status.checking, this.ctx.session.sessionUser.accountId,
  131. audit.status.uncheck, audit.status.back, audit.status.revise,
  132. ];
  133. break;
  134. case 5: // 待上报(所有的)PS:取未上报,退回,修订的变更令
  135. sql =
  136. 'SELECT a.*, p.name as account_name FROM ?? AS a LEFT JOIN ?? AS p On a.uid = p.id WHERE ' +
  137. // 'a.id IN (SELECT b.cpid FROM ?? AS b WHERE b.aid = ? AND a.times = b.times GROUP BY b.cpid) AND ' +
  138. 'a.uid = ? AND a.tid = ? AND (a.status = ? OR a.status = ? OR a.status = ?)';
  139. sqlParam = [
  140. this.tableName,
  141. this.ctx.service.projectAccount.tableName,
  142. // this.ctx.service.changeProjectAudit.tableName,
  143. this.ctx.session.sessionUser.accountId,
  144. tenderId,
  145. audit.status.uncheck,
  146. audit.status.back,
  147. audit.status.revise,
  148. ];
  149. break;
  150. case 2: // 进行中(所有的)
  151. case 4: // 终止(所有的)
  152. sql =
  153. 'SELECT a.*, p.name as account_name FROM ?? AS a LEFT JOIN ?? AS p On a.uid = p.id WHERE ' +
  154. 'a.status = ? AND a.tid = ? AND (a.uid = ? OR a.id IN (SELECT b.cpid FROM ?? AS b WHERE b.aid = ? AND a.times = b.times GROUP BY b.cpid) OR a.id IN (SELECT c.cpid FROM ?? AS c WHERE c.aid = ? AND c.tid = ?))';
  155. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, status, tenderId, this.ctx.session.sessionUser.accountId, this.ctx.service.changeProjectAudit.tableName, this.ctx.session.sessionUser.accountId, this.ctx.service.changeProjectXsAudit.tableName, this.ctx.session.sessionUser.accountId, tenderId];
  156. break;
  157. case 3: // 已完成(所有的)
  158. sql = 'SELECT a.*, p.name as account_name FROM ?? AS a LEFT JOIN ?? AS p On a.uid = p.id WHERE a.status = ? AND a.tid = ?';
  159. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, status, tenderId];
  160. break;
  161. default:
  162. break;
  163. }
  164. }
  165. if (sortBy && orderBy) {
  166. if (sortBy === 'code') {
  167. sql += ' ORDER BY CHAR_LENGTH(a.code) ' + orderBy + ',convert(a.code using gbk) ' + orderBy;
  168. } else {
  169. sql += ' ORDER BY a.in_time ' + orderBy;
  170. }
  171. } else {
  172. sql += ' ORDER BY a.in_time DESC';
  173. }
  174. if (hadlimit) {
  175. const limit = this.ctx.pageSize ? this.ctx.pageSize : this.app.config.pageSize;
  176. const offset = limit * (this.ctx.page - 1);
  177. const limitString = offset >= 0 ? offset + ',' + limit : limit;
  178. sql += ' LIMIT ' + limitString;
  179. }
  180. const list = await this.db.query(sql, sqlParam);
  181. return list;
  182. }
  183. // 获取最后一个变更立项
  184. async getLastChange(tenderId) {
  185. const sql = 'select * from ?? where tid = ? order by in_time desc LIMIT 1';
  186. const sqlParam = [this.tableName, tenderId];
  187. const result = await this.db.queryOne(sql, sqlParam);
  188. return result;
  189. }
  190. /**
  191. * 获取变更令个数
  192. * @param {int} tenderId - 标段id
  193. * @param {int} status - 状态
  194. * @return {void}
  195. */
  196. async getCountByStatus(tenderId, status) {
  197. if (this.ctx.tender.isTourist) {
  198. let touristSql;
  199. let touristSqlParam;
  200. let touristResult;
  201. switch (status) {
  202. case 0:
  203. touristSql = 'SELECT count(*) AS count FROM ?? WHERE tid = ? ORDER BY in_time DESC';
  204. touristSqlParam = [this.tableName, tenderId];
  205. touristResult = await this.db.query(touristSql, touristSqlParam);
  206. return touristResult[0].count;
  207. // case 1:
  208. // touristSql = 'SELECT count(*) AS count FROM ?? WHERE tid = ? AND (status = ? OR status = ?) ORDER BY in_time DESC';
  209. // touristSqlParam = [this.tableName, tenderId, audit.status.uncheck, audit.status.checking];
  210. // touristResult = await this.db.query(touristSql, touristSqlParam);
  211. // return touristResult[0].count;
  212. // case 5:
  213. // touristSql = 'SELECT count(*) AS count FROM ?? WHERE tid = ? AND status = ? ORDER BY in_time DESC';
  214. // touristSqlParam = [this.tableName, tenderId, audit.status.uncheck];
  215. // touristResult = await this.db.query(touristSql, touristSqlParam);
  216. // return touristResult[0].count;
  217. // case 2:
  218. // touristSql = 'SELECT count(*) AS count FROM ?? WHERE tid = ? AND status = ? ORDER BY in_time DESC';
  219. // touristSqlParam = [this.tableName, tenderId, audit.status.uncheck];
  220. // touristResult = await this.db.query(touristSql, touristSqlParam);
  221. // return touristResult[0].count;
  222. // case 4:
  223. // touristSql = 'SELECT count(*) AS count FROM ?? WHERE tid = ? AND status = ? ORDER BY in_time DESC';
  224. // touristSqlParam = [this.tableName, tenderId, status];
  225. // touristResult = await this.db.query(touristSql, touristSqlParam);
  226. // return touristResult[0].count;
  227. default:
  228. break;
  229. }
  230. }
  231. switch (status) {
  232. case 0: // 包含你的所有变更令
  233. const sql =
  234. 'SELECT count(*) AS count FROM ?? AS a WHERE a.tid = ? AND ' +
  235. '(a.uid = ? OR (a.status != ? AND (a.id IN (SELECT b.cpid FROM ?? AS b WHERE b.aid = ? AND a.times = b.times GROUP BY b.cpid) OR a.id IN (SELECT c.cpid FROM ?? AS c WHERE c.aid = ? AND c.tid = ?))) OR a.status = ? )';
  236. const sqlParam = [
  237. this.tableName,
  238. tenderId,
  239. this.ctx.session.sessionUser.accountId,
  240. audit.status.uncheck,
  241. this.ctx.service.changeProjectAudit.tableName,
  242. this.ctx.session.sessionUser.accountId,
  243. this.ctx.service.changeProjectXsAudit.tableName,
  244. this.ctx.session.sessionUser.accountId,
  245. tenderId,
  246. audit.status.checked,
  247. ];
  248. const result = await this.db.query(sql, sqlParam);
  249. return result[0].count;
  250. case 1: // 待处理(你的)
  251. // return await this.ctx.service.changeAudit.count({
  252. // tid: tenderId,
  253. // uid: this.ctx.session.sessionUser.accountId,
  254. // status: 2,
  255. // });
  256. const sql6 = 'SELECT count(*) AS count FROM ?? as a WHERE a.tid = ? AND (a.id in(SELECT b.cpid FROM ?? as b WHERE b.tid = ? AND b.aid = ? AND b.status = ?) OR (a.uid = ? AND (a.status = ? OR a.status = ? OR a.status = ?)))';
  257. const sqlParam6 = [this.tableName, tenderId, this.ctx.service.changeProjectAudit.tableName, tenderId, this.ctx.session.sessionUser.accountId, audit.status.checking, this.ctx.session.sessionUser.accountId, audit.status.uncheck, audit.status.back, audit.status.revise];
  258. const result6 = await this.db.query(sql6, sqlParam6);
  259. return result6[0].count;
  260. case 5: // 待上报(所有的)PS:取未上报,退回的变更立项
  261. const sql2 =
  262. 'SELECT count(*) AS count FROM ?? AS a WHERE ' +
  263. // 'a.id IN (SELECT b.cpid FROM ?? AS b WHERE b.aid = ? AND a.times = b.times GROUP BY b.cpid) ' +
  264. 'a.uid = ? AND a.tid = ? AND (a.status = ? OR a.status = ? OR a.status = ?)';
  265. const sqlParam2 = [
  266. this.tableName,
  267. // this.ctx.service.changeProjectAudit.tableName,
  268. this.ctx.session.sessionUser.accountId,
  269. tenderId,
  270. audit.status.uncheck,
  271. audit.status.back,
  272. audit.status.revise,
  273. ];
  274. const result2 = await this.db.query(sql2, sqlParam2);
  275. return result2[0].count;
  276. case 2: // 进行中(所有的)
  277. case 4: // 终止(所有的)
  278. const sql3 =
  279. 'SELECT count(*) AS count FROM ?? AS a WHERE ' +
  280. 'a.status = ? AND a.tid = ? AND (a.uid = ? OR a.id IN (SELECT b.cpid FROM ?? AS b WHERE b.aid = ? AND a.times = b.times GROUP BY b.cpid) OR a.id IN (SELECT c.cpid FROM ?? AS c WHERE c.aid = ? AND c.tid = ?))';
  281. const sqlParam3 = [this.tableName, status, tenderId, this.ctx.session.sessionUser.accountId, this.ctx.service.changeProjectAudit.tableName, this.ctx.session.sessionUser.accountId, this.ctx.service.changeProjectXsAudit.tableName, this.ctx.session.sessionUser.accountId, tenderId];
  282. const result3 = await this.db.query(sql3, sqlParam3);
  283. return result3[0].count;
  284. case 3: // 已完成(所有的)
  285. const sql4 = 'SELECT count(*) AS count FROM ?? WHERE status = ? AND tid = ?';
  286. const sqlParam4 = [this.tableName, status, tenderId];
  287. const result4 = await this.db.query(sql4, sqlParam4);
  288. return result4[0].count;
  289. default:
  290. break;
  291. }
  292. }
  293. /**
  294. * 保存变更信息
  295. * @param {int} postData - 表单提交的数据
  296. * @param {int} tenderId - 标段id
  297. * @return {void}
  298. */
  299. async saveInfo(cpId, postData) {
  300. // 初始化事务
  301. const transaction = await this.db.beginTransaction();
  302. let result = false;
  303. try {
  304. const updateData = {
  305. id: cpId,
  306. };
  307. updateData[postData.name] = postData.val;
  308. await transaction.update(this.tableName, updateData);
  309. await transaction.commit();
  310. result = true;
  311. } catch (error) {
  312. await transaction.rollback();
  313. result = false;
  314. }
  315. return result;
  316. }
  317. /**
  318. * 判断是否有重名的变更立项
  319. * @param cpid
  320. * @param code
  321. * @param tid
  322. * @return {Promise<void>}
  323. */
  324. async isRepeat(cpId, code, tid, type) {
  325. const sql = 'SELECT COUNT(*) as count FROM ?? WHERE `code` = ? AND `id` != ? AND `tid` = ? AND `type` = ?';
  326. const sqlParam = [this.tableName, code, cpId, tid, type];
  327. const result = await this.db.queryOne(sql, sqlParam);
  328. return result.count !== 0;
  329. }
  330. /**
  331. * 查询可用的变更令
  332. * @param { string } cid - 查询的清单
  333. * @return {Promise<*>} - 可用的变更令列表
  334. */
  335. async delete(id) {
  336. // 初始化事务
  337. this.transaction = await this.db.beginTransaction();
  338. let result = false;
  339. try {
  340. const changeInfo = await this.getDataById(id);
  341. // 先删除审批人列表
  342. await this.transaction.delete(this.ctx.service.changeProjectAudit.tableName, { cpid: id });
  343. // 再删除附件和附件文件ni zuo
  344. const attList = await this.ctx.service.changeProjectAtt.getAllDataByCondition({ where: { cpid: id } });
  345. await this.ctx.helper.delFiles(attList);
  346. await this.transaction.delete(this.ctx.service.changeProjectAtt.tableName, { cpid: id });
  347. // if (attList.length !== 0) {
  348. // for (const att of attList) {
  349. // await fs.unlinkSync(path.join(this.app.baseDir, att.filepath));
  350. // }
  351. // await this.transaction.delete(this.ctx.service.changeAtt.tableName, { cid });
  352. // }
  353. // 最后删除变更令
  354. await this.transaction.delete(this.tableName, { id });
  355. // 删除history
  356. await this.transaction.delete(this.ctx.service.changeProjectHistory.tableName, { cpid: id });
  357. // 记录删除日志
  358. await this.ctx.service.projectLog.addProjectLog(this.transaction, projectLogConst.type.changeProject, projectLogConst.status.delete, changeInfo.code);
  359. await this.transaction.commit();
  360. result = true;
  361. } catch (e) {
  362. await this.transaction.rollback();
  363. result = false;
  364. }
  365. return result;
  366. }
  367. async doCheckChangeCanCancel(change) {
  368. // 获取当前审批人的上一个审批人,判断是否是当前登录人,并赋予撤回功能,(当审批人存在有审批过时,上一人不允许再撤回)
  369. const status = audit.status;
  370. const accountId = this.ctx.session.sessionUser.accountId;
  371. const auditors = change.auditors;
  372. change.cancancel = 0;
  373. if (change.status !== status.checked && change.status !== status.uncheck && change.status !== status.revise) {
  374. if (change.status === status.back) {
  375. const lastAuditors = await this.service.changeProjectAudit.getAuditors(change.id, change.times - 1);
  376. const onAuditor = this._.findLast(lastAuditors, { status: status.back });
  377. if (onAuditor && onAuditor.aid === accountId) {
  378. change.cancancel = 4;// 审批人撤回退回原报
  379. }
  380. } else if (change.status === status.checkNo) {
  381. const onAuditor = this._.findLast(auditors, function(item) {
  382. return item.status === status.checkNo;
  383. });
  384. if (onAuditor && onAuditor.aid === accountId) {
  385. change.cancancel = 3; // 审批人撤回审批终止
  386. }
  387. } else {
  388. // 找出当前操作人上一个审批人,包括审批完成的和退回上一个审批人的,同时当前操作人为第一人时,就是则为原报
  389. const onAuditor = this._.find(auditors, function(item) {
  390. return item.aid === change.curAuditor.aid && item.status === status.checking;
  391. });
  392. const preAudit = onAuditor.order > 1 ? this._.find(auditors, { order: onAuditor.order - 1 }) : false;
  393. const preAid = preAudit ? (preAudit.status !== status.checkAgain ? preAudit.aid : false) : change.uid;
  394. // console.log(onAuditor, preAudit, auditors);
  395. if (onAuditor.aid === preAid && preAudit.status === status.checkCancel) {
  396. return;// 不可以多次撤回
  397. } else if (preAid === accountId && preAid !== change.uid) {
  398. if (preAudit.status === status.checked) {
  399. change.cancancel = 2;// 审批人撤回审批通过
  400. }
  401. change.preAudit = preAudit;
  402. } else if (preAid === accountId && preAid === change.uid) {
  403. change.cancancel = 1;// 原报撤回
  404. }
  405. }
  406. }
  407. }
  408. async getListByArchives(tid, ids) {
  409. if (ids.length === 0) return [];
  410. const sql = 'SELECT c.* FROM ?? as c LEFT JOIN (SELECT cpid, MAX(end_time) as end_time FROM ?? WHERE ' +
  411. 'tid = ? AND cpid in (' + this.ctx.helper.getInArrStrSqlFilter(ids) + ') GROUP BY cpid) as ca ON c.id = ca.cpid WHERE' +
  412. ' c.tid = ? AND c.id in (' + this.ctx.helper.getInArrStrSqlFilter(ids) + ') AND c.status = ? ORDER BY ca.end_time DESC';
  413. const params = [this.tableName, this.ctx.service.changeProjectAudit.tableName, tid, tid, audit.status.checked];
  414. const list = await this.db.query(sql, params);
  415. return list;
  416. }
  417. }
  418. return ChangeProject;
  419. };