tender.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. if (tender) {
  93. tender.category = tender.category && tender.category !== '' ? JSON.parse(tender.category) : null;
  94. }
  95. return tender;
  96. }
  97. /**
  98. * 新增标段
  99. *
  100. * @param {Object} data - 提交的数据
  101. * @return {Boolean} - 返回新增结果
  102. */
  103. async add(data) {
  104. let result = false;
  105. this.transaction = await this.db.beginTransaction();
  106. try {
  107. // 获取当前用户信息
  108. const sessionUser = this.ctx.session.sessionUser;
  109. // 获取当前项目信息
  110. const sessionProject = this.ctx.session.sessionProject;
  111. const insertData = {
  112. name: data.name,
  113. status: tenderConst.status.APPROVAL,
  114. project_id: sessionProject.id,
  115. user_id: sessionUser.accountId,
  116. create_time: new Date(),
  117. category: JSON.stringify(data.category),
  118. };
  119. const operate = await this.transaction.insert(this.tableName, insertData);
  120. result = operate.insertId > 0;
  121. if (!result) {
  122. throw '新增标段数据失败';
  123. }
  124. // 获取标段项目节点模板
  125. const tenderNodeTemplateData = await this.ctx.service.tenderNodeTemplate.getData();
  126. // 复制模板数据到标段数据表
  127. result = await this.ctx.service.ledger.innerAdd(tenderNodeTemplateData, operate.insertId, this.transaction);
  128. if (!result) {
  129. throw '新增标段项目节点失败';
  130. }
  131. await this.transaction.commit();
  132. return await this.getTender(operate.insertId);
  133. } catch (error) {
  134. await this.transaction.rollback();
  135. throw error;
  136. }
  137. }
  138. /**
  139. * 保存标段
  140. *
  141. * @param {Object} postData - 表单post过来的数据
  142. * @param {Number} id - 用于判断修改还是新增的id
  143. * @return {Boolean} - 返回执行结果
  144. */
  145. async save(postData, id = 0) {
  146. id = parseInt(id);
  147. const rowData = {
  148. id: id,
  149. name: postData.name,
  150. type: postData.type,
  151. category: JSON.stringify(postData.category),
  152. };
  153. const result = await this.db.update(this.tableName, rowData);
  154. return result.affectedRows > 0;
  155. }
  156. /**
  157. * 假删除
  158. *
  159. * @param {Number} id - 删除的id
  160. * @return {Boolean} - 删除结果
  161. */
  162. async deleteTenderById(id) {
  163. const updateData = {
  164. status: this.status.DISABLE,
  165. id,
  166. };
  167. const result = this.db.update(this.tableName, updateData);
  168. return result.affectedRows > 0;
  169. }
  170. /**
  171. * 真删除
  172. * @param {Number} id - 删除的标段id
  173. * @returns {Promise<boolean>} - 结果
  174. */
  175. async deleteTenderNoBackup(id) {
  176. const transaction = await this.db.beginTransaction();
  177. try {
  178. await transaction.delete(this.tableName, {id: id});
  179. await transaction.delete(this.ctx.service.tenderInfo.tableName, {tid: id});
  180. await transaction.delete(this.ctx.service.ledger.tableName, {tender_id: id});
  181. await transaction.delete(this.ctx.service.ledgerAudit.tableName, {tender_id: id});
  182. await transaction.delete(this.ctx.service.ledgerAudit.tableName + '_copy', {tender_id: id});
  183. await transaction.delete(this.ctx.service.pos.tableName, {tid: id});
  184. //await transaction.delete(this.ctx.service.pay.tableName, {tid: id});
  185. await transaction.delete(this.ctx.service.stage.tableName, {tid: id});
  186. await transaction.delete(this.ctx.service.stageAudit.tableName, {tid: id});
  187. await transaction.delete(this.ctx.service.stageBills.tableName, {tid: id});
  188. await transaction.delete(this.ctx.service.stagePos.tableName, {tid: id});
  189. await transaction.delete(this.ctx.service.stageDetail.tableName, {tid: id});
  190. //await transaction.delete(this.ctx.service.stagePay.tableName, {tid: id});
  191. await transaction.commit();
  192. return true;
  193. } catch (err) {
  194. console.log(err);
  195. await transaction.rollback();
  196. return false;
  197. }
  198. }
  199. /**
  200. * 切换标段
  201. *
  202. * @param {Number} tenderId - 标段id
  203. * @return {Boolean} - 返回切换结果
  204. */
  205. async switchTender(tenderId) {
  206. // 获取该用户拥有的项目数据
  207. const sessionUser = this.ctx.session.sessionUser;
  208. const tenderInfo = await this.ctx.service.projectAccount.getProjectInfoByAccount(sessionUser.account);
  209. let result = false;
  210. // 判断切换的标段是否属于对应用户
  211. if (tenderInfo.length < 0) {
  212. return result;
  213. }
  214. let targetTender = {};
  215. for (const tmp of tenderInfo) {
  216. if (tmp.id === tenderId) {
  217. result = true;
  218. targetTender = tmp;
  219. }
  220. }
  221. // 成功后更改session
  222. if (result) {
  223. this.ctx.session.sessionTender = {
  224. id: targetTender.id,
  225. name: targetTender.name,
  226. userAccount: targetTender.user_account,
  227. };
  228. }
  229. return result;
  230. }
  231. }
  232. return Tender;
  233. };