tender.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. // 获取合同支付模板 并添加到标段
  132. result = await this.ctx.service.pay.addDefaultPayData(operate.insertId, this.transaction);
  133. if (!result) {
  134. throw '新增合同支付数据失败';
  135. }
  136. await this.transaction.commit();
  137. return await this.getTender(operate.insertId);
  138. } catch (error) {
  139. await this.transaction.rollback();
  140. throw error;
  141. }
  142. }
  143. /**
  144. * 保存标段
  145. *
  146. * @param {Object} postData - 表单post过来的数据
  147. * @param {Number} id - 用于判断修改还是新增的id
  148. * @return {Boolean} - 返回执行结果
  149. */
  150. async save(postData, id = 0) {
  151. id = parseInt(id);
  152. const rowData = {
  153. id: id,
  154. name: postData.name,
  155. type: postData.type,
  156. category: JSON.stringify(postData.category),
  157. };
  158. const result = await this.db.update(this.tableName, rowData);
  159. return result.affectedRows > 0;
  160. }
  161. /**
  162. * 假删除
  163. *
  164. * @param {Number} id - 删除的id
  165. * @return {Boolean} - 删除结果
  166. */
  167. async deleteTenderById(id) {
  168. const updateData = {
  169. status: this.status.DISABLE,
  170. id,
  171. };
  172. const result = this.db.update(this.tableName, updateData);
  173. return result.affectedRows > 0;
  174. }
  175. /**
  176. * 真删除
  177. * @param {Number} id - 删除的标段id
  178. * @returns {Promise<boolean>} - 结果
  179. */
  180. async deleteTenderNoBackup(id) {
  181. const transaction = await this.db.beginTransaction();
  182. try {
  183. await transaction.delete(this.tableName, {id: id});
  184. await transaction.delete(this.ctx.service.tenderInfo.tableName, {tid: id});
  185. await transaction.delete(this.ctx.service.ledger.tableName, {tender_id: id});
  186. await transaction.delete(this.ctx.service.ledgerAudit.tableName, {tender_id: id});
  187. await transaction.delete(this.ctx.service.ledgerAudit.tableName + '_copy', {tender_id: id});
  188. await transaction.delete(this.ctx.service.pos.tableName, {tid: id});
  189. //await transaction.delete(this.ctx.service.pay.tableName, {tid: id});
  190. await transaction.delete(this.ctx.service.stage.tableName, {tid: id});
  191. await transaction.delete(this.ctx.service.stageAudit.tableName, {tid: id});
  192. await transaction.delete(this.ctx.service.stageBills.tableName, {tid: id});
  193. await transaction.delete(this.ctx.service.stagePos.tableName, {tid: id});
  194. await transaction.delete(this.ctx.service.stageDetail.tableName, {tid: id});
  195. //await transaction.delete(this.ctx.service.stagePay.tableName, {tid: id});
  196. await transaction.commit();
  197. return true;
  198. } catch (err) {
  199. console.log(err);
  200. await transaction.rollback();
  201. return false;
  202. }
  203. }
  204. /**
  205. * 切换标段
  206. *
  207. * @param {Number} tenderId - 标段id
  208. * @return {Boolean} - 返回切换结果
  209. */
  210. async switchTender(tenderId) {
  211. // 获取该用户拥有的项目数据
  212. const sessionUser = this.ctx.session.sessionUser;
  213. const tenderInfo = await this.ctx.service.projectAccount.getProjectInfoByAccount(sessionUser.account);
  214. let result = false;
  215. // 判断切换的标段是否属于对应用户
  216. if (tenderInfo.length < 0) {
  217. return result;
  218. }
  219. let targetTender = {};
  220. for (const tmp of tenderInfo) {
  221. if (tmp.id === tenderId) {
  222. result = true;
  223. targetTender = tmp;
  224. }
  225. }
  226. // 成功后更改session
  227. if (result) {
  228. this.ctx.session.sessionTender = {
  229. id: targetTender.id,
  230. name: targetTender.name,
  231. userAccount: targetTender.user_account,
  232. };
  233. }
  234. return result;
  235. }
  236. }
  237. return Tender;
  238. };