Merge branch 'DataMaintenance' of http://123.132.248.154:10000/HC_YFZX/Infrastructure into DataMaintenance

DataMaintenance
zhangbin 2025-04-14 11:13:32 +08:00
commit 7c390544fd
4 changed files with 325 additions and 3 deletions

View File

@ -6,12 +6,12 @@ using System.Threading.Tasks;
namespace OpenAuth.App.Request
{
public class DbTableReq
public class UpdateTableReq
{
public string TableName { get; set; }
public string Description { get; set; }
public int Id { get; set; }
public List<DbColumnInput> DbColumnInfoList { get; set; }
public List<Dictionary<string,object>> list { get; set; }
}
}

View File

@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OpenAuth.App.Request
{
public class DbTableReq
{
public string TableName { get; set; }
public string Description { get; set; }
public List<DbColumnInput> DbColumnInfoList { get; set; }
}
}

View File

@ -1,6 +1,10 @@
using System.Data;
using System.Reflection.PortableExecutable;
using ClosedXML.Excel;
using System.Text.Json.Nodes;
using Infrastructure;
using Infrastructure.Utils;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using NetTopologySuite;
using NetTopologySuite.Geometries;
@ -8,11 +12,16 @@ using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using OpenAuth.App.BaseApp.Base;
using OpenAuth.App.FormModule;
using OpenAuth.App.Interface;
using OpenAuth.App.ServiceApp.LayerManagerApp.Request;
using OpenAuth.Repository;
using OpenAuth.Repository.Domain;
using SqlSugar;
using DocumentFormat.OpenXml.Bibliography;
using OpenAuth.App.Request;
using System.IO;
using DocumentFormat.OpenXml.Math;
namespace OpenAuth.App.ServiceApp.LayerManagerApp;
@ -257,4 +266,213 @@ public class LayerApp : SqlSugarBaseApp<DmLayer, SugarDbContext>
Result = true
};
}
public Response<Dictionary<string, object>> TableDataByTableName(string tablename, string type, string keyword, string keyvalue, int page, int pagesize)
{
Dictionary<string,object> result = new Dictionary<string,object>();
string sql = $"select column_name as name from INFORMATION_SCHEMA.COLUMNS WHERE table_schema = 'public' AND table_name ='" + tablename + "'";
var cloumnlist = _client.Ado.SqlQuery<string>(sql);
result.Add("head", cloumnlist);
string names = "";
string geoms = "";
for (int i = 0; i < cloumnlist.Count; i++)
{ var name = cloumnlist[i];
if (name.Contains("geom"))
{
geoms = name;
}
else
{
names = names +"\""+ name + "\",";
}
}
string sql1 = $"select " + names + "ST_AsText(" + geoms + ") as " + geoms + $" from " + tablename + " where 1=1 ";
if (string.IsNullOrEmpty(keyword) || string.IsNullOrEmpty(keyvalue))
{
sql1 = sql1 + "limit "+ pagesize + " offset "+(page-1)*pagesize;
}
else
{
sql1 = sql1 + " and " + keyword + " like '%" + keyvalue + "%'" + "limit " + pagesize + " offset " + (page - 1) * pagesize;
}
var result1 = _client.Ado.SqlQuery<dynamic>(sql1);
result.Add("data", result1);
return new Response<Dictionary<string, object>>
{
Result = result
};
}
public Response<MemoryStream> TempeleteByTableName(string tablename)
{
Response<MemoryStream> response = new Response<MemoryStream>();
string sql = $"select column_name as name from INFORMATION_SCHEMA.COLUMNS WHERE table_schema = 'public' AND table_name ='" + tablename + "'";
var cloumnlist = _client.Ado.SqlQuery<string>(sql);
try
{
HSSFWorkbook workbook = new HSSFWorkbook();
IFont font = workbook.CreateFont(); //创建一个字体样式对象
font.FontName = "Microsoft YaHei"; //和excel里面的字体对应
font.Boldweight = (short)FontBoldWeight.Bold; //字体加粗
font.FontHeightInPoints = 12; //字体大小
ICellStyle style1 = workbook.CreateCellStyle(); //创建样式对象
style1.BorderBottom = BorderStyle.Thin;
style1.BorderLeft = BorderStyle.Thin;
style1.BorderRight = BorderStyle.Thin;
style1.BorderTop = BorderStyle.Thin;
style1.Alignment = HorizontalAlignment.Center;
style1.VerticalAlignment = VerticalAlignment.Center;
style1.SetFont(font); //将字体样式赋给样式对象
// 创建一个数值格式的CellStyle
ICellStyle numericStyle = workbook.CreateCellStyle();
// 创建一个数据格式对象
IDataFormat dataFormat = workbook.CreateDataFormat();
var font11 = workbook.CreateFont();
font11.FontName = "Microsoft YaHei";
font11.FontHeightInPoints = 12;
// 设置单元格格式为数值型
numericStyle.DataFormat = dataFormat.GetFormat("0.00");
numericStyle.BorderBottom = BorderStyle.Thin;
numericStyle.BorderLeft = BorderStyle.Thin;
numericStyle.BorderRight = BorderStyle.Thin;
numericStyle.BorderTop = BorderStyle.Thin;
numericStyle.Alignment = HorizontalAlignment.Center;
numericStyle.VerticalAlignment = VerticalAlignment.Center;
numericStyle.SetFont(font11);
ISheet sheet = workbook.CreateSheet("Sheet0");
IRow rowHeader = sheet.CreateRow(0);
rowHeader.Height = 20 * 30;
for (int i = 0; i < cloumnlist.Count; i++)
{
var header = cloumnlist[i];
rowHeader.CreateCell(i);
rowHeader.Cells[i].CellStyle = style1;
rowHeader.Cells[i].SetCellValue(header);
}
response.Result = new MemoryStream();
workbook.Write(response.Result);
workbook = null;
response.Result.Close();
response.Result.Dispose();
response.Code = 200;
response.Message = "获取成功";
}
catch (Exception ex)
{
response.Code = 500;
response.Message = ex.Message;
}
return response;
}
public async Task<bool> UploadExcel1(UploadExcelReq req)
{
using (var workbook = new XLWorkbook(req.file.OpenReadStream()))
{
string tableName = req.tableName;
var wooksheet = workbook.Worksheet(1);
IXLRows row = wooksheet.RowsUsed();
List<IXLRow> rowCells = row.ToList();
IXLRow row1 = rowCells[0];
IXLCells cells = row1.CellsUsed();
var cols = new string[cells.Count()];
List<TableInfo> listTableInfo = new List<TableInfo>();
int k = 0;
JsonObject json = new JsonObject();
List<Dictionary<String, Object>> listmap = new List<Dictionary<String, Object>>();
for (int i = 1; i < rowCells.Count; i++) {
IXLRow rowi = rowCells[i];
IXLCells cellsi = rowi.CellsUsed();
Dictionary<String, Object> map = new Dictionary<String, Object>();
int g = 0;
foreach (IXLCell cell in cellsi)
{
string head = cols[g];
map.Add(head, cell.Value);
g++;
}
listmap.Add(map);
}
var count = wooksheet.RowCount();
_client.Insertable(listmap).AS(tableName).ExecuteCommand();
await GeoUtil.CreateStoreAndLayer("", "EPSG:4326", tableName, "");
return true;
}
}
public bool UpdateTableData(UpdateTableReq req) {
string tableName = req.TableName;
int id = req.Id;
var list = req.list;
string sql = "update " + tableName + " set ";
string sql1 = "";
for (int i = 0; i < list.Count; i++) {
var map = list[i];
sql1 = sql1 + map["name"] + " = " + map["value"] + ",";
}
var sql2 = sql1.Substring(0,sql1.Length-1)+ " where 1=1 and \"Id\"="+id;
_client.Ado.ExecuteCommand(sql + sql2);
return true;
}
[HttpPost]
public async Task<bool> AddUploadExcel(UploadExcelReq req)
{
using (var workbook = new XLWorkbook(req.file.OpenReadStream()))
{
string sql = $"delete from " + req.tableName;
_client.Ado.ExecuteCommand(sql);
string tableName = req.tableName;
var wooksheet = workbook.Worksheet(1);
IXLRows row = wooksheet.RowsUsed();
List<IXLRow> rowCells = row.ToList();
IXLRow row1 = rowCells[0];
IXLCells cells = row1.CellsUsed();
var cols = new string[cells.Count()];
List<TableInfo> listTableInfo = new List<TableInfo>();
int k = 0;
JsonObject json = new JsonObject();
List<Dictionary<String, Object>> listmap = new List<Dictionary<String, Object>>();
for (int i = 1; i < rowCells.Count; i++)
{
IXLRow rowi = rowCells[i];
IXLCells cellsi = rowi.CellsUsed();
Dictionary<String, Object> map = new Dictionary<String, Object>();
int g = 0;
foreach (IXLCell cell in cellsi)
{
string head = cols[g];
map.Add(head, cell.Value);
g++;
}
listmap.Add(map);
}
var count = wooksheet.RowCount();
_client.Insertable(listmap).AS(tableName).ExecuteCommand();
await GeoUtil.CreateStoreAndLayer("", "EPSG:4326", tableName, "");
return true;
}
}
}

