project_col_set.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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(pid, spid) {
  28. const result = await this.getDataByCondition({ pid, spid });
  29. this._analysisData(result);
  30. return result;
  31. }
  32. async initProjectColSet(pid, spid) {
  33. const data = JSON.parse(JSON.stringify(ProjectSetting.defaultColSet));
  34. JsonFields.forEach(jf => { if(data[jf]) data[jf] = JSON.stringify(data[jf]); });
  35. data.pid = pid;
  36. data.spid = spid;
  37. await this.db.insert(this.tableName, data);
  38. }
  39. async getProjectColSet(pid, spid) {
  40. const curSet = await this.loadProjectColSet(pid, spid);
  41. if (curSet) return curSet;
  42. await this.initProjectColSet(pid, spid);
  43. return await this.loadProjectColSet(pid, spid);
  44. }
  45. async setProjectColSet(pid, spid, colSetType, colSet) {
  46. const data = {};
  47. data[colSetType] = JSON.stringify(colSet);
  48. await this.defaultUpdate(data, { where: { pid, spid } });
  49. }
  50. analysisColSetWithDefine(colSetDefine, colSet) {
  51. const result = [];
  52. for (const csd of colSetDefine) {
  53. const cs = colSet.find(x => { return x.field === csd.field });
  54. if (cs) {
  55. result.push({...csd, ...cs});
  56. } else {
  57. result.push({...csd});
  58. }
  59. }
  60. return result;
  61. }
  62. }
  63. return ProjectColSet;
  64. };