| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- 'use strict';
- /**
- *
- *
- * @author Mai
- * @date 2024/3/5
- * @version
- */
- const JsonFields = [ 'info' ];
- const ProjectSetting = require('../const/project_setting');
- module.exports = app => {
- class ProjectColSet extends app.BaseService {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- constructor(ctx) {
- super(ctx);
- this.tableName = 'project_col_set';
- }
- _analysisData(data) {
- if (!data) return;
- JsonFields.forEach(jf => { if(data[jf]) data[jf] = JSON.parse(data[jf]); });
- }
- async loadProjectColSet(pid, spid = '') {
- const result = await this.getDataByCondition({ pid, spid });
- this._analysisData(result);
- return result;
- }
- async initProjectColSet(pid, spid = '') {
- const data = JSON.parse(JSON.stringify(spid ? ProjectSetting.defaultColSet : ProjectSetting.defaultProjectColSet));
- JsonFields.forEach(jf => { if(data[jf]) data[jf] = JSON.stringify(data[jf]); });
- data.pid = pid;
- data.spid = spid;
- await this.db.insert(this.tableName, data);
- }
- async getProjectColSet(pid, spid) {
- const curSet = await this.loadProjectColSet(pid, spid);
- if (curSet) return curSet;
- await this.initProjectColSet(pid, spid);
- return await this.loadProjectColSet(pid, spid);
- }
- async setProjectColSet(pid, spid, colSetType, colSet) {
- const data = {};
- data[colSetType] = JSON.stringify(colSet);
- await this.defaultUpdate(data, { where: { pid, spid: spid || '' } });
- }
- analysisColSetWithDefine(colSetDefine, colSet, defaultColSet) {
- const result = [];
- for (const csd of colSetDefine) {
- const cs = colSet.find(x => { return x.field === csd.field });
- if (cs) {
- result.push({...csd, ...cs});
- } else {
- const dcs = defaultColSet.find(x => { return x.field === csd.field; });
- result.push({...csd, ...dcs});
- }
- }
- return result;
- }
- }
- return ProjectColSet;
- };
|