自定义列头显示

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