change_project.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. module.exports = app => {
  18. class ChangeProject extends app.BaseService {
  19. /**
  20. * 构造函数
  21. *
  22. * @param {Object} ctx - egg全局变量
  23. * @return {void}
  24. */
  25. constructor(ctx) {
  26. super(ctx);
  27. this.tableName = 'change_project';
  28. }
  29. async add(tenderId, userId, code, name) {
  30. const type = userId === this.ctx.tender.data.user_id ? codeRuleConst.ruleType.suggestion : codeRuleConst.ruleType.will;
  31. const sql = 'SELECT COUNT(*) as count FROM ?? WHERE `tid` = ? AND `code` = ? AND type = ?';
  32. const sqlParam = [this.tableName, tenderId, code, type];
  33. const codeCount = await this.db.queryOne(sql, sqlParam);
  34. const count = codeCount.count;
  35. if (count > 0) {
  36. throw '立项书编号重复';
  37. }
  38. // 初始化事务
  39. this.transaction = await this.db.beginTransaction();
  40. let result = false;
  41. try {
  42. const change = {
  43. tid: tenderId,
  44. uid: userId,
  45. status: audit.status.uncheck,
  46. times: 1,
  47. type,
  48. in_time: new Date(),
  49. code,
  50. name,
  51. };
  52. const operate = await this.transaction.insert(this.tableName, change);
  53. if (operate.affectedRows <= 0) {
  54. throw '新建变更令数据失败';
  55. }
  56. change.id = operate.insertId;
  57. // 先找出标段最近存在的变更令审批人的变更令info
  58. const preChangeInfo = await this.getHaveAuditLastInfo(tenderId, type);
  59. if (preChangeInfo) {
  60. // 并把之前存在的变更令审批人添加到zh_change_audit
  61. const auditResult = await this.ctx.service.changeProjectAudit.copyPreChangeProjectAuditors(this.transaction, preChangeInfo, change);
  62. if (!auditResult) {
  63. throw '复制上一次审批流程失败';
  64. }
  65. }
  66. result = change;
  67. this.transaction.commit();
  68. } catch (error) {
  69. console.log(error);
  70. // 回滚
  71. await this.transaction.rollback();
  72. }
  73. return result;
  74. }
  75. async getHaveAuditLastInfo(tenderId, type) {
  76. 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';
  77. const sqlParam = [this.tableName, this.ctx.service.changeProjectAudit.tableName, tenderId, type];
  78. return await this.db.queryOne(sql, sqlParam);
  79. }
  80. /**
  81. * 获取变更立项列表
  82. * @param {int} tenderId - 标段id
  83. * @param {int} status - 状态
  84. * @param {int} hadlimit - 分页
  85. * @return {object} list - 列表
  86. */
  87. async getListByStatus(tenderId, status = 0, hadlimit = 1, sortBy = '', orderBy = '') {
  88. let sql = '';
  89. let sqlParam = '';
  90. if (this.ctx.tender.isTourist && status === 0) {
  91. sql = 'SELECT a.*, p.name as account_name FROM ?? As a LEFT JOIN ?? AS p On a.uid = p.id WHERE a.tid = ?';
  92. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, tenderId];
  93. } else {
  94. switch (status) {
  95. case 0: // 包含你的所有变更立项
  96. sql =
  97. 'SELECT a.*, p.name as account_name FROM ?? AS a LEFT JOIN ?? AS p On a.uid = p.id WHERE a.tid = ? AND ' +
  98. '(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 = ? )';
  99. sqlParam = [
  100. this.tableName,
  101. this.ctx.service.projectAccount.tableName,
  102. tenderId,
  103. this.ctx.session.sessionUser.accountId,
  104. audit.status.uncheck,
  105. this.ctx.service.changeProjectAudit.tableName,
  106. this.ctx.session.sessionUser.accountId,
  107. this.ctx.service.changeProjectXsAudit.tableName,
  108. this.ctx.session.sessionUser.accountId,
  109. tenderId,
  110. audit.status.checked,
  111. ];
  112. break;
  113. case 1: // 待处理(你的)
  114. sql = 'SELECT a.*, p.name as account_name FROM ?? as a LEFT JOIN ?? AS p On a.uid = p.id 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 = ?)))';
  115. sqlParam = [this.tableName, this.ctx.service.projectAccount.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];
  116. break;
  117. case 5: // 待上报(所有的)PS:取未上报,退回,修订的变更令
  118. sql =
  119. 'SELECT a.*, p.name as account_name FROM ?? AS a LEFT JOIN ?? AS p On a.uid = p.id WHERE ' +
  120. // 'a.id IN (SELECT b.cpid FROM ?? AS b WHERE b.aid = ? AND a.times = b.times GROUP BY b.cpid) AND ' +
  121. 'a.uid = ? AND a.tid = ? AND (a.status = ? OR a.status = ?)';
  122. sqlParam = [
  123. this.tableName,
  124. this.ctx.service.projectAccount.tableName,
  125. // this.ctx.service.changeProjectAudit.tableName,
  126. this.ctx.session.sessionUser.accountId,
  127. tenderId,
  128. audit.status.uncheck,
  129. audit.status.back,
  130. ];
  131. break;
  132. case 2: // 进行中(所有的)
  133. case 4: // 终止(所有的)
  134. sql =
  135. 'SELECT a.*, p.name as account_name FROM ?? AS a LEFT JOIN ?? AS p On a.uid = p.id WHERE ' +
  136. '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 = ?))';
  137. 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];
  138. break;
  139. case 3: // 已完成(所有的)
  140. 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 = ?';
  141. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, status, tenderId];
  142. break;
  143. default:
  144. break;
  145. }
  146. }
  147. if (sortBy && orderBy) {
  148. if (sortBy === 'code') {
  149. sql += ' ORDER BY CHAR_LENGTH(a.code) ' + orderBy + ',convert(a.code using gbk) ' + orderBy;
  150. } else {
  151. sql += ' ORDER BY a.in_time ' + orderBy;
  152. }
  153. } else {
  154. sql += ' ORDER BY a.in_time DESC';
  155. }
  156. if (hadlimit) {
  157. const limit = this.app.config.pageSize;
  158. const offset = limit * (this.ctx.page - 1);
  159. const limitString = offset >= 0 ? offset + ',' + limit : limit;
  160. sql += ' LIMIT ' + limitString;
  161. }
  162. const list = await this.db.query(sql, sqlParam);
  163. return list;
  164. }
  165. /**
  166. * 获取变更令个数
  167. * @param {int} tenderId - 标段id
  168. * @param {int} status - 状态
  169. * @return {void}
  170. */
  171. async getCountByStatus(tenderId, status) {
  172. if (this.ctx.tender.isTourist) {
  173. let touristSql;
  174. let touristSqlParam;
  175. let touristResult;
  176. switch (status) {
  177. case 0:
  178. touristSql = 'SELECT count(*) AS count FROM ?? WHERE tid = ? ORDER BY in_time DESC';
  179. touristSqlParam = [this.tableName, tenderId];
  180. touristResult = await this.db.query(touristSql, touristSqlParam);
  181. return touristResult[0].count;
  182. // case 1:
  183. // touristSql = 'SELECT count(*) AS count FROM ?? WHERE tid = ? AND (status = ? OR status = ?) ORDER BY in_time DESC';
  184. // touristSqlParam = [this.tableName, tenderId, audit.status.uncheck, audit.status.checking];
  185. // touristResult = await this.db.query(touristSql, touristSqlParam);
  186. // return touristResult[0].count;
  187. // case 5:
  188. // touristSql = 'SELECT count(*) AS count FROM ?? WHERE tid = ? AND status = ? ORDER BY in_time DESC';
  189. // touristSqlParam = [this.tableName, tenderId, audit.status.uncheck];
  190. // touristResult = await this.db.query(touristSql, touristSqlParam);
  191. // return touristResult[0].count;
  192. // case 2:
  193. // touristSql = 'SELECT count(*) AS count FROM ?? WHERE tid = ? AND status = ? ORDER BY in_time DESC';
  194. // touristSqlParam = [this.tableName, tenderId, audit.status.uncheck];
  195. // touristResult = await this.db.query(touristSql, touristSqlParam);
  196. // return touristResult[0].count;
  197. // case 4:
  198. // touristSql = 'SELECT count(*) AS count FROM ?? WHERE tid = ? AND status = ? ORDER BY in_time DESC';
  199. // touristSqlParam = [this.tableName, tenderId, status];
  200. // touristResult = await this.db.query(touristSql, touristSqlParam);
  201. // return touristResult[0].count;
  202. default:
  203. break;
  204. }
  205. }
  206. switch (status) {
  207. case 0: // 包含你的所有变更令
  208. const sql =
  209. 'SELECT count(*) AS count FROM ?? AS a WHERE a.tid = ? AND ' +
  210. '(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 = ? )';
  211. const sqlParam = [
  212. this.tableName,
  213. tenderId,
  214. this.ctx.session.sessionUser.accountId,
  215. audit.status.uncheck,
  216. this.ctx.service.changeProjectAudit.tableName,
  217. this.ctx.session.sessionUser.accountId,
  218. this.ctx.service.changeProjectXsAudit.tableName,
  219. this.ctx.session.sessionUser.accountId,
  220. tenderId,
  221. audit.status.checked,
  222. ];
  223. const result = await this.db.query(sql, sqlParam);
  224. return result[0].count;
  225. case 1: // 待处理(你的)
  226. // return await this.ctx.service.changeAudit.count({
  227. // tid: tenderId,
  228. // uid: this.ctx.session.sessionUser.accountId,
  229. // status: 2,
  230. // });
  231. 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 = ?)))';
  232. 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];
  233. const result6 = await this.db.query(sql6, sqlParam6);
  234. return result6[0].count;
  235. case 5: // 待上报(所有的)PS:取未上报,退回的变更立项
  236. const sql2 =
  237. 'SELECT count(*) AS count FROM ?? AS a WHERE ' +
  238. // 'a.id IN (SELECT b.cpid FROM ?? AS b WHERE b.aid = ? AND a.times = b.times GROUP BY b.cpid) ' +
  239. 'a.uid = ? AND a.tid = ? AND (a.status = ? OR a.status = ?)';
  240. const sqlParam2 = [
  241. this.tableName,
  242. // this.ctx.service.changeProjectAudit.tableName,
  243. this.ctx.session.sessionUser.accountId,
  244. tenderId,
  245. audit.status.uncheck,
  246. audit.status.back,
  247. ];
  248. const result2 = await this.db.query(sql2, sqlParam2);
  249. return result2[0].count;
  250. case 2: // 进行中(所有的)
  251. case 4: // 终止(所有的)
  252. const sql3 =
  253. 'SELECT count(*) AS count FROM ?? AS a WHERE ' +
  254. '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 = ?))';
  255. 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];
  256. const result3 = await this.db.query(sql3, sqlParam3);
  257. return result3[0].count;
  258. case 3: // 已完成(所有的)
  259. const sql4 = 'SELECT count(*) AS count FROM ?? WHERE status = ? AND tid = ?';
  260. const sqlParam4 = [this.tableName, status, tenderId];
  261. const result4 = await this.db.query(sql4, sqlParam4);
  262. return result4[0].count;
  263. default:
  264. break;
  265. }
  266. }
  267. /**
  268. * 保存变更信息
  269. * @param {int} postData - 表单提交的数据
  270. * @param {int} tenderId - 标段id
  271. * @return {void}
  272. */
  273. async saveInfo(cpId, postData) {
  274. // 初始化事务
  275. const transaction = await this.db.beginTransaction();
  276. let result = false;
  277. try {
  278. const updateData = {
  279. id: cpId,
  280. };
  281. updateData[postData.name] = postData.val;
  282. await transaction.update(this.tableName, updateData);
  283. await transaction.commit();
  284. result = true;
  285. } catch (error) {
  286. await transaction.rollback();
  287. result = false;
  288. }
  289. return result;
  290. }
  291. /**
  292. * 判断是否有重名的变更立项
  293. * @param cpid
  294. * @param code
  295. * @param tid
  296. * @return {Promise<void>}
  297. */
  298. async isRepeat(cpId, code, tid, type) {
  299. const sql = 'SELECT COUNT(*) as count FROM ?? WHERE `code` = ? AND `id` != ? AND `tid` = ? AND `type` = ?';
  300. const sqlParam = [this.tableName, code, cpId, tid, type];
  301. const result = await this.db.queryOne(sql, sqlParam);
  302. return result.count !== 0;
  303. }
  304. /**
  305. * 查询可用的变更令
  306. * @param { string } cid - 查询的清单
  307. * @return {Promise<*>} - 可用的变更令列表
  308. */
  309. async delete(id) {
  310. // 初始化事务
  311. this.transaction = await this.db.beginTransaction();
  312. let result = false;
  313. try {
  314. const changeInfo = await this.getDataById(id);
  315. // 先删除审批人列表
  316. await this.transaction.delete(this.ctx.service.changeProjectAudit.tableName, { cpid: id });
  317. // 再删除附件和附件文件ni zuo
  318. const attList = await this.ctx.service.changeProjectAtt.getAllDataByCondition({ where: { cpid: id } });
  319. await this.ctx.helper.delFiles(attList);
  320. await this.transaction.delete(this.ctx.service.changeProjectAtt.tableName, { cpid: id });
  321. // if (attList.length !== 0) {
  322. // for (const att of attList) {
  323. // await fs.unlinkSync(path.join(this.app.baseDir, att.filepath));
  324. // }
  325. // await this.transaction.delete(this.ctx.service.changeAtt.tableName, { cid });
  326. // }
  327. // 最后删除变更令
  328. await this.transaction.delete(this.tableName, { id });
  329. // 记录删除日志
  330. await this.ctx.service.projectLog.addProjectLog(this.transaction, projectLogConst.type.changeProject, projectLogConst.status.delete, changeInfo.code);
  331. await this.transaction.commit();
  332. result = true;
  333. } catch (e) {
  334. await this.transaction.rollback();
  335. result = false;
  336. }
  337. return result;
  338. }
  339. }
  340. return ChangeProject;
  341. };