tender.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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.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. 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`,' +
  107. ' pa.`name` As `user_name`, pa.`role` As `user_role`, pa.`company` As `user_company` ' +
  108. // ' FROM ?? As t, ?? As pa ' +
  109. // ' WHERE t.`project_id` = ? AND t.`user_id` = pa.`id` AND (' +
  110. ' FROM ?? As t ' +
  111. ' Left Join ?? As pa ' +
  112. ' ON t.`user_id` = pa.`id` ' +
  113. ' WHERE t.`project_id` = ? AND (' +
  114. // 创建的标段
  115. ' t.`user_id` = ?' +
  116. // 参与审批 台账 的标段
  117. ' OR (t.`ledger_status` != ' + auditConst.ledger.status.uncheck + ' AND ' +
  118. ' t.id IN ( SELECT la.`tender_id` FROM ?? As la WHERE la.`audit_id` = ? GROUP BY la.`tender_id`))' +
  119. // 参与审批 计量期 的标段
  120. ' OR (t.`ledger_status` = ' + auditConst.ledger.status.checked + ' AND ' +
  121. ' t.id IN ( SELECT sa.`tid` FROM ?? As sa WHERE sa.`aid` = ? GROUP BY sa.`tid`))' +
  122. // 参与审批 变更令 的标段
  123. ' OR (t.`ledger_status` = ' + auditConst.ledger.status.checked + ' AND ' +
  124. ' t.id IN ( SELECT ca.`tid` FROM ?? AS ca WHERE ca.`uid` = ? GROUP BY ca.`tid`))' +
  125. // 参与审批 台账修订 的标段
  126. ' OR (t.`ledger_status` = ' + auditConst.ledger.status.checked + ' AND ' +
  127. ' t.id IN ( SELECT ra.`tender_id` FROM ?? AS ra WHERE ra.`audit_id` = ? GROUP BY ra.`tender_id`))' +
  128. // 参与审批 材料调差 的标段
  129. ' OR (t.`ledger_status` = ' + auditConst.ledger.status.checked + ' AND ' +
  130. ' t.id IN ( SELECT ma.`tid` FROM ?? AS ma WHERE ma.`aid` = ? GROUP BY ma.`tid`))' +
  131. // 参与审批 预付款 的标段
  132. ' OR (t.id IN ( SELECT ad.`tid` FROM ?? AS ad WHERE ad.`audit_id` = ? GROUP BY ad.`tid`))' +
  133. // 参与审批 变更立项书 的标段
  134. changeProjectSql +
  135. // 游客权限的标段
  136. ' OR (t.id IN ( SELECT tt.`tid` FROM ?? AS tt WHERE tt.`user_id` = ?))' +
  137. // 未参与,但可见的标段
  138. ') ORDER BY CONVERT(t.`name` USING GBK) ASC';
  139. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, session.sessionProject.id, session.sessionUser.accountId,
  140. this.ctx.service.ledgerAudit.tableName, session.sessionUser.accountId,
  141. this.ctx.service.stageAudit.tableName, session.sessionUser.accountId,
  142. this.ctx.service.changeAudit.tableName, session.sessionUser.accountId,
  143. this.ctx.service.reviseAudit.tableName, session.sessionUser.accountId,
  144. this.ctx.service.materialAudit.tableName, session.sessionUser.accountId,
  145. this.ctx.service.advanceAudit.tableName, session.sessionUser.accountId,
  146. this.ctx.service.tenderTourist.tableName, session.sessionUser.accountId,
  147. ];
  148. }
  149. const list = await this.db.query(sql, sqlParam);
  150. for (const l of list) {
  151. l.category = l.category && l.category !== '' ? JSON.parse(l.category) : null;
  152. }
  153. return list;
  154. }
  155. async getList4Select(selectType) {
  156. const accountInfo = await this.ctx.service.projectAccount.getDataById(this.ctx.session.sessionUser.accountId);
  157. const userPermission = accountInfo !== undefined && accountInfo.permission !== '' ? JSON.parse(accountInfo.permission) : null;
  158. const tenderList = await this.ctx.service.tender.getList('', userPermission);
  159. for (const t of tenderList) {
  160. if (t.ledger_status === auditConst.ledger.status.checked) {
  161. t.lastStage = await this.ctx.service.stage.getLastestStage(t.id, false);
  162. t.completeStage = await this.ctx.service.stage.getLastestCompleteStage(t.id);
  163. }
  164. }
  165. switch (selectType) {
  166. case 'ledger': return tenderList.filter(x => {
  167. return x.ledger_status === auditConst.ledger.status.checked;
  168. });
  169. case 'revise': tenderList.filter(x => {
  170. return x.ledger_status === auditConst.ledger.status.checked;
  171. });
  172. case 'stage': return tenderList.filter(x => {
  173. return x.ledger_status === auditConst.ledger.status.checked && !!x.lastStage;
  174. });
  175. case 'stage-checked': return tenderList.filter(x => {
  176. return !!x.completeStage;
  177. });
  178. default: return tenderList;
  179. }
  180. }
  181. async getTender(id) {
  182. this.initSqlBuilder();
  183. this.sqlBuilder.setAndWhere('id', {
  184. value: id,
  185. operate: '=',
  186. });
  187. this.sqlBuilder.columns = commonQueryColumns;
  188. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  189. const tender = await this.db.queryOne(sql, sqlParam);
  190. if (tender) {
  191. tender.category = tender.category && tender.category !== '' ? JSON.parse(tender.category) : null;
  192. }
  193. return tender;
  194. }
  195. /**
  196. * 新增标段
  197. *
  198. * @param {Object} data - 提交的数据
  199. * @return {Boolean} - 返回新增结果
  200. */
  201. async add(data) {
  202. let result = false;
  203. this.transaction = await this.db.beginTransaction();
  204. try {
  205. // 获取当前用户信息
  206. const sessionUser = this.ctx.session.sessionUser;
  207. // 获取当前项目信息
  208. const sessionProject = this.ctx.session.sessionProject;
  209. const insertData = {
  210. name: data.name,
  211. status: tenderConst.status.APPROVAL,
  212. project_id: sessionProject.id,
  213. user_id: sessionUser.accountId,
  214. create_time: new Date(),
  215. category: JSON.stringify(data.category),
  216. valuation: data.valuation,
  217. };
  218. const operate = await this.transaction.insert(this.tableName, insertData);
  219. result = operate.insertId > 0;
  220. if (!result) {
  221. throw '新增标段数据失败';
  222. }
  223. // 获取合同支付模板 并添加到标段
  224. result = await this.ctx.service.pay.addDefaultPayData(operate.insertId, this.transaction);
  225. if (!result) {
  226. throw '新增合同支付数据失败';
  227. }
  228. await this.ctx.service.tenderTag.addTenderTag(operate.insertId, sessionProject.id, this.transaction);
  229. await this.transaction.commit();
  230. 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`,' +
  231. ' pa.`name` As `user_name`, pa.`role` As `user_role`, pa.`company` As `user_company` ' +
  232. ' FROM ?? As t ' +
  233. ' Left Join ?? As pa ' +
  234. ' ON t.`user_id` = pa.`id` ' +
  235. ' WHERE t.`id` = ?';
  236. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, operate.insertId];
  237. const tender = await this.db.queryOne(sql, sqlParam);
  238. if (tender) {
  239. tender.category = tender.category && tender.category !== '' ? JSON.parse(tender.category) : null;
  240. }
  241. // 是否加入到决策大屏中
  242. if (sessionProject.page_show.addDataCollect === 0) {
  243. await this.ctx.service.datacollectTender.add(sessionProject.id, operate.insertId);
  244. }
  245. return tender;
  246. } catch (error) {
  247. await this.transaction.rollback();
  248. throw error;
  249. }
  250. }
  251. /**
  252. * 保存标段
  253. *
  254. * @param {Object} postData - 表单post过来的数据
  255. * @param {Number} id - 用于判断修改还是新增的id
  256. * @return {Boolean} - 返回执行结果
  257. */
  258. async save(postData, id = 0) {
  259. id = parseInt(id);
  260. const rowData = {
  261. id,
  262. name: postData.name,
  263. type: postData.type,
  264. category: JSON.stringify(postData.category),
  265. };
  266. const result = await this.db.update(this.tableName, rowData);
  267. return result.affectedRows > 0;
  268. }
  269. /**
  270. * 假删除
  271. *
  272. * @param {Number} id - 删除的id
  273. * @return {Boolean} - 删除结果
  274. */
  275. async deleteTenderById(id) {
  276. const updateData = {
  277. status: this.status.DISABLE,
  278. id,
  279. };
  280. const result = await this.db.update(this.tableName, updateData);
  281. return result.affectedRows > 0;
  282. }
  283. /**
  284. * 真删除
  285. * @param {Number} id - 删除的标段id
  286. * @return {Promise<boolean>} - 结果
  287. */
  288. async deleteTenderNoBackup(id) {
  289. const transaction = await this.db.beginTransaction();
  290. try {
  291. const tenderMsg = await this.getDataById(id);
  292. // 先删除附件文件
  293. const attList = await this.ctx.service.changeAtt.getAllDataByCondition({ where: { tid: id } });
  294. const newAttList = await this.ctx.service.materialFile.getAllMaterialFiles(id);
  295. attList.concat(newAttList);
  296. await this.ctx.helper.delFiles(attList);
  297. await transaction.delete(this.tableName, { id });
  298. await transaction.delete(this.ctx.service.tenderInfo.tableName, { tid: id });
  299. await transaction.delete(this.ctx.service.ledger.departTableName(id), { tender_id: id });
  300. await transaction.delete(this.ctx.service.ledgerAudit.tableName, { tender_id: id });
  301. await transaction.delete(this.ctx.service.pos.departTableName(id), { tid: id });
  302. await transaction.delete(this.ctx.service.pay.tableName, { tid: id });
  303. await transaction.delete(this.ctx.service.stage.tableName, { tid: id });
  304. await transaction.delete(this.ctx.service.stageAudit.tableName, { tid: id });
  305. await transaction.delete(this.ctx.service.stageBills.departTableName(id), { tid: id });
  306. await transaction.delete(this.ctx.service.stagePos.departTableName(id), { tid: id });
  307. await transaction.delete(this.ctx.service.stageBillsDgn.tableName, { tid: id });
  308. await transaction.delete(this.ctx.service.stageBillsFinal.departTableName(id), { tid: id });
  309. await transaction.delete(this.ctx.service.stagePosFinal.departTableName(id), { tid: id });
  310. await transaction.delete(this.ctx.service.stageDetail.tableName, { tid: id });
  311. await transaction.delete(this.ctx.service.stagePay.tableName, { tid: id });
  312. await transaction.delete(this.ctx.service.stageChange.tableName, { tid: id });
  313. await transaction.delete(this.ctx.service.stageJgcl.tableName, { tid: id });
  314. await transaction.delete(this.ctx.service.stageBonus.tableName, { tid: id });
  315. await transaction.delete(this.ctx.service.stageOther.tableName, { tid: id });
  316. await transaction.delete(this.ctx.service.stageRela.tableName, { tid: id });
  317. await transaction.delete(this.ctx.service.stageRelaBills.tableName, { tid: id });
  318. await transaction.delete(this.ctx.service.stageRelaBillsFinal.tableName, { tid: id });
  319. await transaction.delete(this.ctx.service.change.tableName, { tid: id });
  320. await transaction.delete(this.ctx.service.changeAudit.tableName, { tid: id });
  321. await transaction.delete(this.ctx.service.changeAuditList.tableName, { tid: id });
  322. await transaction.delete(this.ctx.service.changeCompany.tableName, { tid: id });
  323. await transaction.delete(this.ctx.service.ledgerRevise.tableName, { tid: id });
  324. await transaction.delete(this.ctx.service.reviseAudit.tableName, { tender_id: id });
  325. await transaction.delete(this.ctx.service.reviseBills.departTableName(id), { tender_id: id });
  326. await transaction.delete(this.ctx.service.revisePos.departTableName(id), { tid: id });
  327. await transaction.delete(this.ctx.service.material.tableName, { tid: id });
  328. await transaction.delete(this.ctx.service.materialAudit.tableName, { tid: id });
  329. await transaction.delete(this.ctx.service.materialBills.tableName, { tid: id });
  330. await transaction.delete(this.ctx.service.materialBillsHistory.tableName, { tid: id });
  331. await transaction.delete(this.ctx.service.materialList.tableName, { tid: id });
  332. await transaction.delete(this.ctx.service.materialListNotjoin.tableName, { tid: id });
  333. await transaction.delete(this.ctx.service.materialExponent.tableName, { tid: id });
  334. await transaction.delete(this.ctx.service.materialExponentHistory.tableName, { tid: id });
  335. await transaction.delete(this.ctx.service.materialFile.tableName, { tid: id });
  336. await transaction.delete(this.ctx.service.signatureUsed.tableName, { tender_id: id });
  337. await transaction.delete(this.ctx.service.signatureRole.tableName, { tender_id: id });
  338. await transaction.delete(this.ctx.service.changeAtt.tableName, { tid: id });
  339. await transaction.delete(this.ctx.service.materialFile.tableName, { tid: id });
  340. await transaction.delete(this.ctx.service.advanceFile.tableName, { tid: id });
  341. await transaction.delete(this.ctx.service.datacollectTender.tableName, { pid: this.ctx.session.sessionProject.id, tid: id });
  342. // 记录删除日志
  343. await this.ctx.service.projectLog.addProjectLog(transaction, projectLogConst.type.tender, projectLogConst.status.delete, tenderMsg.name, id);
  344. await transaction.commit();
  345. return true;
  346. } catch (err) {
  347. this.ctx.helper.log(err);
  348. await transaction.rollback();
  349. return false;
  350. }
  351. }
  352. async getCheckTender(tid) {
  353. const tender = await this.ctx.service.tender.getTender(tid);
  354. tender.info = await this.ctx.service.tenderInfo.getTenderInfo(tid);
  355. return tender;
  356. }
  357. async checkTender(tid) {
  358. if (this.ctx.tender) return;
  359. this.ctx.tender = await this.getCheckTender(tid);
  360. }
  361. async setTenderType(tender, type) {
  362. const templateId = await this.ctx.service.valuation.getValuationTemplate(tender.valuation, type);
  363. if (templateId === -1) throw '该模式下,台账模板不存在';
  364. // 获取标段项目节点模板
  365. const tenderNodeTemplateData = await this.ctx.service.tenderNodeTemplate.getData(templateId);
  366. const conn = await this.db.beginTransaction();
  367. try {
  368. await conn.update(this.tableName, { id: tender.id, measure_type: type });
  369. // 复制模板数据到标段数据表
  370. const result = await this.ctx.service.ledger.innerAdd(tenderNodeTemplateData, tender.id, conn);
  371. if (!result) {
  372. throw '初始化台账失败';
  373. }
  374. await conn.commit();
  375. } catch (err) {
  376. await conn.rollback();
  377. throw err;
  378. }
  379. }
  380. async saveApiRela(tid, updateData) {
  381. await this.db.update(this.tableName, updateData, {where: { id: tid } });
  382. }
  383. async saveTenderData(tid, updateData) {
  384. return await this.db.update(this.tableName, updateData, { where: { id: tid } });
  385. }
  386. }
  387. return Tender;
  388. };