change_apply.js 15 KB

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