using Bowin.Common.Linq; using Bowin.Common.Linq.Entity; using Bowin.Common.Utility; using YLShipBuildLandMap.Entity; using YLShipBuildLandMap.Entity.ViewModel; using YLShipBuildLandMap.Entity.ViewModel.SystemSetting; /*using Microsoft.EntityFrameworkCore.Query.ExpressionTranslators.Internal;*/ using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Options; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Threading.Tasks; using Z.EntityFramework.Plus; using Microsoft.Extensions.DependencyInjection; namespace YLShipBuildLandMap.Services.SystemSetting { public class DictionaryService : IDictionaryService { private YLShipBuildLandMapContext DbContext { get; set; } readonly MemoryCacheEntryOptions mCacheOp; public DictionaryService(YLShipBuildLandMapContext dbContext, IOptions iopCacheOp) { DbContext = dbContext; mCacheOp = iopCacheOp.Value; } private IQueryable GetDictionaryItemQuery(Expression> dexp, Expression> iexp) { var query = from r in DbContext.SysDictionaryItem.Where(iexp).Where(x => x.RecordStatus > 0) from d in DbContext.SysDictionary.Where(dexp).Where(x => x.DictionaryCode == r.DictionaryCode && x.RecordStatus > 0 && x.IsEditable == true) select new DictionaryItemView { DictionaryItemID = r.DictionaryItemId, Code = r.Code, DictionaryCode = r.DictionaryCode, Value = r.Value, Name = r.Name, DictionaryName = d.Name, OrderNo = r.OrderNo }; return query; } private IQueryable GetDictionaryQuery(Expression> exp) { var query = from r in DbContext.SysDictionary.Where(exp).Where(x => x.RecordStatus > 0 && x.IsEditable == true) select r; return query; } public Task> GetDictionaryItemList(int? pageIndex, int? pageSize, string dictionaryName, string dictionaryCode, string name) { Expression> dexp = (e => true); Expression> iexp = (e => true); if (!string.IsNullOrEmpty(dictionaryName)) { dexp = dexp.And(e => e.Name.Contains(dictionaryName.Trim())); } if (!string.IsNullOrEmpty(name)) { iexp = iexp.And(e => e.Name.Contains(name.Trim())); } if (!string.IsNullOrEmpty(dictionaryCode)) { iexp = iexp.And(e => e.DictionaryCode.Contains(dictionaryCode.Trim())); } var result = GetDictionaryItemQuery(dexp, iexp).OrderBy(e => e.DictionaryCode).ThenBy(e => e.OrderNo).ToGridResultSet(pageIndex, pageSize); return Task.FromResult(result); } public List GetDictionaryItemList(string dictionaryCode, string name) { var sql = this.DbContext.SysDictionaryItem.AsQueryable(); sql = sql.WhereIf(!string.IsNullOrEmpty(dictionaryCode), x => x.DictionaryCode == dictionaryCode); sql = sql.WhereIf(!string.IsNullOrEmpty(name), x => x.Name == name); return sql.Where(x => x.RecordStatus > 0).OrderBy(x => x.DictionaryCode).ThenBy(x => x.Value).ToList(); } public Task> GetDictionaryItemsbyCache(string dictionaryCode) { //查询 需要一个参数 var dics = DbContext.SysDictionaryItem.Where(x => x.RecordStatus > 0 && x.DictionaryCode == dictionaryCode).OrderBy(o=>o.OrderNo).FromCacheAsync(mCacheOp); return dics; } public Task> GetTugboatTitleDictionary(string dictionaryCode) { var dics = from a in DbContext.SysDictionaryItem.Where(x => x.RecordStatus > 0 && x.DictionaryCode == "Title") from b in DbContext.SysDictionaryItem.Where(x => x.RecordStatus > 0 && x.DictionaryCode == dictionaryCode && x.Value == a.Value) select a; //var dics = DbContext.SysDictionaryItem.Where(x => x.RecordStatus > 0 && x.DictionaryCode == dictionaryCode).OrderBy(o => o.OrderNo).FromCacheAsync(mCacheOp); return dics.OrderBy(x => x.OrderNo).FromCacheAsync(mCacheOp); } public Task> GetDepartmentTitleDictionary(string dictionaryCode) { var dics = from a in DbContext.SysDictionaryItem.Where(x => x.RecordStatus > 0 && x.DictionaryCode == "Title") from b in DbContext.SysDictionaryItem.Where(x => x.RecordStatus > 0 && x.DictionaryCode == dictionaryCode && x.Value == a.Value).DefaultIfEmpty() where b.DictionaryItemId == null select a; //var dics = DbContext.SysDictionaryItem.Where(x => x.RecordStatus > 0 && x.DictionaryCode == dictionaryCode).OrderBy(o => o.OrderNo).FromCacheAsync(mCacheOp); return dics.OrderBy(x => x.OrderNo).FromCacheAsync(mCacheOp); } public string GetDictionaryName(IEnumerable dics,int? value) { string name = ""; var item= dics.FirstOrDefault(w => w.Value == value); if (item != null) { name = item.Name; } return name; } public Task> GetDictionaryList(int? pageIndex, int? pageSize, string dictionaryName, string dictionaryCode) { Expression> exp = (e => true); if (!string.IsNullOrEmpty(dictionaryName)) { exp = exp.And(e => e.Name.Contains(dictionaryName.Trim())); } if (!string.IsNullOrEmpty(dictionaryCode)) { exp = exp.And(e => e.DictionaryCode.Contains(dictionaryCode.Trim())); } var result = GetDictionaryQuery(exp).OrderBy(e => e.OrderNo).ThenBy(e => e.DictionaryCode).ToGridResultSet(pageIndex, pageSize); return Task.FromResult(result); } public SysDictionaryItem GetDicItem(Guid? dicItemID) { return DbContext.SysDictionaryItem.FirstOrDefault(x => x.DictionaryItemId == dicItemID); } public async Task Save(SysDictionaryItem dicItem, Guid userID) { SysDictionaryItem dbDicItem = DbContext.SysDictionaryItem.FirstOrDefault(x => x.DictionaryItemId == dicItem.DictionaryItemId); if (dbDicItem != null) { dbDicItem.DictionaryCode = dicItem.DictionaryCode; dbDicItem.Value = dicItem.Value; dbDicItem.Name = dicItem.Name; dbDicItem.OrderNo = dicItem.OrderNo; DbContext.SysDictionaryItem.Update(dbDicItem); } else { dicItem.DictionaryItemId = Guid.NewGuid(); dicItem.Code = ""; dicItem.RecordStatus = 1; dicItem.IsEditable = true; this.DbContext.Add(dicItem); } await this.DbContext.SaveChangesAsync(); } public async Task Delete(List dicItemIDList) { await this.DbContext.SysDictionaryItem.Where(x => dicItemIDList.Contains(x.DictionaryItemId)).DeleteAsync(); } public List GetEmployeeStatus() { var list = new List() { 1,2,4,5,6 }; return list; } } }