change_apply.js 15 KB

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