tender.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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', 'copy_id'];
  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.id IN ( SELECT ad.`tid` FROM ?? AS ad WHERE ad.`audit_id` = ? GROUP BY ad.`tid`))' +
  127. // 未参与,但可见的标段
  128. ') ORDER BY CONVERT(t.`name` USING GBK) ASC';
  129. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, session.sessionProject.id, session.sessionUser.accountId,
  130. this.ctx.service.ledgerAudit.tableName, session.sessionUser.accountId,
  131. this.ctx.service.stageAudit.tableName, session.sessionUser.accountId,
  132. this.ctx.service.changeAudit.tableName, session.sessionUser.accountId,
  133. this.ctx.service.reviseAudit.tableName, session.sessionUser.accountId,
  134. this.ctx.service.materialAudit.tableName, session.sessionUser.accountId,
  135. this.ctx.service.advanceAudit.tableName, session.sessionUser.accountId,
  136. ];
  137. }
  138. const list = await this.db.query(sql, sqlParam);
  139. for (const l of list) {
  140. l.category = l.category && l.category !== '' ? JSON.parse(l.category) : null;
  141. }
  142. return list;
  143. }
  144. async getTender(id) {
  145. this.initSqlBuilder();
  146. this.sqlBuilder.setAndWhere('id', {
  147. value: id,
  148. operate: '=',
  149. });
  150. this.sqlBuilder.columns = commonQueryColumns;
  151. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  152. const tender = await this.db.queryOne(sql, sqlParam);
  153. if (tender) {
  154. tender.category = tender.category && tender.category !== '' ? JSON.parse(tender.category) : null;
  155. }
  156. return tender;
  157. }
  158. /**
  159. * 新增标段
  160. *
  161. * @param {Object} data - 提交的数据
  162. * @return {Boolean} - 返回新增结果
  163. */
  164. async add(data) {
  165. let result = false;
  166. this.transaction = await this.db.beginTransaction();
  167. try {
  168. // 获取当前用户信息
  169. const sessionUser = this.ctx.session.sessionUser;
  170. // 获取当前项目信息
  171. const sessionProject = this.ctx.session.sessionProject;
  172. const insertData = {
  173. name: data.name,
  174. status: tenderConst.status.APPROVAL,
  175. project_id: sessionProject.id,
  176. user_id: sessionUser.accountId,
  177. create_time: new Date(),
  178. category: JSON.stringify(data.category),
  179. valuation: data.valuation,
  180. };
  181. const operate = await this.transaction.insert(this.tableName, insertData);
  182. result = operate.insertId > 0;
  183. if (!result) {
  184. throw '新增标段数据失败';
  185. }
  186. // 获取合同支付模板 并添加到标段
  187. result = await this.ctx.service.pay.addDefaultPayData(operate.insertId, this.transaction);
  188. if (!result) {
  189. throw '新增合同支付数据失败';
  190. }
  191. await this.ctx.service.tenderTag.addTenderTag(operate.insertId, sessionProject.id, this.transaction);
  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.stageBillsDgn.tableName, { tid: id });
  266. await transaction.delete(this.ctx.service.stageBillsFinal.tableName, { tid: id });
  267. await transaction.delete(this.ctx.service.stagePosFinal.tableName, { tid: id });
  268. await transaction.delete(this.ctx.service.stageDetail.tableName, { tid: id });
  269. await transaction.delete(this.ctx.service.stagePay.tableName, { tid: id });
  270. await transaction.delete(this.ctx.service.stageChange.tableName, { tid: id });
  271. await transaction.delete(this.ctx.service.stageJgcl.tableName, { tid: id });
  272. await transaction.delete(this.ctx.service.stageBonus.tableName, { tid: id });
  273. await transaction.delete(this.ctx.service.stageOther.tableName, { tid: id });
  274. await transaction.delete(this.ctx.service.change.tableName, { tid: id });
  275. await transaction.delete(this.ctx.service.changeAudit.tableName, { tid: id });
  276. await transaction.delete(this.ctx.service.changeAuditList.tableName, { tid: id });
  277. await transaction.delete(this.ctx.service.changeCompany.tableName, { tid: id });
  278. await transaction.delete(this.ctx.service.ledgerRevise.tableName, { tid: id });
  279. await transaction.delete(this.ctx.service.reviseAudit.tableName, { tender_id: id });
  280. await transaction.delete(this.ctx.service.reviseBills.tableName, { tender_id: id });
  281. await transaction.delete(this.ctx.service.revisePos.tableName, { tid: id });
  282. await transaction.delete(this.ctx.service.material.tableName, { tid: id });
  283. await transaction.delete(this.ctx.service.materialAudit.tableName, { tid: id });
  284. await transaction.delete(this.ctx.service.materialBills.tableName, { tid: id });
  285. await transaction.delete(this.ctx.service.materialBillsHistory.tableName, { tid: id });
  286. await transaction.delete(this.ctx.service.materialList.tableName, { tid: id });
  287. await transaction.delete(this.ctx.service.materialListNotjoin.tableName, { tid: id });
  288. await transaction.delete(this.ctx.service.materialExponent.tableName, { tid: id });
  289. await transaction.delete(this.ctx.service.materialExponentHistory.tableName, { tid: id });
  290. await transaction.delete(this.ctx.service.materialFile.tableName, { tid: id });
  291. await transaction.delete(this.ctx.service.signatureUsed.tableName, { tender_id: id });
  292. await transaction.delete(this.ctx.service.signatureRole.tableName, { tender_id: id });
  293. await transaction.delete(this.ctx.service.changeAtt.tableName, { tid: id });
  294. await transaction.delete(this.ctx.service.materialFile.tableName, { tid: id });
  295. await transaction.delete(this.ctx.service.advanceFile.tableName, { tid: id });
  296. await transaction.commit();
  297. return true;
  298. } catch (err) {
  299. this.ctx.helper.log(err);
  300. await transaction.rollback();
  301. return false;
  302. }
  303. }
  304. /**
  305. * 切换标段
  306. *
  307. * @param {Number} tenderId - 标段id
  308. * @return {Boolean} - 返回切换结果
  309. */
  310. async switchTender(tenderId) {
  311. // 获取该用户拥有的项目数据
  312. const sessionUser = this.ctx.session.sessionUser;
  313. const tenderInfo = await this.ctx.service.projectAccount.getProjectInfoByAccount(sessionUser.account);
  314. let result = false;
  315. // 判断切换的标段是否属于对应用户
  316. if (tenderInfo.length < 0) {
  317. return result;
  318. }
  319. let targetTender = {};
  320. for (const tmp of tenderInfo) {
  321. if (tmp.id === tenderId) {
  322. result = true;
  323. targetTender = tmp;
  324. }
  325. }
  326. // 成功后更改session
  327. if (result) {
  328. this.ctx.session.sessionTender = {
  329. id: targetTender.id,
  330. name: targetTender.name,
  331. userAccount: targetTender.user_account,
  332. };
  333. }
  334. return result;
  335. }
  336. async getCheckTender(tid) {
  337. const tender = await this.ctx.service.tender.getTender(tid);
  338. tender.info = await this.ctx.service.tenderInfo.getTenderInfo(tid);
  339. return tender;
  340. }
  341. async checkTender(tid) {
  342. if (this.ctx.tender) return;
  343. this.ctx.tender = await this.getCheckTender(tid);
  344. }
  345. async setTenderType(tender, type) {
  346. const templateId = await this.ctx.service.valuation.getValuationTemplate(tender.valuation, type);
  347. if (templateId === -1) throw '该模式下,台账模板不存在';
  348. // 获取标段项目节点模板
  349. const tenderNodeTemplateData = await this.ctx.service.tenderNodeTemplate.getData(templateId);
  350. const conn = await this.db.beginTransaction();
  351. try {
  352. await conn.update(this.tableName, { id: tender.id, measure_type: type });
  353. // 复制模板数据到标段数据表
  354. const result = await this.ctx.service.ledger.innerAdd(tenderNodeTemplateData, tender.id, conn);
  355. if (!result) {
  356. throw '初始化台账失败';
  357. }
  358. await conn.commit();
  359. } catch (err) {
  360. await conn.rollback();
  361. throw err;
  362. }
  363. }
  364. }
  365. return Tender;
  366. };