| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- '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);
- });
- const gdResult = []; // 固定列结果,最后合并到 result 前面
- 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(兜底)
- if (csd.gd) {
- gdResult.push({ ...dcs, ...cs, ...csd }); // 固定列先保存到 gdResult,最后合并到 result 前面
- } else {
- 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); // 取默认配置
- if (dcs.gd) { // 补充到结果头中
- gdResult.push({ ...dcs, ...csd });
- } else {
- result.push({ ...dcs, ...csd });
- }
- }
- }
- // 合并固定列和非固定列,固定列在前
- result.unshift(...gdResult);
- return result;
- }
- }
- return ContractColSet;
- };
|