tender.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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.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.transaction.commit();
  192. 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`,' +
  193. ' pa.`name` As `user_name`, pa.`role` As `user_role`, pa.`company` As `user_company` ' +
  194. ' FROM ?? As t ' +
  195. ' Left Join ?? As pa ' +
  196. ' ON t.`user_id` = pa.`id` ' +
  197. ' WHERE t.`id` = ?';
  198. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, operate.insertId];
  199. const tender = await this.db.queryOne(sql, sqlParam);
  200. if (tender) {
  201. tender.category = tender.category && tender.category !== '' ? JSON.parse(tender.category) : null;
  202. }
  203. return tender;
  204. } catch (error) {
  205. await this.transaction.rollback();
  206. throw error;
  207. }
  208. }
  209. /**
  210. * 保存标段
  211. *
  212. * @param {Object} postData - 表单post过来的数据
  213. * @param {Number} id - 用于判断修改还是新增的id
  214. * @return {Boolean} - 返回执行结果
  215. */
  216. async save(postData, id = 0) {
  217. id = parseInt(id);
  218. const rowData = {
  219. id,
  220. name: postData.name,
  221. type: postData.type,
  222. category: JSON.stringify(postData.category),
  223. };
  224. const result = await this.db.update(this.tableName, rowData);
  225. return result.affectedRows > 0;
  226. }
  227. /**
  228. * 假删除
  229. *
  230. * @param {Number} id - 删除的id
  231. * @return {Boolean} - 删除结果
  232. */
  233. async deleteTenderById(id) {
  234. const updateData = {
  235. status: this.status.DISABLE,
  236. id,
  237. };
  238. const result = await this.db.update(this.tableName, updateData);
  239. return result.affectedRows > 0;
  240. }
  241. /**
  242. * 真删除
  243. * @param {Number} id - 删除的标段id
  244. * @return {Promise<boolean>} - 结果
  245. */
  246. async deleteTenderNoBackup(id) {
  247. const transaction = await this.db.beginTransaction();
  248. try {
  249. // 先删除附件文件
  250. const attList = await this.ctx.service.changeAtt.getAllDataByCondition({ where: { tid: id } });
  251. const newAttList = await this.ctx.service.materialFile.getAllMaterialFiles(id);
  252. attList.concat(newAttList);
  253. await this.ctx.helper.delFiles(attList);
  254. await transaction.delete(this.tableName, { id });
  255. await transaction.delete(this.ctx.service.tenderInfo.tableName, { tid: id });
  256. await transaction.delete(this.ctx.service.ledger.tableName, { tender_id: id });
  257. await transaction.delete(this.ctx.service.ledgerAudit.tableName, { tender_id: id });
  258. await transaction.delete(this.ctx.service.pos.tableName, { tid: id });
  259. await transaction.delete(this.ctx.service.pay.tableName, { tid: id });
  260. await transaction.delete(this.ctx.service.stage.tableName, { tid: id });
  261. await transaction.delete(this.ctx.service.stageAudit.tableName, { tid: id });
  262. await transaction.delete(this.ctx.service.stageBills.tableName, { tid: id });
  263. await transaction.delete(this.ctx.service.stagePos.tableName, { tid: id });
  264. await transaction.delete(this.ctx.service.stageBillsDgn.tableName, {tid: id});
  265. await transaction.delete(this.ctx.service.stageBillsFinal.tableName, {tid: id});
  266. await transaction.delete(this.ctx.service.stagePosFinal.tableName, {tid: id});
  267. await transaction.delete(this.ctx.service.stageDetail.tableName, { tid: id });
  268. await transaction.delete(this.ctx.service.stagePay.tableName, { tid: id });
  269. await transaction.delete(this.ctx.service.stageChange.tableName, { tid: id });
  270. await transaction.delete(this.ctx.service.stageJgcl.tableName, {tid: id});
  271. await transaction.delete(this.ctx.service.stageBonus.tableName, {tid: id});
  272. await transaction.delete(this.ctx.service.stageOther.tableName, {tid: id});
  273. await transaction.delete(this.ctx.service.change.tableName, { tid: id });
  274. await transaction.delete(this.ctx.service.changeAudit.tableName, { tid: id });
  275. await transaction.delete(this.ctx.service.changeAuditList.tableName, { tid: id });
  276. await transaction.delete(this.ctx.service.changeCompany.tableName, { tid: id });
  277. await transaction.delete(this.ctx.service.ledgerRevise.tableName, { tid: id });
  278. await transaction.delete(this.ctx.service.reviseAudit.tableName, { tender_id: id });
  279. await transaction.delete(this.ctx.service.reviseBills.tableName, { tender_id: id });
  280. await transaction.delete(this.ctx.service.revisePos.tableName, { tid: id });
  281. await transaction.delete(this.ctx.service.material.tableName, { tid: id });
  282. await transaction.delete(this.ctx.service.materialAudit.tableName, { tid: id });
  283. await transaction.delete(this.ctx.service.materialBills.tableName, { tid: id });
  284. await transaction.delete(this.ctx.service.materialBillsHistory.tableName, { tid: id });
  285. await transaction.delete(this.ctx.service.materialList.tableName, { tid: id });
  286. await transaction.delete(this.ctx.service.materialListNotjoin.tableName, { tid: id });
  287. await transaction.delete(this.ctx.service.materialFile.tableName, { tid: id });
  288. await transaction.delete(this.ctx.service.signatureUsed.tableName, { tender_id: id });
  289. await transaction.delete(this.ctx.service.signatureRole.tableName, { tender_id: id });
  290. await transaction.delete(this.ctx.service.changeAtt.tableName, { tid: id });
  291. await transaction.delete(this.ctx.service.materialFile.tableName, { tid: id });
  292. await transaction.delete(this.ctx.service.advanceFile.tableName, { tid: id });
  293. await transaction.commit();
  294. return true;
  295. } catch (err) {
  296. this.ctx.helper.log(err);
  297. await transaction.rollback();
  298. return false;
  299. }
  300. }
  301. /**
  302. * 切换标段
  303. *
  304. * @param {Number} tenderId - 标段id
  305. * @return {Boolean} - 返回切换结果
  306. */
  307. async switchTender(tenderId) {
  308. // 获取该用户拥有的项目数据
  309. const sessionUser = this.ctx.session.sessionUser;
  310. const tenderInfo = await this.ctx.service.projectAccount.getProjectInfoByAccount(sessionUser.account);
  311. let result = false;
  312. // 判断切换的标段是否属于对应用户
  313. if (tenderInfo.length < 0) {
  314. return result;
  315. }
  316. let targetTender = {};
  317. for (const tmp of tenderInfo) {
  318. if (tmp.id === tenderId) {
  319. result = true;
  320. targetTender = tmp;
  321. }
  322. }
  323. // 成功后更改session
  324. if (result) {
  325. this.ctx.session.sessionTender = {
  326. id: targetTender.id,
  327. name: targetTender.name,
  328. userAccount: targetTender.user_account,
  329. };
  330. }
  331. return result;
  332. }
  333. async getCheckTender(tid) {
  334. const tender = await this.ctx.service.tender.getTender(tid);
  335. tender.info = await this.ctx.service.tenderInfo.getTenderInfo(tid);
  336. return tender;
  337. }
  338. async checkTender(tid) {
  339. if (this.ctx.tender) return;
  340. this.ctx.tender = await this.getCheckTender(tid);
  341. }
  342. async setTenderType(tender, type) {
  343. const templateId = await this.ctx.service.valuation.getValuationTemplate(tender.valuation, type);
  344. if (templateId === -1) throw '该模式下,台账模板不存在';
  345. // 获取标段项目节点模板
  346. const tenderNodeTemplateData = await this.ctx.service.tenderNodeTemplate.getData(templateId);
  347. const conn = await this.db.beginTransaction();
  348. try {
  349. await conn.update(this.tableName, { id: tender.id, measure_type: type });
  350. // 复制模板数据到标段数据表
  351. const result = await this.ctx.service.ledger.innerAdd(tenderNodeTemplateData, tender.id, conn);
  352. if (!result) {
  353. throw '初始化台账失败';
  354. }
  355. await conn.commit();
  356. } catch (err) {
  357. await conn.rollback();
  358. throw err;
  359. }
  360. }
  361. }
  362. return Tender;
  363. };