change_plan.js 21 KB

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