change_project.js 19 KB

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