tender.js 7.2 KB

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