分支自 SuZhouGuanHong/TaiYuanTaiZhong

WMS
dengjunjie
2023-12-13 d772a6cddc8ed656a2cb5604fc02f6e891db546e
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
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using WIDESEA_Core.CacheManager;
using WIDESEA_Core.DBManager;
using WIDESEA_Core.Extensions.AutofacManager;
using WIDESEA_Core.Utilities;
using WIDESEA_Entity.DomainModels;
 
namespace WIDESEA_Core.UserManager
{
   public static class DepartmentContext
    {
        static DepartmentContext()
        {
            GetAllDept();
        }
        private static object _deptObj = new object();
 
 
        private static string _deptVersionn = "";
 
        public const string _deptCacheKey = "inernalDept";
        private static List<Dept> _depts { get; set; }
        public static ICacheService CacheContext
        {
            get
            {
                return AutofacContainerModule.GetService<ICacheService>();
            }
        }
 
        public static List<Dept> GetAllDept()
        {
            //每次比较缓存是否更新过,如果更新则重新获取数据
            string _cacheVersion = CacheContext.Get(_deptCacheKey);
            if (_deptVersionn != "" && _deptVersionn == _cacheVersion)
            {
                return _depts ?? new List<Dept>();
            }
            lock (_deptObj)
            {
                if (_deptVersionn != "" && _depts != null && _deptVersionn == _cacheVersion)
                {
                    return _depts;
                }
 
                _depts = DBServerProvider.DbContext.Set<Sys_Department>()
                    .Select(s => new Dept()
                    {
                        id = s.DepartmentId,
                        key = s.DepartmentId,
                        value = s.DepartmentName,
                        parentId = s.ParentId
                    }).ToList();
 
                string cacheVersion = CacheContext.Get(_deptCacheKey);
                if (string.IsNullOrEmpty(cacheVersion))
                {
                    cacheVersion = DateTime.Now.ToString("yyyyMMddHHMMssfff");
                    CacheContext.Add(_deptCacheKey, cacheVersion);
                }
                else
                {
                    _deptVersionn = cacheVersion;
                }
            }
            return _depts;
        }
 
 
        public static List<Guid> GetAllChildrenIds([NotNull] List<Guid> ids)
        {
            ids = ids.Distinct().ToList();
            if (ids.Count == 0)
            {
                return new List<Guid>() { Guid.NewGuid() };
            }
             
            for (int i = 0; i < ids.Count(); i++)
            {
                Guid id = ids[i];
                var list = _depts.Where(x => x.parentId == id && !ids.Contains(x.id)).Select(s => s.id).Distinct().ToList();
                if (list.Count > 0)
                { 
                    ids.AddRange(list);
                }
            }
 
            return ids;
        }
        public static List<Guid> GetAllChildrenIds(Guid id)
        {
            return GetAllChildrenIds(new List<Guid>() { id });
        }
 
        public static WebResponseContent Reload(this WebResponseContent webResponse)
        {
            if (webResponse.Status)
            {
                CacheContext.Remove(_deptCacheKey);
            }
            return webResponse;
        }
    }
 
}
public partial class Dept
{
    public Guid id { get; set; }
    public Guid? parentId { get; set; }
    public Guid key { get; set; }
    public string value { get; set; }
}