bklLiudl
2025-04-08 a82e815f97b789dc4ca208df09dd96534a023f4a
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Model.ModelDto.LogDto;
using Model.ModelDto.SysDto;
using SqlSugar;
using WMS.BLL.LogServer;
using WMS.Entity.Context;
using WMS.Entity.SysEntity;
using WMS.IBLL.ILogServer;
using WMS.IBLL.ISysServer;
using WMS.IDAL.ISysInterface;
 
namespace WMS.BLL.SysServer
{
    public class PalletsServer : IPalletsServer
    {
        public IPalletsRepository PalletsRst { get; set; }
        private readonly IOperationSysServer _operation; //操作日志
        public PalletsServer(IPalletsRepository palletsRst, IOperationSysServer operation)
        {
            PalletsRst = palletsRst;
            _operation = operation;
        }
        /// <summary>
        /// 查询托盘表信息
        /// </summary>
        /// <param name="palletNo">托盘码</param>
        /// <param name="status">状态 0未使用 1使用中</param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public List<PalletsDto> GetPalletsList(string palletNo, string status, int page, int limit, out int count)
        {
            try
            {
                //创建表达式
                Expression<Func<SysPallets, bool>> item = Expressionable.Create<SysPallets>()
                    .AndIF(!string.IsNullOrWhiteSpace(palletNo), it => it.PalletNo.Contains(palletNo.Trim()))
                    .AndIF(!string.IsNullOrWhiteSpace(status), it => it.Status == status)
                    .ToExpression();//注意 这一句 不能少
 
 
                var data = PalletsRst.GetAllByOrderPage(item, limit, page, out int counts)
                    .Includes(x => x.CreateUserInfo)
                    .ToList();
                count = counts;
                return data.Select(m => new PalletsDto()
                {
                    Id = m.Id,
                    PalletNo = m.PalletNo,
                    Type = m.Type == "0" ? "托盘" : m.Type == "1" ? "中转箱" : "",
                    Status = m.Status,
                    LastUse = m.LastUse,
                    CreateTime = m.CreateTime,
                    CreateUserName = m.CreateUserInfo == null ? "" : m.CreateUserInfo.RealName
                }).ToList();
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
 
        public string GetPalletsNo(string palletNo)
        {
            try
            {
                var time = DateTime.Now.ToString("yyyy");
                var time2 = time.Substring(2, 2);
                int codeId;
 
                if (!string.IsNullOrWhiteSpace(palletNo))
                {
                    var code = PalletsRst.GetAllWhere(m => m.PalletNo == palletNo).First();
                    return code.PalletNo;
                }
                else
                {
                    var code = PalletsRst.GetAll().OrderByDescending(m => m.PalletNo).First();
 
                    if (code != null)
                    {
                        string riQi = code.PalletNo.Substring(1, 2);
                        if (riQi == time2)
                        {
                            codeId = int.Parse(code.PalletNo.Substring(3, 5)) + 1;
                        }
                        else
                        {
                            codeId = int.Parse("00001");
                        }
                    }
                    else
                    {
                        codeId = int.Parse("00001");
                    }
                    int liuShuiId = codeId;
                    var pallet = "T"+time2 + Convert.ToString(liuShuiId).PadLeft(5, '0');
                    return pallet;
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
 
        public async Task<bool> AddPallets(int groupCount, int userId)
        {
            try
            {
                var db = DataContext.Db;
                if (groupCount <= 0)
                {
                    throw new Exception("组数需大于0");
                }
 
                var pallNo = db.Queryable<SysPallets>().Max(m => m.PalletNo);
 
                var str = pallNo.Substring(3, 5);
                string remove = pallNo.Substring(0, 3);
                int sibelius = 0;
                var num = 0;
                if (remove != "T" + DateTime.Now.ToString("yy"))
                {
                    remove = "T" + DateTime.Now.ToString("yy");
                    sibelius = 0;
                }
                else
                {
                    sibelius = Convert.ToInt16(str);
                }
                for (int i = 0; i < groupCount; i++)
                {
                    sibelius += 1;
                    if (sibelius>99999)
                    {
                        throw new Exception("托盘码位数已达上线");
                    }
                    string code = remove + Convert.ToString(sibelius).PadLeft(5, '0');
                    if (db.Queryable<SysPallets>().Count(m=>m.PalletNo == code)>=1)
                    {
                        continue;
                    }
                    var item = new SysPallets
                    {
                        PalletNo = code,
                        Status = "0",
                        Type = "0",
                        CreateUser = userId
                    };
                    Thread.Sleep(100);
                    num += db.Insertable(item).ExecuteCommand();
                    if (num > 0)
                    {
                        if (groupCount > 1)
                        {
                            await _operation.InsertOperation("仓库设置", "条码管理", item.PalletNo, "批量添加", "批量添加托盘信息 托盘号:" + item.PalletNo, userId);
                        }
                        else
                        {
                            await _operation.InsertOperation("仓库设置", "条码管理", item.PalletNo, "添加", "添加托盘信息 托盘号:" + item.PalletNo, userId);
                        }
                        
                    }
                }
 
                return num > 0;
 
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
 
    }
}