category_value.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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('', null, 1);
  62. this.transaction = await this.db.beginTransaction();
  63. try {
  64. let sort = 1;
  65. for (const v of value) {
  66. if (v.delete) {
  67. await this.transaction.delete(this.tableName, {id: v.id});
  68. } else if (v.new) {
  69. const result = await this.transaction.insert(this.tableName, {
  70. cid,
  71. pid: this.ctx.session.sessionProject.id,
  72. value: v.value,
  73. sort,
  74. });
  75. v.id = result.insertId;
  76. sort++;
  77. } else {
  78. await this.transaction.update(this.tableName, { id: v.id, value: v.value, sort });
  79. sort++;
  80. }
  81. if (!v.newTenders) { continue }
  82. for (const nt of v.newTenders) {
  83. const tender = this._.find(tenders, function (t) { return t.id === nt; });
  84. let cate = this._.find(tender.category, function (c) { return c.cid === cid; });
  85. if (!cate) {
  86. cate = {cid: cid};
  87. tender.category.push(cate);
  88. }
  89. cate.value = v.id;
  90. await this.transaction.update(this.service.tender.tableName, {
  91. id: tender.id,
  92. category: JSON.stringify(tender.category),
  93. });
  94. }
  95. }
  96. await this.transaction.commit();
  97. } catch(error) {
  98. await this.transaction.rollback();
  99. throw error;
  100. }
  101. }
  102. }
  103. return CategoryValue;
  104. };