tender.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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'];
  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`, ' +
  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`, ' +
  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`, ' +
  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. // console.log(this.db.format(sql, sqlParam));
  124. const list = await this.db.query(sql, sqlParam);
  125. for (const l of list) {
  126. l.category = l.category && l.category !== '' ? JSON.parse(l.category) : null;
  127. }
  128. return list;
  129. }
  130. async getTender (id) {
  131. this.initSqlBuilder();
  132. this.sqlBuilder.setAndWhere('id', {
  133. value: id,
  134. operate: '=',
  135. });
  136. this.sqlBuilder.columns = commonQueryColumns;
  137. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  138. const tender = await this.db.queryOne(sql, sqlParam);
  139. if (tender) {
  140. tender.category = tender.category && tender.category !== '' ? JSON.parse(tender.category) : null;
  141. }
  142. return tender;
  143. }
  144. /**
  145. * 新增标段
  146. *
  147. * @param {Object} data - 提交的数据
  148. * @return {Boolean} - 返回新增结果
  149. */
  150. async add(data) {
  151. let result = false;
  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. };
  166. const operate = await this.transaction.insert(this.tableName, insertData);
  167. result = operate.insertId > 0;
  168. if (!result) {
  169. throw '新增标段数据失败';
  170. }
  171. // 获取标段项目节点模板
  172. const tenderNodeTemplateData = await this.ctx.service.tenderNodeTemplate.getData();
  173. // 复制模板数据到标段数据表
  174. result = await this.ctx.service.ledger.innerAdd(tenderNodeTemplateData, operate.insertId, this.transaction);
  175. if (!result) {
  176. throw '新增标段项目节点失败';
  177. }
  178. // 获取合同支付模板 并添加到标段
  179. result = await this.ctx.service.pay.addDefaultPayData(operate.insertId, this.transaction);
  180. if (!result) {
  181. throw '新增合同支付数据失败';
  182. }
  183. await this.transaction.commit();
  184. return await this.getTender(operate.insertId);
  185. } catch (error) {
  186. await this.transaction.rollback();
  187. throw error;
  188. }
  189. }
  190. /**
  191. * 保存标段
  192. *
  193. * @param {Object} postData - 表单post过来的数据
  194. * @param {Number} id - 用于判断修改还是新增的id
  195. * @return {Boolean} - 返回执行结果
  196. */
  197. async save(postData, id = 0) {
  198. id = parseInt(id);
  199. const rowData = {
  200. id: id,
  201. name: postData.name,
  202. type: postData.type,
  203. category: JSON.stringify(postData.category),
  204. };
  205. const result = await this.db.update(this.tableName, rowData);
  206. return result.affectedRows > 0;
  207. }
  208. /**
  209. * 假删除
  210. *
  211. * @param {Number} id - 删除的id
  212. * @return {Boolean} - 删除结果
  213. */
  214. async deleteTenderById(id) {
  215. const updateData = {
  216. status: this.status.DISABLE,
  217. id,
  218. };
  219. const result = await this.db.update(this.tableName, updateData);
  220. return result.affectedRows > 0;
  221. }
  222. /**
  223. * 真删除
  224. * @param {Number} id - 删除的标段id
  225. * @returns {Promise<boolean>} - 结果
  226. */
  227. async deleteTenderNoBackup(id) {
  228. const transaction = await this.db.beginTransaction();
  229. try {
  230. await transaction.delete(this.tableName, {id: id});
  231. await transaction.delete(this.ctx.service.tenderInfo.tableName, {tid: id});
  232. await transaction.delete(this.ctx.service.ledger.tableName, {tender_id: id});
  233. await transaction.delete(this.ctx.service.ledgerAudit.tableName, {tender_id: id});
  234. await transaction.delete(this.ctx.service.ledgerAudit.tableName + '_copy', {tender_id: id});
  235. await transaction.delete(this.ctx.service.pos.tableName, {tid: id});
  236. await transaction.delete(this.ctx.service.pay.tableName, {tid: id});
  237. await transaction.delete(this.ctx.service.stage.tableName, {tid: id});
  238. await transaction.delete(this.ctx.service.stageAudit.tableName, {tid: id});
  239. await transaction.delete(this.ctx.service.stageBills.tableName, {tid: id});
  240. await transaction.delete(this.ctx.service.stagePos.tableName, {tid: id});
  241. await transaction.delete(this.ctx.service.stageDetail.tableName, {tid: id});
  242. await transaction.delete(this.ctx.service.stagePay.tableName, {tid: id});
  243. await transaction.delete(this.ctx.service.change.tableName, {tid: id});
  244. await transaction.delete(this.ctx.service.changeAudit.tableName, {tid: id});
  245. await transaction.delete(this.ctx.service.changeAuditList.tableName, {tid: id});
  246. await transaction.delete(this.ctx.service.changeCompany.tableName, {tid: id});
  247. // 先删除附件文件
  248. const attList = await this.ctx.service.changeAtt.getAllDataByCondition({ where: { tid: id } });
  249. if (attList.length !== 0) {
  250. for (const att of attList) {
  251. await fs.unlinkSync(path.join(this.app.baseDir, att.filepath));
  252. }
  253. }
  254. await transaction.delete(this.ctx.service.changeAtt.tableName, {tid: id});
  255. await transaction.commit();
  256. return true;
  257. } catch (err) {
  258. console.log(err);
  259. await transaction.rollback();
  260. return false;
  261. }
  262. }
  263. /**
  264. * 切换标段
  265. *
  266. * @param {Number} tenderId - 标段id
  267. * @return {Boolean} - 返回切换结果
  268. */
  269. async switchTender(tenderId) {
  270. // 获取该用户拥有的项目数据
  271. const sessionUser = this.ctx.session.sessionUser;
  272. const tenderInfo = await this.ctx.service.projectAccount.getProjectInfoByAccount(sessionUser.account);
  273. let result = false;
  274. // 判断切换的标段是否属于对应用户
  275. if (tenderInfo.length < 0) {
  276. return result;
  277. }
  278. let targetTender = {};
  279. for (const tmp of tenderInfo) {
  280. if (tmp.id === tenderId) {
  281. result = true;
  282. targetTender = tmp;
  283. }
  284. }
  285. // 成功后更改session
  286. if (result) {
  287. this.ctx.session.sessionTender = {
  288. id: targetTender.id,
  289. name: targetTender.name,
  290. userAccount: targetTender.user_account,
  291. };
  292. }
  293. return result;
  294. }
  295. }
  296. return Tender;
  297. };