category_value.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date 2018/10/15
  7. * @version
  8. */
  9. module.exports = app => {
  10. class CategoryValue extends app.BaseService {
  11. /**
  12. * 构造函数
  13. *
  14. * @param {Object} ctx - egg全局变量
  15. * @return {void}
  16. */
  17. constructor(ctx) {
  18. super(ctx);
  19. this.tableName = 'category_value';
  20. }
  21. /**
  22. * 新增分类值
  23. *
  24. * @param categoryId - 分类Id
  25. * @param value - 值
  26. * @returns {Promise<void>}
  27. * @private
  28. */
  29. async _addValue(categoryId, value) {
  30. const count = await this.count({cid: categoryId, value: value});
  31. if (count > 0) {
  32. throw '该分类下存在相同的值';
  33. }
  34. const newValue = {
  35. cid: categoryId,
  36. value: value,
  37. };
  38. const result = await this.transaction.insert(this.tableName, newValue);
  39. if (result.affectedRows !== 1) {
  40. throw '提交数据失败';
  41. } else {
  42. category.id = result.insertId;
  43. }
  44. category.value = category.value && category.value !== '' ? JSON.parse(category.value) : [];
  45. return category;
  46. }
  47. async _deleteAndUpdateValue(orgValue, newValue) {
  48. if (!orgValue || orgValue.length === 0) { return }
  49. for (const ov of orgValue) {
  50. const nv = _.find(newValue, function (v) {
  51. return v.id === orgValue.id;
  52. });
  53. if (!nv) {
  54. await this.transaction.delete(this.tableName, {id: orgValue.id});
  55. } else if (nv.value !== ov.value) {
  56. await this.transaction.update(this.tableName, {id: orgValue.id, value: nv.value});
  57. }
  58. }
  59. }
  60. async setCategoryValue(cid, value) {
  61. const tenders = await this.ctx.service.tender.getList();
  62. this.transaction = await this.db.beginTransaction();
  63. try {
  64. for (const v of value) {
  65. if (v.delete) {
  66. await this.transaction.delete(this.tableName, {id: v.id});
  67. } else if (v.new) {
  68. const result = await this.transaction.insert(this.tableName, {
  69. cid: cid,
  70. pid: this.ctx.session.sessionProject.id,
  71. value: v.value,
  72. });
  73. v.id = result.insertId;
  74. } else {
  75. await this.transaction.update(this.tableName, {id: v.id, value: v.value});
  76. }
  77. if (!v.newTenders) { continue }
  78. for (const nt of v.newTenders) {
  79. const tender = this._.find(tenders, function (t) { return t.id === nt; });
  80. let cate = this._.find(tender.category, function (c) { return c.cid === cid; });
  81. if (!cate) {
  82. cate = {cid: cid};
  83. tender.category.push(cate);
  84. }
  85. cate.value = v.id;
  86. await this.transaction.update(this.service.tender.tableName, {
  87. id: tender.id,
  88. category: JSON.stringify(tender.category),
  89. });
  90. }
  91. }
  92. await this.transaction.commit();
  93. } catch(error) {
  94. await this.transaction.rollback();
  95. throw error;
  96. }
  97. }
  98. }
  99. return CategoryValue;
  100. };