tender.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. 'use strict';
  2. /**
  3. * 标段数据模型
  4. *
  5. * @author CaiAoLin
  6. * @date 2017/11/30
  7. * @version
  8. */
  9. const tenderConst = require('../const/tender');
  10. module.exports = app => {
  11. class Tender extends app.BaseService {
  12. /**
  13. * 构造函数
  14. *
  15. * @param {Object} ctx - egg全局变量
  16. * @return {void}
  17. */
  18. constructor(ctx) {
  19. super(ctx);
  20. this.tableName = 'tender';
  21. // 状态相关
  22. this.status = {
  23. TRY: 1,
  24. NORMAL: 2,
  25. DISABLE: 3,
  26. };
  27. this.displayStatus = [];
  28. this.displayStatus[this.status.TRY] = '试用';
  29. this.displayStatus[this.status.NORMAL] = '正常';
  30. this.displayStatus[this.status.DISABLE] = '禁用';
  31. this.statusClass = [];
  32. this.statusClass[this.status.TRY] = 'warning';
  33. this.statusClass[this.status.NORMAL] = 'success';
  34. this.statusClass[this.status.DISABLE] = 'danger';
  35. }
  36. /**
  37. * 数据规则
  38. *
  39. * @param {String} scene - 场景
  40. * @return {Object} - 返回数据规则
  41. */
  42. rule(scene) {
  43. let rule = {};
  44. switch (scene) {
  45. case 'add':
  46. rule = {
  47. name: { type: 'string', required: true, min: 2 },
  48. type: { type: 'string', required: true, min: 1 },
  49. };
  50. break;
  51. case 'save':
  52. rule = {
  53. name: { type: 'string', required: true, min: 2 },
  54. type: { type: 'string', required: true, min: 1 },
  55. };
  56. default:
  57. break;
  58. }
  59. return rule;
  60. }
  61. /**
  62. * 获取标段列表
  63. *
  64. * @param {Object} type - 标段类型
  65. * @return {Array} - 返回标段数据
  66. */
  67. async getList(type = 0) {
  68. // 获取当前项目信息
  69. const sessionProject = this.ctx.session.sessionProject;
  70. this.initSqlBuilder();
  71. this.sqlBuilder.setAndWhere('project_id', {
  72. value: sessionProject.id,
  73. operate: '=',
  74. });
  75. if(type !== 0) {
  76. this.sqlBuilder.setAndWhere('type', {
  77. value: type,
  78. operate: '=',
  79. });
  80. }
  81. this.sqlBuilder.limit = 10;
  82. this.sqlBuilder.columns = ['id', 'name', 'status', 'type'];
  83. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  84. return await this.db.query(sql, sqlParam);
  85. }
  86. /**
  87. * 新增标段
  88. *
  89. * @param {Object} postData - 表单post过来的数据
  90. * @return {Boolean} - 返回新增结果
  91. */
  92. async add(postData) {
  93. let result = false;
  94. this.transaction = await this.db.beginTransaction();
  95. try {
  96. // 获取当前用户信息
  97. const sessionUser = this.ctx.session.sessionUser;
  98. // 获取当前项目信息
  99. const sessionProject = this.ctx.session.sessionProject;
  100. const insertData = {
  101. name: postData.name,
  102. status: tenderConst.status.APPROVAL,
  103. project_id: sessionProject.id,
  104. user_id: sessionUser.accountId,
  105. create_time: postData.create_time,
  106. type: postData.type,
  107. };
  108. const operate = await this.transaction.insert(this.tableName, insertData);
  109. result = operate.insertId > 0;
  110. if (!result) {
  111. throw '新增标段数据失败';
  112. }
  113. // 获取标段项目节点模板
  114. const tenderNodeTemplateData = await this.ctx.service.tenderNodeTemplate.getData();
  115. // 复制模板数据到标段数据表
  116. result = await this.ctx.service.ledger.innerAdd(tenderNodeTemplateData, operate.insertId, this.transaction);
  117. if (!result) {
  118. throw '新增标段项目节点失败';
  119. }
  120. this.transaction.commit();
  121. } catch (error) {
  122. console.log(error);
  123. result = false;
  124. this.transaction.rollback();
  125. }
  126. return result;
  127. }
  128. /**
  129. * 保存标段
  130. *
  131. * @param {Object} postData - 表单post过来的数据
  132. * @param {Number} id - 用于判断修改还是新增的id
  133. * @return {Boolean} - 返回执行结果
  134. */
  135. async save(postData,id = 0) {
  136. id = parseInt(id);
  137. const rowData = {
  138. id: id,
  139. name: postData.name,
  140. type: postData.type,
  141. };
  142. const result = await this.db.update(this.tableName, rowData);
  143. return result.affectedRows > 0;
  144. }
  145. /**
  146. * 假删除
  147. *
  148. * @param {Number} id - 删除的id
  149. * @return {Boolean} - 删除结果
  150. */
  151. async deleteTenderById(id) {
  152. const updateData = {
  153. status: this.status.DISABLE,
  154. id,
  155. };
  156. const result = this.db.update(this.tableName, updateData);
  157. return result.affectedRows > 0;
  158. }
  159. /**
  160. * 切换标段
  161. *
  162. * @param {Number} tenderId - 标段id
  163. * @return {Boolean} - 返回切换结果
  164. */
  165. async switchTender(tenderId) {
  166. // 获取该用户拥有的项目数据
  167. const sessionUser = this.ctx.session.sessionUser;
  168. const tenderInfo = await this.ctx.service.projectAccount.getProjectInfoByAccount(sessionUser.account);
  169. let result = false;
  170. // 判断切换的标段是否属于对应用户
  171. if (tenderInfo.length < 0) {
  172. return result;
  173. }
  174. let targetTender = {};
  175. for (const tmp of tenderInfo) {
  176. if (tmp.id === tenderId) {
  177. result = true;
  178. targetTender = tmp;
  179. }
  180. }
  181. // 成功后更改session
  182. if (result) {
  183. this.ctx.session.sessionTender = {
  184. id: targetTender.id,
  185. name: targetTender.name,
  186. userAccount: targetTender.user_account,
  187. };
  188. }
  189. return result;
  190. }
  191. }
  192. return Tender;
  193. };