change_project.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. * 获取变更令个数
  185. * @param {int} tenderId - 标段id
  186. * @param {int} status - 状态
  187. * @return {void}
  188. */
  189. async getCountByStatus(tenderId, status) {
  190. if (this.ctx.tender.isTourist) {
  191. let touristSql;
  192. let touristSqlParam;
  193. let touristResult;
  194. switch (status) {
  195. case 0:
  196. touristSql = 'SELECT count(*) AS count FROM ?? WHERE tid = ? ORDER BY in_time DESC';
  197. touristSqlParam = [this.tableName, tenderId];
  198. touristResult = await this.db.query(touristSql, touristSqlParam);
  199. return touristResult[0].count;
  200. // case 1:
  201. // touristSql = 'SELECT count(*) AS count FROM ?? WHERE tid = ? AND (status = ? OR status = ?) ORDER BY in_time DESC';
  202. // touristSqlParam = [this.tableName, tenderId, audit.status.uncheck, audit.status.checking];
  203. // touristResult = await this.db.query(touristSql, touristSqlParam);
  204. // return touristResult[0].count;
  205. // case 5:
  206. // touristSql = 'SELECT count(*) AS count FROM ?? WHERE tid = ? AND status = ? ORDER BY in_time DESC';
  207. // touristSqlParam = [this.tableName, tenderId, audit.status.uncheck];
  208. // touristResult = await this.db.query(touristSql, touristSqlParam);
  209. // return touristResult[0].count;
  210. // case 2:
  211. // touristSql = 'SELECT count(*) AS count FROM ?? WHERE tid = ? AND status = ? ORDER BY in_time DESC';
  212. // touristSqlParam = [this.tableName, tenderId, audit.status.uncheck];
  213. // touristResult = await this.db.query(touristSql, touristSqlParam);
  214. // return touristResult[0].count;
  215. // case 4:
  216. // touristSql = 'SELECT count(*) AS count FROM ?? WHERE tid = ? AND status = ? ORDER BY in_time DESC';
  217. // touristSqlParam = [this.tableName, tenderId, status];
  218. // touristResult = await this.db.query(touristSql, touristSqlParam);
  219. // return touristResult[0].count;
  220. default:
  221. break;
  222. }
  223. }
  224. switch (status) {
  225. case 0: // 包含你的所有变更令
  226. const sql =
  227. 'SELECT count(*) AS count FROM ?? AS a WHERE a.tid = ? AND ' +
  228. '(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 = ? )';
  229. const sqlParam = [
  230. this.tableName,
  231. tenderId,
  232. this.ctx.session.sessionUser.accountId,
  233. audit.status.uncheck,
  234. this.ctx.service.changeProjectAudit.tableName,
  235. this.ctx.session.sessionUser.accountId,
  236. this.ctx.service.changeProjectXsAudit.tableName,
  237. this.ctx.session.sessionUser.accountId,
  238. tenderId,
  239. audit.status.checked,
  240. ];
  241. const result = await this.db.query(sql, sqlParam);
  242. return result[0].count;
  243. case 1: // 待处理(你的)
  244. // return await this.ctx.service.changeAudit.count({
  245. // tid: tenderId,
  246. // uid: this.ctx.session.sessionUser.accountId,
  247. // status: 2,
  248. // });
  249. 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 = ?)))';
  250. 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];
  251. const result6 = await this.db.query(sql6, sqlParam6);
  252. return result6[0].count;
  253. case 5: // 待上报(所有的)PS:取未上报,退回的变更立项
  254. const sql2 =
  255. 'SELECT count(*) AS count FROM ?? AS a WHERE ' +
  256. // 'a.id IN (SELECT b.cpid FROM ?? AS b WHERE b.aid = ? AND a.times = b.times GROUP BY b.cpid) ' +
  257. 'a.uid = ? AND a.tid = ? AND (a.status = ? OR a.status = ? OR a.status = ?)';
  258. const sqlParam2 = [
  259. this.tableName,
  260. // this.ctx.service.changeProjectAudit.tableName,
  261. this.ctx.session.sessionUser.accountId,
  262. tenderId,
  263. audit.status.uncheck,
  264. audit.status.back,
  265. audit.status.revise,
  266. ];
  267. const result2 = await this.db.query(sql2, sqlParam2);
  268. return result2[0].count;
  269. case 2: // 进行中(所有的)
  270. case 4: // 终止(所有的)
  271. const sql3 =
  272. 'SELECT count(*) AS count FROM ?? AS a WHERE ' +
  273. '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 = ?))';
  274. 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];
  275. const result3 = await this.db.query(sql3, sqlParam3);
  276. return result3[0].count;
  277. case 3: // 已完成(所有的)
  278. const sql4 = 'SELECT count(*) AS count FROM ?? WHERE status = ? AND tid = ?';
  279. const sqlParam4 = [this.tableName, status, tenderId];
  280. const result4 = await this.db.query(sql4, sqlParam4);
  281. return result4[0].count;
  282. default:
  283. break;
  284. }
  285. }
  286. /**
  287. * 保存变更信息
  288. * @param {int} postData - 表单提交的数据
  289. * @param {int} tenderId - 标段id
  290. * @return {void}
  291. */
  292. async saveInfo(cpId, postData) {
  293. // 初始化事务
  294. const transaction = await this.db.beginTransaction();
  295. let result = false;
  296. try {
  297. const updateData = {
  298. id: cpId,
  299. };
  300. updateData[postData.name] = postData.val;
  301. await transaction.update(this.tableName, updateData);
  302. await transaction.commit();
  303. result = true;
  304. } catch (error) {
  305. await transaction.rollback();
  306. result = false;
  307. }
  308. return result;
  309. }
  310. /**
  311. * 判断是否有重名的变更立项
  312. * @param cpid
  313. * @param code
  314. * @param tid
  315. * @return {Promise<void>}
  316. */
  317. async isRepeat(cpId, code, tid, type) {
  318. const sql = 'SELECT COUNT(*) as count FROM ?? WHERE `code` = ? AND `id` != ? AND `tid` = ? AND `type` = ?';
  319. const sqlParam = [this.tableName, code, cpId, tid, type];
  320. const result = await this.db.queryOne(sql, sqlParam);
  321. return result.count !== 0;
  322. }
  323. /**
  324. * 查询可用的变更令
  325. * @param { string } cid - 查询的清单
  326. * @return {Promise<*>} - 可用的变更令列表
  327. */
  328. async delete(id) {
  329. // 初始化事务
  330. this.transaction = await this.db.beginTransaction();
  331. let result = false;
  332. try {
  333. const changeInfo = await this.getDataById(id);
  334. // 先删除审批人列表
  335. await this.transaction.delete(this.ctx.service.changeProjectAudit.tableName, { cpid: id });
  336. // 再删除附件和附件文件ni zuo
  337. const attList = await this.ctx.service.changeProjectAtt.getAllDataByCondition({ where: { cpid: id } });
  338. await this.ctx.helper.delFiles(attList);
  339. await this.transaction.delete(this.ctx.service.changeProjectAtt.tableName, { cpid: id });
  340. // if (attList.length !== 0) {
  341. // for (const att of attList) {
  342. // await fs.unlinkSync(path.join(this.app.baseDir, att.filepath));
  343. // }
  344. // await this.transaction.delete(this.ctx.service.changeAtt.tableName, { cid });
  345. // }
  346. // 最后删除变更令
  347. await this.transaction.delete(this.tableName, { id });
  348. // 删除history
  349. await this.transaction.delete(this.ctx.service.changeProjectHistory.tableName, { cpid: id });
  350. // 记录删除日志
  351. await this.ctx.service.projectLog.addProjectLog(this.transaction, projectLogConst.type.changeProject, projectLogConst.status.delete, changeInfo.code);
  352. await this.transaction.commit();
  353. result = true;
  354. } catch (e) {
  355. await this.transaction.rollback();
  356. result = false;
  357. }
  358. return result;
  359. }
  360. async doCheckChangeCanCancel(change) {
  361. // 获取当前审批人的上一个审批人,判断是否是当前登录人,并赋予撤回功能,(当审批人存在有审批过时,上一人不允许再撤回)
  362. const status = audit.status;
  363. const accountId = this.ctx.session.sessionUser.accountId;
  364. const auditors = change.auditors;
  365. change.cancancel = 0;
  366. if (change.status !== status.checked && change.status !== status.uncheck && change.status !== status.revise) {
  367. if (change.status === status.back) {
  368. const lastAuditors = await this.service.changeProjectAudit.getAuditors(change.id, change.times - 1);
  369. const onAuditor = this._.findLast(lastAuditors, { status: status.back });
  370. if (onAuditor && onAuditor.aid === accountId) {
  371. change.cancancel = 4;// 审批人撤回退回原报
  372. }
  373. } else if (change.status === status.checkNo) {
  374. const onAuditor = this._.findLast(auditors, function(item) {
  375. return item.status === status.checkNo;
  376. });
  377. if (onAuditor && onAuditor.aid === accountId) {
  378. change.cancancel = 3; // 审批人撤回审批终止
  379. }
  380. } else {
  381. // 找出当前操作人上一个审批人,包括审批完成的和退回上一个审批人的,同时当前操作人为第一人时,就是则为原报
  382. const onAuditor = this._.find(auditors, function(item) {
  383. return item.aid === change.curAuditor.aid && item.status === status.checking;
  384. });
  385. const preAudit = onAuditor.order > 1 ? this._.find(auditors, { order: onAuditor.order - 1 }) : false;
  386. const preAid = preAudit ? (preAudit.status !== status.checkAgain ? preAudit.aid : false) : change.uid;
  387. // console.log(onAuditor, preAudit, auditors);
  388. if (onAuditor.aid === preAid && preAudit.status === status.checkCancel) {
  389. return;// 不可以多次撤回
  390. } else if (preAid === accountId && preAid !== change.uid) {
  391. if (preAudit.status === status.checked) {
  392. change.cancancel = 2;// 审批人撤回审批通过
  393. }
  394. change.preAudit = preAudit;
  395. } else if (preAid === accountId && preAid === change.uid) {
  396. change.cancancel = 1;// 原报撤回
  397. }
  398. }
  399. }
  400. }
  401. }
  402. return ChangeProject;
  403. };