external_data.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. module.exports = app => {
  10. class ExternalData extends app.BaseService {
  11. /**
  12. * 构造函数
  13. * @param ctx
  14. */
  15. constructor(ctx) {
  16. super(ctx);
  17. this.tableName = 'external_data';
  18. }
  19. async getExValue(tid, sid, ex_type, ex_field) {
  20. const result = await this.getDataByCondition({
  21. tid: tid, sid: sid,
  22. ex_type: ex_type, ex_field: ex_field
  23. });
  24. return result && result.ex_value !== null ? JSON.parse(result.ex_value) : null;
  25. }
  26. async saveExValue(tid, sid, ex_type, ex_field, ex_value) {
  27. const filter = {
  28. tid: tid, sid: sid,
  29. ex_type: ex_type, ex_field: ex_field
  30. };
  31. const count = await this.count(filter);
  32. if (count >= 0) {
  33. await this.db.update(this.tableName, { ex_value: JSON.stringify(ex_value) }, {where: filter});
  34. } else {
  35. filter.ex_value = JSON.stringify(ex_value);
  36. await this.db.insert(this.tableName, filter);
  37. }
  38. }
  39. }
  40. return ExternalData;
  41. };