change_project.js 19 KB

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