tender.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. 'use strict';
  2. /**
  3. * 标段数据模型
  4. *
  5. * @author CaiAoLin
  6. * @date 2017/11/30
  7. * @version
  8. */
  9. const tenderConst = require('../const/tender');
  10. const auditConst = require('../const/audit');
  11. const projectLogConst = require('../const/project_log');
  12. const fs = require('fs');
  13. const path = require('path');
  14. const commonQueryColumns = [
  15. 'id', 'project_id', 'name', 'status', 'category', 'ledger_times', 'ledger_status', 'measure_type', 'user_id', 'valuation',
  16. 'total_price', 'deal_tp', 'copy_id', 's2b_gxby_check', 's2b_gxby_limit', 's2b_dagl_check', 's2b_dagl_limit', 'has_rela', 'his_id',
  17. ];
  18. module.exports = app => {
  19. class Tender extends app.BaseService {
  20. /**
  21. * 构造函数
  22. *
  23. * @param {Object} ctx - egg全局变量
  24. * @return {void}
  25. */
  26. constructor(ctx) {
  27. super(ctx);
  28. this.tableName = 'tender';
  29. // 状态相关
  30. this.status = {
  31. TRY: 1,
  32. NORMAL: 2,
  33. DISABLE: 3,
  34. };
  35. this.displayStatus = [];
  36. this.displayStatus[this.status.TRY] = '试用';
  37. this.displayStatus[this.status.NORMAL] = '正常';
  38. this.displayStatus[this.status.DISABLE] = '禁用';
  39. this.statusClass = [];
  40. this.statusClass[this.status.TRY] = 'warning';
  41. this.statusClass[this.status.NORMAL] = 'success';
  42. this.statusClass[this.status.DISABLE] = 'danger';
  43. }
  44. /**
  45. * 数据规则
  46. *
  47. * @param {String} scene - 场景
  48. * @return {Object} - 返回数据规则
  49. */
  50. rule(scene) {
  51. let rule = {};
  52. switch (scene) {
  53. case 'add':
  54. rule = {
  55. name: { type: 'string', required: true, min: 2 },
  56. type: { type: 'string', required: true, min: 1 },
  57. };
  58. break;
  59. case 'save':
  60. rule = {
  61. name: { type: 'string', required: true, min: 2 },
  62. type: { type: 'string', required: true, min: 1 },
  63. };
  64. default:
  65. break;
  66. }
  67. return rule;
  68. }
  69. /**
  70. * 获取你所参与的标段的列表
  71. *
  72. * @param {String} listStatus - 取列表状态,如果是管理页要传
  73. * @param {String} permission - 根据权限取值
  74. * @param {Number} getAll - 是否取所有标段
  75. * @return {Array} - 返回标段数据
  76. */
  77. async getList(listStatus = '', permission = null, getAll = 0) {
  78. // 获取当前项目信息
  79. const session = this.ctx.session;
  80. let sql = '';
  81. let sqlParam = [];
  82. if (listStatus === 'manage') {
  83. // 管理页面只取属于自己创建的标段
  84. sql = 'SELECT t.`id`, t.`project_id`, t.`name`, t.`status`, t.`category`, t.`ledger_times`, t.`ledger_status`, t.`measure_type`, t.`user_id`, t.`create_time`, t.`total_price`, t.`deal_tp`,' +
  85. ' pa.`name` As `user_name`, pa.`role` As `user_role`, pa.`company` As `user_company` ' +
  86. ' FROM ?? As t ' +
  87. ' Left Join ?? As pa ' +
  88. ' ON t.`user_id` = pa.`id` ' +
  89. ' WHERE t.`project_id` = ? AND t.`user_id` = ? ORDER BY CONVERT(t.`name` USING GBK) ASC';
  90. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, session.sessionProject.id, session.sessionUser.accountId];
  91. } else if (getAll === 1 || (permission !== null && permission.tender !== undefined && permission.tender.indexOf('2') !== -1)) {
  92. // 具有查看所有标段权限的用户查阅标段
  93. sql = 'SELECT t.`id`, t.`project_id`, t.`name`, t.`status`, t.`category`, t.`ledger_times`, t.`ledger_status`, t.`measure_type`, t.`user_id`, t.`create_time`, t.`total_price`, t.`deal_tp`,' +
  94. ' pa.`name` As `user_name`, pa.`role` As `user_role`, pa.`company` As `user_company` ' +
  95. ' FROM ?? As t ' +
  96. ' Left Join ?? As pa ' +
  97. ' ON t.`user_id` = pa.`id` ' +
  98. ' WHERE t.`project_id` = ? ORDER BY CONVERT(t.`name` USING GBK) ASC';
  99. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, session.sessionProject.id];
  100. } else {
  101. // 根据用户权限查阅标段
  102. // tender 163条数据,project_account 68条数据测试
  103. // 查询两张表耗时0.003s,查询tender左连接project_account耗时0.002s
  104. const changeProjectSql = this.ctx.session.sessionProject.page_show.openChangeProject ? ' OR (t.`ledger_status` = ' + auditConst.ledger.status.checked + ' AND ' +
  105. ' t.id IN ( SELECT cpa.`tid` FROM ' + this.ctx.service.changeProjectAudit.tableName + ' AS cpa WHERE cpa.`aid` = ' + session.sessionUser.accountId + ' GROUP BY cpa.`tid`))' : '';
  106. const changeApplySql = this.ctx.session.sessionProject.page_show.openChangeApply ? ' OR (t.`ledger_status` = ' + auditConst.ledger.status.checked + ' AND ' +
  107. ' t.id IN ( SELECT caa.`tid` FROM ' + this.ctx.service.changeApplyAudit.tableName + ' AS caa WHERE caa.`aid` = ' + session.sessionUser.accountId + ' GROUP BY caa.`tid`))' : '';
  108. // 协审sql
  109. const changeProjectXsSql = this.ctx.session.sessionProject.page_show.openChangeProject ? ' OR (t.`ledger_status` = ' + auditConst.ledger.status.checked + ' AND ' +
  110. ' t.id IN ( SELECT cpxa.`tid` FROM ' + this.ctx.service.changeProjectXsAudit.tableName + ' AS cpxa WHERE cpxa.`aid` = ' + session.sessionUser.accountId + ' GROUP BY cpxa.`tid`))' : '';
  111. sql = 'SELECT t.`id`, t.`project_id`, t.`name`, t.`status`, t.`category`, t.`ledger_times`, t.`ledger_status`, t.`measure_type`, t.`user_id`, t.`create_time`, t.`total_price`, t.`deal_tp`,' +
  112. ' pa.`name` As `user_name`, pa.`role` As `user_role`, pa.`company` As `user_company` ' +
  113. // ' FROM ?? As t, ?? As pa ' +
  114. // ' WHERE t.`project_id` = ? AND t.`user_id` = pa.`id` AND (' +
  115. ' FROM ?? As t ' +
  116. ' Left Join ?? As pa ' +
  117. ' ON t.`user_id` = pa.`id` ' +
  118. ' WHERE t.`project_id` = ? AND (' +
  119. // 创建的标段
  120. ' t.`user_id` = ?' +
  121. // 参与审批 台账 的标段
  122. ' OR (t.`ledger_status` != ' + auditConst.ledger.status.uncheck + ' AND ' +
  123. ' t.id IN ( SELECT la.`tender_id` FROM ?? As la WHERE la.`audit_id` = ? GROUP BY la.`tender_id`))' +
  124. // 参与审批 计量期 的标段
  125. ' OR (t.`ledger_status` = ' + auditConst.ledger.status.checked + ' AND ' +
  126. ' t.id IN ( SELECT sa.`tid` FROM ?? As sa WHERE sa.`aid` = ? GROUP BY sa.`tid`))' +
  127. // 参与审批 变更令 的标段
  128. ' OR (t.`ledger_status` = ' + auditConst.ledger.status.checked + ' AND ' +
  129. ' t.id IN ( SELECT ca.`tid` FROM ?? AS ca WHERE ca.`uid` = ? GROUP BY ca.`tid`))' +
  130. // 参与审批 台账修订 的标段
  131. ' OR (t.`ledger_status` = ' + auditConst.ledger.status.checked + ' AND ' +
  132. ' t.id IN ( SELECT ra.`tender_id` FROM ?? AS ra WHERE ra.`audit_id` = ? GROUP BY ra.`tender_id`))' +
  133. // 参与审批 材料调差 的标段
  134. ' OR (t.`ledger_status` = ' + auditConst.ledger.status.checked + ' AND ' +
  135. ' t.id IN ( SELECT ma.`tid` FROM ?? AS ma WHERE ma.`aid` = ? GROUP BY ma.`tid`))' +
  136. // 参与审批 预付款 的标段
  137. ' OR (t.id IN ( SELECT ad.`tid` FROM ?? AS ad WHERE ad.`audit_id` = ? GROUP BY ad.`tid`))' +
  138. // 参与审批 变更立项书及变更申请 的标段
  139. changeProjectSql + changeApplySql + changeProjectXsSql +
  140. // 游客权限的标段
  141. ' OR (t.id IN ( SELECT tt.`tid` FROM ?? AS tt WHERE tt.`user_id` = ?))' +
  142. // 未参与,但可见的标段
  143. ') ORDER BY CONVERT(t.`name` USING GBK) ASC';
  144. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, session.sessionProject.id, session.sessionUser.accountId,
  145. this.ctx.service.ledgerAudit.tableName, session.sessionUser.accountId,
  146. this.ctx.service.stageAudit.tableName, session.sessionUser.accountId,
  147. this.ctx.service.changeAudit.tableName, session.sessionUser.accountId,
  148. this.ctx.service.reviseAudit.tableName, session.sessionUser.accountId,
  149. this.ctx.service.materialAudit.tableName, session.sessionUser.accountId,
  150. this.ctx.service.advanceAudit.tableName, session.sessionUser.accountId,
  151. this.ctx.service.tenderTourist.tableName, session.sessionUser.accountId,
  152. ];
  153. }
  154. const list = await this.db.query(sql, sqlParam);
  155. for (const l of list) {
  156. l.category = l.category && l.category !== '' ? JSON.parse(l.category) : null;
  157. }
  158. return list;
  159. }
  160. async getList4Select(selectType) {
  161. const accountInfo = await this.ctx.service.projectAccount.getDataById(this.ctx.session.sessionUser.accountId);
  162. const userPermission = accountInfo !== undefined && accountInfo.permission !== '' ? JSON.parse(accountInfo.permission) : null;
  163. const tenderList = await this.ctx.service.tender.getList('', userPermission);
  164. for (const t of tenderList) {
  165. if (t.ledger_status === auditConst.ledger.status.checked) {
  166. t.lastStage = await this.ctx.service.stage.getLastestStage(t.id, false);
  167. t.completeStage = await this.ctx.service.stage.getLastestCompleteStage(t.id);
  168. }
  169. }
  170. switch (selectType) {
  171. case 'ledger': return tenderList.filter(x => {
  172. return x.ledger_status === auditConst.ledger.status.checked;
  173. });
  174. case 'revise': tenderList.filter(x => {
  175. return x.ledger_status === auditConst.ledger.status.checked;
  176. });
  177. case 'stage': return tenderList.filter(x => {
  178. return x.ledger_status === auditConst.ledger.status.checked && !!x.lastStage;
  179. });
  180. case 'stage-checked': return tenderList.filter(x => {
  181. return !!x.completeStage;
  182. });
  183. default: return tenderList;
  184. }
  185. }
  186. async getTender(id) {
  187. this.initSqlBuilder();
  188. this.sqlBuilder.setAndWhere('id', {
  189. value: id,
  190. operate: '=',
  191. });
  192. this.sqlBuilder.columns = commonQueryColumns;
  193. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  194. const tender = await this.db.queryOne(sql, sqlParam);
  195. if (tender) {
  196. tender.category = tender.category && tender.category !== '' ? JSON.parse(tender.category) : null;
  197. }
  198. return tender;
  199. }
  200. /**
  201. * 新增标段
  202. *
  203. * @param {Object} data - 提交的数据
  204. * @return {Boolean} - 返回新增结果
  205. */
  206. async add(data) {
  207. let result = false;
  208. this.transaction = await this.db.beginTransaction();
  209. try {
  210. // 获取当前用户信息
  211. const sessionUser = this.ctx.session.sessionUser;
  212. // 获取当前项目信息
  213. const sessionProject = this.ctx.session.sessionProject;
  214. const insertData = {
  215. name: data.name,
  216. status: tenderConst.status.APPROVAL,
  217. project_id: sessionProject.id,
  218. user_id: sessionUser.accountId,
  219. create_time: new Date(),
  220. category: JSON.stringify(data.category),
  221. valuation: data.valuation,
  222. };
  223. const operate = await this.transaction.insert(this.tableName, insertData);
  224. result = operate.insertId > 0;
  225. if (!result) {
  226. throw '新增标段数据失败';
  227. }
  228. // 获取合同支付模板 并添加到标段
  229. result = await this.ctx.service.pay.addDefaultPayData(operate.insertId, this.transaction);
  230. if (!result) {
  231. throw '新增合同支付数据失败';
  232. }
  233. await this.ctx.service.tenderTag.addTenderTag(operate.insertId, sessionProject.id, this.transaction);
  234. await this.transaction.commit();
  235. const sql = 'SELECT t.`id`, t.`project_id`, t.`name`, t.`status`, t.`category`, t.`ledger_times`, t.`ledger_status`, t.`measure_type`, t.`user_id`, t.`create_time`, t.`total_price`, t.`deal_tp`,' +
  236. ' pa.`name` As `user_name`, pa.`role` As `user_role`, pa.`company` As `user_company` ' +
  237. ' FROM ?? As t ' +
  238. ' Left Join ?? As pa ' +
  239. ' ON t.`user_id` = pa.`id` ' +
  240. ' WHERE t.`id` = ?';
  241. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, operate.insertId];
  242. const tender = await this.db.queryOne(sql, sqlParam);
  243. if (tender) {
  244. tender.category = tender.category && tender.category !== '' ? JSON.parse(tender.category) : null;
  245. }
  246. // 是否加入到决策大屏中
  247. if (sessionProject.page_show.addDataCollect === 0) {
  248. await this.ctx.service.datacollectTender.add(sessionProject.id, operate.insertId);
  249. }
  250. return tender;
  251. } catch (error) {
  252. await this.transaction.rollback();
  253. throw error;
  254. }
  255. }
  256. /**
  257. * 保存标段
  258. *
  259. * @param {Object} postData - 表单post过来的数据
  260. * @param {Number} id - 用于判断修改还是新增的id
  261. * @return {Boolean} - 返回执行结果
  262. */
  263. async save(postData, id = 0) {
  264. id = parseInt(id);
  265. const rowData = {
  266. id,
  267. name: postData.name,
  268. type: postData.type,
  269. category: JSON.stringify(postData.category),
  270. };
  271. const result = await this.db.update(this.tableName, rowData);
  272. return result.affectedRows > 0;
  273. }
  274. /**
  275. * 假删除
  276. *
  277. * @param {Number} id - 删除的id
  278. * @return {Boolean} - 删除结果
  279. */
  280. async deleteTenderById(id) {
  281. const updateData = {
  282. status: this.status.DISABLE,
  283. id,
  284. };
  285. const result = await this.db.update(this.tableName, updateData);
  286. return result.affectedRows > 0;
  287. }
  288. /**
  289. * 真删除
  290. * @param {Number} id - 删除的标段id
  291. * @return {Promise<boolean>} - 结果
  292. */
  293. async deleteTenderNoBackup(id) {
  294. const transaction = await this.db.beginTransaction();
  295. try {
  296. const tenderMsg = await this.getDataById(id);
  297. // 先删除附件文件
  298. const attList = await this.ctx.service.changeAtt.getAllDataByCondition({ where: { tid: id } });
  299. const newAttList = await this.ctx.service.materialFile.getAllMaterialFiles(id);
  300. attList.concat(newAttList);
  301. await this.ctx.helper.delFiles(attList);
  302. await transaction.delete(this.tableName, { id });
  303. await transaction.delete(this.ctx.service.tenderInfo.tableName, { tid: id });
  304. await transaction.delete(this.ctx.service.ledger.departTableName(id), { tender_id: id });
  305. await transaction.delete(this.ctx.service.ledgerAudit.tableName, { tender_id: id });
  306. await transaction.delete(this.ctx.service.pos.departTableName(id), { tid: id });
  307. await transaction.delete(this.ctx.service.pay.tableName, { tid: id });
  308. await transaction.delete(this.ctx.service.stage.tableName, { tid: id });
  309. await transaction.delete(this.ctx.service.stageAudit.tableName, { tid: id });
  310. await transaction.delete(this.ctx.service.stageBills.departTableName(id), { tid: id });
  311. await transaction.delete(this.ctx.service.stagePos.departTableName(id), { tid: id });
  312. await transaction.delete(this.ctx.service.stageBillsDgn.tableName, { tid: id });
  313. await transaction.delete(this.ctx.service.stageBillsFinal.departTableName(id), { tid: id });
  314. await transaction.delete(this.ctx.service.stagePosFinal.departTableName(id), { tid: id });
  315. await transaction.delete(this.ctx.service.stageDetail.tableName, { tid: id });
  316. await transaction.delete(this.ctx.service.stagePay.tableName, { tid: id });
  317. await transaction.delete(this.ctx.service.stageChange.tableName, { tid: id });
  318. await transaction.delete(this.ctx.service.stageJgcl.tableName, { tid: id });
  319. await transaction.delete(this.ctx.service.stageBonus.tableName, { tid: id });
  320. await transaction.delete(this.ctx.service.stageOther.tableName, { tid: id });
  321. await transaction.delete(this.ctx.service.stageRela.tableName, { tid: id });
  322. await transaction.delete(this.ctx.service.stageRelaBills.tableName, { tid: id });
  323. await transaction.delete(this.ctx.service.stageRelaBillsFinal.tableName, { tid: id });
  324. await transaction.delete(this.ctx.service.change.tableName, { tid: id });
  325. await transaction.delete(this.ctx.service.changeAudit.tableName, { tid: id });
  326. await transaction.delete(this.ctx.service.changeAuditList.tableName, { tid: id });
  327. await transaction.delete(this.ctx.service.changeCompany.tableName, { tid: id });
  328. await transaction.delete(this.ctx.service.ledgerRevise.tableName, { tid: id });
  329. await transaction.delete(this.ctx.service.reviseAudit.tableName, { tender_id: id });
  330. await transaction.delete(this.ctx.service.reviseBills.departTableName(id), { tender_id: id });
  331. await transaction.delete(this.ctx.service.revisePos.departTableName(id), { tid: id });
  332. await transaction.delete(this.ctx.service.material.tableName, { tid: id });
  333. await transaction.delete(this.ctx.service.materialAudit.tableName, { tid: id });
  334. await transaction.delete(this.ctx.service.materialBills.tableName, { tid: id });
  335. await transaction.delete(this.ctx.service.materialBillsHistory.tableName, { tid: id });
  336. await transaction.delete(this.ctx.service.materialList.tableName, { tid: id });
  337. await transaction.delete(this.ctx.service.materialListNotjoin.tableName, { tid: id });
  338. await transaction.delete(this.ctx.service.materialExponent.tableName, { tid: id });
  339. await transaction.delete(this.ctx.service.materialExponentHistory.tableName, { tid: id });
  340. await transaction.delete(this.ctx.service.materialFile.tableName, { tid: id });
  341. await transaction.delete(this.ctx.service.signatureUsed.tableName, { tender_id: id });
  342. await transaction.delete(this.ctx.service.signatureRole.tableName, { tender_id: id });
  343. await transaction.delete(this.ctx.service.changeAtt.tableName, { tid: id });
  344. await transaction.delete(this.ctx.service.materialFile.tableName, { tid: id });
  345. await transaction.delete(this.ctx.service.advanceFile.tableName, { tid: id });
  346. await transaction.delete(this.ctx.service.datacollectTender.tableName, { pid: this.ctx.session.sessionProject.id, tid: id });
  347. // 记录删除日志
  348. await this.ctx.service.projectLog.addProjectLog(transaction, projectLogConst.type.tender, projectLogConst.status.delete, tenderMsg.name, id);
  349. await transaction.commit();
  350. return true;
  351. } catch (err) {
  352. this.ctx.helper.log(err);
  353. await transaction.rollback();
  354. return false;
  355. }
  356. }
  357. async getCheckTender(tid) {
  358. const tender = await this.ctx.service.tender.getTender(tid);
  359. tender.info = await this.ctx.service.tenderInfo.getTenderInfo(tid);
  360. return tender;
  361. }
  362. async checkTender(tid) {
  363. if (this.ctx.tender) return;
  364. this.ctx.tender = await this.getCheckTender(tid);
  365. }
  366. async setTenderType(tender, type) {
  367. const templateId = await this.ctx.service.valuation.getValuationTemplate(tender.valuation, type);
  368. if (templateId === -1) throw '该模式下,台账模板不存在';
  369. // 获取标段项目节点模板
  370. const tenderNodeTemplateData = await this.ctx.service.tenderNodeTemplate.getData(templateId);
  371. const conn = await this.db.beginTransaction();
  372. try {
  373. await conn.update(this.tableName, { id: tender.id, measure_type: type });
  374. // 复制模板数据到标段数据表
  375. const result = await this.ctx.service.ledger.innerAdd(tenderNodeTemplateData, tender.id, conn);
  376. if (!result) {
  377. throw '初始化台账失败';
  378. }
  379. await conn.commit();
  380. } catch (err) {
  381. await conn.rollback();
  382. throw err;
  383. }
  384. }
  385. async saveApiRela(tid, updateData) {
  386. await this.db.update(this.tableName, updateData, {where: { id: tid } });
  387. }
  388. async saveTenderData(tid, updateData) {
  389. return await this.db.update(this.tableName, updateData, { where: { id: tid } });
  390. }
  391. }
  392. return Tender;
  393. };