View File

@ -1,5 +1,8 @@
using Infrastructure;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using OpenAuth.App;
using OpenAuth.App.Request;
using OpenAuth.App.ServiceApp.LayerManagerApp;
using OpenAuth.App.ServiceApp.LayerManagerApp.Request;
@ -40,4 +43,88 @@ public class LayerController : ControllerBase
{
return await _app.UploadShape(req);
}
[HttpGet]
[AllowAnonymous]
public Response<Dictionary<string, object>> TableDataByTableName(string tablename,string type,string keyword,string keyvalue,int page=1,int limit = 10) {
return _app.TableDataByTableName(tablename,type, keyword,keyvalue, page,limit);
}
[HttpGet]
[AllowAnonymous]
public IActionResult TempeleteByTableName(string tablename)
{
var res = new Response();
var excelRes = _app.TempeleteByTableName(tablename);
if (excelRes.Code == 200)
{
return File(excelRes.Result.ToArray(),
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"数据导出" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls");
}
else
{
res.Code = excelRes.Code;
res.Message = "导出失败";
}
return Ok(res);
}
[HttpPost]
[AllowAnonymous]
public async Task<Response<bool>> UploadExcelAll([FromBody]UploadExcelReq req)
{
Response<bool> response = new Response<bool>();
try
{
response.Result = await _app.UploadExcel1(req);
}
catch (Exception ex)
{
response.Code = 500;
response.Message = ex.InnerException?.Message ?? ex.Message;
}
return response;
}
[HttpPost]
[AllowAnonymous]
public Response<bool> UpdateTableData(UpdateTableReq req)
{
Response<bool> response = new Response<bool>();
try
{
response.Result = _app.UpdateTableData(req);
}
catch (Exception ex)
{
response.Code = 500;
response.Message = ex.InnerException?.Message ?? ex.Message;
}
return response;
}
[HttpPost]
[AllowAnonymous]
public async Task<Response<bool>> AddUploadExcel([FromBody] UploadExcelReq req)
{
Response<bool> response = new Response<bool>();
try
{
response.Result = await _app.AddUploadExcel(req);
}
catch (Exception ex)
{
response.Code = 500;
response.Message = ex.InnerException?.Message ?? ex.Message;
}
return response;
}
}