project_col_set.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date 2024/3/5
  7. * @version
  8. */
  9. const JsonFields = [ 'info' ];
  10. const ProjectSetting = require('../const/project_setting');
  11. module.exports = app => {
  12. class ProjectColSet extends app.BaseService {
  13. /**
  14. * 构造函数
  15. *
  16. * @param {Object} ctx - egg全局变量
  17. * @return {void}
  18. */
  19. constructor(ctx) {
  20. super(ctx);
  21. this.tableName = 'project_col_set';
  22. }
  23. _analysisData(data) {
  24. if (!data) return;
  25. JsonFields.forEach(jf => { if(data[jf]) data[jf] = JSON.parse(data[jf]); });
  26. }
  27. async loadProjectColSet(id) {
  28. const result = await this.getDataById(id);
  29. this._analysisData(result);
  30. return result;
  31. }
  32. async initProjectColSet(id) {
  33. const data = ProjectSetting.defaultColSet;
  34. JsonFields.forEach(jf => { if(data[jf]) data[jf] = JSON.stringify(data[jf]); });
  35. data.id = id;
  36. await this.db.insert(this.tableName, data);
  37. }
  38. async getProjectColSet(id) {
  39. const curSet = await this.loadProjectColSet(id);
  40. if (curSet) return curSet;
  41. await this.initProjectColSet(id);
  42. return await this.loadProjectColSet(id);
  43. }
  44. async setProjectColSet(id, colSetType, colSet) {
  45. const data = {id};
  46. data[colSetType] = JSON.stringify(colSet);
  47. await this.defaultUpdate(data);
  48. }
  49. analysisColSetWithDefine(colSetDefine, colSet) {
  50. const result = [];
  51. for (const csd of colSetDefine) {
  52. const cs = colSet.find(x => { return x.field === csd.field });
  53. if (cs) {
  54. result.push({...csd, ...cs});
  55. } else {
  56. result.push({...csd});
  57. }
  58. }
  59. return result;
  60. }
  61. }
  62. return ProjectColSet;
  63. };