using Dm; using Model.ModelDto.SysDto; using SqlSugar; using System; using System.Collections.Generic; using System.Linq; using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; using WMS.Entity.Context; using WMS.Entity.SysEntity; using WMS.IDAL.ISysInterface; namespace WMS.DAL.SysInfrastructure { public class DictionaryRepository : BaseRepository, IDictionaryRepository { private static readonly SqlSugarScope Db = DataContext.Db; public DictionaryRepository() : base(Db) { } /// /// 获取字典信息列表 /// /// 字典名称 /// 父级字典号 /// 层级 /// 允许编辑 /// 允许新增 /// public List GetDicList(string DictName, string DictNo, string Level, string IsEdit, string IsAdd) { string str = "select dic.*,user1.RealName CreateName from SysDictionary dic left join SysUserInfor user1 on dic.CreateUser = user1.Id where dic.IsDel = @isdel"; //判断层级 if (Level == "1") { str += " and dic.Level = @level"; } //判断允许编辑 if (!string.IsNullOrEmpty(IsEdit)) { str += " and dic.IsEdit = @isedit"; } //判断允许新增 if (!string.IsNullOrEmpty(IsAdd)) { str += " and dic.IsAdd = @isadd"; } //判断字典名称是否为空 if (!string.IsNullOrEmpty(DictName)) { if (Level == "1") { str += " and dic.ParentNo in (select DictNo from SysDictionary where DictName like @dictname)"; } if (Level == "0") { str += " and dic.DictName like @dictname"; } if (string.IsNullOrEmpty(Level)) { str += " and dic.DictName like @dictname or dic.ParentNo in (select DictNo from SysDictionary where DictName like @dictname)"; } } //判断父级字典号是否为空 if (!string.IsNullOrEmpty(DictNo)) { if (Level == "1") { str += " and dic.ParentNo = @dictno"; } if (Level == "0") { str += " and dic.DictNo = @dictno"; } if (string.IsNullOrEmpty(Level)) { str += " and dic.DictNo = @dictno or dic.ParentNo = @dictno"; } } //判断层级 if (Level == "0") { str += " and dic.Level = @level"; } //排序(根据ord显示顺序) str += " and dic.IsPublic ='0'"; List diclist = Db.Ado.SqlQuery(str, new { isdel = "0", //是否删除 dictname = "%" + DictName + "%", //字典名称 dictno = DictNo, //字典号 level = Level, //层级 isedit = IsEdit, //允许编辑 isadd = IsAdd //允许新增 }); diclist = diclist.OrderBy(a => int.Parse(a.Ord)).ToList(); return diclist; } /// /// 根据id获取字典信息 /// /// 字典id /// public SysDictionary GetDicById(int id) { string str = "select dic.*,user1.UserName CreateName from SysDictionary dic left join SysUserInfor user1 on dic.CreateUser = user1.Id where dic.IsDel = @isdel and dic.Id = @id"; SysDictionary dic = Db.Ado.SqlQuery(str, new { isdel = "0", //是否删除 id //id }).First(); return dic; } /// /// 获取父级字典号(根据层级根目录) /// /// public List GetDicParentListByLevel() { string str = "select Id,DictNo,DictName,ParentNo,Level from SysDictionary where Level = @level and IsDel = @isdel and IsAdd = @isadd"; List diclist = Db.Ado.SqlQuery(str, new { level = "0", //层级 isdel = "0", //是否删除 isadd = "0" //允许添加 }); return diclist; } /// /// 根据编号查询字典信息 /// /// 字典编号 /// public List GetDicByNo(string DictNo) { string str = $"select * from SysDictionary where DictNo = @dictno"; List diclist = Db.Ado.SqlQuery(str, new { dictno = DictNo //字典编号 }); return diclist; } /// /// 新增字典信息 /// /// 数据字典实体模型 /// public async Task AddDic(SysDictionary dic) { //获取是否拥有相同字典名称 if (!string.IsNullOrEmpty(dic.DictName)) { var dicno = Db.Queryable().First(a => a.DictName == dic.DictName); if (dicno != null) { throw new Exception("获取是否拥有相同字典名称"); } } else { throw new Exception("字典名称为空,请核实!"); } string str = "insert into SysDictionary values(@dictno, @dictname, @parentno, @ord, @level, @ispublic, @isedit, @isadd, @isdel, @createtime, @createuser, null, null)"; //int i = await CudAsync(str, new int i = await Db.Ado.ExecuteCommandAsync(str, new { dictno = dic.DictNo, //字典编号 dictname = dic.DictName, //字典名称 parentno = dic.ParentNo, //父级字典号 ord = dic.Ord, //显示顺序 level = dic.Level, //层级 ispublic = dic.IsPublic, //是否公开 isedit = dic.IsEdit, //允许编辑 isadd = dic.IsAdd, //允许添加 isdel = "0", //是否删除 createtime = Db.GetDate(), //创建时间 createuser = dic.CreateUser //创建人 }); return i; } /// /// 删除字典信息 /// /// 数据字典实体模型 /// public async Task DelDic(SysDictionary dic) { string str = $"update SysDictionary set IsDel = @isdel, UpdateTime = @updatetime, UpdateUser = @updateuser where id = @id"; int i = await Db.Ado.ExecuteCommandAsync(str, new { isdel = "1", //是否删除 updatetime = Db.GetDate(), //更改时间 updateuser = dic.UpdateUser, //更改人 id = dic.Id //id }); return i; } /// /// 编辑字典信息 /// /// 数据字典实体模型 /// public async Task ExitDic(SysDictionary dic) { //获取是否拥有相同字典名称 if (!string.IsNullOrEmpty(dic.DictName)) { var dicno = Db.Queryable().First(a => a.DictName == dic.DictName); if (dicno != null) { throw new Exception("获取是否拥有相同字典名称"); } } else { throw new Exception("字典名称为空,请核实!"); } string str = "update SysDictionary set DictNo = @dictno, DictName = @dictname, ParentNo = @parentno, Ord = @ord, IsPublic = @ispublic, IsEdit = @isedit, IsAdd = @isadd, UpdateTime = @updatetime, UpdateUser = @updateuser where id = @id"; int i = await Db.Ado.ExecuteCommandAsync(str, new { dictno = dic.DictNo, //字典编号 dictname = dic.DictName, //字典名称 parentno = dic.ParentNo, //父级字典号 ord = dic.Ord, //显示顺序 //level = dic.Level, //层级 ispublic = dic.IsPublic, //是否公开 isedit = dic.IsEdit, //允许编辑 isadd = dic.IsAdd, //允许添加 updatetime = Db.GetDate(), //更改时间 updateuser = dic.UpdateUser, //更改人 id = dic.Id //id }); return i; } } internal class NewClass { public NewClass() { } public override bool Equals(object obj) { return obj is NewClass other; } public override int GetHashCode() { return 0; } } }