1.0
z8018
2025-03-20 ee4ec0b0c5d4451d2fa03948488c879dae96a869
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
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;
            }
        }
    }
}