3433 lines
186 KiB
C#
3433 lines
186 KiB
C#
using System.Text;
|
|
using System.Text.RegularExpressions;
|
|
using JNPF.Common.Const;
|
|
using JNPF.Common.Core.Manager;
|
|
using JNPF.Common.Dtos.VisualDev;
|
|
using JNPF.Common.Enums;
|
|
using JNPF.Common.Extension;
|
|
using JNPF.Common.Filter;
|
|
using JNPF.Common.Manager;
|
|
using JNPF.Common.Models.VisualDev;
|
|
using JNPF.Common.Security;
|
|
using JNPF.DependencyInjection;
|
|
using JNPF.FriendlyException;
|
|
using JNPF.JsonSerialization;
|
|
using JNPF.RemoteRequest.Extensions;
|
|
using JNPF.Systems.Entitys.Model.DataBase;
|
|
using JNPF.Systems.Entitys.Permission;
|
|
using JNPF.Systems.Entitys.System;
|
|
using JNPF.Systems.Interfaces.System;
|
|
using JNPF.VisualDev.Engine;
|
|
using JNPF.VisualDev.Engine.Core;
|
|
using JNPF.VisualDev.Entitys;
|
|
using JNPF.VisualDev.Entitys.Dto.VisualDevModelData;
|
|
using JNPF.VisualDev.Interfaces;
|
|
using JNPF.WorkFlow.Entitys.Entity;
|
|
using JNPF.WorkFlow.Interfaces.Repository;
|
|
using Mapster;
|
|
using Newtonsoft.Json.Linq;
|
|
using SqlSugar;
|
|
|
|
namespace JNPF.VisualDev;
|
|
|
|
/// <summary>
|
|
/// 在线开发运行服务 .
|
|
/// </summary>
|
|
public class RunService : IRunService, ITransient
|
|
{
|
|
#region 构造
|
|
|
|
/// <summary>
|
|
/// 服务基础仓储.
|
|
/// </summary>
|
|
private readonly ISqlSugarRepository<VisualDevEntity> _visualDevRepository; // 在线开发功能实体
|
|
|
|
/// <summary>
|
|
/// 表单数据解析.
|
|
/// </summary>
|
|
private readonly FormDataParsing _formDataParsing;
|
|
|
|
/// <summary>
|
|
/// 切库.
|
|
/// </summary>
|
|
private readonly IDataBaseManager _databaseService;
|
|
|
|
/// <summary>
|
|
/// 单据.
|
|
/// </summary>
|
|
private readonly IBillRullService _billRuleService;
|
|
|
|
/// <summary>
|
|
/// 缓存管理.
|
|
/// </summary>
|
|
private readonly ICacheManager _cacheManager;
|
|
|
|
/// <summary>
|
|
/// 用户管理.
|
|
/// </summary>
|
|
private readonly IUserManager _userManager;
|
|
|
|
/// <summary>
|
|
/// 数据连接服务.
|
|
/// </summary>
|
|
private readonly IDbLinkService _dbLinkService;
|
|
|
|
/// <summary>
|
|
/// 流程数据.
|
|
/// </summary>
|
|
private readonly IFlowTaskRepository _flowTaskRepository;
|
|
|
|
/// <summary>
|
|
/// 事务.
|
|
/// </summary>
|
|
private readonly ITenant _db;
|
|
|
|
/// <summary>
|
|
/// 构造.
|
|
/// </summary>
|
|
public RunService(
|
|
ISqlSugarRepository<VisualDevEntity> visualDevRepository,
|
|
FormDataParsing formDataParsing,
|
|
IUserManager userManager,
|
|
IDbLinkService dbLinkService,
|
|
IDataBaseManager databaseService,
|
|
IFlowTaskRepository flowTaskRepository,
|
|
ISqlSugarClient context,
|
|
IBillRullService billRuleService)
|
|
{
|
|
_visualDevRepository = visualDevRepository;
|
|
_formDataParsing = formDataParsing;
|
|
_userManager = userManager;
|
|
_databaseService = databaseService;
|
|
_dbLinkService = dbLinkService;
|
|
_billRuleService = billRuleService;
|
|
_flowTaskRepository = flowTaskRepository;
|
|
_db = context.AsTenant();
|
|
}
|
|
#endregion
|
|
|
|
#region Get
|
|
|
|
/// <summary>
|
|
/// 列表数据处理.
|
|
/// </summary>
|
|
/// <param name="entity">功能实体.</param>
|
|
/// <param name="input">查询参数.</param>
|
|
/// <param name="actionType"></param>
|
|
/// <returns></returns>
|
|
public async Task<PageResult<Dictionary<string, object>>> GetListResult(VisualDevEntity entity, VisualDevModelListQueryInput input, string actionType = "List")
|
|
{
|
|
PageResult<Dictionary<string, object>>? realList = new PageResult<Dictionary<string, object>>() { list = new List<Dictionary<string, object>>() }; // 返回结果集
|
|
TemplateParsingBase templateInfo = new TemplateParsingBase(entity); // 解析模板控件
|
|
|
|
// 处理查询
|
|
Dictionary<string, object> queryJson = string.IsNullOrEmpty(input.queryJson) ? null : input.queryJson.ToObject<Dictionary<string, object>>();
|
|
if (queryJson != null)
|
|
{
|
|
foreach (KeyValuePair<string, object> item in queryJson)
|
|
{
|
|
if (!templateInfo.ColumnData.searchList.Any(it => it.__vModel__.Equals(item.Key)))
|
|
{
|
|
var vmodel = templateInfo.AllFieldsModel.Find(it => it.__vModel__.Equals(item.Key));
|
|
templateInfo.ColumnData.searchList.Add(vmodel.ToObject<IndexSearchFieldModel>());
|
|
}
|
|
if (!templateInfo.AppColumnData.searchList.Any(it => it.__vModel__.Equals(item.Key)))
|
|
{
|
|
var vmodel = templateInfo.AllFieldsModel.Find(it => it.__vModel__.Equals(item.Key));
|
|
templateInfo.AppColumnData.searchList.Add(vmodel.ToObject<IndexSearchFieldModel>());
|
|
}
|
|
}
|
|
}
|
|
|
|
input.superQueryJson = GetSuperQueryInput(input.superQueryJson);
|
|
|
|
string? primaryKey = "F_Id"; // 列表主键
|
|
|
|
// 获取请求端类型,并对应获取 数据权限
|
|
DbLinkEntity link = await GetDbLink(entity.DbLinkId);
|
|
templateInfo.DbLink = link;
|
|
LogicalDelete(templateInfo); // 处理软删除
|
|
primaryKey = GetPrimary(link, templateInfo.MainTableName);
|
|
bool udp = _userManager.UserOrigin == "pc" ? templateInfo.ColumnData.useDataPermission : templateInfo.AppColumnData.useDataPermission;
|
|
var pvalue = await _userManager.GetCondition<Dictionary<string, object>>(primaryKey, input.menuId, udp);
|
|
var pvalueJson = pvalue.ToJsonString();
|
|
foreach (var item in templateInfo.AllTableFields)
|
|
{
|
|
if (pvalueJson.Contains(string.Format("\"FieldName\":\"{0}\",", item.Key)))
|
|
pvalueJson.Replace(string.Format("\"FieldName\":\"{0}\",", item.Value), string.Format("\"FieldName\":\"{0}\",", item.Key));
|
|
}
|
|
pvalue = _visualDevRepository.AsSugarClient().Utilities.JsonToConditionalModels(pvalueJson);
|
|
|
|
// 所有查询条件
|
|
var queryWhere = new List<IConditionalModel>();
|
|
var superQueryWhere = new List<IConditionalModel>();
|
|
if (input.queryJson.IsNotEmptyOrNull()) queryWhere = GetQueryJson(input.queryJson, _userManager.UserOrigin == "pc" ? templateInfo.ColumnData : templateInfo.AppColumnData);
|
|
if (input.superQueryJson.IsNotEmptyOrNull()) superQueryWhere = GetSuperQueryJson(input.superQueryJson);
|
|
|
|
if (templateInfo.ColumnData.type == 4) await OptimisticLocking(link, templateInfo); // 开启行编辑 处理 开启并发锁定
|
|
Dictionary<string, string>? tableFieldKeyValue = new Dictionary<string, string>(); // 联表查询 表字段名称 对应 前端字段名称 (应对oracle 查询字段长度不能超过30个)
|
|
string? sql = GetListQuerySql(primaryKey, templateInfo, ref input, ref tableFieldKeyValue, pvalue); // 查询sql
|
|
|
|
// 如果排序字段没有在显示列中,按默认排序
|
|
if (input.sidx.IsNotEmptyOrNull() && !templateInfo.ColumnData.columnList.Any(x => x.prop == input.sidx)) input.sidx = string.Empty;
|
|
|
|
// 处理查询
|
|
input.queryJson = GetQueryJson(input.queryJson, _userManager.UserOrigin == "pc" ? templateInfo.ColumnData : templateInfo.AppColumnData).ToJsonString();
|
|
input.superQueryJson = GetSuperQueryJson(input.superQueryJson).ToJsonString();
|
|
|
|
realList = _databaseService.GetInterFaceData(link, sql, input, templateInfo.ColumnData.Adapt<MainBeltViceQueryModel>(), new List<IConditionalModel>(), tableFieldKeyValue);
|
|
|
|
// 显示列有子表字段
|
|
if ((templateInfo.ColumnData.type != 4 || _userManager.UserOrigin.Equals("app")) && templateInfo.ColumnData.columnList.Any(x => templateInfo.ChildTableFields.ContainsKey(x.__vModel__)) && realList.list.Any())
|
|
realList = await GetListChildTable(templateInfo, primaryKey, queryWhere, superQueryWhere, realList, pvalue);
|
|
|
|
// 处理 自增长ID 流程表单 自增长Id转成 流程Id
|
|
if (entity.FlowId.IsNotEmptyOrNull() && entity.EnableFlow.Equals(1) && realList.list.Any())
|
|
{
|
|
var ids = realList.list.Select(x => x[primaryKey]).ToList();
|
|
var newIds = GetPIdsByFlowIds(link, templateInfo, primaryKey, ids.ToObject<List<string>>(), true);
|
|
realList.list.ForEach(item => item[primaryKey] = newIds.First(x => x.Value.Equals(item[primaryKey].ToString())).Key);
|
|
}
|
|
|
|
input.sidx = string.IsNullOrEmpty(input.sidx) ? (templateInfo.ColumnData.defaultSidx == string.Empty ? primaryKey : templateInfo.ColumnData.defaultSidx) : input.sidx;
|
|
|
|
// 增加前端回显字段 : key_name
|
|
var roweditId = SnowflakeIdHelper.NextId();
|
|
if (templateInfo.ColumnData.type.Equals(4) && _userManager.UserOrigin.Equals("pc"))
|
|
{
|
|
realList.list.ForEach(items =>
|
|
{
|
|
var addItem = new Dictionary<string, object>();
|
|
foreach (var item in items) if (item.Key != "RowIndex") addItem.Add(item.Key + roweditId, item.Value);
|
|
foreach (var item in addItem) items.Add(item.Key, item.Value);
|
|
});
|
|
}
|
|
|
|
if (realList.list.Any())
|
|
{
|
|
if (templateInfo.SingleFormData.Any(x => x.__config__.templateJson != null && x.__config__.templateJson.Any()))
|
|
realList.list = await _formDataParsing.GetKeyData(templateInfo.SingleFormData.Where(x => x.__config__.templateJson != null && x.__config__.templateJson.Any()).ToList(), realList.list, templateInfo.ColumnData, actionType, templateInfo.WebType, primaryKey);
|
|
else //modified by ly on 20230407
|
|
realList.list = await _formDataParsing.GetKeyData(templateInfo.SingleFormData.Where(x => x.__config__.templateJson == null|| !x.__config__.templateJson.Any()).ToList(), realList.list, templateInfo.ColumnData, actionType, templateInfo.WebType, primaryKey);
|
|
|
|
// 如果是无表数据并且排序字段不为空,再进行数据排序
|
|
if (!templateInfo.IsHasTable && input.sidx.IsNotEmptyOrNull())
|
|
{
|
|
if (input.sort == "desc")
|
|
{
|
|
realList.list = realList.list.OrderByDescending(x =>
|
|
{
|
|
var dic = x as IDictionary<string, object>;
|
|
dic.GetOrAdd(input.sidx, () => null);
|
|
return dic[input.sidx];
|
|
}).ToList();
|
|
}
|
|
else
|
|
{
|
|
realList.list = realList.list.OrderBy(x =>
|
|
{
|
|
var dic = x as IDictionary<string, object>;
|
|
dic.GetOrAdd(input.sidx, () => null);
|
|
return dic[input.sidx];
|
|
}).ToList();
|
|
}
|
|
}
|
|
}
|
|
|
|
if (input.dataType == "0")
|
|
{
|
|
if (!string.IsNullOrEmpty(entity.Tables) && !"[]".Equals(entity.Tables))
|
|
{
|
|
}
|
|
else
|
|
{
|
|
realList.pagination = new PageResult();
|
|
realList.pagination.total = realList.list.Count;
|
|
realList.pagination.pageSize = input.pageSize;
|
|
realList.pagination.currentPage = input.currentPage;
|
|
realList.list = realList.list.Skip(input.pageSize * (input.currentPage - 1)).Take(input.pageSize).ToList();
|
|
}
|
|
|
|
// 分组表格
|
|
if (templateInfo.ColumnData.type == 3 && _userManager.UserOrigin == "pc")
|
|
realList.list = CodeGenHelper.GetGroupList(realList.list, templateInfo.ColumnData.groupField, templateInfo.ColumnData.columnList.Find(x => x.__vModel__.ToLower() != templateInfo.ColumnData.groupField.ToLower()).__vModel__);
|
|
}
|
|
else
|
|
{
|
|
if (!string.IsNullOrEmpty(entity.Tables) && !"[]".Equals(entity.Tables))
|
|
{
|
|
}
|
|
else
|
|
{
|
|
realList.pagination = new PageResult();
|
|
realList.pagination.total = realList.list.Count;
|
|
realList.pagination.pageSize = input.pageSize;
|
|
realList.pagination.currentPage = input.currentPage;
|
|
realList.list = realList.list.ToList();
|
|
}
|
|
}
|
|
|
|
// 增加前端回显字段 : key_name
|
|
if (templateInfo.ColumnData.type.Equals(4) && _userManager.UserOrigin.Equals("pc"))
|
|
{
|
|
var newList = new List<Dictionary<string, object>>();
|
|
realList.list.ForEach(items =>
|
|
{
|
|
var newItem = new Dictionary<string, object>();
|
|
foreach (var item in items)
|
|
{
|
|
if (item.Key.Contains(roweditId) && item.Value.IsNotEmptyOrNull())
|
|
{
|
|
var obj = item.Value;
|
|
if (obj.ToString().Contains("[[")) obj = item.Value.ToString().ToObject<List<List<object>>>();
|
|
else if (obj.ToString().Contains("[")) obj = item.Value.ToString().ToObject<List<object>>();
|
|
|
|
var value = items.FirstOrDefault(x => x.Key == item.Key.Replace(roweditId, string.Empty)).Value;
|
|
if (value.IsNullOrEmpty()) obj = null;
|
|
if (!newItem.ContainsKey(item.Key.Replace(roweditId, string.Empty))) newItem.Add(item.Key.Replace(roweditId, string.Empty), obj);
|
|
if (!newItem.ContainsKey(item.Key.Replace(roweditId, string.Empty) + "_name")) newItem.Add(item.Key.Replace(roweditId, string.Empty) + "_name", value);
|
|
}
|
|
if (item.Key.Equals("flowState") || item.Key.Equals("flowState_name")) newItem.Add(item.Key, item.Value);
|
|
if (item.Key.Equals("id") && !newItem.ContainsKey(item.Key)) newItem.Add(item.Key, item.Value);
|
|
if (templateInfo.AllFieldsModel.Any(x => x.__vModel__.Equals(item.Key) && x.__config__.jnpfKey.Equals(JnpfKeyConst.TIME))) newItem[item.Key] = items[item.Key];
|
|
}
|
|
|
|
newList.Add(newItem);
|
|
});
|
|
realList.list = newList;
|
|
}
|
|
|
|
return realList;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 关联表单列表数据处理.
|
|
/// </summary>
|
|
/// <param name="entity">功能实体.</param>
|
|
/// <param name="input">查询参数.</param>
|
|
/// <param name="actionType"></param>
|
|
/// <returns></returns>
|
|
public async Task<PageResult<Dictionary<string, object>>> GetRelationFormList(VisualDevEntity entity, VisualDevModelListQueryInput input, string actionType = "List")
|
|
{
|
|
PageResult<Dictionary<string, object>>? realList = new PageResult<Dictionary<string, object>>() { list = new List<Dictionary<string, object>>() }; // 返回结果集
|
|
TemplateParsingBase? templateInfo = new TemplateParsingBase(entity); // 解析模板控件
|
|
string? primaryKey = "F_Id"; // 列表主键
|
|
|
|
List<IConditionalModel>? pvalue = new List<IConditionalModel>(); // 关联表单调用 数据全部放开
|
|
|
|
DbLinkEntity link = await GetDbLink(entity.DbLinkId);
|
|
primaryKey = GetPrimary(link, templateInfo.MainTableName);
|
|
Dictionary<string, string>? tableFieldKeyValue = new Dictionary<string, string>(); // 联表查询 表字段名称 对应 前端字段名称 (应对oracle 查询字段长度不能超过30个)
|
|
string? sql = GetListQuerySql(primaryKey, templateInfo, ref input, ref tableFieldKeyValue, pvalue, true); // 查询sql
|
|
|
|
// 如果排序字段没有在显示列中,按默认排序
|
|
if (input.sidx.IsNotEmptyOrNull() && !templateInfo.ColumnData.columnList.Any(x => x.prop == input.sidx)) input.sidx = string.Empty;
|
|
|
|
string? queryJson = input.queryJson;
|
|
input.queryJson = string.Empty;
|
|
|
|
realList = _databaseService.GetInterFaceData(link, sql, input, templateInfo.ColumnData.Adapt<MainBeltViceQueryModel>(), pvalue, tableFieldKeyValue);
|
|
input.queryJson = queryJson;
|
|
|
|
// 处理 自增长ID 流程表单 自增长Id转成 流程Id
|
|
if (entity.FlowId.IsNotEmptyOrNull() && entity.EnableFlow.Equals(1) && realList.list.Any())
|
|
{
|
|
var ids = realList.list.Select(x => x[primaryKey]).ToList();
|
|
var newIds = GetPIdsByFlowIds(link, templateInfo, primaryKey, ids.ToObject<List<string>>(), true);
|
|
realList.list.ForEach(item => item[primaryKey] = newIds.First(x => x.Value.Equals(item[primaryKey].ToString())).Key);
|
|
}
|
|
|
|
input.sidx = string.IsNullOrEmpty(input.sidx) ? (templateInfo.ColumnData.defaultSidx == string.Empty ? primaryKey : templateInfo.ColumnData.defaultSidx) : input.sidx;
|
|
|
|
if (realList.list.Any())
|
|
{
|
|
if (templateInfo.SingleFormData.Any(x => x.__config__.templateJson != null && x.__config__.templateJson.Any()))
|
|
realList.list = await _formDataParsing.GetKeyData(templateInfo.SingleFormData.Where(x => x.__config__.templateJson != null && x.__config__.templateJson.Any()).ToList(), realList.list, templateInfo.ColumnData, actionType, templateInfo.WebType, primaryKey);
|
|
realList.list = await _formDataParsing.GetKeyData(templateInfo.SingleFormData.Where(x => !x.__config__.jnpfKey.Equals(JnpfKeyConst.RELATIONFORM) && x.__config__.templateJson == null).ToList(), realList.list, templateInfo.ColumnData, actionType, templateInfo.WebType.ParseToInt(), primaryKey);
|
|
|
|
if (input.queryJson.IsNotEmptyOrNull())
|
|
{
|
|
Dictionary<string, string>? search = input.queryJson.ToObject<Dictionary<string, string>>();
|
|
if (search.FirstOrDefault().Value.IsNotEmptyOrNull())
|
|
{
|
|
string? keword = search.FirstOrDefault().Value;
|
|
List<Dictionary<string, object>>? newList = new List<Dictionary<string, object>>();
|
|
List<string>? columnName = templateInfo.ColumnData.columnList.Select(x => x.prop).ToList();
|
|
realList.list.ForEach(item =>
|
|
{
|
|
if (item.Where(x => x.Value != null && columnName.Contains(x.Key)).Where(x => x.Value.ToString().Contains(keword)).Any()) newList.Add(item);
|
|
});
|
|
|
|
realList.list = newList;
|
|
}
|
|
}
|
|
|
|
if (input.sort == "desc")
|
|
{
|
|
realList.list = realList.list.OrderByDescending(x =>
|
|
{
|
|
IDictionary<string, object>? dic = x as IDictionary<string, object>;
|
|
dic.GetOrAdd(input.sidx, () => null);
|
|
return dic[input.sidx];
|
|
}).ToList();
|
|
}
|
|
else
|
|
{
|
|
realList.list = realList.list.OrderBy(x =>
|
|
{
|
|
IDictionary<string, object>? dic = x as IDictionary<string, object>;
|
|
dic.GetOrAdd(input.sidx, () => null);
|
|
return dic[input.sidx];
|
|
}).ToList();
|
|
}
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(entity.Tables) && !"[]".Equals(entity.Tables))
|
|
{
|
|
}
|
|
else
|
|
{
|
|
realList.pagination = new PageResult();
|
|
realList.pagination.total = realList.list.Count;
|
|
realList.pagination.pageSize = input.pageSize;
|
|
realList.pagination.currentPage = input.currentPage;
|
|
realList.list = realList.list.ToList();
|
|
}
|
|
|
|
return realList;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取模型数据信息.
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
public async Task<VisualDevModelDataEntity> GetInfo(string id)
|
|
{
|
|
return await _visualDevRepository.AsSugarClient().Queryable<VisualDevModelDataEntity>().FirstAsync(m => m.Id == id);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取有表详情.
|
|
/// </summary>
|
|
/// <param name="id">主键.</param>
|
|
/// <param name="templateEntity">模板实体.</param>
|
|
/// <returns></returns>
|
|
public async Task<Dictionary<string, object>> GetHaveTableInfo(string id, VisualDevEntity templateEntity)
|
|
{
|
|
TemplateParsingBase templateInfo = new TemplateParsingBase(templateEntity); // 解析模板控件
|
|
DbLinkEntity link = await GetDbLink(templateEntity.DbLinkId);
|
|
string? mainPrimary = GetPrimary(link, templateInfo.MainTableName);
|
|
await OptimisticLocking(link, templateInfo); // 处理 开启 并发锁定
|
|
if (id.Equals("0") || id.IsNullOrWhiteSpace()) return new Dictionary<string, object>();
|
|
id = GetPIdsByFlowIds(link, templateInfo, mainPrimary, new List<string>() { id }).First().Value;
|
|
Dictionary<string, string>? tableFieldKeyValue = new Dictionary<string, string>(); // 联表查询 表字段 别名
|
|
var sql = GetInfoQuerySql(id, mainPrimary, templateInfo, ref tableFieldKeyValue); // 获取查询Sql
|
|
|
|
Dictionary<string, object>? data = _databaseService.GetInterFaceData(link, sql).ToJsonString().ToObject<List<Dictionary<string, object>>>().FirstOrDefault();
|
|
if (data == null) return null;
|
|
|
|
// 记录全部数据
|
|
Dictionary<string, object> dataMap = new Dictionary<string, object>();
|
|
|
|
// 查询别名转换
|
|
if (templateInfo.AuxiliaryTableFieldsModelList.Any()) foreach (KeyValuePair<string, object> item in data) dataMap.Add(tableFieldKeyValue[item.Key.ToUpper()], item.Value);
|
|
else dataMap = data;
|
|
|
|
Dictionary<string, object> newDataMap = new Dictionary<string, object>();
|
|
|
|
dataMap = _formDataParsing.GetTableDataInfo(new List<Dictionary<string, object>>() { dataMap }, templateInfo.FieldsModelList, "detail").FirstOrDefault();
|
|
|
|
// 处理子表数据
|
|
newDataMap = await GetChildTableData(templateInfo, link, dataMap, newDataMap, false);
|
|
|
|
int dicCount = newDataMap.Keys.Count;
|
|
string[] strKey = new string[dicCount];
|
|
newDataMap.Keys.CopyTo(strKey, 0);
|
|
for (int i = 0; i < strKey.Length; i++)
|
|
{
|
|
FieldsModel? model = templateInfo.FieldsModelList.Where(m => m.__vModel__ == strKey[i]).FirstOrDefault();
|
|
if (model != null)
|
|
{
|
|
List<Dictionary<string, object>> tables = newDataMap[strKey[i]].ToObject<List<Dictionary<string, object>>>();
|
|
List<Dictionary<string, object>> newTables = new List<Dictionary<string, object>>();
|
|
foreach (Dictionary<string, object>? item in tables)
|
|
{
|
|
Dictionary<string, object> dic = new Dictionary<string, object>();
|
|
foreach (KeyValuePair<string, object> value in item)
|
|
{
|
|
FieldsModel? child = model.__config__.children.Find(c => c.__vModel__ == value.Key);
|
|
if (child != null) dic.Add(value.Key, value.Value);
|
|
}
|
|
|
|
newTables.Add(dic);
|
|
}
|
|
|
|
if (newTables.Count > 0) newDataMap[strKey[i]] = newTables;
|
|
}
|
|
}
|
|
|
|
foreach (KeyValuePair<string, object> entryMap in dataMap)
|
|
{
|
|
FieldsModel? model = templateInfo.FieldsModelList.Where(m => m.__vModel__.ToLower() == entryMap.Key.ToLower()).FirstOrDefault();
|
|
if (model != null && entryMap.Key.ToLower().Equals(model.__vModel__.ToLower())) newDataMap[entryMap.Key] = entryMap.Value;
|
|
}
|
|
|
|
if (!newDataMap.ContainsKey("id")) newDataMap.Add("id", data[mainPrimary]);
|
|
_formDataParsing.GetBARAndQR(templateInfo.FieldsModelList, newDataMap, dataMap); // 处理 条形码 、 二维码 控件
|
|
|
|
return await _formDataParsing.GetSystemComponentsData(templateInfo.FieldsModelList, newDataMap.ToJsonString());
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取有表详情转换.
|
|
/// </summary>
|
|
/// <param name="id">主键.</param>
|
|
/// <param name="templateEntity">模板实体.</param>
|
|
/// <param name="isFlowTask"></param>
|
|
/// <returns></returns>
|
|
/// modified by pf 2023-04-12 返回值不序列化
|
|
public async Task<Dictionary<string, object>?> GetHaveTableInfoDetails(string id, VisualDevEntity templateEntity, bool isFlowTask = false)
|
|
{
|
|
TemplateParsingBase? templateInfo = new TemplateParsingBase(templateEntity, isFlowTask); // 解析模板控件
|
|
DbLinkEntity link = await GetDbLink(templateEntity.DbLinkId);
|
|
string? mainPrimary = GetPrimary(link, templateInfo.MainTableName);
|
|
id = GetPIdsByFlowIds(link, templateInfo, mainPrimary, new List<string>() { id }).First().Value;
|
|
Dictionary<string, string>? tableFieldKeyValue = new Dictionary<string, string>(); // 联表查询 表字段 别名
|
|
var sql = GetInfoQuerySql(id, mainPrimary, templateInfo, ref tableFieldKeyValue); // 获取查询Sql
|
|
|
|
Dictionary<string, object>? data = _databaseService.GetInterFaceData(link, sql).ToJsonString().ToObject<List<Dictionary<string, string>>>().ToObject<List<Dictionary<string, object>>>().FirstOrDefault();
|
|
// modified by pf 2023-04-12 返回值不序列化
|
|
if (data == null) return data;
|
|
|
|
// 记录全部数据
|
|
Dictionary<string, object> dataMap = new Dictionary<string, object>();
|
|
|
|
// 查询别名转换
|
|
if (templateInfo.AuxiliaryTableFieldsModelList.Any()) foreach (KeyValuePair<string, object> item in data) dataMap.Add(tableFieldKeyValue[item.Key.ToUpper()], item.Value);
|
|
else dataMap = data;
|
|
|
|
Dictionary<string, object> newDataMap = new Dictionary<string, object>();
|
|
|
|
// 处理子表数据
|
|
newDataMap = await GetChildTableData(templateInfo, link, dataMap, newDataMap, true);
|
|
|
|
int dicCount = newDataMap.Keys.Count;
|
|
string[] strKey = new string[dicCount];
|
|
newDataMap.Keys.CopyTo(strKey, 0);
|
|
for (int i = 0; i < strKey.Length; i++)
|
|
{
|
|
FieldsModel? model = templateInfo.FieldsModelList.Find(m => m.__vModel__ == strKey[i]);
|
|
if (model != null)
|
|
{
|
|
List<Dictionary<string, object>> childModelData = new List<Dictionary<string, object>>();
|
|
foreach (Dictionary<string, object>? item in newDataMap[strKey[i]].ToObject<List<Dictionary<string, object>>>())
|
|
{
|
|
Dictionary<string, object> dic = new Dictionary<string, object>();
|
|
foreach (KeyValuePair<string, object> value in item)
|
|
{
|
|
FieldsModel? child = model.__config__.children.Find(c => c.__vModel__ == value.Key);
|
|
if (child != null && value.Value != null)
|
|
{
|
|
if (child.__config__.jnpfKey.Equals(JnpfKeyConst.DATE))
|
|
{
|
|
var keyValue = value.Value.ToString();
|
|
DateTime dtDate;
|
|
if (DateTime.TryParse(keyValue, out dtDate)) dic.Add(value.Key, keyValue.ParseToDateTime().ParseToUnixTime());
|
|
else dic.Add(value.Key, value.Value.ToString().TimeStampToDateTime().ParseToUnixTime());
|
|
}
|
|
else dic.Add(value.Key, value.Value);
|
|
}
|
|
}
|
|
|
|
childModelData.Add(dic);
|
|
}
|
|
|
|
if (childModelData.Count > 0)
|
|
{
|
|
// 将关键字查询传输的id转换成名称
|
|
if (model.__config__.children.Any(x => x.__config__.templateJson != null && x.__config__.templateJson.Any()))
|
|
newDataMap[strKey[i]] = await _formDataParsing.GetKeyData(model.__config__.children.Where(x => x.__config__.templateJson != null && x.__config__.templateJson.Any()).ToList(), childModelData, templateInfo.ColumnData, "List", 2, "F_Id", dataMap);
|
|
newDataMap[strKey[i]] = await _formDataParsing.GetKeyData(model.__config__.children.Where(x => x.__config__.templateJson == null).ToList(), childModelData, templateInfo.ColumnData.ToObject<ColumnDesignModel>());
|
|
}
|
|
}
|
|
}
|
|
|
|
List<Dictionary<string, object>> listEntity = new List<Dictionary<string, object>>() { dataMap };
|
|
|
|
// 控件联动
|
|
var tempDataMap = new Dictionary<string, object>();
|
|
if (templateInfo.SingleFormData.Any(x => x.__config__.templateJson != null && x.__config__.templateJson.Any()))
|
|
tempDataMap = (await _formDataParsing.GetKeyData(templateInfo.SingleFormData.Where(x => x.__config__.templateJson != null && x.__config__.templateJson.Any()).ToList(), listEntity, templateInfo.ColumnData)).FirstOrDefault();
|
|
tempDataMap = (await _formDataParsing.GetKeyData(templateInfo.SingleFormData.Where(x => x.__config__.templateJson == null).ToList(), listEntity, templateInfo.ColumnData)).FirstOrDefault();
|
|
|
|
// 将关键字查询传输的id转换成名称
|
|
foreach (var entryMap in tempDataMap)
|
|
{
|
|
if (entryMap.Value != null)
|
|
{
|
|
var model = templateInfo.FieldsModelList.Where(m => m.__vModel__.Contains(entryMap.Key)).FirstOrDefault();
|
|
if (model != null && entryMap.Key.Equals(model.__vModel__)) newDataMap[entryMap.Key] = entryMap.Value;
|
|
else if (templateInfo.FieldsModelList.Where(m => m.__vModel__ == entryMap.Key.Replace("_id", string.Empty)).Any()) newDataMap[entryMap.Key] = entryMap.Value;
|
|
}
|
|
}
|
|
|
|
_formDataParsing.GetBARAndQR(templateInfo.FieldsModelList, newDataMap, dataMap); // 处理 条形码 、 二维码 控件
|
|
|
|
if (!newDataMap.ContainsKey("id")) newDataMap.Add("id", id);
|
|
// modified by pf 2023-04-12 返回值不序列化
|
|
return newDataMap;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Post
|
|
|
|
/// <summary>
|
|
/// 创建在线开发功能.
|
|
/// </summary>
|
|
/// <param name="templateEntity">功能模板实体.</param>
|
|
/// <param name="dataInput">数据输入.</param>
|
|
/// <returns></returns>
|
|
public async Task Create(VisualDevEntity templateEntity, VisualDevModelDataCrInput dataInput)
|
|
{
|
|
string? mainId = SnowflakeIdHelper.NextId();
|
|
DbLinkEntity link = await GetDbLink(templateEntity.DbLinkId);
|
|
var haveTableSql = await CreateHaveTableSql(templateEntity, dataInput, mainId);
|
|
|
|
// 主表自增长Id.
|
|
if (haveTableSql.ContainsKey("MainTableReturnIdentity")) haveTableSql.Remove("MainTableReturnIdentity");
|
|
|
|
try
|
|
{
|
|
_db.BeginTran();
|
|
foreach (var item in haveTableSql) await _databaseService.ExecuteSql(link, item.Key, item.Value); // 新增功能数据
|
|
_db.CommitTran();
|
|
}
|
|
catch (Exception)
|
|
{
|
|
_db.RollbackTran();
|
|
throw;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 创建有表SQL.
|
|
/// </summary>
|
|
/// <param name="templateEntity"></param>
|
|
/// <param name="dataInput"></param>
|
|
/// <param name="mainId"></param>
|
|
/// <returns></returns>
|
|
public async Task<Dictionary<string, List<Dictionary<string, object>>>> CreateHaveTableSql(VisualDevEntity templateEntity, VisualDevModelDataCrInput dataInput, string mainId)
|
|
{
|
|
TemplateParsingBase templateInfo = new TemplateParsingBase(templateEntity); // 解析模板控件
|
|
templateInfo.DbLink = await GetDbLink(templateEntity.DbLinkId);
|
|
return await GetCreateSqlByTemplate(templateInfo, dataInput, mainId);
|
|
}
|
|
public async Task<Dictionary<string, List<Dictionary<string, object>>>> GetCreateSqlByTemplate(TemplateParsingBase templateInfo, VisualDevModelDataCrInput dataInput, string mainId)
|
|
{
|
|
// modified by PhilPan 2023-04-12 提交和返回值不序列化
|
|
Dictionary<string, object>? allDataMap = dataInput.data;
|
|
if (!templateInfo.VerifyTemplate()) throw Oops.Oh(ErrorCode.D1401); // 验证模板
|
|
|
|
// 处理系统控件(模板开启行编辑)
|
|
if (templateInfo.ColumnData.type.Equals(4) && _userManager.UserOrigin.Equals("pc"))
|
|
{
|
|
templateInfo.GenerateFields.ForEach(item =>
|
|
{
|
|
if (!allDataMap.ContainsKey(item.__vModel__)) allDataMap.Add(item.__vModel__, string.Empty);
|
|
if (item.__config__.jnpfKey.Equals(JnpfKeyConst.CREATETIME) && allDataMap.ContainsKey(item.__vModel__))
|
|
{
|
|
var value = allDataMap[item.__vModel__].ToString();
|
|
allDataMap.Remove(item.__vModel__);
|
|
allDataMap.Add(item.__vModel__, DateTime.Now.ToString());
|
|
}
|
|
});
|
|
}
|
|
|
|
allDataMap = await GenerateFeilds(templateInfo.FieldsModelList.ToJsonString(), allDataMap, true); // 生成系统自动生成字段
|
|
DbLinkEntity link = templateInfo.DbLink;
|
|
|
|
List<DbTableFieldModel>? tableList = _databaseService.GetFieldList(link, templateInfo.MainTableName); // 获取主表 表结构 信息
|
|
DbTableFieldModel? mainPrimary = tableList.Find(t => t.primaryKey); // 主表主键
|
|
string? dbType = link?.DbType != null ? link.DbType : _visualDevRepository.AsSugarClient().CurrentConnectionConfig.DbType.ToString();
|
|
|
|
// 验证唯一值
|
|
UniqueVerify(link, templateInfo, allDataMap, mainPrimary?.field, mainId, false);
|
|
|
|
// 新增SQL
|
|
Dictionary<string, List<Dictionary<string, object>>> dictionarySql = new Dictionary<string, List<Dictionary<string, object>>>();
|
|
var tableField = new Dictionary<string, object>(); // 字段和值
|
|
templateInfo?.MainTableFieldsModelList.ForEach(item =>
|
|
{
|
|
if (allDataMap.ContainsKey(item.__vModel__))
|
|
{
|
|
object? itemData = allDataMap[item.__vModel__];
|
|
if (item.__vModel__.IsNotEmptyOrNull() && itemData != null && !string.IsNullOrEmpty(itemData.ToString()) && itemData.ToString() != "[]")
|
|
{
|
|
var value = _formDataParsing.InsertValueHandle(dbType, tableList, item.__vModel__, itemData, templateInfo.MainTableFieldsModelList);
|
|
tableField.Add(item.__vModel__, value);
|
|
}
|
|
}
|
|
});
|
|
|
|
// 主键策略(雪花Id)
|
|
if (templateInfo.FormModel.primaryKeyPolicy.Equals(1)) tableField.Add(mainPrimary?.field, mainId);
|
|
|
|
// 前端空提交
|
|
if (!tableField.Any()) tableField.Add(tableList.Where(x => !x.primaryKey).First().field, null);
|
|
|
|
// 拼接主表 sql
|
|
dictionarySql.Add(templateInfo.MainTableName, new List<Dictionary<string, object>>() { tableField });
|
|
|
|
// 流程表单 需要增加字段 F_FlowTaskId
|
|
if (templateInfo.visualDevEntity != null && templateInfo.visualDevEntity.EnableFlow.Equals(1))
|
|
{
|
|
if (!tableList.Any(x => SqlFunc.ToLower(x.field) == "f_flowtaskid"))
|
|
{
|
|
List<DbTableFieldModel>? pFieldList = new List<DbTableFieldModel>() { new DbTableFieldModel() { field = "F_FlowTaskId", fieldName = "流程Id", dataType = "varchar", dataLength = "50", allowNull = 1 } };
|
|
_databaseService.AddTableColumn(link, templateInfo.MainTableName, pFieldList);
|
|
}
|
|
|
|
dictionarySql[templateInfo.MainTableName].First().Add("F_FlowTaskId", mainId);
|
|
}
|
|
|
|
// 自增长主键 需要返回的自增id
|
|
if (templateInfo.FormModel.primaryKeyPolicy.Equals(2))
|
|
{
|
|
var mainSql = dictionarySql.First();
|
|
mainId = (await _databaseService.ExecuteReturnIdentityAsync(link, mainSql.Key, mainSql.Value)).ToString();
|
|
if (mainId.Equals("0")) throw Oops.Oh(ErrorCode.D1402);
|
|
tableField.Clear();
|
|
dictionarySql.Clear();
|
|
tableField.Add("ReturnIdentity", mainId);
|
|
dictionarySql.Add("MainTableReturnIdentity", new List<Dictionary<string, object>>() { tableField });
|
|
}
|
|
|
|
// 拼接副表 sql
|
|
if (templateInfo.AuxiliaryTableFieldsModelList.Any())
|
|
{
|
|
templateInfo.AuxiliaryTableFieldsModelList.Select(x => x.__config__.tableName).Distinct().ToList().ForEach(tbname =>
|
|
{
|
|
tableField = new Dictionary<string, object>();
|
|
|
|
// 主键策略(雪花Id)
|
|
if (templateInfo.FormModel.primaryKeyPolicy.Equals(1))
|
|
tableField.Add(_databaseService.GetFieldList(link, tbname)?.Find(x => x.primaryKey).field, SnowflakeIdHelper.NextId());
|
|
|
|
// 外键
|
|
tableField.Add(templateInfo?.AllTable?.Find(t => t.table == tbname).tableField, mainId);
|
|
|
|
// 字段
|
|
templateInfo.AuxiliaryTableFieldsModelList.Select(x => x.__vModel__).Where(x => x.Contains("jnpf_" + tbname + "_jnpf_")).ToList().ForEach(item =>
|
|
{
|
|
object? itemData = allDataMap.Where(x => x.Key == item).Count() > 0 ? allDataMap[item] : null;
|
|
if (item.IsNotEmptyOrNull() && itemData != null && !string.IsNullOrEmpty(itemData.ToString()) && itemData.ToString() != "[]")
|
|
{
|
|
var value = _formDataParsing.InsertValueHandle(dbType, tableList, item, allDataMap[item], templateInfo.FieldsModelList);
|
|
tableField.Add(item.ReplaceRegex(@"(\w+)_jnpf_", string.Empty), value);
|
|
}
|
|
});
|
|
|
|
dictionarySql.Add(tbname, new List<Dictionary<string, object>>() { tableField });
|
|
});
|
|
}
|
|
|
|
// 拼接子表 sql
|
|
foreach (string? item in allDataMap.Where(d => d.Key.ToLower().Contains("tablefield")).Select(d => d.Key).ToList())
|
|
{
|
|
if (!templateInfo.AllFieldsModel.Any(x => x.__vModel__.Equals(item)) || !templateInfo.AllFieldsModel.Find(x => x.__vModel__.Equals(item)).__config__.jnpfKey.Equals(JnpfKeyConst.TABLE)) continue;
|
|
|
|
// 查找到该控件数据
|
|
object? objectData = allDataMap[item];
|
|
List<Dictionary<string, object>>? model = objectData.ToObject<List<Dictionary<string, object>>>();
|
|
if (model != null && model.Count > 0)
|
|
{
|
|
// 利用key去找模板
|
|
FieldsModel? fieldsModel = templateInfo.FieldsModelList.Find(f => f.__vModel__ == item);
|
|
Engine.Model.TableModel? childTable = templateInfo.AllTable.Find(t => t.table == fieldsModel.__config__.tableName);
|
|
tableList = new List<DbTableFieldModel>();
|
|
tableList = _databaseService.GetFieldList(link, childTable?.table);
|
|
DbTableFieldModel? childPrimary = tableList.Find(t => t.primaryKey);
|
|
foreach (Dictionary<string, object>? data in model)
|
|
{
|
|
if (data.Count > 0)
|
|
{
|
|
tableField = new Dictionary<string, object>();
|
|
|
|
// 主键策略(雪花Id)
|
|
if (templateInfo.FormModel.primaryKeyPolicy.Equals(1)) tableField.Add(childPrimary.field, SnowflakeIdHelper.NextId());
|
|
|
|
// 外键
|
|
tableField.Add(childTable.tableField, mainId);
|
|
|
|
// 字段
|
|
foreach (KeyValuePair<string, object> child in data)
|
|
{
|
|
if (child.Key.IsNotEmptyOrNull() && child.Value != null && child.Value.ToString() != "[]" && child.Value.ToString() != string.Empty)
|
|
{
|
|
var value = _formDataParsing.InsertValueHandle(dbType, tableList, child.Key, child.Value, fieldsModel?.__config__.children);
|
|
tableField.Add(child.Key, value);
|
|
}
|
|
}
|
|
|
|
if (dictionarySql.ContainsKey(fieldsModel.__config__.tableName))
|
|
dictionarySql[fieldsModel.__config__.tableName].Add(tableField);
|
|
else
|
|
dictionarySql.Add(fieldsModel.__config__.tableName, new List<Dictionary<string, object>>() { tableField });
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// 处理 开启 并发锁定
|
|
await OptimisticLocking(link, templateInfo);
|
|
return dictionarySql;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 修改在线开发功能.
|
|
/// </summary>
|
|
/// <param name="id">修改ID.</param>
|
|
/// <param name="templateEntity"></param>
|
|
/// <param name="visualdevModelDataUpForm"></param>
|
|
/// <returns></returns>
|
|
public async Task Update(string id, VisualDevEntity templateEntity, VisualDevModelDataUpInput visualdevModelDataUpForm)
|
|
{
|
|
TemplateParsingBase templateInfo = new TemplateParsingBase(templateEntity); // 解析模板控件
|
|
if (templateInfo.ColumnData.type.Equals(4) && _userManager.UserOrigin.Equals("pc"))
|
|
{
|
|
// 剔除 [增加前端回显字段 : key_name]
|
|
// modified by PhilPan 2023-04-12 提交和返回值不序列化
|
|
Dictionary<string, object> oldDataMap = visualdevModelDataUpForm.data;
|
|
Dictionary<string, object> newDataMap = new Dictionary<string, object>();
|
|
foreach (var item in oldDataMap)
|
|
{
|
|
var key = item.Key.Replace("_name", string.Empty);
|
|
if (!newDataMap.ContainsKey(key)) newDataMap.Add(key, oldDataMap[key]);
|
|
}
|
|
// modified by PhilPan 2023-04-12 提交和返回值不序列化
|
|
if (newDataMap.Any()) visualdevModelDataUpForm.data = newDataMap;
|
|
}
|
|
|
|
DbLinkEntity link = await GetDbLink(templateEntity.DbLinkId);
|
|
var haveTableSql = await UpdateHaveTableSql(templateEntity, visualdevModelDataUpForm, id);
|
|
|
|
try
|
|
{
|
|
_db.BeginTran();
|
|
foreach (var item in haveTableSql) await _databaseService.ExecuteSql(link, item); // 修改功能数据
|
|
_db.CommitTran();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_db.RollbackTran();
|
|
throw;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 修改有表SQL.
|
|
/// </summary>
|
|
/// <param name="templateEntity"></param>
|
|
/// <param name="visualdevModelDataUpForm"></param>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
public async Task<List<string>> UpdateHaveTableSql(VisualDevEntity templateEntity, VisualDevModelDataUpInput visualdevModelDataUpForm, string id)
|
|
{
|
|
TemplateParsingBase templateInfo = new TemplateParsingBase(templateEntity); // 解析模板控件
|
|
templateInfo.DbLink = await GetDbLink(templateEntity.DbLinkId);
|
|
return await GetUpdateSqlByTemplate(templateInfo, visualdevModelDataUpForm, id);
|
|
}
|
|
public async Task<List<string>> GetUpdateSqlByTemplate(TemplateParsingBase templateInfo, VisualDevModelDataUpInput visualdevModelDataUpForm, string id)
|
|
{
|
|
// modified by PhilPan 2023-04-12 提交和返回值不序列化
|
|
Dictionary<string, object>? allDataMap = visualdevModelDataUpForm.data;
|
|
if (!templateInfo.VerifyTemplate()) throw Oops.Oh(ErrorCode.D1401); // 验证模板
|
|
|
|
// 处理系统控件(模板开启行编辑)
|
|
if (_userManager.UserOrigin.Equals("pc") && templateInfo.ColumnData.type.Equals(4))
|
|
{
|
|
// 处理显示列和提交的表单数据匹配(行编辑空数据 前端会过滤该控件)
|
|
templateInfo.ColumnData.columnList.Where(x => !allDataMap.ContainsKey(x.prop) && x.__config__.visibility.Equals("pc")).ToList()
|
|
.ForEach(item => allDataMap.Add(item.prop, string.Empty));
|
|
|
|
templateInfo.GenerateFields.ForEach(item =>
|
|
{
|
|
if (!allDataMap.ContainsKey(item.__vModel__)) allDataMap.Add(item.__vModel__, string.Empty);
|
|
if (item.__config__.jnpfKey.Equals(JnpfKeyConst.CREATETIME) && allDataMap.ContainsKey(item.__vModel__))
|
|
{
|
|
var value = allDataMap[item.__vModel__].ToString();
|
|
allDataMap.Remove(item.__vModel__);
|
|
DateTime dtDate;
|
|
if (DateTime.TryParse(value, out dtDate)) value = string.Format("{0:yyyy-MM-dd HH:mm:ss} ", value);
|
|
else value = string.Format("{0:yyyy-MM-dd HH:mm:ss} ", value.TimeStampToDateTime());
|
|
allDataMap.Add(item.__vModel__, value);
|
|
}
|
|
});
|
|
}
|
|
|
|
allDataMap = await GenerateFeilds(templateInfo.FieldsModelList.ToJsonString(), allDataMap, false); // 生成系统自动生成字段
|
|
DbLinkEntity link = templateInfo.DbLink;
|
|
List<DbTableFieldModel>? tableList = _databaseService.GetFieldList(link, templateInfo.MainTableName); // 获取主表 表结构 信息
|
|
DbTableFieldModel? mainPrimary = tableList.Find(t => t.primaryKey); // 主表主键
|
|
string? dbType = link?.DbType != null ? link.DbType : _visualDevRepository.AsSugarClient().CurrentConnectionConfig.DbType.ToString();
|
|
id = GetPIdsByFlowIds(link, templateInfo, mainPrimary.field, new List<string>() { id }).First().Value;
|
|
|
|
// 验证唯一值
|
|
UniqueVerify(link, templateInfo, allDataMap, mainPrimary?.field, id, true);
|
|
|
|
// 主表查询语句
|
|
List<string> mainSql = new List<string>();
|
|
var fieldSql = new List<string>(); // key 字段名, value 修改值
|
|
|
|
// 拼接主表 sql
|
|
templateInfo?.MainTableFieldsModelList.ForEach(item =>
|
|
{
|
|
if (item.__vModel__.IsNotEmptyOrNull() && allDataMap.ContainsKey(item.__vModel__))
|
|
fieldSql.Add(string.Format("{0}={1}", item.__vModel__, _formDataParsing.InsertValueHandle(dbType, tableList, item.__vModel__, allDataMap[item.__vModel__], templateInfo.MainTableFieldsModelList, "update")));
|
|
});
|
|
|
|
if (fieldSql.Any()) mainSql.Add(string.Format("update {0} set {1} where {2}='{3}';", templateInfo?.MainTableName, string.Join(",", fieldSql), mainPrimary?.field, id));
|
|
|
|
// 拼接副表 sql
|
|
if (templateInfo.AuxiliaryTableFieldsModelList.Any())
|
|
{
|
|
templateInfo.AuxiliaryTableFieldsModelList.Select(x => x.__config__.tableName).Distinct().ToList().ForEach(tbname =>
|
|
{
|
|
List<DbTableFieldModel>? tableAllField = _databaseService.GetFieldList(link, tbname); // 数据库里获取表的所有字段
|
|
|
|
List<string>? tableFieldList = templateInfo.AuxiliaryTableFieldsModelList.Where(x => x.__config__.tableName.Equals(tbname)).Select(x => x.__vModel__).ToList();
|
|
|
|
fieldSql.Clear(); // key 字段名, value 修改值
|
|
templateInfo.AuxiliaryTableFieldsModelList.Where(x => x.__config__.tableName.Equals(tbname)).Select(x => x.__vModel__).ToList().ForEach(item =>
|
|
{
|
|
// 前端未填写数据的字段,默认会找不到字段名,需要验证
|
|
object? itemData = allDataMap.Where(x => x.Key == item).Count() > 0 ? allDataMap[item] : null;
|
|
if (item.IsNotEmptyOrNull() && itemData != null)
|
|
fieldSql.Add(string.Format("{0}={1}", item.ReplaceRegex(@"(\w+)_jnpf_", string.Empty), _formDataParsing.InsertValueHandle(dbType, tableList, item, allDataMap[item], templateInfo.FieldsModelList, "update")));
|
|
});
|
|
|
|
var tableField = templateInfo.AllTable.Find(t => t.table.Equals(tbname)).tableField;
|
|
if (fieldSql.Any()) mainSql.Add(string.Format("update {0} set {1} where {2}='{3}';", tbname, string.Join(",", fieldSql), tableField, id));
|
|
});
|
|
}
|
|
|
|
// 非行编辑
|
|
if (!templateInfo.ColumnData.type.Equals(4))
|
|
{
|
|
// 删除子表数据
|
|
if (templateInfo.AllTable.Any(x => x.typeId.Equals("0")))
|
|
{
|
|
templateInfo.AllTable.Where(x => x.typeId.Equals("0") && templateInfo.ChildTableFieldsModelList.Select(x => x.__config__.tableName).Contains(x.table))
|
|
.ToList().ForEach(item => mainSql.Add(string.Format("delete from {0} where {1}='{2}';", item.table, item.tableField, id))); // 删除所有涉及子表数据 sql
|
|
|
|
// 拼接子表 sql
|
|
foreach (string? item in allDataMap.Where(d => d.Key.ToLower().Contains("tablefield")).Select(d => d.Key).ToList())
|
|
{
|
|
if (!templateInfo.AllFieldsModel.Any(x => x.__vModel__.Equals(item)) || !templateInfo.AllFieldsModel.Find(x => x.__vModel__.Equals(item)).__config__.jnpfKey.Equals(JnpfKeyConst.TABLE)) continue;
|
|
|
|
// 查找到该控件数据
|
|
object? objectData = allDataMap[item];
|
|
List<Dictionary<string, object>>? model = objectData.ToObject<List<Dictionary<string, object>>>();
|
|
if (model != null && model.Count > 0)
|
|
{
|
|
// 利用key去找模板
|
|
FieldsModel? fieldsModel = templateInfo.FieldsModelList.Find(f => f.__vModel__ == item);
|
|
ConfigModel? fieldsConfig = fieldsModel?.__config__;
|
|
StringBuilder childColumn = new StringBuilder();
|
|
List<object>? childValues = new List<object>();
|
|
Engine.Model.TableModel? childTable = templateInfo.AllTable.Find(t => t.table == fieldsModel.__config__.tableName && t.table != templateInfo.MainTableName);
|
|
if (childTable != null)
|
|
{
|
|
tableList = new List<DbTableFieldModel>();
|
|
tableList = _databaseService.GetFieldList(link, childTable?.table);
|
|
DbTableFieldModel? childPrimary = tableList.Find(t => t.primaryKey);
|
|
foreach (Dictionary<string, object>? data in model)
|
|
{
|
|
if (data.Count > 0)
|
|
{
|
|
foreach (KeyValuePair<string, object> child in data)
|
|
{
|
|
if (child.Key.IsNotEmptyOrNull() && child.Value != null && child.Value.ToString() != "[]" && child.Value.ToString() != string.Empty)
|
|
{
|
|
childColumn.AppendFormat("{0},", child.Key); // Column部分
|
|
childValues.Add(_formDataParsing.InsertValueHandle(dbType, tableList, child.Key, child.Value, fieldsConfig.children, "update")); // Values部分
|
|
}
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(childColumn.ToString()))
|
|
{
|
|
// 主键策略(雪花Id)
|
|
if (templateInfo.FormModel.primaryKeyPolicy.Equals(1))
|
|
{
|
|
mainSql.Add(string.Format(
|
|
"insert into {0}({6},{4},{1}) values('{3}','{5}',{2});",
|
|
fieldsModel.__config__.tableName,
|
|
childColumn.ToString().Trim(','),
|
|
string.Join(",", childValues),
|
|
SnowflakeIdHelper.NextId(),
|
|
childTable.tableField,
|
|
id,
|
|
childPrimary.field));
|
|
}
|
|
else
|
|
{
|
|
mainSql.Add(string.Format(
|
|
"insert into {0}({1},{2}) values('{3}',{4});",
|
|
fieldsModel.__config__.tableName,
|
|
childTable.tableField,
|
|
childColumn.ToString().Trim(','),
|
|
id,
|
|
string.Join(",", childValues)));
|
|
}
|
|
}
|
|
|
|
childColumn = new StringBuilder();
|
|
childValues = new List<object>();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// 处理 开启 并发锁定
|
|
await OptimisticLocking(link, templateInfo, mainSql, allDataMap);
|
|
|
|
return mainSql;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 流程表单模块
|
|
|
|
/// <summary>
|
|
/// 添加、修改 流程表单数据.
|
|
/// </summary>
|
|
/// <param name="fEntity">表单模板.</param>
|
|
/// <param name="formData">表单数据json.</param>
|
|
/// <param name="dataId">主键Id.</param>
|
|
/// <param name="isUpdate">是否修改.</param>
|
|
/// <returns></returns>
|
|
public async Task SaveFlowFormData(FlowFormEntity fEntity, string formData, string dataId, bool isUpdate = false)
|
|
{
|
|
if (fEntity != null)
|
|
{
|
|
// 自定义表单
|
|
if (fEntity.FormType.Equals(2))
|
|
{
|
|
var vEntity = new VisualDevEntity() { FormData = fEntity.PropertyJson, Tables = fEntity.TableJson, WebType = 2, FullName = fEntity.FullName, FlowId = fEntity.FlowId, EnableFlow = 1 };
|
|
var tInfo = new TemplateParsingBase(vEntity, true);
|
|
tInfo.DbLink = await GetDbLink(fEntity.DbLinkId);
|
|
if (isUpdate)
|
|
{
|
|
// modified by PhilPan 2023-04-12 提交和返回值不序列化
|
|
var sqlList = await GetUpdateSqlByTemplate(tInfo, new VisualDevModelDataUpInput() { data = formData.ToObject<Dictionary<string, object>>() }, dataId);
|
|
foreach (var item in sqlList) await _databaseService.ExecuteSql(tInfo.DbLink, item); // 修改功能数据
|
|
}
|
|
else
|
|
{
|
|
// modified by PhilPan 2023-04-12 提交和返回值不序列化
|
|
var sqlList = await GetCreateSqlByTemplate(tInfo, new VisualDevModelDataUpInput() { data = formData.ToObject<Dictionary<string, object>>() }, dataId);
|
|
|
|
// 主表自增长Id.
|
|
if (sqlList.ContainsKey("MainTableReturnIdentity")) sqlList.Remove("MainTableReturnIdentity");
|
|
foreach (var item in sqlList) await _databaseService.ExecuteSql(tInfo.DbLink, item.Key, item.Value); // 新增功能数据
|
|
}
|
|
}
|
|
else if (fEntity.FormType.Equals(1))
|
|
{
|
|
// 新增,修改
|
|
var dic = formData.ToObject<Dictionary<string, object>>();
|
|
var dicHerader = new Dictionary<string, object>();
|
|
dicHerader.Add("JNPF_API", true);
|
|
if (_userManager.ToKen != null && !_userManager.ToKen.Contains("::"))
|
|
dicHerader.Add("Authorization", _userManager.ToKen);
|
|
|
|
// 本地url地址
|
|
var localAddress = App.Configuration["Kestrel:Endpoints:Http:Url"];
|
|
|
|
// 请求地址拼接
|
|
var path = string.Format("{0}/{1}/{2}", localAddress, fEntity.InterfaceUrl, dataId);
|
|
try
|
|
{
|
|
await path.SetJsonSerialization<NewtonsoftJsonSerializerProvider>().SetContentType("application/json").SetHeaders(dicHerader).SetBody(dic).PostAsStringAsync();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取流程表单数据解析详情.
|
|
/// </summary>
|
|
/// <param name="fId">表单模板id.</param>
|
|
/// <param name="dataId">主键Id.</param>
|
|
/// <returns></returns>
|
|
public async Task<Dictionary<string, object>> GetFlowFormDataDetails(string fId, string dataId)
|
|
{
|
|
var fEntity = await _visualDevRepository.AsSugarClient().Queryable<FlowFormEntity>().FirstAsync(x => x.Id.Equals(fId));
|
|
if (fEntity == null) return new Dictionary<string, object>();
|
|
var vEntity = new VisualDevEntity() { FormData = fEntity.PropertyJson, Tables = fEntity.TableJson, FlowId = fEntity.FlowId, EnableFlow = 1, WebType = 3, FullName = fEntity.FullName, DbLinkId = fEntity.DbLinkId };
|
|
if (fEntity.FormType.Equals(1))
|
|
{
|
|
var res = new Dictionary<string, object>();
|
|
// 获取详情
|
|
var dicHerader = new Dictionary<string, object>();
|
|
dicHerader.Add("JNPF_API", true);
|
|
if (_userManager.ToKen != null && !_userManager.ToKen.Contains("::"))
|
|
dicHerader.Add("Authorization", _userManager.ToKen);
|
|
|
|
// 本地url地址
|
|
var localAddress = App.Configuration["Kestrel:Endpoints:Http:Url"];
|
|
|
|
// 请求地址拼接
|
|
var path = string.Format("{0}/{1}/{2}", localAddress, fEntity.InterfaceUrl, dataId);
|
|
try
|
|
{
|
|
var dataStr = await path.SetHeaders(dicHerader).GetAsStringAsync();
|
|
return dataStr.ToObject<Dictionary<string, object>>();
|
|
}
|
|
catch (Exception)
|
|
{
|
|
}
|
|
return res;
|
|
}
|
|
else
|
|
{
|
|
return (await GetHaveTableInfo(dataId, vEntity));
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 流程表单数据传递.
|
|
/// </summary>
|
|
/// <param name="oldFId">旧表单模板Id.</param>
|
|
/// <param name="newFId">传递表单模板Id.</param>
|
|
/// <param name="mapRule">映射规则字段 : Key 原字段, Value 映射字段.</param>
|
|
/// <param name="formData">表单数据.</param>
|
|
/// <param name="isSubFlow">是否子流程.</param>
|
|
public async Task<Dictionary<string, object>> SaveDataToDataByFId(string oldFId, string newFId, List<Dictionary<string, string>> mapRule, Dictionary<string, object> formData, bool isSubFlow = false)
|
|
{
|
|
// if (oldFId.Equals(newFId)) return formData; // 新旧一致.
|
|
var oldFEntity = await _visualDevRepository.AsSugarClient().Queryable<FlowFormEntity>().FirstAsync(x => x.Id.Equals(oldFId));
|
|
var newFEntity = await _visualDevRepository.AsSugarClient().Queryable<FlowFormEntity>().FirstAsync(x => x.Id.Equals(newFId));
|
|
if (oldFEntity == null || newFEntity == null) throw Oops.Oh(ErrorCode.WF0039); // 未找到流程表单模板
|
|
var oldTInfo = new TemplateParsingBase(oldFEntity.PropertyJson, oldFEntity.TableJson, (int)oldFEntity.FormType); // 旧模板
|
|
var newTInfo = new TemplateParsingBase(newFEntity.PropertyJson, newFEntity.TableJson, (int)newFEntity.FormType); // 新模板
|
|
|
|
if (oldFEntity.FormType.Equals(1) || newFEntity.FormType.Equals(1))
|
|
{
|
|
oldTInfo.AllFieldsModel.ForEach(it =>
|
|
{
|
|
if (!it.__config__.jnpfKey.Equals(JnpfKeyConst.MODIFYTIME) && !it.__config__.jnpfKey.Equals(JnpfKeyConst.MODIFYUSER) && !it.__config__.jnpfKey.Equals(JnpfKeyConst.TABLE))
|
|
it.__config__.jnpfKey = JnpfKeyConst.COMINPUT;
|
|
});
|
|
newTInfo.AllFieldsModel.ForEach(it =>
|
|
{
|
|
if (!it.__config__.jnpfKey.Equals(JnpfKeyConst.MODIFYTIME) && !it.__config__.jnpfKey.Equals(JnpfKeyConst.MODIFYUSER) && !it.__config__.jnpfKey.Equals(JnpfKeyConst.TABLE))
|
|
it.__config__.jnpfKey = JnpfKeyConst.COMINPUT;
|
|
});
|
|
}
|
|
|
|
var newMapRule = new Dictionary<string, string>();
|
|
oldTInfo.AllFieldsModel.ForEach(item =>
|
|
{
|
|
if (newTInfo.AllFieldsModel.Any(x => x.__vModel__.Equals(item.__vModel__) && x.multiple.Equals(item.multiple))
|
|
&& item.__vModel__.IsNotEmptyOrNull()
|
|
&& !newMapRule.ContainsKey(item.__vModel__))
|
|
newMapRule.Add(item.__vModel__, item.__vModel__);
|
|
});
|
|
if (mapRule.IsNullOrEmpty()) mapRule = new List<Dictionary<string, string>>();
|
|
foreach (var item in newMapRule)
|
|
{
|
|
if (!mapRule.Any(x => x.Any(xx => xx.Value.Equals(item.Value)) || x.Any(xx => xx.Key.Equals(item.Key)))) mapRule.Add(new Dictionary<string, string>() { { item.Key, item.Value } });
|
|
}
|
|
|
|
var res = new Dictionary<string, object>();
|
|
var childTableSplitKey = "tablefield";
|
|
|
|
// 三个特殊的系统表单 (请假申请、销售订单、订单示例)
|
|
if (oldFEntity.EnCode.Equals("leaveApply") || oldFEntity.EnCode.Equals("salesOrder") || oldFEntity.EnCode.Equals("crmOrder") ||
|
|
newFEntity.EnCode.Equals("leaveApply") || newFEntity.EnCode.Equals("salesOrder") || newFEntity.EnCode.Equals("crmOrder"))
|
|
{
|
|
childTableSplitKey = "-";
|
|
}
|
|
|
|
foreach (var items in mapRule)
|
|
{
|
|
var item = items.First();
|
|
var oldModel = oldTInfo.AllFieldsModel.Find(x => x.__vModel__.Equals(item.Key));
|
|
var newModel = newTInfo.AllFieldsModel.Find(x => x.__vModel__.Equals(item.Value));
|
|
if (oldModel == null || newModel == null || oldModel.__config__.jnpfKey.Equals(JnpfKeyConst.MODIFYTIME) || oldModel.__config__.jnpfKey.Equals(JnpfKeyConst.MODIFYUSER))
|
|
{
|
|
formData[item.Key] = string.Empty; // 找不到 默认赋予 空字符串
|
|
continue;
|
|
}
|
|
if (oldModel.__config__.jnpfKey.Equals(JnpfKeyConst.TABLE) || newModel.__config__.jnpfKey.Equals(JnpfKeyConst.TABLE))
|
|
continue;
|
|
|
|
// 子表字段 - 子表字段
|
|
if ((oldModel.__vModel__.ToLower().Contains(childTableSplitKey) && newModel.__vModel__.ToLower().Contains(childTableSplitKey)))
|
|
{
|
|
if (DataTransferVerify(oldModel, newModel))
|
|
{
|
|
var oldCTable = oldModel.__vModel__.Split("-").First();
|
|
var oldCField = oldModel.__vModel__.Split("-").Last();
|
|
var newCTable = newModel.__vModel__.Split("-").First();
|
|
var newCField = newModel.__vModel__.Split("-").Last();
|
|
if (oldCField.IsNullOrWhiteSpace() || newCField.IsNullOrWhiteSpace()) continue;
|
|
|
|
if (!formData.ContainsKey(newCTable)) formData.Add(newCTable, new List<Dictionary<string, object>>());
|
|
if (formData.ContainsKey(oldCTable) && formData[oldCTable] != null && formData[oldCTable].ToString() != "[]")
|
|
{
|
|
var oldCTData = formData[oldCTable].ToObject<List<Dictionary<string, object>>>();
|
|
var newCTData = formData.ContainsKey(newCTable) ? formData[newCTable].ToObject<List<Dictionary<string, object>>>() : new List<Dictionary<string, object>>();
|
|
|
|
for (var i = 0; i < oldCTData.Count; i++)
|
|
{
|
|
if (oldCTData[i].ContainsKey(oldCField))
|
|
{
|
|
if (newCTData.Count > i) newCTData[i][newCField] = oldCTData[i][oldCField];
|
|
else newCTData.Add(new Dictionary<string, object>() { { newCField, oldCTData[i][oldCField] } });
|
|
}
|
|
}
|
|
formData[newCTable] = newCTData;
|
|
}
|
|
}
|
|
}
|
|
else if (oldModel.__vModel__.ToLower().Contains(childTableSplitKey) || newModel.__vModel__.ToLower().Contains(childTableSplitKey))
|
|
{
|
|
if (DataTransferVerify(oldModel, newModel))
|
|
{
|
|
// 子表字段 - 非子表字段
|
|
// 传递规则:默认选用上节点的第一条子表数据赋值到下节点的非子表字段内
|
|
if (oldModel.__vModel__.ToLower().Contains(childTableSplitKey) && !newModel.__vModel__.ToLower().Contains(childTableSplitKey))
|
|
{
|
|
var childTable = oldModel.__vModel__.Split("-").First();
|
|
var childField = oldModel.__vModel__.Split("-").Last();
|
|
var childTableData = formData[childTable].ToObject<List<Dictionary<string, object>>>();
|
|
if (childTableData.Any() && childTableData.Any(x => x.ContainsKey(childField)))
|
|
{
|
|
if (formData.ContainsKey(oldModel.__vModel__)) formData[oldModel.__vModel__] = childTableData.First()[childField];
|
|
else formData.Add(oldModel.__vModel__, childTableData.First()[childField]);
|
|
}
|
|
}
|
|
|
|
// 非子表字段 - 子表字段
|
|
// 传递规则:下节点子表新增一行将上节点字段赋值进去
|
|
if (!oldModel.__vModel__.ToLower().Contains(childTableSplitKey) && newModel.__vModel__.ToLower().Contains(childTableSplitKey))
|
|
{
|
|
var childKey = newModel.__vModel__.Split("-");
|
|
var childTableKey = childKey.First();
|
|
|
|
var childFieldKey = childKey.Last();
|
|
if (formData.ContainsKey(oldModel.__vModel__))
|
|
{
|
|
var childFieldValue = formData[oldModel.__vModel__];
|
|
|
|
if (!formData.ContainsKey(childTableKey)) formData.Add(childTableKey, new List<Dictionary<string, object>>());
|
|
|
|
var childItems = formData[childTableKey].ToObject<List<Dictionary<string, object>>>();
|
|
if (!childItems.Any())
|
|
{
|
|
childItems.Add(new Dictionary<string, object>() { { childFieldKey, childFieldValue } });
|
|
}
|
|
else
|
|
{
|
|
if (childItems.Any(x => x.ContainsKey(childFieldKey))) childItems.First()[childFieldKey] = childFieldValue;
|
|
else childItems.First().Add(childFieldKey, childFieldValue);
|
|
}
|
|
|
|
formData[childTableKey] = childItems;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// 三个特殊的系统表单,不做验证规则
|
|
if (!childTableSplitKey.Equals("-") && !DataTransferVerify(oldModel, newModel)) formData[oldModel.__vModel__] = null;
|
|
}
|
|
}
|
|
|
|
foreach (var dicItems in mapRule)
|
|
{
|
|
var dicItem = dicItems.First();
|
|
if (formData.ContainsKey(dicItem.Key) && dicItem.Value.IsNotEmptyOrNull())
|
|
{
|
|
var itemValue = formData.First(x => x.Key.Equals(dicItem.Key)).Value;
|
|
if (!res.ContainsKey(dicItem.Value)) res.Add(dicItem.Value, itemValue);
|
|
}
|
|
if (dicItem.Value.ToLower().Contains(childTableSplitKey))
|
|
{
|
|
var cTableKey = dicItem.Value.Split("-").First();
|
|
var itemValue = formData.First(x => x.Key.Equals(cTableKey));
|
|
if (!res.ContainsKey(cTableKey)) res.Add(itemValue.Key, itemValue.Value);
|
|
}
|
|
}
|
|
|
|
// 系统表单 直接请求接口.
|
|
if (newFEntity.FormType.Equals(1))
|
|
{
|
|
if (!isSubFlow)
|
|
{
|
|
// 新增,修改
|
|
var dic = formData.ToObject<Dictionary<string, object>>();
|
|
var dicHerader = new Dictionary<string, object>();
|
|
dicHerader.Add("JNPF_API", true);
|
|
if (_userManager.ToKen != null && !_userManager.ToKen.Contains("::"))
|
|
dicHerader.Add("Authorization", _userManager.ToKen);
|
|
|
|
// 本地url地址
|
|
var localAddress = App.Configuration["Kestrel:Endpoints:Http:Url"];
|
|
|
|
// 请求地址拼接
|
|
var path = string.Format("{0}/{1}/{2}", localAddress, newFEntity.InterfaceUrl, formData["id"].ToString());
|
|
try
|
|
{
|
|
await path.SetJsonSerialization<NewtonsoftJsonSerializerProvider>().SetContentType("application/json").SetHeaders(dicHerader).SetBody(dic).PostAsStringAsync();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
}
|
|
}
|
|
res["id"] = formData["id"];
|
|
return res;
|
|
}
|
|
|
|
// 获取请求端类型,并对应获取 数据权限
|
|
DbLinkEntity link = await GetDbLink(newFEntity.DbLinkId);
|
|
newTInfo.DbLink = link;
|
|
List<DbTableFieldModel>? tableList = _databaseService.GetFieldList(link, newTInfo.MainTableName); // 获取主表 表结构 信息
|
|
newTInfo.MainPrimary = tableList.Find(t => t.primaryKey).field;
|
|
if (!tableList.Any(x => SqlFunc.ToLower(x.field) == "f_flowtaskid"))
|
|
{
|
|
List<DbTableFieldModel>? pFieldList = new List<DbTableFieldModel>() { new DbTableFieldModel() { field = "F_FlowTaskId", fieldName = "流程Id", dataType = "varchar", dataLength = "50", allowNull = 1 } };
|
|
_databaseService.AddTableColumn(link, newTInfo.MainTableName, pFieldList);
|
|
}
|
|
|
|
var sqlFormat = "select {0},{1} from {2} where F_FlowTaskId='{3}';";
|
|
if (newTInfo.FormModel.primaryKeyPolicy.Equals(2)) sqlFormat = "select {0},{1} from {2} where {1}={3};";
|
|
var isUpdate = false;
|
|
|
|
// 处理数据传递 乐观锁 场景.
|
|
if (newTInfo.FormModel.concurrencyLock)
|
|
{
|
|
var sql = string.Format(sqlFormat, "F_Version", newTInfo.MainPrimary, newTInfo.MainTableName, formData["id"].ToString());
|
|
var querData = _databaseService.GetInterFaceData(link, sql).ToJsonString().ToObject<List<Dictionary<string, string>>>();
|
|
if (querData.Any() && querData.Any(x => x.ContainsKey("F_Version") || x.ContainsKey("F_VERSION")))
|
|
{
|
|
res.Add("f_version", querData.FirstOrDefault(x => x.Any(x => x.Key.Equals("F_Version") || x.Key.Equals("F_VERSION")))?.FirstOrDefault().Value);
|
|
isUpdate = true; // 修改
|
|
}
|
|
else
|
|
{
|
|
isUpdate = false; // 新增
|
|
}
|
|
}
|
|
else
|
|
{
|
|
var sql = string.Format(sqlFormat, newTInfo.MainPrimary, newTInfo.MainPrimary, newTInfo.MainTableName, formData["id"].ToString());
|
|
var querData = _databaseService.GetInterFaceData(link, sql).ToJsonString().ToObject<List<Dictionary<string, string>>>();
|
|
|
|
if (querData.Any() && querData.Any(x => x.ContainsKey(newTInfo.MainPrimary))) isUpdate = true; // 修改
|
|
else isUpdate = false; // 新增
|
|
}
|
|
|
|
// 保存到数据库
|
|
res["id"] = formData["id"];
|
|
if (!isSubFlow)
|
|
{
|
|
await SaveFlowFormData(newFEntity, res.ToJsonString(), formData["id"].ToString(), isUpdate);
|
|
}
|
|
|
|
return res;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 公用方法
|
|
|
|
/// <summary>
|
|
/// 删除有表信息.
|
|
/// </summary>
|
|
/// <param name="id">主键</param>
|
|
/// <param name="templateEntity">模板实体</param>
|
|
/// <returns></returns>
|
|
public async Task DelHaveTableInfo(string id, VisualDevEntity templateEntity)
|
|
{
|
|
if (templateEntity.EnableFlow == 1)
|
|
{
|
|
var flowTask = await _visualDevRepository.AsSugarClient().Queryable<FlowTaskEntity>().Where(f => f.Id.Equals(id) && f.Status != 4).FirstAsync();
|
|
if (flowTask != null)
|
|
{
|
|
if (flowTask.ParentId != "0") throw Oops.Oh(ErrorCode.WF0003, flowTask.FullName);
|
|
else throw Oops.Oh(ErrorCode.D1417);
|
|
}
|
|
}
|
|
|
|
if (id.IsNotEmptyOrNull())
|
|
{
|
|
TemplateParsingBase templateInfo = new TemplateParsingBase(templateEntity); // 解析模板控件
|
|
DbLinkEntity link = await GetDbLink(templateEntity.DbLinkId);
|
|
templateInfo.DbLink = link;
|
|
string? mainPrimary = GetPrimary(link, templateInfo.MainTableName);
|
|
var resId = GetPIdsByFlowIds(link, templateInfo, mainPrimary, new List<string>() { id });
|
|
id = resId.First().Value;
|
|
if (templateInfo.FormModel.logicalDelete)
|
|
{
|
|
LogicalDelete(templateInfo); // 处理软删除.
|
|
await _databaseService.ExecuteSql(link, string.Format("update {0} set f_deletemark=1 where {1}='{2}'", templateInfo.MainTableName, mainPrimary, id)); // 删除标识
|
|
if (templateEntity.EnableFlow == 1)
|
|
{
|
|
FlowTaskEntity? entity = _flowTaskRepository.GetTaskFirstOrDefault(resId.First().Key);
|
|
if (entity != null)
|
|
{
|
|
if (!entity.ParentId.Equals("0")) throw Oops.Oh(ErrorCode.WF0003, entity.FullName);
|
|
await _flowTaskRepository.DeleteTask(entity);
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
List<string>? allDelSql = new List<string>(); // 拼接语句
|
|
allDelSql.Add(string.Format("delete from {0} where {1} = '{2}';", templateInfo.MainTable.table, mainPrimary, id));
|
|
if (templateInfo.AllTable.Any(x => x.typeId.Equals("0")))
|
|
{
|
|
templateInfo.AllTable.Where(x => x.typeId.Equals("0")).ToList()
|
|
.ForEach(item => allDelSql.Add(string.Format("delete from {0} where {1}='{2}';", item.table, item.tableField, id))); // 删除所有涉及表数据 sql
|
|
}
|
|
|
|
foreach (string? item in allDelSql) await _databaseService.ExecuteSql(link, item); // 删除有表数据
|
|
|
|
if (templateEntity.EnableFlow == 1)
|
|
{
|
|
FlowTaskEntity? entity = _flowTaskRepository.GetTaskFirstOrDefault(resId.First().Key);
|
|
if (entity != null)
|
|
{
|
|
if (!entity.ParentId.Equals("0")) throw Oops.Oh(ErrorCode.WF0003, entity.FullName);
|
|
await _flowTaskRepository.DeleteTask(entity);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 批量删除有表数据.
|
|
/// </summary>
|
|
/// <param name="ids">id数组</param>
|
|
/// <param name="templateEntity">模板实体</param>
|
|
/// <returns></returns>
|
|
public async Task BatchDelHaveTableData(List<string> ids, VisualDevEntity templateEntity)
|
|
{
|
|
List<string>? idList = ids.Copy();
|
|
if (templateEntity.EnableFlow == 1)
|
|
{
|
|
var fList = await _visualDevRepository.AsSugarClient().Queryable<FlowTaskEntity>().Where(f => ids.Contains(f.Id) && f.Status != 4).ToListAsync();
|
|
if (fList.Any(x => x.ParentId != "0")) throw Oops.Oh(ErrorCode.WF0003, fList.First(x => x.ParentId != "0").FullName);
|
|
if (fList.Any()) throw Oops.Oh(ErrorCode.D1417);
|
|
}
|
|
|
|
if (ids.Count > 0)
|
|
{
|
|
TemplateParsingBase templateInfo = new TemplateParsingBase(templateEntity); // 解析模板控件
|
|
DbLinkEntity link = await GetDbLink(templateEntity.DbLinkId);
|
|
string? mainPrimary = GetPrimary(link, templateInfo.MainTableName);
|
|
var resIds = GetPIdsByFlowIds(link, templateInfo, mainPrimary, ids);
|
|
ids = resIds.Select(x => x.Value).ToList();
|
|
|
|
if (templateInfo.FormModel.logicalDelete)
|
|
{
|
|
LogicalDelete(templateInfo); // 处理软删除.
|
|
await _databaseService.ExecuteSql(link, string.Format("update {0} set f_deletemark=1 where {1} in ('{2}')", templateInfo.MainTableName, mainPrimary, string.Join("','", ids))); // 删除标识
|
|
if (templateEntity.EnableFlow == 1)
|
|
{
|
|
ids = resIds.Select(x => x.Key).ToList();
|
|
foreach (var it in ids)
|
|
{
|
|
FlowTaskEntity? entity = _flowTaskRepository.GetTaskFirstOrDefault(it);
|
|
if (entity != null && entity.ParentId.Equals("0"))
|
|
{
|
|
if (!entity.ParentId.Equals("0")) throw Oops.Oh(ErrorCode.WF0003, entity.FullName);
|
|
_flowTaskRepository.DeleteTaskNoAwait(entity);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
List<string>? allDelSql = new List<string>(); // 拼接语句
|
|
allDelSql.Add(string.Format("delete from {0} where {1} in ('{2}');", templateInfo.MainTable.table, mainPrimary, string.Join("','", ids))); // 主表数据
|
|
if (templateInfo.AllTable.Any(x => x.typeId.Equals("0")))
|
|
{
|
|
templateInfo.AllTable.Where(x => x.typeId.Equals("0")).ToList()
|
|
.ForEach(item => allDelSql.Add(string.Format("delete from {0} where {1} in ('{2}');", item.table, item.tableField, string.Join("','", ids))));
|
|
}
|
|
|
|
try
|
|
{
|
|
foreach (string? item in allDelSql) await _databaseService.ExecuteSql(link, item); // 删除有表数据
|
|
|
|
if (templateEntity.EnableFlow == 1)
|
|
{
|
|
ids = resIds.Select(x => x.Key).ToList();
|
|
foreach (var it in ids)
|
|
{
|
|
FlowTaskEntity? entity = _flowTaskRepository.GetTaskFirstOrDefault(it);
|
|
if (entity != null && entity.ParentId.Equals("0"))
|
|
{
|
|
if (!entity.ParentId.Equals("0")) throw Oops.Oh(ErrorCode.WF0003, entity.FullName);
|
|
_flowTaskRepository.DeleteTaskNoAwait(entity);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (templateEntity.EnableFlow == 1 && ids.Count < 1 && !idList.Count.Equals(ids.Count)) throw Oops.Oh(ErrorCode.D1417);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 生成系统自动生成字段.
|
|
/// </summary>
|
|
/// <param name="fieldsModelListJson">模板数据.</param>
|
|
/// <param name="allDataMap">真实数据.</param>
|
|
/// <param name="IsCreate">创建与修改标识 true创建 false 修改.</param>
|
|
/// <returns></returns>
|
|
public async Task<Dictionary<string, object>> GenerateFeilds(string fieldsModelListJson, Dictionary<string, object> allDataMap, bool IsCreate)
|
|
{
|
|
List<FieldsModel> fieldsModelList = fieldsModelListJson.ToList<FieldsModel>();
|
|
UserEntity? userInfo = _userManager.User;
|
|
int dicCount = allDataMap.Keys.Count;
|
|
string[] strKey = new string[dicCount];
|
|
|
|
// 修改时 把 创建用户 和 创建时间 去掉.
|
|
if (!IsCreate)
|
|
{
|
|
fieldsModelList.ForEach(item =>
|
|
{
|
|
switch (item.__config__.jnpfKey)
|
|
{
|
|
case JnpfKeyConst.CREATETIME:
|
|
case JnpfKeyConst.CREATEUSER:
|
|
case JnpfKeyConst.BILLRULE:
|
|
case JnpfKeyConst.CURRPOSITION:
|
|
case JnpfKeyConst.CURRORGANIZE:
|
|
allDataMap.Remove(item.__vModel__);
|
|
break;
|
|
case JnpfKeyConst.TABLE:
|
|
var fList = item.__config__.children.Where(x => x.__config__.jnpfKey.Equals(JnpfKeyConst.CREATETIME)
|
|
|| x.__config__.jnpfKey.Equals(JnpfKeyConst.CREATEUSER)
|
|
|| x.__config__.jnpfKey.Equals(JnpfKeyConst.BILLRULE)
|
|
|| x.__config__.jnpfKey.Equals(JnpfKeyConst.CURRPOSITION)
|
|
|| x.__config__.jnpfKey.Equals(JnpfKeyConst.CURRORGANIZE)).ToList();
|
|
fList.ForEach(child =>
|
|
{
|
|
if (allDataMap.ContainsKey(item.__vModel__))
|
|
{
|
|
var cDataMap = allDataMap[item.__vModel__].ToObject<List<Dictionary<string, object>>>();
|
|
cDataMap.ForEach(x => x.Remove(child.__vModel__));
|
|
allDataMap[item.__vModel__] = cDataMap;
|
|
}
|
|
});
|
|
break;
|
|
}
|
|
});
|
|
}
|
|
|
|
allDataMap.Keys.CopyTo(strKey, 0);
|
|
for (int i = 0; i < strKey.Length; i++)
|
|
{
|
|
// 根据KEY查找模板
|
|
FieldsModel? model = fieldsModelList.Find(f => f.__vModel__ == strKey[i]);
|
|
if (model != null)
|
|
{
|
|
// 如果模板jnpfKey为table为子表数据
|
|
if (model.__config__.jnpfKey.Equals(JnpfKeyConst.TABLE) && allDataMap[strKey[i]] != null)
|
|
{
|
|
List<FieldsModel> childFieldsModelList = model.__config__.children;
|
|
object? objectData = allDataMap[strKey[i]];
|
|
List<Dictionary<string, object>> childAllDataMapList = objectData.ToJsonString().ToObject<List<Dictionary<string, object>>>();
|
|
if (childAllDataMapList != null && childAllDataMapList.Count > 0)
|
|
{
|
|
List<Dictionary<string, object>> newChildAllDataMapList = new List<Dictionary<string, object>>();
|
|
foreach (Dictionary<string, object>? childmap in childAllDataMapList)
|
|
{
|
|
Dictionary<string, object>? newChildData = new Dictionary<string, object>();
|
|
foreach (KeyValuePair<string, object> item in childmap)
|
|
{
|
|
FieldsModel? childFieldsModel = childFieldsModelList.Where(c => c.__vModel__ == item.Key).FirstOrDefault();
|
|
if (childFieldsModel != null && childFieldsModel.__vModel__.Equals(item.Key))
|
|
{
|
|
switch (childFieldsModel.__config__.jnpfKey)
|
|
{
|
|
case JnpfKeyConst.BILLRULE:
|
|
if (IsCreate || childmap[item.Key].IsNullOrEmpty())
|
|
{
|
|
string billNumber = await _billRuleService.GetBillNumber(childFieldsModel.__config__.rule);
|
|
if (!"单据规则不存在".Equals(billNumber)) newChildData[item.Key] = billNumber;
|
|
else newChildData[item.Key] = string.Empty;
|
|
}
|
|
else
|
|
{
|
|
newChildData[item.Key] = childmap[item.Key];
|
|
}
|
|
|
|
break;
|
|
case JnpfKeyConst.CREATEUSER:
|
|
if (IsCreate) newChildData[item.Key] = userInfo.Id;
|
|
break;
|
|
case JnpfKeyConst.MODIFYUSER:
|
|
if (!IsCreate) newChildData[item.Key] = userInfo.Id;
|
|
break;
|
|
case JnpfKeyConst.CREATETIME:
|
|
if (IsCreate) newChildData[item.Key] = string.Format("{0:yyyy-MM-dd HH:mm:ss}", DateTime.Now);
|
|
break;
|
|
case JnpfKeyConst.MODIFYTIME:
|
|
if (!IsCreate) newChildData[item.Key] = string.Format("{0:yyyy-MM-dd HH:mm:ss}", DateTime.Now);
|
|
break;
|
|
case JnpfKeyConst.CURRPOSITION:
|
|
if (IsCreate)
|
|
{
|
|
if (allDataMap.ContainsKey("Jnpf_FlowDelegate_CurrPosition")) // 流程委托 需要指定所属岗位
|
|
{
|
|
allDataMap[strKey[i]] = allDataMap["Jnpf_FlowDelegate_CurrPosition"];
|
|
}
|
|
else
|
|
{
|
|
string? pid = await _visualDevRepository.AsSugarClient().Queryable<UserEntity, PositionEntity>((a, b) => new JoinQueryInfos(JoinType.Left, b.Id == a.PositionId))
|
|
.Where((a, b) => a.Id == userInfo.Id && a.DeleteMark == null).Select((a, b) => a.PositionId).FirstAsync();
|
|
if (pid.IsNotEmptyOrNull()) newChildData[item.Key] = pid;
|
|
else newChildData[item.Key] = string.Empty;
|
|
}
|
|
}
|
|
|
|
break;
|
|
case JnpfKeyConst.CURRORGANIZE:
|
|
if (IsCreate)
|
|
{
|
|
if (allDataMap.ContainsKey("Jnpf_FlowDelegate_CurrOrganize")) // 流程委托 需要指定所属组织
|
|
{
|
|
allDataMap[strKey[i]] = allDataMap["Jnpf_FlowDelegate_CurrOrganize"];
|
|
}
|
|
else
|
|
{
|
|
if (userInfo.OrganizeId != null) newChildData[item.Key] = userInfo.OrganizeId;
|
|
else newChildData[item.Key] = string.Empty;
|
|
}
|
|
}
|
|
|
|
break;
|
|
case JnpfKeyConst.UPLOADFZ: // 文件上传
|
|
if (allDataMap[strKey[i]].IsNullOrEmpty()) newChildData[item.Key] = new string[] { };
|
|
else newChildData[item.Key] = childmap[item.Key];
|
|
break;
|
|
default:
|
|
newChildData[item.Key] = childmap[item.Key];
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
newChildAllDataMapList.Add(newChildData);
|
|
allDataMap[strKey[i]] = newChildAllDataMapList;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (model.__vModel__.Equals(strKey[i]))
|
|
{
|
|
switch (model.__config__.jnpfKey)
|
|
{
|
|
case JnpfKeyConst.BILLRULE:
|
|
if (IsCreate)
|
|
{
|
|
string billNumber = await _billRuleService.GetBillNumber(model.__config__.rule);
|
|
if (!"单据规则不存在".Equals(billNumber)) allDataMap[strKey[i]] = billNumber;
|
|
else allDataMap[strKey[i]] = string.Empty;
|
|
}
|
|
break;
|
|
case JnpfKeyConst.CREATEUSER:
|
|
if (IsCreate)
|
|
{
|
|
allDataMap[strKey[i]] = userInfo.Id;
|
|
}
|
|
break;
|
|
case JnpfKeyConst.CREATETIME:
|
|
if (IsCreate) allDataMap[strKey[i]] = string.Format("{0:yyyy-MM-dd HH:mm:ss}", DateTime.Now);
|
|
break;
|
|
case JnpfKeyConst.MODIFYUSER:
|
|
if (!IsCreate) allDataMap[strKey[i]] = userInfo.Id;
|
|
break;
|
|
case JnpfKeyConst.MODIFYTIME:
|
|
if (!IsCreate) allDataMap[strKey[i]] = string.Format("{0:yyyy-MM-dd HH:mm:ss}", DateTime.Now);
|
|
break;
|
|
case JnpfKeyConst.CURRPOSITION:
|
|
if (IsCreate)
|
|
{
|
|
if (allDataMap.ContainsKey("Jnpf_FlowDelegate_CurrPosition")) // 流程委托 需要指定所属岗位
|
|
{
|
|
allDataMap[strKey[i]] = allDataMap["Jnpf_FlowDelegate_CurrPosition"];
|
|
}
|
|
else
|
|
{
|
|
string? pid = await _visualDevRepository.AsSugarClient().Queryable<UserEntity, PositionEntity>((a, b) => new JoinQueryInfos(JoinType.Left, b.Id == a.PositionId))
|
|
.Where((a, b) => a.Id == userInfo.Id && a.DeleteMark == null).Select((a, b) => a.PositionId).FirstAsync();
|
|
if (pid.IsNotEmptyOrNull()) allDataMap[strKey[i]] = pid;
|
|
else allDataMap[strKey[i]] = string.Empty;
|
|
}
|
|
}
|
|
|
|
break;
|
|
case JnpfKeyConst.CURRORGANIZE:
|
|
if (IsCreate)
|
|
{
|
|
if (allDataMap.ContainsKey("Jnpf_FlowDelegate_CurrOrganize")) // 流程委托 需要指定所属组织
|
|
{
|
|
allDataMap[strKey[i]] = allDataMap["Jnpf_FlowDelegate_CurrOrganize"];
|
|
}
|
|
else
|
|
{
|
|
if (userInfo.OrganizeId != null) allDataMap[strKey[i]] = userInfo.OrganizeId;
|
|
else allDataMap[strKey[i]] = string.Empty;
|
|
}
|
|
}
|
|
|
|
break;
|
|
case JnpfKeyConst.UPLOADFZ: // 文件上传
|
|
if (allDataMap[strKey[i]].IsNullOrEmpty()) allDataMap[strKey[i]] = new string[] { };
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return allDataMap;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取数据连接, 根据连接Id.
|
|
/// </summary>
|
|
/// <param name="linkId"></param>
|
|
/// <returns></returns>
|
|
public async Task<DbLinkEntity> GetDbLink(string linkId)
|
|
{
|
|
DbLinkEntity link = await _dbLinkService.GetInfo(linkId);
|
|
if (link == null) link = _databaseService.GetTenantDbLink(_userManager.TenantId, _userManager.TenantDbName); // 当前数据库连接
|
|
return link;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 私有方法
|
|
|
|
/// <summary>
|
|
/// 获取数据表主键.
|
|
/// </summary>
|
|
/// <param name="link"></param>
|
|
/// <param name="MainTableName"></param>
|
|
/// <returns></returns>
|
|
private string GetPrimary(DbLinkEntity link, string MainTableName)
|
|
{
|
|
var keys = _databaseService.GetPrimaries(link, MainTableName);
|
|
if (keys.Count < 1) throw Oops.Oh(ErrorCode.D1402); // 主表未设置主键
|
|
return keys.First();
|
|
//List<DbTableFieldModel>? tableList = _databaseService.GetFieldList(link, MainTableName); // 获取主表所有列
|
|
//DbTableFieldModel? mainPrimary = tableList.Find(t => t.primaryKey); // 主表主键
|
|
//if (mainPrimary == null || mainPrimary.IsNullOrEmpty()) throw Oops.Oh(ErrorCode.D1402); // 主表未设置主键
|
|
//return mainPrimary.field;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据流程Id 获取 主键 Id.
|
|
/// </summary>
|
|
/// <param name="link">数据库连接.</param>
|
|
/// <param name="templateInfo">模板配置.</param>
|
|
/// <param name="mainPrimary">主表主键名.</param>
|
|
/// <param name="Ids">流程Ids.</param>
|
|
/// <param name="isList">是否列表.</param>
|
|
/// <param name="currIndex">.</param>
|
|
/// <returns>F_FlowTaskId, mainPrimary.</returns>
|
|
private Dictionary<string, string> GetPIdsByFlowIds(DbLinkEntity link, TemplateParsingBase templateInfo, string mainPrimary, List<string> Ids, bool isList = false, int currIndex = 0)
|
|
{
|
|
Dictionary<string, string> res = new Dictionary<string, string>();
|
|
if (templateInfo.visualDevEntity != null && templateInfo.visualDevEntity.EnableFlow.Equals(1) && templateInfo.FormModel.primaryKeyPolicy.Equals(2) && currIndex < 3)
|
|
{
|
|
var sql = string.Format("select {0},F_FlowTaskId from {1} where F_FlowTaskId in ('{2}');", mainPrimary, templateInfo.MainTableName, string.Join("','", Ids));
|
|
if (isList) sql = string.Format("select {0},F_FlowTaskId from {1} where {0} in ('{2}');", mainPrimary, templateInfo.MainTableName, string.Join("','", Ids));
|
|
var data = _databaseService.GetInterFaceData(link, sql).ToJsonString().ToObject<List<Dictionary<string, string>>>();
|
|
currIndex++;
|
|
if (!data.Any()) return GetPIdsByFlowIds(link, templateInfo, mainPrimary, Ids, true, currIndex);
|
|
data.ForEach(item => res.Add(item["F_FlowTaskId"], item[mainPrimary]));
|
|
}
|
|
else
|
|
{
|
|
Ids.ForEach(item => res.Add(item, item));
|
|
}
|
|
|
|
return res;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取允许删除任务列表.
|
|
/// </summary>
|
|
/// <param name="ids">id数组.</param>
|
|
/// <returns></returns>
|
|
private async Task<List<string>> GetAllowDeleteFlowTaskList(List<string> ids)
|
|
{
|
|
List<string>? idList = await _visualDevRepository.AsSugarClient().Queryable<FlowTaskEntity>().Where(f => ids.Contains(f.Id) && f.Status != 4).Select(f => f.Id).ToListAsync();
|
|
|
|
return ids.Except(idList).ToList();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 组装高级查询信息.
|
|
/// </summary>
|
|
/// <param name="superQueryJson">查询条件json.</param>
|
|
private string GetSuperQueryInput(string superQueryJson)
|
|
{
|
|
Dictionary<string, object> result = new Dictionary<string, object>();
|
|
Dictionary<string, string>? dic = string.IsNullOrEmpty(superQueryJson) ? null : superQueryJson.ToObject<Dictionary<string, string>>();
|
|
|
|
if (dic != null)
|
|
{
|
|
var matchLogic = dic.FirstOrDefault().Value;
|
|
var whereType = matchLogic.Equals("AND") ? WhereType.And : WhereType.Or;
|
|
var queryList = new List<Dictionary<string, object>>();
|
|
|
|
foreach (var item in dic.LastOrDefault().Value.ToObject<List<Dictionary<string, object>>>())
|
|
{
|
|
var query = new Dictionary<string, object>();
|
|
query.Add("whereType", whereType);
|
|
query.Add("jnpfKey", item["jnpfKey"]);
|
|
query.Add("field", item["field"].ToString());
|
|
string itemValue = string.Empty;
|
|
if (item.ContainsKey("fieldValue") && item["fieldValue"].IsNotEmptyOrNull())
|
|
itemValue = item["fieldValue"].ToString().Replace("\r\n", string.Empty).Replace(" ", string.Empty);
|
|
|
|
switch (item["jnpfKey"])
|
|
{
|
|
case JnpfKeyConst.USERSSELECT:
|
|
{
|
|
if (item["fieldValue"] != null && (item["symbol"].Equals("like") || item["symbol"].Equals("notLike")))
|
|
{
|
|
var rIdList = _visualDevRepository.AsSugarClient().Queryable<UserRelationEntity>().Where(x => x.UserId.Equals(item["fieldValue"].ToString().Replace("--user", string.Empty))).Select(x => new { x.ObjectId, x.ObjectType }).ToList();
|
|
var objIdList = new List<string>() { item["fieldValue"].ToString() };
|
|
rIdList.ForEach(x =>
|
|
{
|
|
if (x.ObjectType.Equals("Organize"))
|
|
{
|
|
objIdList.Add(x.ObjectId + "--company");
|
|
objIdList.Add(x.ObjectId + "--department");
|
|
}
|
|
else
|
|
{
|
|
objIdList.Add(x.ObjectId + "--" + x.ObjectType.ToLower());
|
|
}
|
|
});
|
|
|
|
var whereList = new List<KeyValuePair<WhereType, ConditionalModel>>();
|
|
for (var i = 0; i < objIdList.Count(); i++)
|
|
{
|
|
if (i == 0)
|
|
{
|
|
var queryOr = new Dictionary<string, object>();
|
|
queryOr.Add("whereType", WhereType.And);
|
|
queryOr.Add("jnpfKey", item["jnpfKey"]);
|
|
queryOr.Add("field", item["field"].ToString());
|
|
queryOr.Add("fieldValue", objIdList[i]);
|
|
queryOr.Add("ConditionalType", item["symbol"].Equals("like") ? ConditionalType.Like : ConditionalType.NoLike);
|
|
queryList.Add(queryOr);
|
|
}
|
|
else
|
|
{
|
|
var queryOr = new Dictionary<string, object>();
|
|
queryOr.Add("whereType", item["symbol"].Equals("like") ? WhereType.Or : WhereType.And);
|
|
queryOr.Add("jnpfKey", item["jnpfKey"]);
|
|
queryOr.Add("field", item["field"].ToString());
|
|
queryOr.Add("fieldValue", objIdList[i]);
|
|
queryOr.Add("ConditionalType", item["symbol"].Equals("like") ? ConditionalType.Like : ConditionalType.NoLike);
|
|
queryList.Add(queryOr);
|
|
}
|
|
}
|
|
|
|
if (item["symbol"].Equals("notLike"))
|
|
{
|
|
var queryOr = new Dictionary<string, object>();
|
|
queryOr.Add("whereType", WhereType.Or);
|
|
queryOr.Add("jnpfKey", item["jnpfKey"]);
|
|
queryOr.Add("field", item["field"].ToString());
|
|
queryOr.Add("fieldValue", null);
|
|
queryOr.Add("ConditionalType", ConditionalType.EqualNull);
|
|
queryList.Add(queryOr);
|
|
}
|
|
}
|
|
}
|
|
break;
|
|
case JnpfKeyConst.DATE:
|
|
case JnpfKeyConst.CREATETIME:
|
|
case JnpfKeyConst.MODIFYTIME:
|
|
if (itemValue.IsNotEmptyOrNull()) itemValue = string.Format("{0:yyyy-MM-dd HH:mm:ss}", itemValue.TimeStampToDateTime());
|
|
item["symbol"] = item["symbol"].Equals("like") ? "in" : item["symbol"];
|
|
item["symbol"] = item["symbol"].Equals("notLike") ? "notIn" : item["symbol"];
|
|
break;
|
|
case JnpfKeyConst.COMSELECT:
|
|
if (item["symbol"].Equals("like") && item["fieldValue"].IsNotEmptyOrNull())
|
|
{
|
|
var queryOr = new Dictionary<string, object>();
|
|
queryOr.Add("whereType", WhereType.Or);
|
|
queryOr.Add("jnpfKey", item["jnpfKey"]);
|
|
queryOr.Add("field", item["field"].ToString());
|
|
queryOr.Add("fieldValue", item["fieldValue"].ToObject<List<string>>().ToJsonString().Replace("[", string.Empty));
|
|
queryOr.Add("ConditionalType", ConditionalType.Like);
|
|
queryList.Add(queryOr);
|
|
continue;
|
|
}
|
|
if (item["symbol"].Equals("notLike") && item["fieldValue"].IsNotEmptyOrNull())
|
|
{
|
|
var queryOr = new Dictionary<string, object>();
|
|
queryOr.Add("whereType", whereType);
|
|
queryOr.Add("jnpfKey", item["jnpfKey"]);
|
|
queryOr.Add("field", item["field"].ToString());
|
|
queryOr.Add("fieldValue", item["fieldValue"].ToObject<List<string>>().ToJsonString().Replace("[", string.Empty));
|
|
queryOr.Add("ConditionalType", ConditionalType.NoLike);
|
|
queryList.Add(queryOr);
|
|
itemValue = itemValue.Replace("[", string.Empty);
|
|
}
|
|
|
|
break;
|
|
case JnpfKeyConst.CURRORGANIZE:
|
|
itemValue = itemValue.Contains("[") ? itemValue.ToObject<List<string>>().LastOrDefault() : itemValue;
|
|
break;
|
|
case JnpfKeyConst.NUMINPUT:
|
|
case JnpfKeyConst.CALCULATE:
|
|
query.Add("CSharpTypeName", "int");
|
|
break;
|
|
}
|
|
|
|
//if (itemValue.Contains("[")) itemValue = itemValue.Replace("[", string.Empty).Replace("]", string.Empty);
|
|
query.Add("fieldValue", itemValue);
|
|
if ((!item.ContainsKey("fieldValue") || item.ContainsKey("fieldValue").Equals("[]")) && item["symbol"].Equals("=="))
|
|
{
|
|
if (item["jnpfKey"].Equals(JnpfKeyConst.CALCULATE) || item["jnpfKey"].Equals(JnpfKeyConst.NUMINPUT)) query.Add("ConditionalType", ConditionalType.EqualNull);
|
|
else query.Add("ConditionalType", ConditionalType.IsNullOrEmpty);
|
|
queryList.Add(query);
|
|
continue;
|
|
}
|
|
|
|
if ((!item.ContainsKey("fieldValue") || item.ContainsKey("fieldValue").Equals("[]") || itemValue.Equals(string.Empty)) && item["symbol"].Equals("<>"))
|
|
{
|
|
query.Add("ConditionalType", ConditionalType.IsNot);
|
|
queryList.Add(query);
|
|
continue;
|
|
}
|
|
|
|
switch (item["symbol"])
|
|
{
|
|
case ">=":
|
|
query.Add("ConditionalType", ConditionalType.GreaterThanOrEqual);
|
|
break;
|
|
case ">":
|
|
query.Add("ConditionalType", ConditionalType.GreaterThan);
|
|
break;
|
|
case "==":
|
|
query.Add("ConditionalType", ConditionalType.Equal);
|
|
break;
|
|
case "<=":
|
|
query.Add("ConditionalType", ConditionalType.LessThanOrEqual);
|
|
break;
|
|
case "<":
|
|
query.Add("ConditionalType", ConditionalType.LessThan);
|
|
break;
|
|
case "like":
|
|
query.Add("ConditionalType", itemValue.IsNotEmptyOrNull() ? ConditionalType.Like : ((item["jnpfKey"].Equals(JnpfKeyConst.CALCULATE) || item["jnpfKey"].Equals(JnpfKeyConst.NUMINPUT)) ? ConditionalType.EqualNull : ConditionalType.IsNullOrEmpty));
|
|
if (query["fieldValue"] != null && query["fieldValue"].ToString().Contains("["))
|
|
query["fieldValue"] = query["fieldValue"].ToString().Replace("[", string.Empty).Replace("]", string.Empty);
|
|
break;
|
|
case "<>":
|
|
query.Add("ConditionalType", ConditionalType.NoEqual);
|
|
break;
|
|
case "notLike":
|
|
query.Add("ConditionalType", ConditionalType.NoLike);
|
|
if (query["fieldValue"] != null && query["fieldValue"].ToString().Contains("["))
|
|
query["fieldValue"] = query["fieldValue"].ToString().Replace("[", string.Empty).Replace("]", string.Empty);
|
|
break;
|
|
case "in":
|
|
query.Add("ConditionalType", itemValue.IsNotEmptyOrNull() ? ConditionalType.In : ConditionalType.IsNullOrEmpty);
|
|
break;
|
|
case "notIn":
|
|
query.Add("ConditionalType", ConditionalType.NotIn);
|
|
break;
|
|
}
|
|
|
|
queryList.Add(query);
|
|
|
|
if ((item["symbol"].Equals("<>") || item["symbol"].Equals("notLike")) && itemValue.IsNotEmptyOrNull() && item["jnpfKey"].Equals(JnpfKeyConst.NUMINPUT) && item["jnpfKey"].Equals(JnpfKeyConst.CALCULATE))
|
|
{
|
|
var queryOr = new Dictionary<string, object>();
|
|
queryOr.Add("whereType", WhereType.Or);
|
|
queryOr.Add("jnpfKey", item["jnpfKey"]);
|
|
queryOr.Add("field", item["field"].ToString());
|
|
queryOr.Add("fieldValue", null);
|
|
queryOr.Add("ConditionalType", ConditionalType.EqualNull);
|
|
queryOr.Add("CSharpTypeName", "int");
|
|
queryList.Add(queryOr);
|
|
}
|
|
if ((item["symbol"].Equals("<>") || item["symbol"].Equals("notLike"))
|
|
&& itemValue.IsNotEmptyOrNull()
|
|
&& !item["jnpfKey"].Equals(JnpfKeyConst.NUMINPUT)
|
|
&& !item["jnpfKey"].Equals(JnpfKeyConst.CALCULATE)
|
|
&& !item["jnpfKey"].Equals(JnpfKeyConst.SWITCH))
|
|
{
|
|
var queryOr = new Dictionary<string, object>();
|
|
queryOr.Add("whereType", WhereType.Or);
|
|
queryOr.Add("jnpfKey", item["jnpfKey"]);
|
|
queryOr.Add("field", item["field"].ToString());
|
|
queryOr.Add("fieldValue", null);
|
|
queryOr.Add("ConditionalType", ConditionalType.IsNullOrEmpty);
|
|
queryList.Add(queryOr);
|
|
}
|
|
|
|
if (item["symbol"].Equals("notLike") && itemValue.IsNullOrWhiteSpace())
|
|
{
|
|
var queryOr = new Dictionary<string, object>();
|
|
queryOr.Add("whereType", WhereType.Or);
|
|
queryOr.Add("jnpfKey", item["jnpfKey"]);
|
|
queryOr.Add("field", item["field"].ToString());
|
|
queryOr.Add("fieldValue", null);
|
|
queryOr.Add("ConditionalType", ConditionalType.IsNot);
|
|
queryList.Add(queryOr);
|
|
}
|
|
|
|
if (item["symbol"].Equals("notIn") && itemValue.IsNotEmptyOrNull())
|
|
{
|
|
var queryOr = new Dictionary<string, object>();
|
|
queryOr.Add("whereType", WhereType.Or);
|
|
queryOr.Add("jnpfKey", item["jnpfKey"]);
|
|
queryOr.Add("field", item["field"].ToString());
|
|
queryOr.Add("fieldValue", null);
|
|
queryOr.Add("ConditionalType", ConditionalType.IsNullOrEmpty);
|
|
queryList.Add(queryOr);
|
|
}
|
|
}
|
|
|
|
return queryList.ToJsonString();
|
|
}
|
|
else return string.Empty;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 数据唯一 验证.
|
|
/// </summary>
|
|
/// <param name="link">DbLinkEntity.</param>
|
|
/// <param name="templateInfo">模板信息.</param>
|
|
/// <param name="allDataMap">数据.</param>
|
|
/// <param name="mainPrimary">主键名.</param>
|
|
/// <param name="mainId">主键Id.</param>
|
|
/// <param name="isUpdate">是否修改.</param>
|
|
private void UniqueVerify(DbLinkEntity link, TemplateParsingBase templateInfo, Dictionary<string, object> allDataMap, string mainPrimary, string mainId, bool isUpdate = false)
|
|
{
|
|
// 单行输入 唯一验证
|
|
if (templateInfo.AllFieldsModel.Any(x => x.__config__.jnpfKey.Equals(JnpfKeyConst.COMINPUT) && x.__config__.unique))
|
|
{
|
|
List<string>? relationKey = new List<string>();
|
|
List<string>? auxiliaryFieldList = templateInfo.AuxiliaryTableFieldsModelList.Select(x => x.__config__.tableName).Distinct().ToList();
|
|
auxiliaryFieldList.ForEach(tName =>
|
|
{
|
|
string? tableField = templateInfo.AllTable.Find(tf => tf.table == tName)?.tableField;
|
|
relationKey.Add(templateInfo.MainTableName + "." + mainPrimary + "=" + tName + "." + tableField);
|
|
});
|
|
|
|
List<string>? fieldList = new List<string>();
|
|
var whereList = new List<IConditionalModel>();
|
|
|
|
templateInfo.SingleFormData.Where(x => x.__config__.jnpfKey.Equals(JnpfKeyConst.COMINPUT) && x.__config__.unique).ToList().ForEach(item =>
|
|
{
|
|
if (allDataMap.ContainsKey(item.__vModel__) && allDataMap[item.__vModel__].IsNotEmptyOrNull())
|
|
{
|
|
fieldList.Add(string.Format("{0}.{1}", item.__config__.tableName, item.__vModel__.Split("_jnpf_").Last()));
|
|
whereList.Add(new ConditionalCollections()
|
|
{
|
|
ConditionalList = new List<KeyValuePair<WhereType, SqlSugar.ConditionalModel>>()
|
|
{
|
|
new KeyValuePair<WhereType, ConditionalModel>(WhereType.Or, new ConditionalModel
|
|
{
|
|
FieldName = string.Format("{0}.{1}", item.__config__.tableName, item.__vModel__.Split("_jnpf_").Last()),
|
|
ConditionalType =allDataMap.ContainsKey(item.__vModel__) ? ConditionalType.Equal: ConditionalType.IsNullOrEmpty,
|
|
FieldValue = allDataMap.ContainsKey(item.__vModel__) ? allDataMap[item.__vModel__].ToString() : string.Empty,
|
|
})
|
|
}
|
|
});
|
|
}
|
|
});
|
|
|
|
var itemWhere = _visualDevRepository.AsSugarClient().SqlQueryable<dynamic>("@").Where(whereList).ToSqlString();
|
|
if (!itemWhere.Equals("@"))
|
|
{
|
|
relationKey.Add(itemWhere.Split("WHERE").Last());
|
|
var querStr = string.Format(
|
|
"select {0} from {1} where {2}",
|
|
string.Join(",", fieldList),
|
|
auxiliaryFieldList.Any() ? templateInfo.MainTableName + "," + string.Join(",", auxiliaryFieldList) : templateInfo.MainTableName,
|
|
string.Join(" and ", relationKey)); // 多表, 联合查询
|
|
|
|
if (isUpdate) querStr = string.Format("{0} and {1}<>'{2}'", querStr, templateInfo.MainTableName + "." + mainPrimary, mainId);
|
|
|
|
var res = _databaseService.GetInterFaceData(link, querStr, null).ToObject<List<Dictionary<string, string>>>();
|
|
|
|
if (res.Any())
|
|
{
|
|
var errorList = new List<string>();
|
|
|
|
res.ForEach(items =>
|
|
{
|
|
foreach (var item in items)
|
|
errorList.Add(templateInfo.SingleFormData.FirstOrDefault(x => x.__vModel__.Equals(item.Key) || x.__vModel__.Contains("_jnpf_" + item.Key))?.__config__.label);
|
|
});
|
|
|
|
throw Oops.Oh(ErrorCode.D1407, string.Join(",", errorList.Distinct()));
|
|
}
|
|
}
|
|
|
|
foreach (var citem in templateInfo.ChildTableFieldsModelList)
|
|
{
|
|
if (allDataMap.ContainsKey(citem.__vModel__))
|
|
{
|
|
var childrenValues = allDataMap[citem.__vModel__].ToObject<List<Dictionary<string, object>>>();
|
|
if (childrenValues.Any())
|
|
{
|
|
citem.__config__.children.Where(x => x.__config__.jnpfKey.Equals(JnpfKeyConst.COMINPUT) && x.__config__.unique).ToList().ForEach(item =>
|
|
{
|
|
var vList = childrenValues.Where(xx => xx.ContainsKey(item.__vModel__)).ToList();
|
|
vList.ForEach(vitem =>
|
|
{
|
|
if (vitem[item.__vModel__] != null && childrenValues.Where(x => x.ContainsKey(item.__vModel__) && x.ContainsValue(vitem[item.__vModel__])).Count() > 1)
|
|
throw Oops.Oh(ErrorCode.D1407, item.__config__.label);
|
|
});
|
|
});
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 组装列表查询sql.
|
|
/// </summary>
|
|
/// <param name="primaryKey">主键.</param>
|
|
/// <param name="templateInfo">模板.</param>
|
|
/// <param name="input">查询输入.</param>
|
|
/// <param name="tableFieldKeyValue">联表查询 表字段名称 对应 前端字段名称 (应对oracle 查询字段长度不能超过30个).</param>
|
|
/// <param name="dataPermissions">数据权限.</param>
|
|
/// <param name="showColumnList">是否只查询显示列.</param>
|
|
/// <returns></returns>
|
|
private string GetListQuerySql(string primaryKey, TemplateParsingBase templateInfo, ref VisualDevModelListQueryInput input, ref Dictionary<string, string> tableFieldKeyValue, List<IConditionalModel> dataPermissions, bool showColumnList = false)
|
|
{
|
|
List<string> fields = new List<string>();
|
|
|
|
string? sql = string.Empty; // 查询sql
|
|
|
|
// 显示列和搜索列有子表字段
|
|
if (templateInfo.ColumnData.columnList.Any(x => templateInfo.ChildTableFields.ContainsKey(x.__vModel__)) || templateInfo.ColumnData.searchList.Any(x => templateInfo.ChildTableFields.ContainsKey(x.__vModel__)))
|
|
{
|
|
var queryJson = input.queryJson;
|
|
var superQueryJson = input.superQueryJson;
|
|
foreach (var item in templateInfo.AllTableFields)
|
|
{
|
|
if (queryJson.Contains(string.Format("\"{0}\"", item.Key)))
|
|
{
|
|
queryJson = queryJson.Replace(string.Format("\"{0}\"", item.Key), string.Format("\"{0}\"", item.Value));
|
|
var vmodel = templateInfo.ColumnData.searchList.FirstOrDefault(x => x != null && x.__vModel__.Equals(item.Key));
|
|
if (vmodel != null)
|
|
{
|
|
vmodel.__vModel__ = item.Value;
|
|
fields.Add(item.Value);
|
|
}
|
|
|
|
var appVModel = templateInfo.AppColumnData.searchList.FirstOrDefault(x => x != null && x.__vModel__.Equals(item.Key));
|
|
if (appVModel != null)
|
|
{
|
|
appVModel.__vModel__ = item.Value;
|
|
fields.Add(item.Value);
|
|
}
|
|
}
|
|
|
|
if (superQueryJson.IsNotEmptyOrNull() && superQueryJson.Contains(string.Format("\"{0}\"", item.Key)))
|
|
superQueryJson = superQueryJson.Replace(string.Format("\"{0}\"", item.Key), string.Format("\"{0}\"", item.Value));
|
|
}
|
|
|
|
var querDic = queryJson.IsNullOrEmpty() ? null : queryJson.ToObject<Dictionary<string, object>>();
|
|
|
|
var superQuerDic = new List<ConditionalCollections>();
|
|
var superCond = superQueryJson.IsNullOrEmpty() ? null : GetSuperQueryJson(superQueryJson);
|
|
if (superCond != null) superQuerDic = superCond.ToObject<List<ConditionalCollections>>();
|
|
var tableWhere = new Dictionary<string, List<IConditionalModel>>();
|
|
|
|
templateInfo.AllTable.Select(x => x.table).Distinct().ToList().ForEach(tName =>
|
|
{
|
|
var query = new Dictionary<string, object>();
|
|
if (querDic != null && querDic.Any())
|
|
{
|
|
querDic.Where(x => x.Key.Contains(tName + ".")).ToList().ForEach(x => query.Add(x.Key, x.Value));
|
|
if (query.Any())
|
|
{
|
|
if (!tableWhere.ContainsKey(tName)) tableWhere.Add(tName, GetQueryJson(query.ToJsonString(), _userManager.UserOrigin == "pc" ? templateInfo.ColumnData : templateInfo.AppColumnData));
|
|
else tableWhere[tName].AddRange(GetQueryJson(query.ToJsonString(), _userManager.UserOrigin == "pc" ? templateInfo.ColumnData : templateInfo.AppColumnData));
|
|
}
|
|
}
|
|
|
|
if (superQuerDic != null && superQuerDic.Any())
|
|
{
|
|
var conList = new ConditionalCollections() { ConditionalList = new List<KeyValuePair<WhereType, ConditionalModel>>() };
|
|
var dicList = superQuerDic.Where(x => x.ConditionalList.Any(xx => xx.Value.FieldName.Contains(tName + "."))).ToList();
|
|
for (var i = 0; i < dicList.Count; i++)
|
|
{
|
|
var items = dicList[i];
|
|
var newItems = items.ToObject<ConditionalCollections>();
|
|
if (querDic != null && querDic.Any() && i == 0) newItems = newItems.ToJsonString().Replace("\"Key\":1,", "\"Key\":0,").ToObject<ConditionalCollections>();
|
|
conList.ConditionalList.AddRange(newItems.ConditionalList);
|
|
}
|
|
|
|
if (!tableWhere.ContainsKey(tName)) tableWhere.Add(tName, new List<IConditionalModel>() { conList });
|
|
else tableWhere[tName].AddRange(new List<IConditionalModel>() { conList });
|
|
}
|
|
|
|
if (!tableWhere.ContainsKey(tName)) tableWhere.Add(tName, new List<IConditionalModel>());
|
|
});
|
|
|
|
var querySqlList = new List<string>();
|
|
var sqlStr = "select {0} from {1} ";
|
|
if (templateInfo.FormModel.logicalDelete && _databaseService.IsAnyColumn(templateInfo.DbLink, templateInfo.MainTableName, "f_deletemark"))
|
|
querySqlList.Add(string.Format(" {0} in ({1}) ", primaryKey, string.Format(" select {0} from {1} where f_deletemark is null ", primaryKey, templateInfo.MainTableName))); // 处理软删除
|
|
foreach (var item in tableWhere)
|
|
{
|
|
// 拼接数据权限
|
|
var dataPermissionsList = new List<IConditionalModel>();
|
|
if (dataPermissions != null && dataPermissions.Any())
|
|
{
|
|
var pList = new List<object>();
|
|
var allPersissions = dataPermissions.ToObject<List<object>>();
|
|
allPersissions.ForEach(it =>
|
|
{
|
|
if (it.ToJsonString().Contains(item.Key)) pList.Add(it);
|
|
else if (item.Key.Equals(templateInfo.MainTableName) && it.ToJsonString().Contains(string.Format("\"FieldName\":\"{0}\"", primaryKey))) pList.Add(it);
|
|
});
|
|
if (pList.Any())
|
|
dataPermissionsList = _visualDevRepository.AsSugarClient().Utilities.JsonToConditionalModels(pList.ToJsonString());
|
|
}
|
|
|
|
var idField = templateInfo.AllTable.Where(x => x.table.Equals(item.Key)).First().tableField;
|
|
var itemSql = string.Format(sqlStr, idField.IsNullOrEmpty() ? primaryKey : idField, item.Key);
|
|
dataPermissionsList = GetIConditionalModelListByTableName(dataPermissionsList, item.Key);// 删除非当前表条件
|
|
var itemWhere = _visualDevRepository.AsSugarClient().SqlQueryable<dynamic>("@").Where(item.Value).Where(dataPermissionsList).ToSqlString();
|
|
if (itemWhere.Contains("WHERE"))
|
|
{
|
|
itemSql = itemSql + " where " + itemWhere.Split("WHERE").Last();
|
|
querySqlList.Add(string.Format(" {0} in ({1}) ", primaryKey, itemSql));
|
|
}
|
|
}
|
|
if (!querySqlList.Any()) querySqlList.Add(string.Format(" {0} in ({1}) ", primaryKey, string.Format(sqlStr, primaryKey, templateInfo.MainTableName)));
|
|
if (templateInfo.ChildTableFields.ContainsKey(input.sidx)) input.sidx = string.Empty; // 子表字段暂时无法排序
|
|
if (templateInfo.ChildTableFields.ContainsKey(templateInfo.ColumnData.defaultSidx)) templateInfo.ColumnData.defaultSidx = string.Empty; // 子表字段暂时无法排序
|
|
|
|
var ctFields = templateInfo.ChildTableFields;
|
|
templateInfo.ChildTableFields = new Dictionary<string, string>();
|
|
var strSql = GetListQuerySql(primaryKey, templateInfo, ref input, ref tableFieldKeyValue, new List<IConditionalModel>());
|
|
input.queryJson = string.Empty;
|
|
input.superQueryJson = string.Empty;
|
|
templateInfo.ChildTableFields = ctFields;
|
|
|
|
sql = string.Format("select * from ({0}) mt where {1}", strSql, string.Join(" and ", querySqlList));
|
|
}
|
|
else if (!templateInfo.AuxiliaryTableFieldsModelList.Any())
|
|
{
|
|
fields.Add(primaryKey); // 主键
|
|
|
|
// 只查询 要显示的列
|
|
if (showColumnList && (templateInfo.SingleFormData.Count > 0 || templateInfo.ColumnData.columnList.Count > 0))
|
|
templateInfo.ColumnData.columnList.ForEach(item => { if (templateInfo.SingleFormData.Any(x => x.__vModel__.Equals(item.prop))) fields.Add(item.prop); });
|
|
else
|
|
templateInfo.MainTableFieldsModelList.Where(x => x.__vModel__.IsNotEmptyOrNull()).ToList().ForEach(item => fields.Add(item.__vModel__)); // 字段
|
|
|
|
sql = string.Format("select {0} from {1}", string.Join(",", fields), templateInfo.MainTableName);
|
|
if (templateInfo.FormModel.logicalDelete && _databaseService.IsAnyColumn(templateInfo.DbLink, templateInfo.MainTableName, "f_deletemark"))
|
|
sql += " where f_deletemark is null "; // 处理软删除
|
|
|
|
// 拼接数据权限
|
|
if (dataPermissions != null && dataPermissions.Any())
|
|
{
|
|
// 替换数据权限字段 别名
|
|
var pvalue = dataPermissions.ToJsonString();
|
|
foreach (var item in fields)
|
|
{
|
|
if (pvalue.Contains(templateInfo.MainTableName + "." + item))
|
|
pvalue = pvalue.Replace(string.Format("\"FieldName\":\"{0}\",", templateInfo.MainTableName + "." + item), string.Format("\"FieldName\":\"{0}\",", item));
|
|
}
|
|
|
|
List<IConditionalModel>? newPvalue = new List<IConditionalModel>();
|
|
if (pvalue.IsNotEmptyOrNull()) newPvalue = _visualDevRepository.AsSugarClient().Utilities.JsonToConditionalModels(pvalue);
|
|
|
|
sql = _visualDevRepository.AsSugarClient().SqlQueryable<dynamic>(sql).Where(newPvalue).ToSqlString();
|
|
}
|
|
|
|
}
|
|
else
|
|
{
|
|
#region 所有主、副表 字段名 和 处理查询、排序字段
|
|
|
|
// 所有主、副表 字段名
|
|
fields.Add(templateInfo.MainTableName + "." + primaryKey);
|
|
tableFieldKeyValue.Add(primaryKey.ToUpper(), primaryKey);
|
|
Dictionary<string, object>? inputJson = input.queryJson?.ToObject<Dictionary<string, object>>();
|
|
for (int i = 0; i < templateInfo.SingleFormData.Count; i++)
|
|
{
|
|
string? vmodel = templateInfo.SingleFormData[i].__vModel__.ReplaceRegex(@"(\w+)_jnpf_", string.Empty); // Field
|
|
|
|
// 只显示要显示的列
|
|
if (showColumnList && !templateInfo.ColumnData.columnList.Any(x => x.prop == templateInfo.SingleFormData[i].__vModel__))
|
|
vmodel = string.Empty;
|
|
|
|
if (vmodel.IsNotEmptyOrNull())
|
|
{
|
|
fields.Add(templateInfo.SingleFormData[i].__config__.tableName + "." + vmodel + " FIELD_" + i); // TableName.Field_0
|
|
tableFieldKeyValue.Add("FIELD_" + i, templateInfo.SingleFormData[i].__vModel__);
|
|
|
|
// 查询字段替换
|
|
if (inputJson != null && inputJson.Count > 0 && inputJson.ContainsKey(templateInfo.SingleFormData[i].__vModel__))
|
|
input.queryJson = input.queryJson.Replace("\"" + templateInfo.SingleFormData[i].__vModel__ + "\":", "\"FIELD_" + i + "\":");
|
|
if (input.superQueryJson.IsNotEmptyOrNull())
|
|
input.superQueryJson = input.superQueryJson.Replace(string.Format("\"field\":\"{0}\"", templateInfo.SingleFormData[i].__vModel__), string.Format("\"field\":\"{0}\"", "FIELD_" + i));
|
|
|
|
templateInfo.ColumnData.searchList.Where(x => x.__vModel__ == templateInfo.SingleFormData[i].__vModel__).ToList().ForEach(item =>
|
|
{
|
|
item.__vModel__ = item.__vModel__.Replace(templateInfo.SingleFormData[i].__vModel__, "FIELD_" + i);
|
|
});
|
|
|
|
// 排序字段替换
|
|
if (templateInfo.ColumnData.defaultSidx.IsNotEmptyOrNull() && templateInfo.ColumnData.defaultSidx == templateInfo.SingleFormData[i].__vModel__)
|
|
templateInfo.ColumnData.defaultSidx = "FIELD_" + i;
|
|
|
|
if (input.sidx.IsNotEmptyOrNull() && input.sidx == templateInfo.SingleFormData[i].__vModel__) input.sidx = "FIELD_" + i;
|
|
}
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 关联字段
|
|
|
|
List<string>? relationKey = new List<string>();
|
|
List<string>? auxiliaryFieldList = templateInfo.AuxiliaryTableFieldsModelList.Select(x => x.__config__.tableName).Distinct().ToList();
|
|
auxiliaryFieldList.ForEach(tName =>
|
|
{
|
|
var tableField = templateInfo.AllTable.Find(tf => tf.table == tName);
|
|
relationKey.Add(templateInfo.MainTableName + "." + tableField.relationField + "=" + tName + "." + tableField.tableField);
|
|
});
|
|
if (templateInfo.FormModel.logicalDelete && _databaseService.IsAnyColumn(templateInfo.DbLink, templateInfo.MainTableName, "f_deletemark"))
|
|
relationKey.Add(templateInfo.MainTableName + ".f_deletemark is null "); // 处理软删除
|
|
string? whereStr = string.Join(" and ", relationKey);
|
|
|
|
#endregion
|
|
|
|
sql = string.Format("select {0} from {1} where {2}", string.Join(",", fields), templateInfo.MainTableName + "," + string.Join(",", auxiliaryFieldList), whereStr); // 多表, 联合查询
|
|
|
|
// 拼接数据权限
|
|
if (dataPermissions != null && dataPermissions.Any())
|
|
{
|
|
// 替换数据权限字段 别名
|
|
var pvalue = dataPermissions.ToJsonString();
|
|
foreach (var item in tableFieldKeyValue)
|
|
{
|
|
string? newValue = item.Value;
|
|
if (templateInfo.AllTableFields.ContainsKey(item.Value)) newValue = templateInfo.AllTableFields[item.Value];
|
|
if (pvalue.Contains(newValue))
|
|
{
|
|
pvalue = pvalue.Replace(string.Format("\"FieldName\":\"{0}\",", newValue), string.Format("\"FieldName\":\"{0}\",", item.Key));
|
|
}
|
|
else
|
|
{
|
|
if (newValue.Contains(templateInfo.MainTableName)) newValue = newValue.Replace(templateInfo.MainTableName + ".", string.Empty);
|
|
if (pvalue.Contains(newValue)) pvalue = pvalue.Replace(string.Format("\"FieldName\":\"{0}\",", newValue), string.Format("\"FieldName\":\"{0}\",", item.Key));
|
|
}
|
|
}
|
|
|
|
List<IConditionalModel>? newPvalue = new List<IConditionalModel>();
|
|
if (pvalue.IsNotEmptyOrNull()) newPvalue = _visualDevRepository.AsSugarClient().Utilities.JsonToConditionalModels(pvalue);
|
|
|
|
sql = _visualDevRepository.AsSugarClient().SqlQueryable<dynamic>(sql).Where(newPvalue).ToSqlString();
|
|
}
|
|
}
|
|
|
|
return sql;
|
|
}
|
|
private List<IConditionalModel> GetIConditionalModelListByTableName(List<IConditionalModel> cList, string tableName)
|
|
{
|
|
for (int i = 0; i < cList.Count; i++)
|
|
{
|
|
if (cList[i] is ConditionalTree)
|
|
{
|
|
var newItem = (ConditionalTree)cList[i];
|
|
for (int j = 0; j < newItem.ConditionalList.Count; j++)
|
|
{
|
|
var value = GetIConditionalModelListByTableName(new List<IConditionalModel>() { newItem.ConditionalList[j].Value }, tableName);
|
|
if (value != null && value.Any()) newItem.ConditionalList[j] = new KeyValuePair<WhereType, IConditionalModel>(newItem.ConditionalList[j].Key, value.First());
|
|
else newItem.ConditionalList.RemoveAt(j);
|
|
}
|
|
|
|
if (newItem.ConditionalList.Any()) cList[i] = newItem;
|
|
else cList.RemoveAt(i);
|
|
}
|
|
else if (cList[i] is ConditionalModel)
|
|
{
|
|
var newItem = (ConditionalModel)cList[i];
|
|
if (!newItem.FieldName.Contains(tableName)) cList.RemoveAt(i);
|
|
}
|
|
}
|
|
|
|
return cList;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 组装单条信息查询sql.
|
|
/// </summary>
|
|
/// <param name="id">id.</param>
|
|
/// <param name="mainPrimary">主键.</param>
|
|
/// <param name="templateInfo">模板.</param>
|
|
/// <param name="tableFieldKeyValue">联表查询 表字段名称 对应 前端字段名称 (应对oracle 查询字段长度不能超过30个).</param>
|
|
/// <returns></returns>
|
|
private string GetInfoQuerySql(string id, string mainPrimary, TemplateParsingBase templateInfo, ref Dictionary<string, string> tableFieldKeyValue)
|
|
{
|
|
List<string> fields = new List<string>();
|
|
string? sql = string.Empty; // 查询sql
|
|
|
|
// 没有副表,只查询主表
|
|
if (!templateInfo.AuxiliaryTableFieldsModelList.Any())
|
|
{
|
|
fields.Add(mainPrimary); // 主表主键
|
|
templateInfo.MainTableFieldsModelList.Where(x => x.__vModel__.IsNotEmptyOrNull()).ToList().ForEach(item => fields.Add(item.__vModel__)); // 主表列名
|
|
sql = string.Format("select {0} from {1} where {2}='{3}'", string.Join(",", fields), templateInfo.MainTableName, mainPrimary, id);
|
|
}
|
|
else
|
|
{
|
|
#region 所有主表、副表 字段名
|
|
fields.Add(templateInfo.MainTableName + "." + mainPrimary); // 主表主键
|
|
tableFieldKeyValue.Add(mainPrimary.ToUpper(), mainPrimary);
|
|
for (int i = 0; i < templateInfo.SingleFormData.Count; i++)
|
|
{
|
|
string? vmodel = templateInfo.SingleFormData[i].__vModel__.ReplaceRegex(@"(\w+)_jnpf_", ""); // Field
|
|
if (vmodel.IsNotEmptyOrNull())
|
|
{
|
|
fields.Add(templateInfo.SingleFormData[i].__config__.tableName + "." + vmodel + " FIELD" + i); // TableName.Field_0
|
|
tableFieldKeyValue.Add("FIELD" + i, templateInfo.SingleFormData[i].__vModel__);
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region 所有副表 关联字段
|
|
List<string>? ctNameList = templateInfo.AuxiliaryTableFieldsModelList.Select(x => x.__config__.tableName).Distinct().ToList();
|
|
List<string>? relationKey = new List<string>();
|
|
relationKey.Add(string.Format(" {0}.{1}='{2}' ", templateInfo.MainTableName, mainPrimary, id)); // 主表ID
|
|
ctNameList.ForEach(tName =>
|
|
{
|
|
string? tableField = templateInfo.AllTable.Find(tf => tf.table == tName)?.tableField;
|
|
relationKey.Add(string.Format(" {0}.{1}={2}.{3} ", templateInfo.MainTableName, mainPrimary, tName, tableField));
|
|
});
|
|
string? whereStr = string.Join(" and ", relationKey);
|
|
#endregion
|
|
|
|
sql = string.Format("select {0} from {1} where {2}", string.Join(",", fields), templateInfo.MainTableName + "," + string.Join(",", ctNameList), whereStr); // 多表, 联合查询
|
|
}
|
|
|
|
return sql;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 组装 查询 json.
|
|
/// </summary>
|
|
/// <param name="queryJson"></param>
|
|
/// <param name="columnDesign"></param>
|
|
/// <returns></returns>
|
|
private List<IConditionalModel> GetQueryJson(string queryJson, ColumnDesignModel columnDesign)
|
|
{
|
|
// 将查询的关键字json转成Dictionary
|
|
Dictionary<string, object> keywordJsonDic = string.IsNullOrEmpty(queryJson) ? null : queryJson.ToObject<Dictionary<string, object>>();
|
|
var conModels = new List<IConditionalModel>();
|
|
if (keywordJsonDic != null)
|
|
{
|
|
foreach (KeyValuePair<string, object> item in keywordJsonDic)
|
|
{
|
|
var model = columnDesign.searchList.Find(it => it.__vModel__.Equals(item.Key));
|
|
switch (model.jnpfKey)
|
|
{
|
|
case JnpfKeyConst.DATE:
|
|
{
|
|
var timeRange = item.Value.ToObject<List<string>>();
|
|
var startTime = timeRange.First().TimeStampToDateTime();
|
|
var endTime = timeRange.Last().TimeStampToDateTime();
|
|
if (model.format == "yyyy-MM-dd HH:mm:ss")
|
|
{
|
|
conModels.Add(new ConditionalCollections()
|
|
{
|
|
ConditionalList = new List<KeyValuePair<WhereType, ConditionalModel>>()
|
|
{
|
|
new KeyValuePair<WhereType, ConditionalModel>(WhereType.And, new ConditionalModel
|
|
{
|
|
FieldName = item.Key,
|
|
ConditionalType = ConditionalType.GreaterThanOrEqual,
|
|
FieldValue = startTime.ToString(),
|
|
FieldValueConvertFunc = it => Convert.ToDateTime(it)
|
|
}),
|
|
new KeyValuePair<WhereType, ConditionalModel>(WhereType.And, new ConditionalModel
|
|
{
|
|
FieldName = item.Key,
|
|
ConditionalType = ConditionalType.LessThanOrEqual,
|
|
FieldValue = endTime.ToString(),
|
|
FieldValueConvertFunc = it => Convert.ToDateTime(it)
|
|
})
|
|
}
|
|
});
|
|
}
|
|
else
|
|
{
|
|
conModels.Add(new ConditionalCollections()
|
|
{
|
|
ConditionalList = new List<KeyValuePair<WhereType, ConditionalModel>>()
|
|
{
|
|
new KeyValuePair<WhereType, ConditionalModel>(WhereType.And, new ConditionalModel
|
|
{
|
|
FieldName = item.Key,
|
|
ConditionalType = ConditionalType.GreaterThanOrEqual,
|
|
FieldValue = new DateTime(startTime.Year, startTime.Month, startTime.Day, 0, 0, 0, 0).ToString(),
|
|
FieldValueConvertFunc = it => Convert.ToDateTime(it)
|
|
}),
|
|
new KeyValuePair<WhereType, ConditionalModel>(WhereType.And, new ConditionalModel
|
|
{
|
|
FieldName = item.Key,
|
|
ConditionalType = ConditionalType.LessThanOrEqual,
|
|
FieldValue = new DateTime(endTime.Year, endTime.Month, endTime.Day, 23, 59, 59, 999).ToString(),
|
|
FieldValueConvertFunc = it => Convert.ToDateTime(it)
|
|
})
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
break;
|
|
case JnpfKeyConst.TIME:
|
|
{
|
|
var timeRange = item.Value.ToObject<List<string>>();
|
|
var startTime = timeRange.First();
|
|
var endTime = timeRange.Last();
|
|
conModels.Add(new ConditionalCollections()
|
|
{
|
|
ConditionalList = new List<KeyValuePair<WhereType, ConditionalModel>>()
|
|
{
|
|
new KeyValuePair<WhereType, ConditionalModel>(WhereType.And, new ConditionalModel
|
|
{
|
|
FieldName = item.Key,
|
|
ConditionalType = ConditionalType.GreaterThanOrEqual,
|
|
FieldValue = startTime,
|
|
FieldValueConvertFunc = it => Convert.ToDateTime(it)
|
|
}),
|
|
new KeyValuePair<WhereType, ConditionalModel>(WhereType.And, new ConditionalModel
|
|
{
|
|
FieldName = item.Key,
|
|
ConditionalType = ConditionalType.LessThanOrEqual,
|
|
FieldValue = endTime,
|
|
FieldValueConvertFunc = it => Convert.ToDateTime(it)
|
|
})
|
|
}
|
|
});
|
|
}
|
|
|
|
break;
|
|
case JnpfKeyConst.CREATETIME:
|
|
case JnpfKeyConst.MODIFYTIME:
|
|
{
|
|
var timeRange = item.Value.ToObject<List<string>>();
|
|
var startTime = timeRange.First().TimeStampToDateTime();
|
|
var endTime = timeRange.Last().TimeStampToDateTime();
|
|
conModels.Add(new ConditionalCollections()
|
|
{
|
|
ConditionalList = new List<KeyValuePair<WhereType, ConditionalModel>>()
|
|
{
|
|
new KeyValuePair<WhereType, ConditionalModel>(WhereType.And, new ConditionalModel
|
|
{
|
|
FieldName = item.Key,
|
|
ConditionalType = ConditionalType.GreaterThanOrEqual,
|
|
FieldValue = new DateTime(startTime.Year, startTime.Month, startTime.Day, 0, 0, 0, 0).ToString(),
|
|
FieldValueConvertFunc = it => Convert.ToDateTime(it)
|
|
}),
|
|
new KeyValuePair<WhereType, ConditionalModel>(WhereType.And, new ConditionalModel
|
|
{
|
|
FieldName = item.Key,
|
|
ConditionalType = ConditionalType.LessThanOrEqual,
|
|
FieldValue = new DateTime(endTime.Year, endTime.Month, endTime.Day, 23, 59, 59, 999).ToString(),
|
|
FieldValueConvertFunc = it => Convert.ToDateTime(it)
|
|
})
|
|
}
|
|
});
|
|
}
|
|
|
|
break;
|
|
case JnpfKeyConst.NUMINPUT:
|
|
case JnpfKeyConst.CALCULATE:
|
|
{
|
|
List<string> numArray = item.Value.ToObject<List<string>>();
|
|
var startNum = numArray.First().ParseToInt();
|
|
var endNum = numArray.Last() == null ? Int32.MaxValue : numArray.Last().ParseToInt();
|
|
conModels.Add(new ConditionalCollections()
|
|
{
|
|
ConditionalList = new List<KeyValuePair<WhereType, ConditionalModel>>()
|
|
{
|
|
new KeyValuePair<WhereType, ConditionalModel>(WhereType.And, new ConditionalModel
|
|
{
|
|
CSharpTypeName="int",
|
|
FieldName = item.Key,
|
|
ConditionalType = ConditionalType.GreaterThanOrEqual,
|
|
FieldValue = startNum.ToString()
|
|
}),
|
|
new KeyValuePair<WhereType, ConditionalModel>(WhereType.And, new ConditionalModel
|
|
{
|
|
CSharpTypeName="int",
|
|
FieldName = item.Key,
|
|
ConditionalType = ConditionalType.LessThanOrEqual,
|
|
FieldValue = endNum.ToString()
|
|
})
|
|
}
|
|
});
|
|
}
|
|
|
|
break;
|
|
case JnpfKeyConst.CHECKBOX:
|
|
{
|
|
//if (model.searchType.Equals(1))
|
|
// conModels.Add(new ConditionalModel { FieldName = item.Key, ConditionalType = ConditionalType.Equal, FieldValue = item.Value.ToString() });
|
|
//else
|
|
conModels.Add(new ConditionalCollections()
|
|
{
|
|
ConditionalList = new List<KeyValuePair<WhereType, ConditionalModel>>()
|
|
{
|
|
new KeyValuePair<WhereType, ConditionalModel>(WhereType.And, new ConditionalModel
|
|
{
|
|
FieldName = item.Key,
|
|
ConditionalType = ConditionalType.Like,
|
|
FieldValue = item.Value.ToJsonString()
|
|
})
|
|
}
|
|
});
|
|
}
|
|
|
|
break;
|
|
case JnpfKeyConst.ROLESELECT:
|
|
case JnpfKeyConst.GROUPSELECT:
|
|
case JnpfKeyConst.POSSELECT:
|
|
case JnpfKeyConst.USERSELECT:
|
|
case JnpfKeyConst.DEPSELECT:
|
|
{
|
|
var itemValue = item.Value.ToString().Contains("[") ? item.Value.ToJsonString() : item.Value.ToString();
|
|
|
|
// 多选时为模糊查询
|
|
if (model.multiple)
|
|
{
|
|
conModels.Add(new ConditionalCollections()
|
|
{
|
|
ConditionalList = new List<KeyValuePair<WhereType, ConditionalModel>>()
|
|
{
|
|
new KeyValuePair<WhereType, ConditionalModel>(WhereType.And, new ConditionalModel
|
|
{
|
|
FieldName = item.Key,
|
|
ConditionalType = ConditionalType.Like,
|
|
FieldValue = item.Value.ToString()
|
|
})
|
|
}
|
|
});
|
|
}
|
|
else
|
|
{
|
|
conModels.Add(new ConditionalCollections()
|
|
{
|
|
ConditionalList = new List<KeyValuePair<WhereType, ConditionalModel>>()
|
|
{
|
|
new KeyValuePair<WhereType, ConditionalModel>(WhereType.And, new ConditionalModel
|
|
{
|
|
FieldName = item.Key,
|
|
ConditionalType = ConditionalType.Equal,
|
|
FieldValue = item.Value.ToString()
|
|
})
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
break;
|
|
case JnpfKeyConst.USERSSELECT:
|
|
{
|
|
if (item.Value != null)
|
|
{
|
|
if (model.multiple)
|
|
{
|
|
var rIdList = _visualDevRepository.AsSugarClient().Queryable<UserRelationEntity>().Where(x => x.UserId.Equals(item.Value.ToString().Replace("--user", string.Empty))).Select(x => new { x.ObjectId, x.ObjectType }).ToList();
|
|
var objIdList = new List<string>() { item.Value.ToString() };
|
|
rIdList.ForEach(x =>
|
|
{
|
|
if (x.ObjectType.Equals("Organize"))
|
|
{
|
|
objIdList.Add(x.ObjectId + "--company");
|
|
objIdList.Add(x.ObjectId + "--department");
|
|
}
|
|
else
|
|
{
|
|
objIdList.Add(x.ObjectId + "--" + x.ObjectType.ToLower());
|
|
}
|
|
});
|
|
|
|
var whereList = new List<KeyValuePair<WhereType, ConditionalModel>>();
|
|
for (var i = 0; i < objIdList.Count(); i++)
|
|
{
|
|
if (i == 0)
|
|
{
|
|
whereList.Add(new KeyValuePair<WhereType, ConditionalModel>(WhereType.And, new ConditionalModel
|
|
{
|
|
FieldName = item.Key,
|
|
ConditionalType = ConditionalType.Like,
|
|
FieldValue = objIdList[i]
|
|
}));
|
|
}
|
|
else
|
|
{
|
|
whereList.Add(new KeyValuePair<WhereType, ConditionalModel>(WhereType.Or, new ConditionalModel
|
|
{
|
|
FieldName = item.Key,
|
|
ConditionalType = ConditionalType.Like,
|
|
FieldValue = objIdList[i]
|
|
}));
|
|
}
|
|
}
|
|
|
|
conModels.Add(new ConditionalCollections() { ConditionalList = whereList });
|
|
}
|
|
else
|
|
{
|
|
conModels.Add(new ConditionalCollections()
|
|
{
|
|
ConditionalList = new List<KeyValuePair<WhereType, ConditionalModel>>()
|
|
{
|
|
new KeyValuePair<WhereType, ConditionalModel>(WhereType.And, new ConditionalModel
|
|
{
|
|
FieldName = item.Key,
|
|
ConditionalType = ConditionalType.Equal,
|
|
FieldValue = item.Value.ToString()
|
|
})
|
|
}
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
break;
|
|
case JnpfKeyConst.TREESELECT:
|
|
{
|
|
if (item.Value.IsNotEmptyOrNull() && item.Value.ToString().Contains("["))
|
|
{
|
|
var value = item.Value.ToObject<List<string>>();
|
|
|
|
conModels.Add(new ConditionalCollections()
|
|
{
|
|
ConditionalList = new List<KeyValuePair<WhereType, ConditionalModel>>()
|
|
{
|
|
new KeyValuePair<WhereType, ConditionalModel>(WhereType.And, new ConditionalModel
|
|
{
|
|
FieldName = item.Key,
|
|
ConditionalType = ConditionalType.Like,
|
|
FieldValue = value.LastOrDefault()
|
|
})
|
|
}
|
|
});
|
|
}
|
|
else
|
|
{
|
|
// 多选时为模糊查询
|
|
if (model.multiple)
|
|
{
|
|
conModels.Add(new ConditionalCollections()
|
|
{
|
|
ConditionalList = new List<KeyValuePair<WhereType, ConditionalModel>>()
|
|
{
|
|
new KeyValuePair<WhereType, ConditionalModel>(WhereType.And, new ConditionalModel
|
|
{
|
|
FieldName = item.Key,
|
|
ConditionalType = ConditionalType.Like,
|
|
FieldValue = item.Value.ToString()
|
|
})
|
|
}
|
|
});
|
|
}
|
|
else
|
|
{
|
|
conModels.Add(new ConditionalCollections()
|
|
{
|
|
ConditionalList = new List<KeyValuePair<WhereType, ConditionalModel>>()
|
|
{
|
|
new KeyValuePair<WhereType, ConditionalModel>(WhereType.And, new ConditionalModel
|
|
{
|
|
FieldName = item.Key,
|
|
ConditionalType = ConditionalType.Equal,
|
|
FieldValue = item.Value.ToString()
|
|
})
|
|
}
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
break;
|
|
case JnpfKeyConst.CURRORGANIZE:
|
|
{
|
|
var itemValue = item.Value.ToString().Contains("[") ? item.Value?.ToString().ToObject<List<string>>().LastOrDefault() : item.Value.ToString();
|
|
conModels.Add(new ConditionalCollections()
|
|
{
|
|
ConditionalList = new List<KeyValuePair<WhereType, ConditionalModel>>()
|
|
{
|
|
new KeyValuePair<WhereType, ConditionalModel>(WhereType.And, new ConditionalModel
|
|
{
|
|
FieldName = item.Key,
|
|
ConditionalType = ConditionalType.Equal,
|
|
FieldValue = itemValue
|
|
})
|
|
}
|
|
});
|
|
}
|
|
|
|
break;
|
|
case JnpfKeyConst.CASCADER:
|
|
{
|
|
var itemValue = item.Value.ToString().Contains("[") ? item.Value?.ToString().ToObject<List<string>>().LastOrDefault() : item.Value.ToString();
|
|
conModels.Add(new ConditionalCollections()
|
|
{
|
|
ConditionalList = new List<KeyValuePair<WhereType, ConditionalModel>>()
|
|
{
|
|
new KeyValuePair<WhereType, ConditionalModel>(WhereType.And, new ConditionalModel
|
|
{
|
|
FieldName = item.Key,
|
|
ConditionalType = ConditionalType.Like,
|
|
FieldValue = itemValue.Replace("[", string.Empty).Replace("]", string.Empty)
|
|
})
|
|
}
|
|
});
|
|
}
|
|
break;
|
|
case JnpfKeyConst.ADDRESS:
|
|
case JnpfKeyConst.COMSELECT:
|
|
{
|
|
// 多选时为模糊查询
|
|
if (model.multiple)
|
|
{
|
|
var value = item.Value?.ToString().ToObject<List<string>>();
|
|
if (value.Any())
|
|
{
|
|
conModels.Add(new ConditionalCollections()
|
|
{
|
|
ConditionalList = new List<KeyValuePair<WhereType, ConditionalModel>>()
|
|
{
|
|
new KeyValuePair<WhereType, ConditionalModel>(WhereType.And, new ConditionalModel
|
|
{
|
|
FieldName = item.Key,
|
|
ConditionalType = ConditionalType.Like,
|
|
FieldValue = item.Value.ToJsonString().Replace("[", string.Empty)
|
|
})
|
|
}
|
|
});
|
|
}
|
|
}
|
|
else
|
|
{
|
|
var itemValue = item.Value.ToString().Contains("[") ? item.Value.ToJsonString() : item.Value.ToString();
|
|
conModels.Add(new ConditionalCollections()
|
|
{
|
|
ConditionalList = new List<KeyValuePair<WhereType, ConditionalModel>>()
|
|
{
|
|
new KeyValuePair<WhereType, ConditionalModel>(WhereType.And, new ConditionalModel
|
|
{
|
|
FieldName = item.Key,
|
|
ConditionalType = ConditionalType.Equal,
|
|
FieldValue = itemValue
|
|
})
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
break;
|
|
case JnpfKeyConst.SELECT:
|
|
{
|
|
var itemValue = item.Value.ToString().Contains("[") ? item.Value.ToJsonString() : item.Value.ToString();
|
|
//modified by ly on 20230407
|
|
JArray jarr = null;
|
|
if (itemValue!.Contains("["))
|
|
{
|
|
jarr = JArray.Parse(itemValue);
|
|
}
|
|
|
|
// 多选时为模糊查询
|
|
if (model.multiple)
|
|
{
|
|
conModels.Add(new ConditionalCollections()
|
|
{
|
|
ConditionalList = new List<KeyValuePair<WhereType, ConditionalModel>>()
|
|
{
|
|
new KeyValuePair<WhereType, ConditionalModel>(WhereType.And, new ConditionalModel
|
|
{
|
|
FieldName = item.Key,
|
|
ConditionalType = ConditionalType.Like,
|
|
FieldValue = itemValue
|
|
})
|
|
}
|
|
});
|
|
}
|
|
//modified by ly on 20230407
|
|
else if (jarr?.Children() != null && jarr?.Children().ToList().Count > 1)
|
|
{
|
|
var values = jarr.ToList().Select(t => t.Value<string>()).ToList();
|
|
var condition = new ConditionalCollections();
|
|
condition.ConditionalList = new List<KeyValuePair<WhereType, ConditionalModel>>(values.Count);
|
|
values.ForEach(x =>
|
|
{
|
|
condition.ConditionalList.Add(new KeyValuePair<WhereType, ConditionalModel>(WhereType.Or, new ConditionalModel
|
|
{
|
|
FieldName = item.Key,
|
|
ConditionalType = ConditionalType.Equal,
|
|
FieldValue = x,
|
|
}));
|
|
});
|
|
conModels.Add(condition);
|
|
}
|
|
else
|
|
{
|
|
//modified by ly on 20230407
|
|
itemValue = Regex.Match(itemValue, @"\[(.+)\]").Groups[1].Value;
|
|
itemValue = itemValue.Trim('"');
|
|
conModels.Add(new ConditionalCollections()
|
|
{
|
|
ConditionalList = new List<KeyValuePair<WhereType, ConditionalModel>>()
|
|
{
|
|
new KeyValuePair<WhereType, ConditionalModel>(WhereType.And, new ConditionalModel
|
|
{
|
|
FieldName = item.Key,
|
|
ConditionalType = ConditionalType.Equal,
|
|
FieldValue = itemValue
|
|
})
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
break;
|
|
default:
|
|
{
|
|
var itemValue = item.Value.ToString().Contains("[") ? item.Value.ToJsonString() : item.Value.ToString();
|
|
|
|
if (model.searchType == 1)
|
|
{
|
|
conModels.Add(new ConditionalCollections()
|
|
{
|
|
ConditionalList = new List<KeyValuePair<WhereType, ConditionalModel>>()
|
|
{
|
|
new KeyValuePair<WhereType, ConditionalModel>(WhereType.And, new ConditionalModel
|
|
{
|
|
FieldName = item.Key,
|
|
ConditionalType = ConditionalType.Equal,
|
|
FieldValue = itemValue
|
|
})
|
|
}
|
|
});
|
|
}
|
|
else
|
|
{
|
|
conModels.Add(new ConditionalCollections()
|
|
{
|
|
ConditionalList = new List<KeyValuePair<WhereType, ConditionalModel>>()
|
|
{
|
|
new KeyValuePair<WhereType, ConditionalModel>(WhereType.And, new ConditionalModel
|
|
{
|
|
FieldName = item.Key,
|
|
ConditionalType = ConditionalType.Like,
|
|
FieldValue = itemValue
|
|
})
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
return conModels;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 组装高级查询条件.
|
|
/// </summary>
|
|
/// <param name="superQueryJson"></param>
|
|
/// <returns></returns>
|
|
private List<IConditionalModel> GetSuperQueryJson(string superQueryJson)
|
|
{
|
|
List<IConditionalModel> conModels = new List<IConditionalModel>();
|
|
if (superQueryJson.IsNotEmptyOrNull())
|
|
{
|
|
var querList = superQueryJson.ToObject<List<Dictionary<string, object>>>();
|
|
|
|
querList.DistinctBy(x => x["field"]).ToList().ForEach(item =>
|
|
{
|
|
var items = querList.Where(x => x.ContainsValue(item["field"].ToString())).ToList();
|
|
string _CSharpTypeName = item.ContainsKey("CSharpTypeName") ? item["CSharpTypeName"].ToString() : null;
|
|
if (items.Count() > 1)
|
|
{
|
|
var whereTypeList = new List<KeyValuePair<WhereType, SqlSugar.ConditionalModel>>();
|
|
items.ForEach(it =>
|
|
{
|
|
var whereType = it["whereType"].ToString().ToObject<WhereType>();
|
|
var conditionalType = it["ConditionalType"].ToString().ToObject<ConditionalType>();
|
|
whereTypeList.Add(new KeyValuePair<WhereType, ConditionalModel>(whereType, new ConditionalModel
|
|
{
|
|
CSharpTypeName = _CSharpTypeName,
|
|
FieldName = it["field"].ToString(),
|
|
ConditionalType = conditionalType,
|
|
FieldValue = it["fieldValue"].IsNotEmptyOrNull() ? it["fieldValue"].ToString() : null
|
|
}));
|
|
});
|
|
conModels.Add(new ConditionalCollections() { ConditionalList = whereTypeList });
|
|
}
|
|
else
|
|
{
|
|
var whereType = item["whereType"].ToString().ToObject<WhereType>();
|
|
var conditionalType = item["ConditionalType"].ToString().ToObject<ConditionalType>();
|
|
conModels.Add(new ConditionalCollections()
|
|
{
|
|
ConditionalList = new List<KeyValuePair<WhereType, SqlSugar.ConditionalModel>>()
|
|
{
|
|
new KeyValuePair<WhereType, ConditionalModel>(whereType, new ConditionalModel
|
|
{
|
|
CSharpTypeName = _CSharpTypeName,
|
|
FieldName = item["field"].ToString(),
|
|
ConditionalType = conditionalType,
|
|
FieldValue = item["fieldValue"].IsNotEmptyOrNull()?item["fieldValue"].ToString() : null
|
|
})
|
|
}
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
return conModels;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 显示列有子表字段,根据主键查询所有子表.
|
|
/// </summary>
|
|
/// <param name="templateInfo"></param>
|
|
/// <param name="primaryKey"></param>
|
|
/// <param name="querList"></param>
|
|
/// <param name="superQuerList"></param>
|
|
/// <param name="result"></param>
|
|
/// <param name="dataPermissions"></param>
|
|
/// <param name="dbLink"></param>
|
|
/// <returns></returns>
|
|
private async Task<PageResult<Dictionary<string, object>>> GetListChildTable(
|
|
TemplateParsingBase templateInfo,
|
|
string primaryKey,
|
|
List<IConditionalModel> querList,
|
|
List<IConditionalModel> superQuerList,
|
|
PageResult<Dictionary<string, object>> result,
|
|
List<IConditionalModel> dataPermissions)
|
|
{
|
|
var ids = new List<object>();
|
|
result.list.ForEach(item => ids.Add(item[primaryKey]));
|
|
|
|
var childTableList = new Dictionary<string, List<string>>();
|
|
|
|
templateInfo.AllFieldsModel.Where(x => x.__config__.jnpfKey.Equals(JnpfKeyConst.TABLE)).ToList().ForEach(ctitem =>
|
|
{
|
|
templateInfo.AllFieldsModel.Where(x => x.__vModel__.Contains(ctitem.__vModel__ + "-")).ToList().ForEach(item =>
|
|
{
|
|
var value = item.__vModel__.Split("-").Last();
|
|
if (value.IsNotEmptyOrNull())
|
|
{
|
|
if (childTableList.ContainsKey(ctitem.__config__.tableName)) childTableList[ctitem.__config__.tableName].Add(value);
|
|
else childTableList.Add(ctitem.__config__.tableName, new List<string>() { value });
|
|
}
|
|
});
|
|
});
|
|
|
|
var relationField = new Dictionary<string, string>();
|
|
templateInfo.ChildTableFieldsModelList.ForEach(item =>
|
|
{
|
|
var tableField = templateInfo.AllTable.Find(tf => tf.table == item.__config__.tableName)?.tableField;
|
|
if (!relationField.ContainsKey(item.__config__.tableName)) relationField.Add(item.__config__.tableName, tableField);
|
|
});
|
|
|
|
// 捞取 所有子表查询条件 <tableName , where>
|
|
var childTableQuery = new Dictionary<string, List<IConditionalModel>>();
|
|
var query = querList.ToObject<List<ConditionalCollections>>();
|
|
var superQuery = superQuerList.ToObject<List<ConditionalCollections>>();
|
|
foreach (var item in templateInfo.ChildTableFields)
|
|
{
|
|
var conList = query.Where(x => x.ConditionalList.Any(xx => xx.Value.FieldName.Equals(item.Key))).ToList();
|
|
if (conList.Any())
|
|
{
|
|
var tableName = item.Value.Split(".").FirstOrDefault();
|
|
foreach (var it in conList) it.ConditionalList.ForEach(x => x.Value.FieldName = item.Value);
|
|
if (!childTableQuery.ContainsKey(tableName)) childTableQuery.Add(tableName, new List<IConditionalModel>());
|
|
childTableQuery[tableName].AddRange(conList);
|
|
}
|
|
var superConList = superQuery.Where(x => x.ConditionalList.Any(xx => xx.Value.FieldName.Equals(item.Key))).ToList();
|
|
if (superConList.Any())
|
|
{
|
|
var tableName = item.Value.Split(".").FirstOrDefault();
|
|
foreach (var it in superConList) it.ConditionalList.ForEach(x => x.Value.FieldName = item.Value);
|
|
if (!childTableQuery.ContainsKey(tableName)) childTableQuery.Add(tableName, new List<IConditionalModel>());
|
|
childTableQuery[tableName].AddRange(superConList);
|
|
}
|
|
}
|
|
|
|
foreach (var item in childTableList)
|
|
{
|
|
item.Value.Add(relationField[item.Key]);
|
|
var sql = string.Format("select {0} from {1} where {2} in('{3}')", string.Join(",", item.Value), item.Key, relationField[item.Key], string.Join("','", ids));
|
|
if (childTableQuery.ContainsKey(item.Key)) // 子表查询条件
|
|
{
|
|
var itemWhere = _visualDevRepository.AsSugarClient().SqlQueryable<dynamic>("@").Where(childTableQuery[item.Key]).ToSqlString();
|
|
if (itemWhere.Contains("WHERE")) sql = string.Format(" {0} and {1} ", sql, itemWhere.Split("WHERE").Last());
|
|
}
|
|
|
|
// 拼接数据权限
|
|
var dataPermissionsList = new List<IConditionalModel>();
|
|
if (dataPermissions != null && dataPermissions.Any())
|
|
{
|
|
var pList = new List<object>();
|
|
var allPersissions = dataPermissions.ToObject<List<object>>();
|
|
allPersissions.ForEach(it =>
|
|
{
|
|
if (it.ToJsonString().Contains(item.Key + ".")) pList.Add(it);
|
|
});
|
|
if (pList.Any())
|
|
{
|
|
dataPermissionsList = _visualDevRepository.AsSugarClient().Utilities.JsonToConditionalModels(pList.ToJsonString());
|
|
dataPermissionsList = GetIConditionalModelListByTableName(dataPermissionsList, item.Key);
|
|
var json = dataPermissionsList.ToJsonString().Replace(item.Key + ".", string.Empty);
|
|
dataPermissionsList = _visualDevRepository.AsSugarClient().Utilities.JsonToConditionalModels(json);
|
|
}
|
|
}
|
|
sql = _visualDevRepository.AsSugarClient().SqlQueryable<dynamic>(sql).Where(dataPermissionsList).ToSqlString();
|
|
|
|
var dt = _databaseService.GetInterFaceData(templateInfo.DbLink, sql).ToObject<List<Dictionary<string, object>>>();
|
|
var vModel = templateInfo.AllFieldsModel.Find(x => x.__config__.tableName == item.Key)?.__vModel__;
|
|
|
|
if (vModel.IsNotEmptyOrNull())
|
|
{
|
|
foreach (var it in result.list)
|
|
{
|
|
var rows = dt.Where(x => x[relationField[item.Key]].ToString().Equals(it[primaryKey].ToString())).ToList();
|
|
var childTableModel = templateInfo.ChildTableFieldsModelList.First(x => x.__vModel__.Equals(vModel));
|
|
|
|
var datas = new List<Dictionary<string, object>>();
|
|
if (childTableModel.__config__.children.Any(x => x.__config__.templateJson != null && x.__config__.templateJson.Any()))
|
|
datas = (await _formDataParsing.GetKeyData(childTableModel.__config__.children.Where(x => x.__config__.templateJson != null && x.__config__.templateJson.Any()).ToList(), rows, templateInfo.ColumnData, "List", 2, "F_Id", it));
|
|
datas = await _formDataParsing.GetKeyData(childTableModel.__config__.children.Where(x => x.__config__.templateJson == null).ToList(), rows, templateInfo.ColumnData);
|
|
var newDatas = datas.Copy();
|
|
newDatas.ForEach(x => x.Remove(relationField[item.Key]));
|
|
it.Add(vModel, newDatas);
|
|
}
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取处理子表数据.
|
|
/// </summary>
|
|
/// <param name="templateInfo">模板信息.</param>
|
|
/// <param name="link">数据库连接.</param>
|
|
/// <param name="dataMap">全部数据.</param>
|
|
/// <param name="newDataMap">新数据.</param>
|
|
/// <param name="isDetail">是否详情转换.</param>
|
|
/// <returns></returns>
|
|
private async Task<Dictionary<string, object>> GetChildTableData(TemplateParsingBase templateInfo, DbLinkEntity? link, Dictionary<string, object> dataMap, Dictionary<string, object> newDataMap, bool isDetail = false)
|
|
{
|
|
foreach (var model in templateInfo.ChildTableFieldsModelList)
|
|
{
|
|
if (!string.IsNullOrEmpty(model.__vModel__))
|
|
{
|
|
if ("table".Equals(model.__config__.jnpfKey))
|
|
{
|
|
List<string> feilds = new List<string>();
|
|
foreach (FieldsModel? childModel in model.__config__.children) if (!string.IsNullOrEmpty(childModel.__vModel__)) feilds.Add(childModel.__vModel__); // 拼接查询字段
|
|
string relationMainFeildValue = string.Empty;
|
|
string childSql = string.Format("select {0} from {1} where 1=1 ", string.Join(",", feilds), model.__config__.tableName); // 查询子表数据
|
|
foreach (Engine.Model.TableModel? tableMap in templateInfo.AllTable.Where(x => !x.table.Equals(templateInfo.MainTableName)).ToList())
|
|
{
|
|
if (tableMap.table.Equals(model.__config__.tableName))
|
|
{
|
|
if (dataMap.ContainsKey(tableMap.relationField)) childSql += string.Format(" And {0}='{1}'", tableMap.tableField, dataMap[tableMap.relationField]); // 外键
|
|
if (dataMap.ContainsKey(tableMap.relationField.ToUpper())) childSql += string.Format(" And {0}='{1}'", tableMap.tableField, dataMap[tableMap.relationField.ToUpper()]); // 外键
|
|
if (dataMap.ContainsKey(tableMap.relationField.ToLower())) childSql += string.Format(" And {0}='{1}'", tableMap.tableField, dataMap[tableMap.relationField.ToLower()]); // 外键
|
|
List<Dictionary<string, object>>? childTableData = _databaseService.GetInterFaceData(link, childSql).ToJsonString().ToObject<List<Dictionary<string, object>>>();
|
|
if (!isDetail)
|
|
{
|
|
List<Dictionary<string, object>>? childData = _databaseService.GetInterFaceData(link, childSql).ToJsonString().ToObject<List<Dictionary<string, object>>>();
|
|
childTableData = _formDataParsing.GetTableDataInfo(childData, model.__config__.children, "detail");
|
|
}
|
|
|
|
#region 获取关联表单属性 和 弹窗选择属性
|
|
foreach (var item in model.__config__.children.Where(x => x.__config__.jnpfKey == "relationForm").ToList())
|
|
{
|
|
foreach (var dataItem in childTableData)
|
|
{
|
|
if (item.__vModel__.IsNotEmptyOrNull() && dataItem.ContainsKey(item.__vModel__) && dataItem[item.__vModel__] != null)
|
|
{
|
|
var relationValueId = dataItem[item.__vModel__].ToString(); // 获取关联表单id
|
|
var relationInfo = await _visualDevRepository.AsQueryable().FirstAsync(x => x.Id == item.modelId); // 获取 关联表单 转换后的数据
|
|
var relationValueStr = string.Empty;
|
|
// modified by pf 2023-04-12 返回值不序列化
|
|
var relationValue = await GetHaveTableInfoDetails(relationValueId, relationInfo);
|
|
if (relationValue != null)
|
|
{
|
|
// 添加到 子表 列
|
|
model.__config__.children.Where(x => x.relationField.ReplaceRegex(@"_jnpfTable_(\w+)", string.Empty) == item.__vModel__).ToList().ForEach(citem =>
|
|
{
|
|
citem.__vModel__ = item.__vModel__ + "_" + citem.showField;
|
|
if (relationValue.ContainsKey(citem.showField)) dataItem.Add(item.__vModel__ + "_" + citem.showField, relationValue[citem.showField]);
|
|
else dataItem.Add(item.__vModel__ + "_" + citem.showField, string.Empty);
|
|
});
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (model.__config__.children.Where(x => x.__config__.jnpfKey == "popupAttr").Any())
|
|
{
|
|
foreach (var item in model.__config__.children.Where(x => x.__config__.jnpfKey == "popupSelect").ToList())
|
|
{
|
|
var pDataList = await _formDataParsing.GetPopupSelectDataList(item.interfaceId, model); // 获取接口数据列表
|
|
foreach (var dataItem in childTableData)
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(item.__vModel__) && dataItem.ContainsKey(item.__vModel__) && dataItem[item.__vModel__] != null)
|
|
{
|
|
var relationValueId = dataItem[item.__vModel__].ToString(); // 获取关联表单id
|
|
|
|
// 添加到 子表 列
|
|
model.__config__.children.Where(x => x.relationField.ReplaceRegex(@"_jnpfTable_(\w+)", string.Empty) == item.__vModel__).ToList().ForEach(citem =>
|
|
{
|
|
citem.__vModel__ = item.__vModel__ + "_" + citem.showField;
|
|
var value = pDataList.Where(x => x.Values.Contains(dataItem[item.__vModel__].ToString())).FirstOrDefault();
|
|
if (value != null) dataItem.Add(item.__vModel__ + "_" + citem.showField, value[citem.showField]);
|
|
});
|
|
}
|
|
}
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
if (childTableData.Count > 0) newDataMap[model.__vModel__] = childTableData;
|
|
else newDataMap[model.__vModel__] = new List<Dictionary<string, object>>();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return newDataMap;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 处理并发锁定(乐观锁).
|
|
/// </summary>
|
|
/// <param name="link">数据库连接.</param>
|
|
/// <param name="templateInfo">模板信息.</param>
|
|
/// <param name="updateSqlList">修改Sql集合(提交修改时接入).</param>
|
|
/// <param name="allDataMap">前端提交的数据(提交修改时接入).</param>
|
|
private async Task OptimisticLocking(DbLinkEntity? link, TemplateParsingBase templateInfo, List<string>? updateSqlList = null, Dictionary<string, object>? allDataMap = null)
|
|
{
|
|
if (templateInfo.FormModel.concurrencyLock)
|
|
{
|
|
try
|
|
{
|
|
// 主表修改语句, 如果有修改语句 获取执行结果.
|
|
// 不是修改模式, 增加并发锁定字段 f_version.
|
|
if (updateSqlList != null && updateSqlList.Any())
|
|
{
|
|
var mainTableUpdateSql = updateSqlList.Find(x => x.Contains(templateInfo.MainTableName));
|
|
var versoin = (allDataMap.ContainsKey("f_version") && allDataMap["f_version"] != null) ? allDataMap["f_version"] : "-1";
|
|
|
|
// 并发乐观锁 字段 拼接条件
|
|
mainTableUpdateSql = string.Format("{0} and F_Version={1};", mainTableUpdateSql.Replace(";", string.Empty), versoin);
|
|
var res = await _databaseService.ExecuteSql(link, mainTableUpdateSql);
|
|
if (res.Equals(0) && !allDataMap.ContainsKey("jnpf_resurgence")) throw Oops.Oh(ErrorCode.D1408); // 该条数据已经被修改过
|
|
|
|
// f_version +1
|
|
string? sql = string.Format("update {0} set {1}={2};", templateInfo.MainTableName, "F_Version", versoin.ParseToInt() + 1);
|
|
await _databaseService.ExecuteSql(link, sql);
|
|
}
|
|
else
|
|
{
|
|
List<DbTableFieldModel>? fieldList = _databaseService.GetFieldList(link, templateInfo.MainTableName); // 获取主表所有列
|
|
|
|
if (!fieldList.Any(x => SqlFunc.ToLower(x.field) == "f_version"))
|
|
{
|
|
List<DbTableFieldModel>? newField = new List<DbTableFieldModel>() { new DbTableFieldModel() { field = "F_Version", fieldName = "并发锁定字段", dataType = "int", dataLength = "50", allowNull = 1 } };
|
|
_databaseService.AddTableColumn(link, templateInfo.MainTableName, newField);
|
|
}
|
|
|
|
// f_version 赋予默认值 0
|
|
string? sql = string.Format("update {0} set {1}={2} where F_Version IS NULL ;", templateInfo.MainTableName, "F_Version", "0");
|
|
await _databaseService.ExecuteSql(link, sql);
|
|
|
|
var newVModel = new FieldsModel() { __vModel__ = "f_version", __config__ = new ConfigModel() { jnpfKey = JnpfKeyConst.COMINPUT, relationTable = templateInfo.MainTableName, tableName = templateInfo.MainTableName } };
|
|
templateInfo.SingleFormData.Add(newVModel);
|
|
templateInfo.MainTableFieldsModelList.Add(newVModel);
|
|
templateInfo.FieldsModelList.Add(newVModel);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (ex.Message.Contains("[D1408]")) throw Oops.Oh(ErrorCode.D1408);
|
|
else throw Oops.Oh(ErrorCode.COM1008);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 数据是否可以传递.
|
|
/// </summary>
|
|
/// <param name="oldModel">原控件模型.</param>
|
|
/// <param name="newModel">新控件模型.</param>
|
|
/// <returns>true 可以传递, false 不可以</returns>
|
|
private bool DataTransferVerify(FieldsModel oldModel, FieldsModel newModel)
|
|
{
|
|
switch (oldModel.__config__.jnpfKey)
|
|
{
|
|
case JnpfKeyConst.COMINPUT:
|
|
case JnpfKeyConst.TEXTAREA:
|
|
case JnpfKeyConst.RADIO:
|
|
case JnpfKeyConst.EDITOR:
|
|
if (!(newModel.__config__.jnpfKey.Equals(JnpfKeyConst.COMINPUT) ||
|
|
newModel.__config__.jnpfKey.Equals(JnpfKeyConst.TEXTAREA) ||
|
|
newModel.__config__.jnpfKey.Equals(JnpfKeyConst.RADIO) ||
|
|
(newModel.__config__.jnpfKey.Equals(JnpfKeyConst.SELECT) && !newModel.multiple) ||
|
|
newModel.__config__.jnpfKey.Equals(JnpfKeyConst.EDITOR)))
|
|
return false;
|
|
break;
|
|
case JnpfKeyConst.CHECKBOX:
|
|
if (!((newModel.__config__.jnpfKey.Equals(JnpfKeyConst.POPUPTABLESELECT) && newModel.multiple) ||
|
|
(newModel.__config__.jnpfKey.Equals(JnpfKeyConst.SELECT) && newModel.multiple) ||
|
|
(newModel.__config__.jnpfKey.Equals(JnpfKeyConst.TREESELECT) && newModel.multiple) ||
|
|
newModel.__config__.jnpfKey.Equals(JnpfKeyConst.CHECKBOX) ||
|
|
newModel.__config__.jnpfKey.Equals(JnpfKeyConst.CASCADER)))
|
|
return false;
|
|
break;
|
|
case JnpfKeyConst.NUMINPUT:
|
|
case JnpfKeyConst.DATE:
|
|
case JnpfKeyConst.TIME:
|
|
case JnpfKeyConst.UPLOADFZ:
|
|
case JnpfKeyConst.UPLOADIMG:
|
|
case JnpfKeyConst.COLORPICKER:
|
|
case JnpfKeyConst.RATE:
|
|
case JnpfKeyConst.SLIDER:
|
|
if (!(oldModel.__config__.jnpfKey.Equals(newModel.__config__.jnpfKey)))
|
|
return false;
|
|
break;
|
|
case JnpfKeyConst.COMSELECT:
|
|
case JnpfKeyConst.DEPSELECT:
|
|
case JnpfKeyConst.POSSELECT:
|
|
case JnpfKeyConst.USERSELECT:
|
|
case JnpfKeyConst.ROLESELECT:
|
|
case JnpfKeyConst.GROUPSELECT:
|
|
case JnpfKeyConst.ADDRESS:
|
|
if (!(oldModel.__config__.jnpfKey.Equals(newModel.__config__.jnpfKey) && oldModel.multiple.Equals(newModel.multiple)))
|
|
return false;
|
|
break;
|
|
case JnpfKeyConst.TREESELECT:
|
|
if (oldModel.multiple)
|
|
{
|
|
if (!((newModel.__config__.jnpfKey.Equals(JnpfKeyConst.POPUPTABLESELECT) && newModel.multiple) ||
|
|
(newModel.__config__.jnpfKey.Equals(JnpfKeyConst.SELECT) && newModel.multiple) ||
|
|
(newModel.__config__.jnpfKey.Equals(JnpfKeyConst.TREESELECT) && newModel.multiple) ||
|
|
newModel.__config__.jnpfKey.Equals(JnpfKeyConst.CASCADER)))
|
|
return false;
|
|
}
|
|
else
|
|
{
|
|
if (!(newModel.__config__.jnpfKey.Equals(JnpfKeyConst.COMINPUT) ||
|
|
newModel.__config__.jnpfKey.Equals(JnpfKeyConst.TEXTAREA) ||
|
|
newModel.__config__.jnpfKey.Equals(JnpfKeyConst.RADIO) ||
|
|
(newModel.__config__.jnpfKey.Equals(JnpfKeyConst.SELECT) && !newModel.multiple) ||
|
|
(newModel.__config__.jnpfKey.Equals(JnpfKeyConst.TREESELECT) && !newModel.multiple) ||
|
|
newModel.__config__.jnpfKey.Equals(JnpfKeyConst.EDITOR)))
|
|
return false;
|
|
}
|
|
|
|
break;
|
|
case JnpfKeyConst.POPUPTABLESELECT:
|
|
if (oldModel.multiple)
|
|
{
|
|
if (!((newModel.__config__.jnpfKey.Equals(JnpfKeyConst.POPUPTABLESELECT) && newModel.multiple) ||
|
|
(newModel.__config__.jnpfKey.Equals(JnpfKeyConst.SELECT) && newModel.multiple) ||
|
|
(newModel.__config__.jnpfKey.Equals(JnpfKeyConst.TREESELECT) && newModel.multiple) ||
|
|
newModel.__config__.jnpfKey.Equals(JnpfKeyConst.CASCADER)))
|
|
return false;
|
|
}
|
|
else
|
|
{
|
|
if (!((newModel.__config__.jnpfKey.Equals(JnpfKeyConst.POPUPTABLESELECT) && !newModel.multiple) ||
|
|
(newModel.__config__.jnpfKey.Equals(JnpfKeyConst.RELATIONFORM)) ||
|
|
(newModel.__config__.jnpfKey.Equals(JnpfKeyConst.POPUPSELECT))))
|
|
return false;
|
|
}
|
|
|
|
break;
|
|
case JnpfKeyConst.POPUPSELECT:
|
|
case JnpfKeyConst.RELATIONFORM:
|
|
if (!((newModel.__config__.jnpfKey.Equals(JnpfKeyConst.RELATIONFORM)) ||
|
|
(newModel.__config__.jnpfKey.Equals(JnpfKeyConst.POPUPSELECT)) ||
|
|
(newModel.__config__.jnpfKey.Equals(JnpfKeyConst.POPUPTABLESELECT) && !newModel.multiple)))
|
|
return false;
|
|
break;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 是否开启软删除配置.
|
|
/// 开启则增加 删除标识 字段.
|
|
/// </summary>
|
|
/// <param name="tInfo"></param>
|
|
private void LogicalDelete(TemplateParsingBase tInfo)
|
|
{
|
|
if (tInfo.FormModel.logicalDelete)
|
|
{
|
|
if (!_databaseService.IsAnyColumn(tInfo.DbLink, tInfo.MainTableName, "f_deletemark"))
|
|
{
|
|
var pFieldList = new List<DbTableFieldModel>() { new DbTableFieldModel() { field = "f_deletemark", fieldName = "删除标识", dataType = "int", dataLength = "50", allowNull = 1 } };
|
|
_databaseService.AddTableColumn(tInfo.DbLink, tInfo.MainTableName, pFieldList);
|
|
}
|
|
}
|
|
}
|
|
#endregion
|
|
} |