schangxiang@126.com
2 天以前 3091217d4f4ccecde578ef1abddd810094bfc6bc
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
224
225
226
227
using iWareCommon.Common.Service;
using iWareCommon.Utils;
using iWareDataCore.BASE.Dao;
using iWareDataCore.BASE.Entity;
using iWareDataCore.ORM;
using iWareDataCore.Properties;
using iWareExcel.EXCEL.Entity;
using iWareExcel.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace iWareDataCore.BASE.Service
{
    public class PlaceViewService : CommonService<PlaceViewEntity, BASEPlaceView, DbModelCore>
    {
 
        private static object Lock = new object();
 
        private PlaceViewService() : base(PlaceViewDao.GetInstance()) { }
 
        private static PlaceViewService Instance = null;
 
        /// <summary>
        /// 获取单例的方法
        /// </summary>
        /// <returns>角色服务的单例实体</returns>
        public static PlaceViewService GetInstance()
        {
 
            if (Instance == null)
            {
                lock (Lock)
                {
                    if (Instance == null)
                    {
                        Instance = new PlaceViewService();
                    }
                }
            }
            return Instance;
        }
 
 
        
 
 
 
        /// <summary>
        /// 导入Excel
        /// </summary>
        /// <param name="path">文件在服务器上的路径</param>
        /// <param name="workBookEntity">导入使用的目板</param>
        /// <param name="name">导入者姓名</param>
        /// <param name="msg">异常错误消息</param>
        /// <returns>是否导入成功</returns>
        public bool ImportExcel(string path, WorkBookEntity workBookEntity, out string msg)
        {
            msg = "";
 
            using (var dbModel = new DbModelCore())
            {
                try
                {
                    var ds = ExcelHelper.FromExcelByEPPlus(path, workBookEntity, out msg);
                    if (!string.IsNullOrEmpty(msg))
                    {
                        throw new Exception(msg);
                    }
 
                    var places = ds[0];
                    if (places.Count < 1)
                    {
                        msg = "库位记录数为0,请修改后重新导入";
                        return false;
                    }
                    var types = dbModel.BASEPlaceTypes.Where(x => 1 == 1).ToList();
                    var i = 0;
                    var codes = new List<string>();
                    var shelfCodeRowColLayers = new List<string>();
                    foreach (PlaceViewEntity place in places)
                    {
                        if (string.IsNullOrEmpty(place.Code))
                        {
                            msg = string.Format("在第{0}行,库位编号不能为空,请修改后重新导入", i + 1);
                            return false;
                        }
 
                        if (string.IsNullOrEmpty(place.PlaceTypeName))
                        {
                            msg = string.Format("在第{0}行,库位类型不能为空,请修改后重新导入", i + 1);
                            return false;
                        }
 
                       
 
                        var type = types.FirstOrDefault(x => x.name == place.PlaceTypeName);
                        if (type == null)
                        {
                            msg = string.Format("在第{0}行,不存在库位类型{1},请修改后重新导入", i + 1, place.PlaceTypeName);
                            return false;
                        }
                        place.TypeId = type.id;
 
                        if (codes.Contains(place.Code))
                        {
                            msg = string.Format("在第{0}行,库位编号为{1}的库位已重复出现,请修改后重新导入", i + 1, place.Code);
                            return false;
                        }
 
 
                        if (shelfCodeRowColLayers.Contains(place.Row + "#" + place.Col + "#" + place.Layer))
                        {
                            msg = string.Format("在第{0}行,第{1}排第{2}列第{3}列的库位已出现过,请修改后重新导入", i + 1, place.Row, place.Col, place.Layer);
                            return false;
                        }
                        shelfCodeRowColLayers.Add(place.Row + "#" + place.Col + "#" + place.Layer);
 
                        codes.Add(place.Code);
                    }
 
                    var placeSaves = new List<PlaceEntity>();
                    var placeUpdates = new List<PlaceEntity>();
 
                    var existPlaces = dbModel.BASEPlaces.Select(x => new { x.id, x.code }).Where(x => codes.Contains(x.code)).ToList();
 
                    foreach (PlaceViewEntity place in places)
                    {
                        var one = existPlaces.FirstOrDefault(x => x.code == place.Code);
                        if (one == null) { placeSaves.Add(new PlaceEntity(place)); }
                        else
                        {
                            place.Id = one.id;
                            placeUpdates.Add(new PlaceEntity(place));
                        }
                    }
 
                    var sum = placeSaves.Count % 1000 == 0 ? placeSaves.Count / 1000 : placeSaves.Count / 1000 + 1;
                    for (var j = 0; j < (int)Math.Ceiling(placeSaves.Count / 1000.0); j++)
                    {
                        var sql = string.Format("INSERT INTO [dbo].[BASEPlace] (code,typeid,heightlevel,row,col,layer,length,width,height,posx,posy,posz,maxweight,status,islock,isexecute) VALUES");
                        for (var r = j * 1000; r < j * 1000 + 1000; r++)
                        {
                            if (r == placeSaves.Count)
                            {
                                break;
                            }
                            sql += string.Format("( '{0}', '{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}','{15}'),",
                                StringHelper.RelpaceQuot(placeSaves[r].Code),
                                placeSaves[r].TypeId,
                                placeSaves[r].HeightLevel,
                                placeSaves[r].Row,
                                placeSaves[r].Col,
                                placeSaves[r].Layer,
                                placeSaves[r].Length,
                                placeSaves[r].Width,
                                placeSaves[r].Height,
                                placeSaves[r].Posx,
                                placeSaves[r].Posy,
                                placeSaves[r].Posz,
                                placeSaves[r].MaxWeight,
                                placeSaves[r].Status,
                                placeSaves[r].IsLock,
                                placeSaves[r].IsExecute);
                        }
                        sql = sql.Substring(0, sql.Length - 1);
                        var num = dbModel.Database.ExecuteSqlCommand(sql);
                    }
 
                    PlaceDao.GetInstance().Update(placeUpdates, dbModel);
                    dbModel.SaveChanges();
                    return string.IsNullOrEmpty(msg);
                }
                catch (Exception ex)
                {
                    msg = ex.Message;
                    LogTextHelper.WriteLog(Resources.LogDir, this.ToString(), "ImportExcel", ex.Message);
                    return false;
                }
            }
 
        }
 
        /// <summary>
        /// 获取指定字符串开头的库位列表
        /// </summary>
        /// <param name="term">指定字符串</param>
        /// <param name="size">返回的最大条数</param>
        /// <param name="msg">异常错误消息</param>
        /// <returns>送货单号列表</returns>
        public List<string> GetPlaceCodes(string term, int size, out string msg)
        {
            using (var dbModel = new DbModelCore())
            {
                try
                {
                    msg = "";
 
                    if (string.IsNullOrEmpty(term))
                    {
                        return new List<string>();
                    }
 
 
                    var placeviews = dbModel.BASEPlaceViews.Select(x => new { x.id, x.code, }).OrderByDescending(x => x.id).Where(x => x.code.StartsWith(term)).Skip(0).Take(size).ToList();
 
                    var placecodes = new List<string>();
 
                    placeviews.ForEach(x => placecodes.Add(x.code));
 
                    return placecodes;
 
                }
                catch (Exception ex)
                {
                    msg = ex.Message;
                    LogTextHelper.WriteLog(Resources.LogDir, this.ToString(), "GetPlaceCodes", ex.Message);
                    return new List<string>();
                }
            }
        }
       
 
    }
}