category_value.js 3.6 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 orgValue = await this.getAllDataByCondition({ where: {cid: cid} });
  62. const tenders = await this.ctx.service.tender.getList();
  63. this.transaction = await this.db.beginTransaction();
  64. try {
  65. // 删除值 & 更新值
  66. await this._deleteAndUpdateValue(orgValue, value);
  67. // 新增值
  68. const newValue = value.filter(function (v) {
  69. return !v.id;
  70. });
  71. for (const nv of newValue) {
  72. nv.cid = cid;
  73. nv.pid = this.ctx.session.sessionProject.id;
  74. const result = await this.transaction.insert(this.tableName, nv);
  75. nv.id = result.insertId;
  76. }
  77. // 更新标段属性
  78. for (const t of tenders) {
  79. const category = t.category ? t.category : [];
  80. const cate = _.find(t.category, function (c) {
  81. return c.cid === cid;
  82. });
  83. const tenderValue = cate ? _.find(value, function (v) {return v.id === cate.value;}) : null;
  84. if (!tenderValue) {
  85. category.push({cid: cid, value: newValue[0].id});
  86. await this.transaction.update(this.service.tender.tableName, {
  87. id: t.id,
  88. category: JSON.stringify(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. };