change_plan.js 22 KB

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