222
schangxiang@126.com
2024-12-11 b07eb132b5886122d4d154cd163485af611ebc6a
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
using System.Globalization;
using System.Text;
using Admin.NET.Application.Entity;
 
namespace Admin.NET.Application
{
    /// <summary>
    /// 单据号生成帮助类
    /// </summary>
    public class SerialUtil
    {
        private readonly static object _lockTest = new();
        private readonly static object _lockPublic = new();
 
 
        /// <summary>
        /// 生成编号/单据号
        /// </summary>
        /// <param name="SerialType"></param>
        /// <returns></returns>
        public static async Task<string> GetSerial(SerialTypeEnum SerialType, SqlSugarRepository<WmsConfigSerialRule> _repRuleDetailRep, SqlSugarRepository<WmsConfigSerialSN> _repSNRep)
        {
            string _result = string.Empty;
            switch (SerialType)
            {
                case SerialTypeEnum.测试编号:
                    lock (_lockTest)
                    {
                        _result= GetSerialDetails(SerialType, _repRuleDetailRep, _repSNRep).Result;
                        break;
                    };
 
                default:
                    lock (_lockPublic)
                    {//为了避免多线程出现编号一致的情况,加上锁,此处为公共锁,只用于不频繁生成编号的,若需要频繁生成,请参考上面Test设置单独锁
                        _result= GetSerialDetails(SerialType, _repRuleDetailRep, _repSNRep).Result;
                        break;
                    }
            }
 
            await Task.CompletedTask;
            return _result;
        }
        /// <summary>
        /// 生成编号/单据号
        /// </summary>
        /// <param name="SerialType"></param>
        /// <returns></returns>
        private static async Task<string> GetSerialDetails(SerialTypeEnum SerialType, SqlSugarRepository<WmsConfigSerialRule> _repRuleDetailRep, SqlSugarRepository<WmsConfigSerialSN> _repSNRep)
        {
 
            var lst =await _repRuleDetailRep.AsQueryable().Where(u => u.SerialType == (int)SerialType).OrderBy(x => x.ItemNo).ToListAsync();
 
            string[] strs = new string[lst.Count];
 
            WmsConfigSerialSN sl = new WmsConfigSerialSN();
            for (int i = 0; i < lst.Count; i++)
            {
                if (lst[i].SourceType.ToUpper() == SerialSourceTypeEnum.UD.ToString())
                {
                    strs[i] = lst[i].UserDefine ?? string.Empty;//如果数据源是自定义,则取自定义字段
                }
 
                if (lst[i].SourceType.ToUpper() == SerialSourceTypeEnum.BT.ToString())
                {
                    strs[i] = lst[i].UserDefine ?? string.Empty;//如果数据源是自定义,则取自定义字段
                }
 
                if (lst[i].SourceType.ToUpper() == SerialSourceTypeEnum.Y4.ToString())
                {
                    strs[i] = DateTime.Now.Year.ToString();
                }
                if (lst[i].SourceType.ToUpper() == SerialSourceTypeEnum.Y2.ToString())
                {
                    strs[i] = DateTime.Now.Year.ToString().Substring(2, 2);
                }
                if (lst[i].SourceType.ToUpper() == SerialSourceTypeEnum.M2.ToString())
                {
                    strs[i] = DateTime.Now.Month.ToString("00");
                }
                if (lst[i].SourceType.ToUpper() == SerialSourceTypeEnum.D2.ToString())
                {
                    strs[i] = DateTime.Now.Day.ToString("00");
 
                }
                if (lst[i].SourceType.ToUpper() == SerialSourceTypeEnum.W2.ToString())
                {
                    GregorianCalendar gc = new GregorianCalendar();
                    int week = gc.GetWeekOfYear(DateTime.Now, CalendarWeekRule.FirstDay, DayOfWeek.Sunday);
                    strs[i] = "W" + week.ToString();
 
                }
                if (lst[i].SourceType.ToUpper() == SerialSourceTypeEnum.SN.ToString())
                {
                    int cout = lst[i].SerialLength;
                    string c = string.Empty;
                    for (int j = 0; j < cout; j++)
                    {
                        c += "0";
                    }
                    try
                    {
                        if (lst[i].GetData.HasValue && lst[i].GetData == 1)
                        {
                            var sn =await _repSNRep.AsQueryable().Where(x => x.SerialType == lst[i].SerialType && x.CurrentDate == DateTime.Now.Date)
                                            .OrderByDescending(x => x.Sn).FirstAsync();
                            if (sn == null)
                            {
                                sl.Sn = 1;
                            }
                            else
                            {
                                sl.Sn = sn.Sn + 1;
                            }
                           
                        }
                        else
                        {
 
                            var sn =await _repSNRep.AsQueryable().Where(x => x.SerialType == lst[i].SerialType).OrderByDescending(x => x.Sn).FirstAsync();
                            sl.Sn = sn.Sn + 1;
                        }
 
                        strs[i] = sl.Sn.ToString(c);
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                    sl.CurrentDate = DateTime.Now.Date;
                    sl.SerialType = lst[i].SerialType;
                }
            }
 
            StringBuilder WmsConfigSerialSN = new StringBuilder();
            foreach (var item in strs)
            {
                WmsConfigSerialSN.Append(item.ToString());
            }
            // await sl.InsertAsync();
            await _repSNRep.InsertAsync(sl);
            
            //await repositoryBaseSerialSN.Add(sl);
            //edm.SaveChanges();
 
            if (string.IsNullOrEmpty(WmsConfigSerialSN.ToString()))
            {
                throw Oops.Oh("生成的编号为空");
            }
 
            return WmsConfigSerialSN.ToString();
        }
    }
}