using Newtonsoft.Json;
|
using OfficeOpenXml.FormulaParsing.Excel.Functions.Information;
|
using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
|
using System;
|
using System.Collections.Generic;
|
using System.ComponentModel;
|
using System.Linq;
|
using System.Reflection;
|
using System.Text;
|
using System.Threading.Tasks;
|
using WIDESEA_Common.CommonEnum;
|
using WIDESEA_Common.LocationEnum;
|
using WIDESEA_Common.StockEnum;
|
using WIDESEA_Common.TaskEnum;
|
using WIDESEA_Core;
|
using WIDESEA_Core.BaseRepository;
|
using WIDESEA_Core.BaseServices;
|
using WIDESEA_Core.Caches;
|
using WIDESEA_Core.DB;
|
using WIDESEA_Core.Enums;
|
using WIDESEA_Core.Helper;
|
using WIDESEA_Core.Utilities;
|
using WIDESEA_DTO.System;
|
using WIDESEA_ISystemService;
|
using WIDESEA_Model.Models;
|
|
namespace WIDESEA_SystemService
|
{
|
public class Sys_DictionaryService : ServiceBase<Sys_Dictionary, IRepository<Sys_Dictionary>>, ISys_DictionaryService
|
{
|
private readonly IUnitOfWorkManage _unitOfWorkManage;
|
private readonly ICacheService _cacheService;
|
|
public Sys_DictionaryService(IRepository<Sys_Dictionary> BaseDal, IUnitOfWorkManage unitOfWorkManage, ICacheService cacheService) : base(BaseDal)
|
{
|
_unitOfWorkManage = unitOfWorkManage;
|
_cacheService = cacheService;
|
}
|
|
public IRepository<Sys_Dictionary> Repository => BaseDal;
|
|
public override WebResponseContent AddData(SaveModel saveModel)
|
{
|
string validResult = typeof(Sys_Dictionary).ValidateDicInEntity(saveModel.MainData, true, TProperties);
|
|
if (!string.IsNullOrEmpty(validResult))
|
{
|
return WebResponseContent.Instance.Error(validResult);
|
}
|
|
Sys_Dictionary dictionary = saveModel.MainData.DicToModel<Sys_Dictionary>();
|
if (!string.IsNullOrEmpty(_cacheService.Get(dictionary.DicNo)))
|
_cacheService.Remove(dictionary.DicNo);
|
return base.AddData(saveModel);
|
}
|
|
public override WebResponseContent UpdateData(SaveModel saveModel)
|
{
|
Sys_Dictionary dictionary = saveModel.MainData.DicToModel<Sys_Dictionary>();
|
_cacheService.Remove(dictionary.DicNo);
|
return base.UpdateData(saveModel);
|
}
|
|
public IEnumerable<Sys_Dictionary> GetDictionaries(IEnumerable<string> dicNos, bool executeSql = true)
|
{
|
List<Sys_Dictionary> Dictionaries = GetDictionaries(dicNos.ToList());
|
|
foreach (var item in Dictionaries)
|
{
|
if (executeSql)
|
{
|
// 2020.05.01增加根据用户信息加载字典数据源sql
|
string sql = item.Sql;
|
if (!string.IsNullOrEmpty(sql))
|
{
|
item.DicList = Query(sql);
|
}
|
}
|
}
|
|
return Dictionaries;
|
}
|
|
List<Sys_DictionaryList> Query(string sql)
|
{
|
try
|
{
|
return BaseDal.QueryDynamicDataBySql(sql, null).Select(s => new Sys_DictionaryList()
|
{
|
DicName = s.value,
|
DicValue = s.key.ToString()
|
}).ToList();
|
}
|
catch (Exception ex)
|
{
|
return new List<Sys_DictionaryList>();
|
}
|
}
|
|
private List<Sys_Dictionary> GetDictionaries(List<string> dicNos)
|
{
|
//base.QueryData().ToList();
|
List<Sys_Dictionary> _dictionaries = Db.Queryable<Sys_Dictionary>().Where(x => x.Enable == 1 && dicNos.Contains(x.DicNo)).Includes(x => x.DicList).ToList();
|
return _dictionaries;
|
}
|
|
public List<VueDictionaryDTO> GetVueDictionary(string[] dicNos)
|
{
|
if (dicNos == null || dicNos.Count() == 0) return new List<VueDictionaryDTO>();
|
List<VueDictionaryDTO> vueDictionaryDTOs = new List<VueDictionaryDTO>();
|
|
List<string> cacheDicNos = new List<string>();
|
foreach (string n in dicNos)
|
{
|
string str = _cacheService.Get(n);
|
if (!string.IsNullOrEmpty(str))
|
{
|
VueDictionaryDTO? vueDictionary = JsonConvert.DeserializeObject<VueDictionaryDTO>(str);
|
|
if (vueDictionary != null)
|
{
|
vueDictionaryDTOs.Add(vueDictionary);
|
cacheDicNos.Add(n);
|
}
|
}
|
}
|
if (dicNos.Where(x => !cacheDicNos.Contains(x)).Count() > 0)
|
{
|
List<VueDictionaryDTO> selectDics = GetDictionaries(dicNos.Where(x => !cacheDicNos.Contains(x))).Select(s => new VueDictionaryDTO
|
{
|
DicNo = s.DicNo,
|
Config = s.Config,
|
//dbSql = s.Sql,
|
Data = s.DicList.OrderByDescending(o => o.OrderNo).Select(list => new { key = list.DicValue, value = list.DicName }),
|
SaveCache = false
|
}).ToList();
|
|
foreach (var item in selectDics)
|
{
|
if (!_cacheService.Exists(item.DicNo) && item.SaveCache)
|
{
|
_cacheService.Add(item.DicNo, item.Serialize());
|
}
|
}
|
vueDictionaryDTOs.AddRange(selectDics);
|
}
|
|
//object GetSourceData(string dicNo, string dbSql, object data)
|
//{
|
// if (string.IsNullOrEmpty(dbSql))
|
// {
|
// return data;
|
// }
|
// return BaseDal.QueryObjectDataBySql(dbSql, null);
|
//}
|
//List<VueDictionaryDTO> vueDictionaryDTOs = dicConfig.Select(item => new VueDictionaryDTO
|
//{
|
// DicNo = item.dicNo,
|
// Config = item.config,
|
// Data = GetSourceData(item.dicNo, item.dbSql, item.list)
|
//}).ToList();
|
|
try
|
{
|
List<string> dicList = dicNos.ToList();
|
if (dicNos.Where(x => !cacheDicNos.Contains(x)).Count() > 0)
|
{
|
string str = AppSettings.Configuration["dics"];
|
if (!string.IsNullOrEmpty(str))
|
{
|
List<string> cusDics = new List<string>();
|
|
List<string> dics = str.Split(",").ToList();
|
|
foreach (var item in dics)
|
{
|
if (dicNos.Contains(item) && !cacheDicNos.Contains(item))
|
{
|
dicList.Remove(item);
|
cusDics.Add(item);
|
}
|
}
|
|
foreach (var item in cusDics)
|
{
|
VueDictionaryDTO vueDictionaryDTO = GetVueDictionary(item.Trim());
|
if (vueDictionaryDTO != null)
|
{
|
vueDictionaryDTOs.Add(vueDictionaryDTO);
|
if (!_cacheService.Exists(item) && vueDictionaryDTO.SaveCache)
|
{
|
_cacheService.Add(item, vueDictionaryDTO.Serialize());
|
}
|
}
|
}
|
}
|
}
|
}
|
catch
|
{
|
|
}
|
return vueDictionaryDTOs;
|
}
|
|
private VueDictionaryDTO GetVueDictionary(string key)
|
{
|
VueDictionaryDTO result = null;
|
try
|
{
|
switch (key)
|
{
|
default:
|
return result;
|
}
|
}
|
catch (Exception ex)
|
{
|
return null;
|
}
|
}
|
}
|
}
|