| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- 'use strict';
- /**
- *
- *
- * @author Lan
- * @date 2025/11/24
- * @version
- */
- const ContractSetting = require('../const/contract');
- module.exports = app => {
- class ContractColSet extends app.BaseService {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- constructor(ctx) {
- super(ctx);
- this.tableName = 'contract_col_set';
- }
- _analysisData(data) {
- if (!data) return;
- if(data['info']) data['info'] = JSON.parse(data['info']);
- }
- async loadContractColSet(spid, tid = null, type) {
- const result = await this.getDataByCondition({ spid, tid, type });
- this._analysisData(result);
- return result;
- }
- async initContractColSet(spid, tid = null, type) {
- const data = {
- spid,
- tid,
- type,
- info: JSON.stringify(ContractSetting.defaultColSet[type])
- }
- await this.db.insert(this.tableName, data);
- }
- async getContractColSet(spid, tid = null, type) {
- const curSet = await this.loadContractColSet(spid, tid, type);
- if (curSet) return curSet;
- await this.initContractColSet(spid, tid, type);
- return await this.loadContractColSet(spid, tid, type);
- }
- async setContractColSet(spid, tid = null, type, colSetType, colSet) {
- const data = {};
- data[colSetType] = JSON.stringify(colSet);
- await this.defaultUpdate(data, { where: { spid, tid, type } });
- }
- analysisColSetWithDefine(colSetDefine, colSet, defaultColSet) {
- const result = [];
- const colSetDefineMap = new Map(); // 用 Map 优化字段查找(O(1) 复杂度)
- const colSetFieldSet = new Set(); // 记录 colSet 中已存在的字段,避免重复
- colSetDefine.forEach(csd => {
- colSetDefineMap.set(csd.field, csd);
- });
- for (const cs of colSet) {
- const field = cs.field;
- const csd = colSetDefineMap.get(field); // 从字段定义中取 name、fixed
- const dcs = defaultColSet.find(x => x.field === field); // 取默认配置(兜底)
- if (csd) {
- // 合并:colSetDefine(name、fixed)> colSet(show、alias、顺序)> defaultColSet(兜底)
- result.push({ ...dcs, ...cs, ...csd });
- }
- colSetFieldSet.add(field); // 标记该字段已处理
- }
- // 按 colSetDefine 中未处理的顺序,补充到结果末尾
- for (const csd of colSetDefine) {
- const field = csd.field;
- if (!colSetFieldSet.has(field)) { // 未在 colSet 中找到的字段
- const dcs = defaultColSet.find(x => x.field === field); // 取默认配置
- result.push({ ...dcs, ...csd });
- }
- }
-
- return result;
- }
- }
- return ContractColSet;
- };
|