using BaseOUDAL; using YLErp.Model; namespace YLErp.Modules.ClientModule { public class ClientMarginTemplateService : YLBaseService { public ClientMarginTemplateService(OptUserInfo userInfo) : base(userInfo) { } public SearchListResult SearchClientMarginTemplateList(ClientMarginTemplateReq req) { var query = from cmt in DbContext.client_margin_template join mt in DbContext.margin_template_v2 on cmt.MarginTemplateId equals mt.id into mts from mt in mts.DefaultIfEmpty() select new ClientMarginTemplate() { ClientId = cmt.ClientId, ClientLevel = cmt.ClientLevel, id = cmt.id, MarginRuleType = mt == null ? -1 : mt.RuleType, MarginTemplateName = mt == null ? "" : mt.Name, ValueDate = cmt.ValueDate }; if (req.ClientId != null) { query = query.Where(x => x.ClientId == req.ClientId); } if(req.ClientIds != null && req.ClientIds.Any()) { query = query.Where(x => req.ClientIds.Contains(x.ClientId)); } if (req.MarginRuleType != null) { query = query.Where(x => x.MarginRuleType == req.MarginRuleType); } if (string.IsNullOrEmpty(req.sidx)) { req.sidx = "id"; req.sord = "desc"; } SearchListResult retListResult = query.ToSearchList(req); foreach(var item in retListResult.rows) { var client = DataCacheProvider.GetClientDataSource().GetData(item.ClientId); if(client != null) { item.ClientName = client.Name; item.ClientNumber = client.Number; } if (!string.IsNullOrWhiteSpace(item.ClientLevel)) { var level = DataCacheProvider.GetClientLevelDataSource().GetData(int.Parse(item.ClientLevel)); if (level != null) { item.ClientLevel = level.LevelName; } } } return retListResult; } /// /// 获取预付金系数 /// /// /// /// /// public Dictionary GetMarginFactorDict(DateTime settleDate, IEnumerable clientIds, bool useLevelIdData = false) { if (clientIds is null) { throw new ArgumentNullException(nameof(clientIds)); } Dictionary result = new Dictionary(); var levelDict = new Dictionary(); if (useLevelIdData) { levelDict = clientIds.Distinct() .Select(O => { var c = DataCacheProvider.GetClientDataSource().GetData(O); return new { ClientId = c.id, c.LevelId }; }) .ToDictionary(K => K.ClientId, V => V.LevelId + ""); } var db = DbContextFactory.GetYLDbContext(); var query = db.client_margin_template .Join(db.margin_template_detail, c => c.MarginTemplateId, cm => cm.MarginTemplateId, (c, cm) => new { ClientId = c.ClientId, ClientLevel = c.ClientLevel, ValueDate = c.ValueDate, SpanConfigJson = cm.SpanConfigJson }).Where(s => s.ValueDate <= settleDate); if (levelDict.Count > 0) { levelDict = clientIds.Distinct() .Select(O => { var c = DataCacheProvider.GetClientDataSource().GetData(O); return new { ClientId = c.id, c.LevelId }; }) .ToDictionary(K => K.ClientId, V => V.LevelId + ""); var levelId = levelDict.Values; query = query.Where(O => clientIds.Contains(O.ClientId) || levelId.Contains(O.ClientLevel)); } else { query = query.Where(O => clientIds.Contains(O.ClientId)); } var span2List = query.ToArray(); foreach (var item in clientIds) { var obj = span2List.LastOrDefault(O => O.ClientId == item); if (obj == null) { var lId = levelDict[item]; obj = span2List.LastOrDefault(O => O.ClientLevel == lId); } if (obj != null) { result[item] = JsonHelper.Deserialize(obj.SpanConfigJson); } } return result; } } }