tender.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. * @return {Array} - 返回标段数据
  65. */
  66. async getList() {
  67. // 获取当前项目信息
  68. const sessionProject = this.ctx.session.sessionProject;
  69. this.initSqlBuilder();
  70. this.sqlBuilder.setAndWhere('project_id', {
  71. value: sessionProject.id,
  72. operate: '=',
  73. });
  74. this.sqlBuilder.columns = ['id', 'project_id', 'name', 'status', 'category', 'ledger_times', 'ledger_status'];
  75. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  76. const list = await this.db.query(sql, sqlParam);
  77. for (const l of list) {
  78. l.category = l.category && l.category !== '' ? JSON.parse(l.category) : null;
  79. }
  80. return list;
  81. }
  82. async getTender (id) {
  83. this.initSqlBuilder();
  84. this.sqlBuilder.setAndWhere('id', {
  85. value: id,
  86. operate: '=',
  87. });
  88. this.sqlBuilder.columns = ['id', 'project_id', 'name', 'status', 'category', 'ledger_times', 'ledger_status'];
  89. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  90. const tender = await this.db.queryOne(sql, sqlParam);
  91. tender.category = tender.category && tender.category !== '' ? JSON.parse(tender.category) : null;
  92. return tender;
  93. }
  94. /**
  95. * 新增标段
  96. *
  97. * @param {Object} data - 提交的数据
  98. * @return {Boolean} - 返回新增结果
  99. */
  100. async add(data) {
  101. let result = false;
  102. this.transaction = await this.db.beginTransaction();
  103. try {
  104. // 获取当前用户信息
  105. const sessionUser = this.ctx.session.sessionUser;
  106. // 获取当前项目信息
  107. const sessionProject = this.ctx.session.sessionProject;
  108. const insertData = {
  109. name: data.name,
  110. status: tenderConst.status.APPROVAL,
  111. project_id: sessionProject.id,
  112. user_id: sessionUser.accountId,
  113. create_time: new Date(),
  114. category: JSON.stringify(data.category),
  115. };
  116. const operate = await this.transaction.insert(this.tableName, insertData);
  117. result = operate.insertId > 0;
  118. if (!result) {
  119. throw '新增标段数据失败';
  120. }
  121. // 获取标段项目节点模板
  122. const tenderNodeTemplateData = await this.ctx.service.tenderNodeTemplate.getData();
  123. // 复制模板数据到标段数据表
  124. result = await this.ctx.service.ledger.innerAdd(tenderNodeTemplateData, operate.insertId, this.transaction);
  125. if (!result) {
  126. throw '新增标段项目节点失败';
  127. }
  128. this.transaction.commit();
  129. return await this.getTender(operate.insertId);
  130. } catch (error) {
  131. console.log(error);
  132. result = false;
  133. this.transaction.rollback();
  134. }
  135. }
  136. /**
  137. * 保存标段
  138. *
  139. * @param {Object} postData - 表单post过来的数据
  140. * @param {Number} id - 用于判断修改还是新增的id
  141. * @return {Boolean} - 返回执行结果
  142. */
  143. async save(postData,id = 0) {
  144. id = parseInt(id);
  145. const rowData = {
  146. id: id,
  147. name: postData.name,
  148. type: postData.type,
  149. };
  150. const result = await this.db.update(this.tableName, rowData);
  151. return result.affectedRows > 0;
  152. }
  153. /**
  154. * 假删除
  155. *
  156. * @param {Number} id - 删除的id
  157. * @return {Boolean} - 删除结果
  158. */
  159. async deleteTenderById(id) {
  160. const updateData = {
  161. status: this.status.DISABLE,
  162. id,
  163. };
  164. const result = this.db.update(this.tableName, updateData);
  165. return result.affectedRows > 0;
  166. }
  167. /**
  168. * 切换标段
  169. *
  170. * @param {Number} tenderId - 标段id
  171. * @return {Boolean} - 返回切换结果
  172. */
  173. async switchTender(tenderId) {
  174. // 获取该用户拥有的项目数据
  175. const sessionUser = this.ctx.session.sessionUser;
  176. const tenderInfo = await this.ctx.service.projectAccount.getProjectInfoByAccount(sessionUser.account);
  177. let result = false;
  178. // 判断切换的标段是否属于对应用户
  179. if (tenderInfo.length < 0) {
  180. return result;
  181. }
  182. let targetTender = {};
  183. for (const tmp of tenderInfo) {
  184. if (tmp.id === tenderId) {
  185. result = true;
  186. targetTender = tmp;
  187. }
  188. }
  189. // 成功后更改session
  190. if (result) {
  191. this.ctx.session.sessionTender = {
  192. id: targetTender.id,
  193. name: targetTender.name,
  194. userAccount: targetTender.user_account,
  195. };
  196. }
  197. return result;
  198. }
  199. }
  200. return Tender;
  201. };