change_project.js 17 KB

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