tender.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. * @return {Array} - 返回标段数据
  69. */
  70. async getList(listStatus = '', permission = null) {
  71. // 获取当前项目信息
  72. const session = this.ctx.session;
  73. let sql = '';
  74. let sqlParam = [];
  75. if (listStatus === 'manage') {
  76. // 管理页面只取属于自己创建的标段
  77. 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`,' +
  78. ' pa.`name` As `user_name`, pa.`role` As `user_role`, pa.`company` As `user_company` ' +
  79. ' FROM ?? As t ' +
  80. ' Left Join ?? As pa ' +
  81. ' ON t.`user_id` = pa.`id` ' +
  82. ' WHERE t.`project_id` = ? AND t.`user_id` = ?';
  83. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, session.sessionProject.id, session.sessionUser.accountId];
  84. } else if (permission !== null && permission.tender !== undefined && permission.tender.indexOf('2') !== -1) {
  85. // 具有查看所有标段权限的用户查阅标段
  86. 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`,' +
  87. ' pa.`name` As `user_name`, pa.`role` As `user_role`, pa.`company` As `user_company` ' +
  88. ' FROM ?? As t ' +
  89. ' Left Join ?? As pa ' +
  90. ' ON t.`user_id` = pa.`id` ' +
  91. ' WHERE t.`project_id` = ?';
  92. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, session.sessionProject.id];
  93. } else {
  94. // 根据用户权限查阅标段
  95. // tender 163条数据,project_account 68条数据测试
  96. // 查询两张表耗时0.003s,查询tender左连接project_account耗时0.002s
  97. 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`,' +
  98. ' pa.`name` As `user_name`, pa.`role` As `user_role`, pa.`company` As `user_company` ' +
  99. // ' FROM ?? As t, ?? As pa ' +
  100. // ' WHERE t.`project_id` = ? AND t.`user_id` = pa.`id` AND (' +
  101. ' FROM ?? As t ' +
  102. ' Left Join ?? As pa ' +
  103. ' ON t.`user_id` = pa.`id` ' +
  104. ' WHERE t.`project_id` = ? AND (' +
  105. // 创建的标段
  106. ' t.`user_id` = ?' +
  107. // 参与审批 台账 的标段
  108. ' OR (t.`ledger_status` != ' + auditConst.ledger.status.uncheck + ' AND ' +
  109. ' t.id IN ( SELECT la.`tender_id` FROM ?? As la WHERE la.`audit_id` = ? GROUP BY la.`tender_id`))' +
  110. // 参与审批 计量期 的标段
  111. ' OR (t.`ledger_status` = ' + auditConst.ledger.status.checked + ' AND ' +
  112. ' t.id IN ( SELECT sa.`tid` FROM ?? As sa WHERE sa.`aid` = ? GROUP BY sa.`tid`))' +
  113. // 参与审批 变更令 的标段
  114. ' OR (t.`ledger_status` = ' + auditConst.ledger.status.checked + ' AND ' +
  115. ' t.id IN ( SELECT ca.`tid` FROM ?? AS ca WHERE ca.`uid` = ? GROUP BY ca.`tid`))' +
  116. // 未参与,但可见的标段
  117. ')';
  118. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, session.sessionProject.id, session.sessionUser.accountId,
  119. this.ctx.service.ledgerAudit.tableName, session.sessionUser.accountId,
  120. this.ctx.service.stageAudit.tableName, session.sessionUser.accountId,
  121. this.ctx.service.changeAudit.tableName, session.sessionUser.accountId];
  122. }
  123. const list = await this.db.query(sql, sqlParam);
  124. for (const l of list) {
  125. l.category = l.category && l.category !== '' ? JSON.parse(l.category) : null;
  126. }
  127. return list;
  128. }
  129. async getTender (id) {
  130. this.initSqlBuilder();
  131. this.sqlBuilder.setAndWhere('id', {
  132. value: id,
  133. operate: '=',
  134. });
  135. this.sqlBuilder.columns = commonQueryColumns;
  136. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  137. const tender = await this.db.queryOne(sql, sqlParam);
  138. if (tender) {
  139. tender.category = tender.category && tender.category !== '' ? JSON.parse(tender.category) : null;
  140. }
  141. return tender;
  142. }
  143. /**
  144. * 新增标段
  145. *
  146. * @param {Object} data - 提交的数据
  147. * @return {Boolean} - 返回新增结果
  148. */
  149. async add(data) {
  150. let result = false;
  151. const templateId = await this.ctx.service.valuation.getValuationTemplate(data.valuation);
  152. this.transaction = await this.db.beginTransaction();
  153. try {
  154. // 获取当前用户信息
  155. const sessionUser = this.ctx.session.sessionUser;
  156. // 获取当前项目信息
  157. const sessionProject = this.ctx.session.sessionProject;
  158. const insertData = {
  159. name: data.name,
  160. status: tenderConst.status.APPROVAL,
  161. project_id: sessionProject.id,
  162. user_id: sessionUser.accountId,
  163. create_time: new Date(),
  164. category: JSON.stringify(data.category),
  165. valuation: data.valuation,
  166. };
  167. const operate = await this.transaction.insert(this.tableName, insertData);
  168. result = operate.insertId > 0;
  169. if (!result) {
  170. throw '新增标段数据失败';
  171. }
  172. // 获取标段项目节点模板
  173. const tenderNodeTemplateData = await this.ctx.service.tenderNodeTemplate.getData(templateId);
  174. // 复制模板数据到标段数据表
  175. result = await this.ctx.service.ledger.innerAdd(tenderNodeTemplateData, operate.insertId, this.transaction);
  176. if (!result) {
  177. throw '新增标段项目节点失败';
  178. }
  179. // 获取合同支付模板 并添加到标段
  180. result = await this.ctx.service.pay.addDefaultPayData(operate.insertId, this.transaction);
  181. if (!result) {
  182. throw '新增合同支付数据失败';
  183. }
  184. await this.transaction.commit();
  185. 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`,' +
  186. ' pa.`name` As `user_name`, pa.`role` As `user_role`, pa.`company` As `user_company` ' +
  187. ' FROM ?? As t ' +
  188. ' Left Join ?? As pa ' +
  189. ' ON t.`user_id` = pa.`id` ' +
  190. ' WHERE t.`id` = ?';
  191. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, operate.insertId];
  192. const tender = await this.db.queryOne(sql, sqlParam);
  193. if (tender) {
  194. tender.category = tender.category && tender.category !== '' ? JSON.parse(tender.category) : null;
  195. }
  196. return tender;
  197. } catch (error) {
  198. await this.transaction.rollback();
  199. throw error;
  200. }
  201. }
  202. /**
  203. * 保存标段
  204. *
  205. * @param {Object} postData - 表单post过来的数据
  206. * @param {Number} id - 用于判断修改还是新增的id
  207. * @return {Boolean} - 返回执行结果
  208. */
  209. async save(postData, id = 0) {
  210. id = parseInt(id);
  211. const rowData = {
  212. id: id,
  213. name: postData.name,
  214. type: postData.type,
  215. category: JSON.stringify(postData.category),
  216. };
  217. const result = await this.db.update(this.tableName, rowData);
  218. return result.affectedRows > 0;
  219. }
  220. /**
  221. * 假删除
  222. *
  223. * @param {Number} id - 删除的id
  224. * @return {Boolean} - 删除结果
  225. */
  226. async deleteTenderById(id) {
  227. const updateData = {
  228. status: this.status.DISABLE,
  229. id,
  230. };
  231. const result = await this.db.update(this.tableName, updateData);
  232. return result.affectedRows > 0;
  233. }
  234. /**
  235. * 真删除
  236. * @param {Number} id - 删除的标段id
  237. * @returns {Promise<boolean>} - 结果
  238. */
  239. async deleteTenderNoBackup(id) {
  240. const transaction = await this.db.beginTransaction();
  241. try {
  242. await transaction.delete(this.tableName, {id: id});
  243. await transaction.delete(this.ctx.service.tenderInfo.tableName, {tid: id});
  244. await transaction.delete(this.ctx.service.ledger.tableName, {tender_id: id});
  245. await transaction.delete(this.ctx.service.ledgerAudit.tableName, {tender_id: id});
  246. await transaction.delete(this.ctx.service.pos.tableName, {tid: id});
  247. await transaction.delete(this.ctx.service.pay.tableName, {tid: id});
  248. await transaction.delete(this.ctx.service.stage.tableName, {tid: id});
  249. await transaction.delete(this.ctx.service.stageAudit.tableName, {tid: id});
  250. await transaction.delete(this.ctx.service.stageBills.tableName, {tid: id});
  251. await transaction.delete(this.ctx.service.stagePos.tableName, {tid: id});
  252. await transaction.delete(this.ctx.service.stageDetail.tableName, {tid: id});
  253. await transaction.delete(this.ctx.service.stagePay.tableName, {tid: id});
  254. await transaction.delete(this.ctx.service.change.tableName, {tid: id});
  255. await transaction.delete(this.ctx.service.changeAudit.tableName, {tid: id});
  256. await transaction.delete(this.ctx.service.changeAuditList.tableName, {tid: id});
  257. await transaction.delete(this.ctx.service.changeCompany.tableName, {tid: id});
  258. // 先删除附件文件
  259. const attList = await this.ctx.service.changeAtt.getAllDataByCondition({ where: { tid: id } });
  260. if (attList.length !== 0) {
  261. for (const att of attList) {
  262. if (fs.existsSync(path.join(this.app.baseDir, att.filepath))) {
  263. await fs.unlinkSync(path.join(this.app.baseDir, att.filepath));
  264. }
  265. }
  266. }
  267. await transaction.delete(this.ctx.service.changeAtt.tableName, {tid: id});
  268. await transaction.commit();
  269. return true;
  270. } catch (err) {
  271. this.ctx.helper.log(err);
  272. await transaction.rollback();
  273. return false;
  274. }
  275. }
  276. /**
  277. * 切换标段
  278. *
  279. * @param {Number} tenderId - 标段id
  280. * @return {Boolean} - 返回切换结果
  281. */
  282. async switchTender(tenderId) {
  283. // 获取该用户拥有的项目数据
  284. const sessionUser = this.ctx.session.sessionUser;
  285. const tenderInfo = await this.ctx.service.projectAccount.getProjectInfoByAccount(sessionUser.account);
  286. let result = false;
  287. // 判断切换的标段是否属于对应用户
  288. if (tenderInfo.length < 0) {
  289. return result;
  290. }
  291. let targetTender = {};
  292. for (const tmp of tenderInfo) {
  293. if (tmp.id === tenderId) {
  294. result = true;
  295. targetTender = tmp;
  296. }
  297. }
  298. // 成功后更改session
  299. if (result) {
  300. this.ctx.session.sessionTender = {
  301. id: targetTender.id,
  302. name: targetTender.name,
  303. userAccount: targetTender.user_account,
  304. };
  305. }
  306. return result;
  307. }
  308. async checkTender(tid) {
  309. if (this.ctx.tender) return;
  310. const tender = await this.ctx.service.tender.getTender(tid);
  311. tender.info = await this.ctx.service.tenderInfo.getTenderInfo(tid);
  312. this.ctx.tender = tender;
  313. }
  314. }
  315. return Tender;
  316. };