自定义列头显示

parent 43b2f77e
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Performance.EntityModels.Other
{
public class ColumnEntity
{
public string Name { get; set; }
public string Comment { get; set; }
}
}
......@@ -2,6 +2,7 @@
using Microsoft.EntityFrameworkCore;
using Performance.DtoModels;
using Performance.EntityModels;
using Performance.EntityModels.Other;
using System;
using System.Collections.Generic;
using System.Data;
......@@ -499,12 +500,12 @@ public CustonPagingData QueryCustom(CustomPagingRequest request, bool IsHead)
return result;
}
public bool QueryIsAllotId(string tableName)
public bool QueryIsAllotId(string tableName, params string[] columns)
{
var database = context.Database.GetDbConnection().Database;
var sql = $@"SELECT column_name FROM information_schema.COLUMNS s
WHERE table_name = @table_name AND TABLE_SCHEMA = @database AND (column_name='allotId' or column_name='AccountingUnit' or column_name='UnitType');";
var result = DapperQuery<string>(sql, new { database = database, table_name = tableName });
WHERE table_name = @table_name AND TABLE_SCHEMA = @database AND column_name in @columns;";
var result = DapperQuery<string>(sql, new { database = database, table_name = tableName, columns });
var isExist = result?.Count() == 3;
......@@ -512,11 +513,11 @@ public bool QueryIsAllotId(string tableName)
}
public List<dynamic> QueryCustomColumn(string tableName)
public List<ColumnEntity> QueryCustomColumn(string tableName)
{
var database = context.Database.GetDbConnection().Database;
var sql = $@"SELECT column_name,column_comment FROM information_schema.`COLUMNS` WHERE table_schema = @table_schema AND table_name = @table_name AND COLUMN_KEY <> 'PRI' ";
var result = DapperQuery<dynamic>(sql, new { table_schema = database, table_name = tableName })?.ToList();
var sql = $@"SELECT column_name as Name,ifnull(column_comment,column_name) as comment FROM information_schema.`COLUMNS` WHERE table_schema = @table_schema AND table_name = @table_name AND COLUMN_KEY <> 'PRI' ";
var result = DapperQuery<ColumnEntity>(sql, new { table_schema = database, table_name = tableName })?.ToList();
return result;
}
......
......@@ -12,6 +12,7 @@
using Performance.Repository.Repository;
using System.Linq.Expressions;
using System.Text.RegularExpressions;
using Performance.EntityModels.Other;
namespace Performance.Services
{
......@@ -1798,22 +1799,19 @@ public void SaveDrugtypeFactor(DrugtypeFactorRequest request)
public HandsonTable QueryHandsCustom(CustomPagingRequest request)
{
var custom = perforReport.QueryCustomColumn(request.TableName);
var isExist = perforReport.QueryIsAllotId(request.TableName);
var isExist = perforReport.QueryIsAllotId(request.TableName, "AllotId", "AccountingUnit", "UnitType");
if (custom == null || isExist == false)
return null;
var dicCustom = new Dictionary<string, string>();
for (int i = 0; i < custom.Count(); i++)
{
var dic = ((IDictionary<string, object>)custom.ElementAt(i)).Values;
if (dic.ElementAt(0).ToString().ToLower() == "allotid")
continue;
dicCustom.Add(dic.ElementAt(0).ToString(), dic.ElementAt(1).ToString());
}
var skip = new string[] { "allotid" };
var customGroup = dicCustom.GroupBy(x => x.Value).Where(x => x.Count() > 1);
if (customGroup.Any())
return null;
var dicCustom = custom
.Where(w => !skip.Any(s => s.Equals(w.Name, StringComparison.OrdinalIgnoreCase)))
.Select(w => new { w.Name, w.Comment });
if (dicCustom.GroupBy(x => x.Comment).Where(x => x.Count() > 1).Any())
throw new PerformanceException("表列头名称重复, 请补全注释或修改重复注释!");
var result = new HandsonTable((int)SheetType.Unidentifiable, new string[] { }, new List<collect_permission>());
......@@ -1827,13 +1825,13 @@ public HandsonTable QueryHandsCustom(CustomPagingRequest request)
var head = new Dictionary<string, object>();
foreach (var dic in dicCustom)
{
if (item.ContainsKey(dic.Key.ToLower()))
head.Add(dic.Value, item.FirstOrDefault(t => t.Key == dic.Key.ToLower()).Value);
if (item.ContainsKey(dic.Name.ToLower()))
head.Add(dic.Comment, item.FirstOrDefault(t => t.Key == dic.Name.ToLower()).Value);
}
headDic.Add(head);
}
var Columns = dicCustom.Values.Select(t => new HandsonColumn(t, false, DataFormat.普通格式)).ToList();
var Columns = dicCustom.Select(t => new HandsonColumn(t.Comment, false, DataFormat.普通格式)).ToList();
//表类型
var columnType = perforReport.QueryType(request.TableName);
......@@ -1841,40 +1839,36 @@ public HandsonTable QueryHandsCustom(CustomPagingRequest request)
{
foreach (var column in Columns)
{
if (dicCustom.FirstOrDefault(t => t.Value.Contains(column.Data)).Key.ToLower() == "unittype")
if (dicCustom.FirstOrDefault(t => t.Comment.Equals(column.Data)).Name.ToLower() == "unittype")
{
{
column.Type = "autocomplete";
column.Source = EnumHelper.GetItems<UnitType>().Select(w => w.Description.Replace("行政后勤", "行政工勤")).ToArray();
column.Strict = true;
}
column.Type = "autocomplete";
column.Source = EnumHelper.GetItems<UnitType>().Select(w => w.Description.Replace("行政后勤", "行政工勤")).ToArray();
column.Strict = true;
}
for (int i = 0; i < columnType.Count(); i++)
{
var type = ((IDictionary<string, object>)columnType.ElementAt(i)).Values;
var itemDic = column.Data.ToUpper() == type.ElementAt(0).ToString().ToUpper();
var dic = dicCustom.FirstOrDefault(t => t.Value == column.Data);
var dic = dicCustom.FirstOrDefault(t => t.Comment == column.Data);
if (dic.Key.ToUpper() == type.ElementAt(0).ToString().ToUpper() && type.ElementAt(1).ToString() == "int")
if (dic.Name.ToUpper() == type.ElementAt(0).ToString().ToUpper() && type.ElementAt(1).ToString() == "int")
{
column.Type = "numeric";
column.NumericFormat = new NumericFormat { Pattern = "0,00" };
}
if (dic.Key.ToUpper() == type.ElementAt(0).ToString().ToUpper() && type.ElementAt(1).ToString() == "decimal")
if (dic.Name.ToUpper() == type.ElementAt(0).ToString().ToUpper() && type.ElementAt(1).ToString() == "decimal")
{
column.Type = "numeric";
column.NumericFormat = new NumericFormat { Pattern = "0,00.00" };
}
}
}
}
result.Data = headDic;
result.ColHeaders = dicCustom.Values.ToList();
result.ColHeaders = dicCustom.Select(w => w.Comment).ToList();
result.Columns = Columns;
return result;
}
......@@ -1890,18 +1884,13 @@ public ApiResponse SaveCustomTable(SaveCustomData request)
if (custom.Count > 50)
return new ApiResponse(ResponseType.ParameterError, "最多支持50列数据!");
var dicCustom = new Dictionary<string, object>();
for (int i = 0; i < custom.Count(); i++)
{
var dic = ((IDictionary<string, object>)custom.ElementAt(i)).Values;
if (dic.ElementAt(0).ToString().ToLower() == "allotid")
continue;
dicCustom.Add(dic.ElementAt(0).ToString(), dic.ElementAt(1).ToString());
}
//重复表头
var customGroup = dicCustom.GroupBy(x => x.Value).Where(x => x.Count() > 1);
if (customGroup.Any())
return new ApiResponse(ResponseType.ParameterError, "表不符合规范,请补全注释或修改重复注释!");
var skip = new string[] { "allotid" };
var dicCustom = custom
.Where(w => !skip.Any(s => s.Equals(w.Name, StringComparison.OrdinalIgnoreCase)));
if (dicCustom.GroupBy(x => x.Comment).Where(x => x.Count() > 1).Any())
return new ApiResponse(ResponseType.ParameterError, "表列头名称重复,请补全注释或修改重复注释!");
var dicData = CreateCustomRow(request.AllotId, request, dicCustom);
//字段类型
......@@ -1928,7 +1917,6 @@ public ApiResponse SaveCustomTable(SaveCustomData request)
{
return new ApiResponse(ResponseType.ParameterError, "请输入正确的日期格式");
}
}
......@@ -1952,35 +1940,28 @@ public ApiResponse QueryCustom(CustomPagingRequest request)
var result = new CustomResponse();
var heads = perforReport.QueryCustomColumn(request.TableName);
var isExist = perforReport.QueryIsAllotId(request.TableName);
var isExist = perforReport.QueryIsAllotId(request.TableName, "AllotId", "AccountingUnit", "UnitType");
if (isExist == false || heads == null)
return new ApiResponse(ResponseType.ParameterError, "表不符合规范,请补全注释或修改重复注释");
if (heads.Count > 50)
return new ApiResponse(ResponseType.ParameterError, "最多支持50列数据");
var headList = new List<Heads>();
for (int i = 0; i < heads.Count(); i++)
{
var dic = ((IDictionary<string, object>)heads.ElementAt(i)).Values;
if (dic.ElementAt(0).ToString().ToLower() == "allotid")
continue;
var head = new Heads();
head.Column = dic.ElementAt(0).ToString().ToLower();
head.Name = dic.ElementAt(1).ToString();
headList.Add(head);
}
var skip = new string[] { "allotid" };
var headList = heads
.Where(w => !skip.Any(s => s.Equals(w.Name, StringComparison.OrdinalIgnoreCase)))
.Select(w => new Heads { Name = w.Comment, Column = w.Name.ToLower() });
var customGroup = headList.GroupBy(x => x.Name).Where(x => x.Count() > 1);
if (customGroup.Any())
return new ApiResponse(ResponseType.ParameterError, "表不符合规范,请补全注释或修改重复注释");
result.Heads = headList;
result.Heads = headList.ToList();
result.Datas = perforReport.QueryCustom(request, false);
return new ApiResponse(ResponseType.OK, result);
}
private List<Dictionary<string, object>> CreateCustomRow(int allotId, SaveCustomData request, Dictionary<string, object> config)
private List<Dictionary<string, object>> CreateCustomRow(int allotId, SaveCustomData request, IEnumerable<ColumnEntity> config)
{
List<Dictionary<string, object>> allData = new List<Dictionary<string, object>>();
......@@ -1995,18 +1976,18 @@ public ApiResponse QueryCustom(CustomPagingRequest request)
return allData;
}
private Dictionary<string, object> CreateCustomData(SaveCustomData request, Dictionary<string, object> config, int rownumber)
private Dictionary<string, object> CreateCustomData(SaveCustomData request, IEnumerable<ColumnEntity> config, int rownumber)
{
Dictionary<string, object> result = new Dictionary<string, object>();
for (int c = 0; c < request.ColHeaders.Length; c++)
{
var header = request.ColHeaders[c];
var first = config.FirstOrDefault(w => w.Value.ToString().ToUpper() == header);
var first = config.FirstOrDefault(w => w.Comment.ToString().ToUpper() == header);
if (!default(KeyValuePair<string, object>).Equals(first)
&& !result.ContainsKey(header)
&& request.Data[rownumber].Length > c)
{
result.Add(first.Key, request.Data[rownumber][c]);
result.Add(first.Name, request.Data[rownumber][c]);
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment