using System; using System.Collections.Generic; using System.Text; using Ys.Core.Common; using djy.Paas.Model; using djy.Paas.IService; using System.Threading.Tasks; using System.Linq; using FreeSql; using System.Diagnostics; using System.Threading; using AutoMapper; using DotNetCore.CAP; using System.Text.Json; using System.Linq.Expressions; using System.Reflection; namespace djy.Paas.Service { /// /// 服务基类 /// public class ServBase : DbContext, ICapSubscribe { /// /// 获取登录者账户详情信息 /// /// /// public djy.Model.User GetUserInfo(string UserId) { if (UserId.IsNull()) { return null; } return DbBus.Get(DbList.djyolddb).Select().Where(w => w.GID == UserId.ToString()).ToOne(); } /// /// 创建日志 /// /// 消息 /// 触发数据对象或分组 /// 触发数据 /// public static bool _LogsAdd(string Message, string ObjName, object DataJson = null) { return _LogsAdd(Message, ObjName, DataJson, null, null); } /// /// 创建日志 /// /// 消息 /// 触发数据对象或分组 /// 触发数据 /// 触发数据guid /// 返回的数据 /// true 属性原型 false 小驼峰模式 /// public static bool _LogsAdd(string Message, string ObjName = null, object DataJson = null, string ObjGid = null, object ResultJson = null, bool JsonNotWebOption = true) { try { if (DataJson == null) { DataJson = ""; } if (ResultJson == null) { ResultJson = ""; } var _datajson = ""; var _resulitjson = ""; if (JsonNotWebOption) { _datajson = DataJson.GetType() == typeof(string) ? DataJson.ToString() : JsonSerializer.Serialize(DataJson); _resulitjson = ResultJson.GetType() == typeof(string) ? ResultJson.ToString() : JsonSerializer.Serialize(ResultJson); } else { var _option = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }; _datajson = DataJson.GetType() == typeof(string) ? DataJson.ToString() : JsonSerializer.Serialize(DataJson, _option); _resulitjson = ResultJson.GetType() == typeof(string) ? ResultJson.ToString() : JsonSerializer.Serialize(ResultJson, _option); } return _LogsAdd(new tb_sys_Logs { Message = Message, ObjName = ObjName, ObjGid = ObjGid, DataJson = _datajson, ResultJson = _resulitjson }); } catch { return false; } } /// /// 创建日志 /// /// /// public static bool _LogsAdd(tb_sys_Logs Logs) { return _LogsAdd(new List() { Logs }); } /// /// 创建日志 /// /// /// public static bool _LogsAdd(List LogsList) { var count = 0; try { LogsList.ForEach(x=> { x.Init();x.SysCode = "djy_syht"; }); count = DbBus.Get(DbList.Logsdb).Insert(LogsList).ExecuteAffrows(); } catch { } return count > 0 ? true : false; } } /// /// 服务基类 /// /// 表名 /// Dto 名称 public class ServBase:ServBase where T:class,new() { /// ///设置 数据库连接池KeyName /// /// 数据库连接池KeyName public ServBase(string DbconnName) { DbKeyName = DbconnName; } /// /// 数据库链接池KeyName /// public string DbKeyName { get; set; } public virtual ReturnResult Add(T Dto) { var rs = new ReturnResult(); try { if (Dto == null) { rs.Not("数据对象不能为空!"); goto Gor; } if (typeof(T).GetMethod("Init") != null) { typeof(T).GetMethod("Init").Invoke(Dto, null); } rs.Data = DbBus.Get(DbKeyName).Insert(Dto).ExecuteAffrows(); rs.OK("创建成功!"); } catch (Exception ex) { rs.Not(ex.Message); } Gor: return rs; } /// /// gid批量删除 /// /// /// public virtual ReturnResult Del(Guid?[] gidlist) { var rs = new ReturnResult(); try { if (typeof(T).GetField("Status") != null) { rs.Data = DbBus.Get(DbKeyName).Update().Set(w => (w as DataBase).Status, -1).Where(w => gidlist.ToList().Contains((w as DataBase).Gid)).ExecuteAffrows(); } else { rs.Data = DbBus.Get(DbKeyName).Delete().Where(w => gidlist.Contains((w as DataBase).Gid)).ExecuteAffrows(); } rs.OK((int)rs.Data == 0 ? "操作成功但没有指定的数据对象!" : "删除成功!"); } catch (Exception ex) { rs.Not(ex.Message); } return rs; } /// /// 条件删除 /// /// /// public virtual ReturnResult Del(Expression> Where) { var rs = new ReturnResult(); if (Where != null) {//条件删除 rs.Data = DbBus.Get(DbKeyName).Delete().Where(Where).ExecuteAffrows(); rs.OK(); } return rs; } /// /// 根据Dto条件查询 /// /// /// /// public virtual ReturnPagedResult GetList(T Dto, ApiFromDto Page) { var rs = new ReturnPagedResult(); rs.Not("未实现"); return rs; } /// /// 条件查询 /// /// /// /// public virtual ReturnPagedResult GetList(Expression> Where,ApiFromDto Page) { var rs = new ReturnPagedResult(); rs.Data = DbBus.Get(DbKeyName).Select().Where(Where).Count(out var TotalCount).Page(Page.Page,Page.Limit).ToList(); rs.Pageset(Page,TotalCount); rs.OK(); return rs; } public virtual ReturnResult GetId(long Id) { var rs = new ReturnResult(); try { if (Id < 0) { rs.Not("错误的请求!"); goto Gor; } rs.Data = DbBus.Get(DbKeyName).Select(Id).ToOne(); if (rs.Data == null) { rs.Not("没有找到此对象"); goto Gor; } rs.OK("OK"); } catch (Exception ex) { rs.Not(ex.Message); } Gor: return rs; } /// /// 根据Gid获取 /// /// /// public ReturnResult GetId(Guid? Id) { var rs = new ReturnResult(); try { rs.Data = DbBus.Get(DbKeyName).Select().Where("Gid=@gid", new { gid = Id }).ToOne(); if (rs.Data == null) { rs.Not("没有找到此对象"); goto Gor; } rs.OK("OK"); } catch (Exception ex) { rs.Not(ex.Message); } Gor: return rs; } /// /// 条件获取 /// /// /// public ReturnResult GetId(Expression> Where) { var rs=new ReturnResult(); try { rs.Data= DbBus.Get(DbKeyName).Select().Where(Where).ToOne(); if (rs.Data == null) { rs.Not("没有找到此对象"); } else { rs.OK("OK"); } } catch (Exception ex) { rs.Not(ex.Message); } return rs; } /// /// 对数据对象更新 /// /// /// 要更新的目标对象 /// 更新的值 /// 指定更新字段 /// 指定的跳过过滤字段 /// public T _UpDataDto(ref T Dto,T UpDto, List UpColumn = null, List Notcolumn = null) { var _notcolumn= new List { "Id", "AddTime", "Gid", "Status" }; if (Notcolumn == null || Notcolumn.Count == 0) { Notcolumn = _notcolumn; } else { Notcolumn.AddRange(_notcolumn); } if (UpColumn == null || UpColumn.Count == 0) { UpColumn = new List(); foreach (var item in typeof(T).GetProperties()) { UpColumn.Add(item.Name); } } for (int i = 0; i < UpColumn.Count; i++) { UpColumn[i] = UpColumn[i].ToLower(); } for (int i = 0; i < Notcolumn.Count; i++) { Notcolumn[i] = Notcolumn[i].ToLower(); } //进行反射循环对比是否需要修改新的值后更新 var collist= new List(); // collist = typeof(T).GetProperties().Where(w => UpColumn.Contains(w.Name) && !Notcolumn.Contains(w.Name)).ToList(); var typeclist = typeof(T).GetProperties().ToList(); foreach (var col in typeclist) { if (UpColumn.IndexOf(col.Name.ToLower()) >= 0 && Notcolumn.IndexOf(col.Name.ToLower()) < 0) { collist.Add(col); } } var initData = new T(); var ilist = initData.GetType().GetMembers(); if (initData.GetType().GetMethod("Init") != null) { initData.GetType().GetMethod("Init").Invoke(initData, new object[] { }); } foreach (var item in collist) {//空值和等于创建初始化的将被忽律 var valuedata = item.GetValue(Dto); var valueup = item.GetValue(UpDto); var init = item.GetValue(initData); //针对status特特殊处理 if (item.Name.ToLower() == "status") { init = 0; } if (valueup != null && (!valueup.Equals(valuedata) && (!valueup.Equals(init)))) { if (valueup.ToString().IsNotNull()) { if (valueup.ToString() == "[null]") item.SetValue(Dto,null); else item.SetValue(Dto, valueup); } } } return Dto; } /// /// 更新 更新规则 自动对比更新数据对 只对修改的进行修改 自动忽略 空值 初始化值 /// /// /// 原始数据条件 /// 不做修改的列名 ///指定要修改的列 /// public virtual ReturnResult Up(T Dto, Expression> Where = null, List UpColumn = null, List Notcolumn = null) { var rs = new ReturnResult(); try { if (Notcolumn == null || Notcolumn.Count == 0) { Notcolumn = new List { "Id", "AddTime" }; } if (UpColumn == null || UpColumn.Count == 0) { UpColumn = new List(); foreach (var item in typeof(T).GetProperties()) { UpColumn.Add(item.Name); } } if (Dto == null) { rs.Not("请求的数据不能空!"); goto Gor; } var GetRs = new ReturnResult(); if (Where == null) { GetRs = this.GetId((long)Dto.GetType().GetProperty("Id").GetValue(Dto)); if (!GetRs.Status) { var gid = Dto.GetType().GetProperty("Gid").GetValue(Dto); if (gid != null) { GetRs = this.GetId((Guid?)gid); } } } else { GetRs = this.GetId(Where); } if (!GetRs.Status) { rs.Not("要修改数据对象不存在!"); goto Gor; } var Updata = GetRs.Data; _UpDataDto(ref Updata,Dto,UpColumn,Notcolumn); rs.Data = DbBus.Get(DbKeyName).Update().SetSource(Updata).ExecuteAffrows(); rs.OK("更新成功!"); } catch (Exception ex) { rs.Not(ex.Message); } Gor: return rs; } } }