using Model.ModelDto.SysDto; using Model.ModelVm.SysVm; using Newtonsoft.Json; using SqlSugar; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using WMS.DAL; using WMS.Entity.Context; using WMS.Entity.SysEntity; using WMS.IBLL.ILogServer; using WMS.IBLL.ISysServer; namespace WMS.BLL.SysServer { public class InterfaceServer : IInterfaceServer { private static readonly SqlSugarScope Db = DataContext.Db; private readonly IOperationSysServer _operation; //操作日志 public InterfaceServer(IOperationSysServer operation) { _operation = operation; } /// /// 获取接口列表 /// /// /// /// public List GetInterfaceList(InterfaceVm model, out int count) { string sqlString = string.Empty; string sqlCount = string.Empty; string sqlPub = string.Empty; try { sqlCount += "SELECT DISTINCT COUNT(tb1.Id) FROM SysInterface AS tb1 "; sqlString += "SELECT DISTINCT tb1.*,tb2.RealName as CreateUserName,tb3.RealName as UpdateUserName FROM SysInterface AS tb1 "; sqlPub += "LEFT JOIN SysUserInfor AS tb2 ON tb1.CreateUser = tb2.Id "; sqlPub += "LEFT JOIN SysUserInfor AS tb3 ON tb1.UpdateUser = tb3.Id "; sqlPub += $"WHERE tb1.IsDel=0 "; if (!string.IsNullOrEmpty(model.InterfaceNo)) { sqlPub += $"AND tb1.InterfaceNo LIKE '%{model.InterfaceNo}%' "; } if (!string.IsNullOrEmpty(model.InterfaceName)) { sqlPub += $"AND tb1.InterfaceName LIKE '%{model.InterfaceName}%' "; } if (!string.IsNullOrEmpty(model.Direction)) { sqlPub += $"AND tb1.Direction = '{model.Direction}' "; } sqlCount += sqlPub; sqlPub += " order by tb1.Id "; if (model.Page == 0) { model.Page = 1; } sqlString += sqlPub + $" offset {((model.Page - 1) * model.Limit)} rows fetch next {model.Limit} rows only;"; var com = new Common(); count = com.GetRowCount(sqlCount); var modelList = Db.Ado.SqlQuery(sqlString); return modelList; } catch (Exception ex) { throw ex; } } /// /// 获取接口明细列表 /// /// /// /// public List GetInterfaceDetailList(InterfaceDetailVm model, out int count) { string sqlString = string.Empty; string sqlCount = string.Empty; int rowCount = 1; try { if (model.Page == 0) { model.Page = 1; } sqlCount += $"SELECT COUNT(ID) FROM SysInterfaceDetail where InterfaceNo = '{model.InterfaceNo}' and isdel = '0';"; var com = new Common(); count = com.GetRowCount(sqlCount); if (count != 0) { rowCount = count; } sqlString += "SELECT tb1.*,tb2.RealName as CreateUserName,tb3.RealName as UpdateUserName FROM SysInterfaceDetail AS tb1 "; sqlString += "LEFT JOIN SysUserInfor AS tb2 ON tb1.CreateUser = tb2.Id "; sqlString += "LEFT JOIN SysUserInfor AS tb3 ON tb1.UpdateUser = tb3.Id "; sqlString += $"WHERE tb1.InterfaceNo = '{model.InterfaceNo}' AND tb1.IsDel = '0' order by tb1.Id "; sqlString += $"offset {((model.Page - 1) * model.Limit)} rows fetch next {rowCount} rows only;"; var modelList = Db.Ado.SqlQuery(sqlString); return modelList; } catch (Exception ex) { throw ex; } } /// /// 添加接口信息 /// /// /// public string AddInterface(InterfaceVm model) { string strMessage = ""; string sqlString = string.Empty; string sqlDetailStr = string.Empty; try { string sqlCount = $"SELECT COUNT(ID) FROM SysInterface where InterfaceNo = '{model.InterfaceNo}' and isdel = '0';"; var com = new Common().GetRowCount(sqlCount); if (com > 0) { strMessage = "-1:接口编号已存在,请勿重复添加!"; return strMessage; } //接口总信息 sqlString += "Insert into SysInterface (InterfaceNo,InterfaceName,Direction,TransmissionMode,Remark,Url,DataSources,CreateUser) values ( "; sqlString += $"'{model.InterfaceNo}','{model.InterfaceName}','{model.Direction}','{model.TransmissionMode}','{model.Remark}','{model.Url}','{model.DataSources}','{model.CreateUser}' );"; //接口明细 var detailModels = model.InterfaceDetail; foreach (InterfaceDetailVm detailModel in detailModels) { sqlDetailStr += "Insert into SysInterfaceDetail (InterfaceNo,ExtField,MapField,Field,"; sqlDetailStr += "FieldType,DataType,IsNull,Remark,FieldFather,CreateUser) values ( "; sqlDetailStr += $"'{model.InterfaceNo}','{detailModel.ExtField}','{detailModel.MapField}','{detailModel.Field}', "; sqlDetailStr += $"'{detailModel.FieldType}','{detailModel.DataType}','{detailModel.IsNull}','{detailModel.Remark}','{detailModel.FieldFather}','{model.CreateUser}'); "; } if (sqlDetailStr == string.Empty) { return strMessage; } Db.Ado.BeginTran(); int rowCount = Db.Ado.ExecuteCommand(sqlString); var rowDetailCount = 1; if (sqlDetailStr.Length > 0) { rowDetailCount = Db.Ado.ExecuteCommand(sqlDetailStr); } Db.Ado.CommitTran(); if (rowCount > 0 && rowDetailCount > 0) { //维护操作日志 _operation.InsertOperation("系统设置", "接口管理", model.InterfaceNo, "添加", $"添加了接口编号为{model.InterfaceNo}的接口信息", Convert.ToInt32(model.CreateUser)); return strMessage; } else { Db.Ado.RollbackTran(); return "-2:添加失败数据回滚!"; } } catch (Exception ex) { throw new Exception(ex.Message); } } /// /// 编辑接口信息 /// /// /// public string EditInterface(InterfaceVm model) { string strMessage = ""; string sqlString = string.Empty; string sqlDetailStr = string.Empty; try { sqlString = "UPDATE SysInterface SET "; sqlString += $"InterfaceName = '{model.InterfaceName}',Direction = '{model.Direction}',"; sqlString += $"TransmissionMode = '{model.TransmissionMode}',Remark = '{model.Remark}',Url = '{model.Url}',DataSources = '{model.DataSources}', "; sqlString += $"UpdateTime = GETDATE(),UpdateUser = '{model.CreateUser}' "; sqlString += $"WHERE InterfaceNo = '{model.InterfaceNo}'"; var detailModels = model.InterfaceDetail; sqlDetailStr += $"SELECT * FROM SysInterfaceDetail WHERE InterfaceNo = '{model.InterfaceNo}' and isDel = '0';"; var dbDetailModels = DataContext.Db.Ado.SqlQuery(sqlDetailStr); sqlDetailStr = string.Empty; // 处理已删除的明细 foreach (SysInterfaceDetail dbDetailModel in dbDetailModels) { if (detailModels.Count(it => it.Id == dbDetailModel.Id) == 0) { sqlDetailStr += $"UPDATE SysInterfaceDetail SET IsDel = '1',"; sqlDetailStr += $"UpdateTime = GETDATE(),UpdateUser = '{model.CreateUser}' "; sqlDetailStr += $"WHERE Id = {dbDetailModel.Id};"; } } // 处理修改和添加的明细 foreach (InterfaceDetailVm detailModel in detailModels) { if (detailModel.Id == 0 || detailModel.Id == null) { sqlDetailStr += "Insert into SysInterfaceDetail (InterfaceNo,ExtField,MapField,Field,"; sqlDetailStr += "FieldType,DataType,IsNull,Remark,FieldFather,CreateUser) values ( "; sqlDetailStr += $"'{model.InterfaceNo}','{detailModel.ExtField}','{detailModel.MapField}','{detailModel.Field}', "; sqlDetailStr += $"'{detailModel.FieldType}','{detailModel.DataType}','{detailModel.IsNull}','{detailModel.Remark}','{detailModel.FieldFather}','{model.CreateUser}'); "; } else { //判断是否更改 int rowNum = dbDetailModels .Count(it => it.Id == detailModel.Id && it.ExtField == detailModel.ExtField && it.MapField == detailModel.MapField && it.Field == detailModel.Field && it.FieldType == detailModel.FieldType && it.DataType == detailModel.DataType && it.IsNull == detailModel.IsNull && it.Remark == detailModel.Remark && it.FieldFather == detailModel.FieldFather); if (rowNum > 0) { continue; } sqlDetailStr += $"UPDATE SysInterfaceDetail SET "; sqlDetailStr += $"ExtField = '{detailModel.ExtField}',MapField = '{detailModel.MapField}',Field='{detailModel.Field}',FieldFather='{detailModel.FieldFather}', "; sqlDetailStr += $"FieldType = '{detailModel.FieldType}',DataType = '{detailModel.DataType}', "; sqlDetailStr += $"IsNull = '{detailModel.IsNull}',Remark = '{detailModel.Remark}', "; sqlDetailStr += $"UpdateTime = GETDATE(),UpdateUser = '{model.CreateUser}' "; sqlDetailStr += $"WHERE Id = {detailModel.Id};"; } } Db.Ado.BeginTran(); int rowCount = Db.Ado.ExecuteCommand(sqlString); var rowDetailCount = 1; if (sqlDetailStr.Length > 0) { rowDetailCount = Db.Ado.ExecuteCommand(sqlDetailStr); } Db.Ado.CommitTran(); if (rowCount > 0 && rowDetailCount > 0) { //维护操作日志 _operation.InsertOperation("系统设置", "接口管理", model.InterfaceNo, "编辑", $"编辑了接口编号为{model.InterfaceNo}的接口信息", Convert.ToInt32(model.CreateUser)); return strMessage; } else { Db.Ado.RollbackTran(); return "-2:添加失败数据回滚!"; } } catch (Exception ex) { throw ex; } } /// /// 删除接口信息 /// /// /// public string DelInterface(InterfaceVm model) { try { string sqlString = ""; // 删除总 sqlString = $"UPDATE SysInterface SET IsDel = '1',"; sqlString += $"UpdateTime = GETDATE(),UpdateUser = '{model.CreateUser}' "; sqlString += $"WHERE InterfaceNo = '{model.InterfaceNo}';"; // 删除明细 sqlString += $"UPDATE SysInterfaceDetail SET IsDel = '1',"; sqlString += $"UpdateTime = GETDATE(),UpdateUser = '{model.CreateUser}' "; sqlString += $"WHERE InterfaceNo = '{model.InterfaceNo}';"; Db.Ado.BeginTran(); int rowCount = Db.Ado.ExecuteCommand(sqlString); if (rowCount < 2) { Db.RollbackTran(); return "-1:删除失败!"; } Db.Ado.CommitTran(); _operation.InsertOperation("系统设置", "接口管理", model.InterfaceNo, "删除", $"删除了接口编号为{model.InterfaceNo}的接口信息", Convert.ToInt32(model.CreateUser)); return ""; } catch (Exception ex) { throw ex; } } /// /// 删除接口明细信息 /// /// /// public string DelInterfaceDetail(InterfaceDetailVm model) { try { string sqlString = ""; // 删除明细 sqlString += $"UPDATE SysInterfaceDetail SET IsDel = '1',"; sqlString += $"UpdateTime = GETDATE(),UpdateUser = '{model.CreateUser}' "; sqlString += $"WHERE Id = '{model.Id}';"; Db.Ado.BeginTran(); int rowCount = Db.Ado.ExecuteCommand(sqlString); if (rowCount < 1) { Db.RollbackTran(); return "-1:删除失败!"; } Db.Ado.CommitTran(); _operation.InsertOperation("系统设置", "接口管理", model.InterfaceNo, "删除", $"删除了接口编号为{model.InterfaceNo},本系统字段为{model.Field}的接口信息", Convert.ToInt32(model.CreateUser)); return ""; } catch (Exception ex) { throw ex; } } //public void ceshi() //{ // string interfaceNo = "A0001"; // string param = "'ASN2023110200002'"; // RespondResult(interfaceNo, param); //} /// /// 回传上游系统公共方法 /// /// 接口编号 /// 存储过程参数 public void RespondResult(string InterfaceNo,string param) { try { var outDto = new Dictionary(); string sqlString = $@"select * from SysInterface where IsDel=0 and InterfaceNo='{InterfaceNo}'"; //接口总信息 var interfaceModel = Db.Ado.SqlQuery(sqlString).FirstOrDefault(); if (interfaceModel == null) { throw new Exception("未查询到接口信息"); } if (string.IsNullOrEmpty(interfaceModel.DataSources)) { throw new Exception("数据来源为空"); } string detailSql = $@"select * from SysInterfaceDetail where IsDel=0 and InterfaceNo='{interfaceModel.InterfaceNo}'"; //接口明细信息 List _detailList = Db.Ado.SqlQuery(detailSql).ToList(); if (_detailList.Count <= 0) { throw new Exception("未查询到接口明细信息"); } //执行存储过程查询数据 string querySql = $@"EXEC {interfaceModel.DataSources} {param}"; var requestDataList = Db.Ado.SqlQuery(querySql).ToList(); //组织请求参数 foreach (var item in _detailList) { if (item.DataType == "0")//表头数据 { var fileldFatherList = _detailList.Where(it => it.FieldFather == item.ExtField).ToList();//判找到该表头下的所有行 if (fileldFatherList.Count > 0)//判断该表头是否有行 { var dicDetailList = new List>(); foreach (var item2 in requestDataList)//行数据 { var dicDetailModel = new Dictionary(); foreach (var item3 in fileldFatherList) { // 使用反射获取字段值 string value = GetFieldValue(item2, item3.MapField); dicDetailModel.Add(item3.ExtField, value); } dicDetailList.Add(dicDetailModel); } outDto.Add(item.ExtField, dicDetailList); } else { var requestDataModel = requestDataList.FirstOrDefault(); // 使用反射获取字段值 string value = GetFieldValue(requestDataModel, item.MapField); outDto.Add(item.ExtField, value); } } } //请求方式,0:Http+Xml 1:Http+Xml if (interfaceModel.TransmissionMode == "0") { //请求参数 var jsonData = Newtonsoft.Json.JsonConvert.SerializeObject(outDto); //发起请求 var response = Utility.Tools.HttpHelper.DoPost(interfaceModel.Url, jsonData, interfaceModel.InterfaceName, interfaceModel.Remark); } else { } } catch (Exception ex) { } } /// /// 通过字段名获取字段值 /// /// /// /// public static string GetFieldValue(object obj, string fieldName) { //使用反射获取字段值 Type type = obj.GetType(); PropertyInfo propertyInfo = type.GetProperty(fieldName); if (propertyInfo != null) { return propertyInfo.GetValue(obj) == null ? "" : propertyInfo.GetValue(obj).ToString(); } return null; } } public class RequestData { public string Field1 { get; set; } public string Field2 { get; set; } public string Field3 { get; set; } public string Field4 { get; set; } public string Field5 { get; set; } public string Field6 { get; set; } public string Field7 { get; set; } public string Field8 { get; set; } public string Field9 { get; set; } public string Field10 { get; set; } public string Field11 { get; set; } public string Field12 { get; set; } public string Field13 { get; set; } public string Field14 { get; set; } public string Field15 { get; set; } public string Field16 { get; set; } public string Field17 { get; set; } public string Field18 { get; set; } public string Field19 { get; set; } public string Field20 { get; set; } } }