tender.js 19 KB

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