change_project.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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, audit.status.checked, code, audit.status.checked, 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.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. audit.status.checked,
  108. ];
  109. break;
  110. case 1: // 待处理(你的)
  111. sql = 'SELECT a.*, p.name as account_name FROM ?? as a LEFT JOIN ?? AS p On a.uid = p.id WHERE 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 = ?))';
  112. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, 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];
  113. break;
  114. case 5: // 待上报(所有的)PS:取未上报,退回,修订的变更令
  115. sql =
  116. 'SELECT a.*, p.name as account_name FROM ?? AS a LEFT JOIN ?? AS p On a.uid = p.id WHERE ' +
  117. // 'a.id IN (SELECT b.cpid FROM ?? AS b WHERE b.aid = ? AND a.times = b.times GROUP BY b.cpid) AND ' +
  118. 'a.uid = ? AND a.tid = ? AND (a.status = ? OR a.status = ?)';
  119. sqlParam = [
  120. this.tableName,
  121. this.ctx.service.projectAccount.tableName,
  122. // this.ctx.service.changeProjectAudit.tableName,
  123. this.ctx.session.sessionUser.accountId,
  124. tenderId,
  125. audit.status.uncheck,
  126. audit.status.back,
  127. ];
  128. break;
  129. case 2: // 进行中(所有的)
  130. case 4: // 终止(所有的)
  131. sql =
  132. 'SELECT a.*, p.name as account_name FROM ?? AS a LEFT JOIN ?? AS p On a.uid = p.id WHERE ' +
  133. '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))';
  134. 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];
  135. break;
  136. case 3: // 已完成(所有的)
  137. 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 = ?';
  138. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, status, tenderId];
  139. break;
  140. default:
  141. break;
  142. }
  143. }
  144. if (sortBy && orderBy) {
  145. if (sortBy === 'code') {
  146. sql += ' ORDER BY CHAR_LENGTH(a.code) ' + orderBy + ',convert(a.code using gbk) ' + orderBy;
  147. } else {
  148. sql += ' ORDER BY a.in_time ' + orderBy;
  149. }
  150. } else {
  151. sql += ' ORDER BY a.in_time DESC';
  152. }
  153. if (hadlimit) {
  154. const limit = this.app.config.pageSize;
  155. const offset = limit * (this.ctx.page - 1);
  156. const limitString = offset >= 0 ? offset + ',' + limit : limit;
  157. sql += ' LIMIT ' + limitString;
  158. }
  159. const list = await this.db.query(sql, sqlParam);
  160. return list;
  161. }
  162. /**
  163. * 获取变更令个数
  164. * @param {int} tenderId - 标段id
  165. * @param {int} status - 状态
  166. * @return {void}
  167. */
  168. async getCountByStatus(tenderId, status) {
  169. if (this.ctx.tender.isTourist && status === 0) {
  170. const sql5 = 'SELECT count(*) AS count FROM ?? WHERE tid = ? ORDER BY in_time DESC';
  171. const sqlParam5 = [this.tableName, tenderId];
  172. const result5 = await this.db.query(sql5, sqlParam5);
  173. return result5[0].count;
  174. }
  175. switch (status) {
  176. case 0: // 包含你的所有变更令
  177. const sql =
  178. 'SELECT count(*) AS count FROM ?? AS a WHERE a.tid = ? AND ' +
  179. '(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.status = ? )';
  180. const sqlParam = [
  181. this.tableName,
  182. tenderId,
  183. this.ctx.session.sessionUser.accountId,
  184. audit.status.uncheck,
  185. this.ctx.service.changeProjectAudit.tableName,
  186. this.ctx.session.sessionUser.accountId,
  187. audit.status.checked,
  188. ];
  189. const result = await this.db.query(sql, sqlParam);
  190. return result[0].count;
  191. case 1: // 待处理(你的)
  192. // return await this.ctx.service.changeAudit.count({
  193. // tid: tenderId,
  194. // uid: this.ctx.session.sessionUser.accountId,
  195. // status: 2,
  196. // });
  197. const sql6 = 'SELECT count(*) AS count FROM ?? as a WHERE 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 = ?))';
  198. const sqlParam6 = [this.tableName, 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];
  199. const result6 = await this.db.query(sql6, sqlParam6);
  200. return result6[0].count;
  201. case 5: // 待上报(所有的)PS:取未上报,退回的变更立项
  202. const sql2 =
  203. 'SELECT count(*) AS count FROM ?? AS a WHERE ' +
  204. // 'a.id IN (SELECT b.cpid FROM ?? AS b WHERE b.aid = ? AND a.times = b.times GROUP BY b.cpid) ' +
  205. 'a.uid = ? AND a.tid = ? AND (a.status = ? OR a.status = ?)';
  206. const sqlParam2 = [
  207. this.tableName,
  208. // this.ctx.service.changeProjectAudit.tableName,
  209. this.ctx.session.sessionUser.accountId,
  210. tenderId,
  211. audit.status.uncheck,
  212. audit.status.back,
  213. ];
  214. const result2 = await this.db.query(sql2, sqlParam2);
  215. return result2[0].count;
  216. case 2: // 进行中(所有的)
  217. case 4: // 终止(所有的)
  218. const sql3 =
  219. 'SELECT count(*) AS count FROM ?? AS a WHERE ' +
  220. '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))';
  221. const sqlParam3 = [this.tableName, status, tenderId, this.ctx.session.sessionUser.accountId, this.ctx.service.changeProjectAudit.tableName, this.ctx.session.sessionUser.accountId];
  222. const result3 = await this.db.query(sql3, sqlParam3);
  223. return result3[0].count;
  224. case 3: // 已完成(所有的)
  225. const sql4 = 'SELECT count(*) AS count FROM ?? WHERE status = ? AND tid = ?';
  226. const sqlParam4 = [this.tableName, status, tenderId];
  227. const result4 = await this.db.query(sql4, sqlParam4);
  228. return result4[0].count;
  229. default:
  230. break;
  231. }
  232. }
  233. /**
  234. * 保存变更信息
  235. * @param {int} postData - 表单提交的数据
  236. * @param {int} tenderId - 标段id
  237. * @return {void}
  238. */
  239. async saveInfo(cpId, postData) {
  240. // 初始化事务
  241. const transaction = await this.db.beginTransaction();
  242. let result = false;
  243. try {
  244. const updateData = {
  245. id: cpId,
  246. };
  247. updateData[postData.name] = postData.val;
  248. await transaction.update(this.tableName, updateData);
  249. await transaction.commit();
  250. result = true;
  251. } catch (error) {
  252. await transaction.rollback();
  253. result = false;
  254. }
  255. return result;
  256. }
  257. /**
  258. * 审批终止
  259. * @param {int} postData - 表单提交的数据
  260. * @return {void}
  261. */
  262. async approvalStop(postData) {
  263. // 初始化事务
  264. this.transaction = await this.db.beginTransaction();
  265. let result = false;
  266. try {
  267. // 设置审批人终止
  268. const audit_update = {
  269. id: postData.audit_id,
  270. sdesc: postData.sdesc,
  271. status: 4,
  272. sin_time: new Date(),
  273. };
  274. await this.transaction.update(this.ctx.service.changeAudit.tableName, audit_update);
  275. // 设置变更令终止
  276. const change_update = {
  277. w_code: postData.w_code,
  278. status: 4,
  279. cin_time: Date.parse(new Date()) / 1000,
  280. };
  281. const options = {
  282. where: {
  283. cid: postData.change_id,
  284. },
  285. };
  286. await this.transaction.update(this.tableName, change_update, options);
  287. await this.transaction.commit();
  288. result = true;
  289. } catch (error) {
  290. await this.transaction.rollback();
  291. result = false;
  292. }
  293. return result;
  294. }
  295. /**
  296. * 查询可用的变更令
  297. * @param { string } cid - 查询的清单
  298. * @return {Promise<*>} - 可用的变更令列表
  299. */
  300. async delete(cid) {
  301. // 初始化事务
  302. this.transaction = await this.db.beginTransaction();
  303. let result = false;
  304. try {
  305. const changeInfo = await this.getDataByCondition({ cid });
  306. // 先删除清单,审批人列表
  307. await this.transaction.delete(this.ctx.service.changeAuditList.tableName, { cid });
  308. await this.transaction.delete(this.ctx.service.changeAudit.tableName, { cid });
  309. // 再删除附件和附件文件ni zuo
  310. const attList = await this.ctx.service.changeAtt.getAllDataByCondition({ where: { cid } });
  311. await this.ctx.helper.delFiles(attList);
  312. await this.transaction.delete(this.ctx.service.changeAtt.tableName, { cid });
  313. // if (attList.length !== 0) {
  314. // for (const att of attList) {
  315. // await fs.unlinkSync(path.join(this.app.baseDir, att.filepath));
  316. // }
  317. // await this.transaction.delete(this.ctx.service.changeAtt.tableName, { cid });
  318. // }
  319. // 最后删除变更令
  320. await this.transaction.delete(this.tableName, { cid });
  321. // 记录删除日志
  322. await this.ctx.service.projectLog.addProjectLog(this.transaction, projectLogConst.type.change, projectLogConst.status.delete, changeInfo.code);
  323. await this.transaction.commit();
  324. result = true;
  325. } catch (e) {
  326. await this.transaction.rollback();
  327. result = false;
  328. }
  329. return result;
  330. }
  331. /**
  332. * 判断是否有重名的变更立项
  333. * @param cpid
  334. * @param code
  335. * @param tid
  336. * @return {Promise<void>}
  337. */
  338. async isRepeat(cpId, code, tid, type) {
  339. const sql = 'SELECT COUNT(*) as count FROM ?? WHERE `code` = ? AND `id` != ? AND `tid` = ? AND `type` = ?';
  340. const sqlParam = [this.tableName, code, cpId, tid, type];
  341. const result = await this.db.queryOne(sql, sqlParam);
  342. return result.count !== 0;
  343. }
  344. }
  345. return ChangeProject;
  346. };