change_project.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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.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, 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. sql += ' ORDER BY convert(a.code using gbk) ' + orderBy;
  151. } else {
  152. sql += ' ORDER BY a.in_time ' + orderBy;
  153. }
  154. } else {
  155. sql += ' ORDER BY a.in_time DESC';
  156. }
  157. if (hadlimit) {
  158. const limit = this.app.config.pageSize;
  159. const offset = limit * (this.ctx.page - 1);
  160. const limitString = offset >= 0 ? offset + ',' + limit : limit;
  161. sql += ' LIMIT ' + limitString;
  162. }
  163. console.log(sql, sqlParam);
  164. const list = await this.db.query(sql, sqlParam);
  165. return list;
  166. }
  167. /**
  168. * 获取变更令个数
  169. * @param {int} tenderId - 标段id
  170. * @param {int} status - 状态
  171. * @return {void}
  172. */
  173. async getCountByStatus(tenderId, status) {
  174. if (this.ctx.tender.isTourist && status === 0) {
  175. const sql5 = 'SELECT count(*) AS count FROM ?? WHERE tid = ? ORDER BY in_time DESC';
  176. const sqlParam5 = [this.tableName, tenderId];
  177. const result5 = await this.db.query(sql5, sqlParam5);
  178. return result5[0].count;
  179. }
  180. switch (status) {
  181. case 0: // 包含你的所有变更令
  182. const sql =
  183. 'SELECT count(*) AS count FROM ?? AS a WHERE a.tid = ? AND ' +
  184. '(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 = ? )';
  185. const sqlParam = [
  186. this.tableName,
  187. tenderId,
  188. this.ctx.session.sessionUser.accountId,
  189. audit.status.uncheck,
  190. this.ctx.service.changeProjectAudit.tableName,
  191. this.ctx.session.sessionUser.accountId,
  192. this.ctx.service.changeProjectXsAudit.tableName,
  193. this.ctx.session.sessionUser.accountId,
  194. tenderId,
  195. audit.status.checked,
  196. ];
  197. const result = await this.db.query(sql, sqlParam);
  198. return result[0].count;
  199. case 1: // 待处理(你的)
  200. // return await this.ctx.service.changeAudit.count({
  201. // tid: tenderId,
  202. // uid: this.ctx.session.sessionUser.accountId,
  203. // status: 2,
  204. // });
  205. 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 = ?))';
  206. 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];
  207. const result6 = await this.db.query(sql6, sqlParam6);
  208. return result6[0].count;
  209. case 5: // 待上报(所有的)PS:取未上报,退回的变更立项
  210. const sql2 =
  211. 'SELECT count(*) AS count FROM ?? AS a WHERE ' +
  212. // 'a.id IN (SELECT b.cpid FROM ?? AS b WHERE b.aid = ? AND a.times = b.times GROUP BY b.cpid) ' +
  213. 'a.uid = ? AND a.tid = ? AND (a.status = ? OR a.status = ?)';
  214. const sqlParam2 = [
  215. this.tableName,
  216. // this.ctx.service.changeProjectAudit.tableName,
  217. this.ctx.session.sessionUser.accountId,
  218. tenderId,
  219. audit.status.uncheck,
  220. audit.status.back,
  221. ];
  222. const result2 = await this.db.query(sql2, sqlParam2);
  223. return result2[0].count;
  224. case 2: // 进行中(所有的)
  225. case 4: // 终止(所有的)
  226. const sql3 =
  227. 'SELECT count(*) AS count FROM ?? AS a WHERE ' +
  228. '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 = ?))';
  229. 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];
  230. const result3 = await this.db.query(sql3, sqlParam3);
  231. return result3[0].count;
  232. case 3: // 已完成(所有的)
  233. const sql4 = 'SELECT count(*) AS count FROM ?? WHERE status = ? AND tid = ?';
  234. const sqlParam4 = [this.tableName, status, tenderId];
  235. const result4 = await this.db.query(sql4, sqlParam4);
  236. return result4[0].count;
  237. default:
  238. break;
  239. }
  240. }
  241. /**
  242. * 保存变更信息
  243. * @param {int} postData - 表单提交的数据
  244. * @param {int} tenderId - 标段id
  245. * @return {void}
  246. */
  247. async saveInfo(cpId, postData) {
  248. // 初始化事务
  249. const transaction = await this.db.beginTransaction();
  250. let result = false;
  251. try {
  252. const updateData = {
  253. id: cpId,
  254. };
  255. updateData[postData.name] = postData.val;
  256. await transaction.update(this.tableName, updateData);
  257. await transaction.commit();
  258. result = true;
  259. } catch (error) {
  260. await transaction.rollback();
  261. result = false;
  262. }
  263. return result;
  264. }
  265. /**
  266. * 判断是否有重名的变更立项
  267. * @param cpid
  268. * @param code
  269. * @param tid
  270. * @return {Promise<void>}
  271. */
  272. async isRepeat(cpId, code, tid, type) {
  273. const sql = 'SELECT COUNT(*) as count FROM ?? WHERE `code` = ? AND `id` != ? AND `tid` = ? AND `type` = ?';
  274. const sqlParam = [this.tableName, code, cpId, tid, type];
  275. const result = await this.db.queryOne(sql, sqlParam);
  276. return result.count !== 0;
  277. }
  278. /**
  279. * 查询可用的变更令
  280. * @param { string } cid - 查询的清单
  281. * @return {Promise<*>} - 可用的变更令列表
  282. */
  283. async delete(id) {
  284. // 初始化事务
  285. this.transaction = await this.db.beginTransaction();
  286. let result = false;
  287. try {
  288. const changeInfo = await this.getDataById(id);
  289. // 先删除审批人列表
  290. await this.transaction.delete(this.ctx.service.changeProjectAudit.tableName, { cpid: id });
  291. // 再删除附件和附件文件ni zuo
  292. const attList = await this.ctx.service.changeProjectAtt.getAllDataByCondition({ where: { cpid: id } });
  293. await this.ctx.helper.delFiles(attList);
  294. await this.transaction.delete(this.ctx.service.changeProjectAtt.tableName, { cpid: id });
  295. // if (attList.length !== 0) {
  296. // for (const att of attList) {
  297. // await fs.unlinkSync(path.join(this.app.baseDir, att.filepath));
  298. // }
  299. // await this.transaction.delete(this.ctx.service.changeAtt.tableName, { cid });
  300. // }
  301. // 最后删除变更令
  302. await this.transaction.delete(this.tableName, { id });
  303. // 记录删除日志
  304. await this.ctx.service.projectLog.addProjectLog(this.transaction, projectLogConst.type.changeProject, projectLogConst.status.delete, changeInfo.code);
  305. await this.transaction.commit();
  306. result = true;
  307. } catch (e) {
  308. await this.transaction.rollback();
  309. result = false;
  310. }
  311. return result;
  312. }
  313. }
  314. return ChangeProject;
  315. };