tender.js 18 KB

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