change_apply.js 15 KB

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