change_plan.js 22 KB

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