zhao
2021-06-04 c7ec496f9e41c2227103b3ef776e4a3f91bce6b2
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
using HH.WMS.Entitys;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Xml;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using HH.WMS.Utils;
using HH.WMS.WebUI.Controllers;
using HH.WMS.Entitys.Algorithm;
using HH.WMS.Entitys.Basic;
using System.Text.RegularExpressions;
using HH.AutoBom.Core;
using HH.WMS.Entitys.Common;
 
namespace HH.WMS.WebUI.Areas.Sys.Controllers
{
    public class StockInitController : BaseController
    {
        //
        // GET: /Sys/StockInit/
 
        public ActionResult Index()
        {
            return View();
        }
 
        public string UnscrambleXls(HttpPostedFileBase file)
        {
            try
            {
                string stockStr = HttpWMS_Get("api/Common/StockList");
                List<TN_AB_STOCKEntity> lstStock = JsonConvert.DeserializeObject<List<TN_AB_STOCKEntity>>(JsonConvert.DeserializeObject<OperateResult>(stockStr).Data.ToString());
 
                string locationStr = HttpWMS_Get("api/LocationExt/GetLocationList");
                List<TN_WM_LOCATION_EXTEntity> lstLocation = JsonConvert.DeserializeObject<List<TN_WM_LOCATION_EXTEntity>>(locationStr);
 
                string trayStr = HttpWMS_Get("api/Tray/GetTrayList");
                List<TN_WM_B_TRAY_INFOEntity> lstTray = JsonConvert.DeserializeObject<List<TN_WM_B_TRAY_INFOEntity>>(trayStr);
 
                //string lotStr = HttpWMS_Get("api/LotInfo/GetLotInfoList");
                //List<TN_WM_LOT_INFOEntity> lstLot = JsonConvert.DeserializeObject<List<TN_WM_LOT_INFOEntity>>(lotStr);
 
 
                List<AutoBomItemEntity> lstItem = new List<AutoBomItemEntity>();
 
                IWorkbook wk = null;
                var stream = file.InputStream;
                var buffer = new byte[stream.Length];
                stream.Read(buffer, 0, buffer.Length);
                stream.Position = 0;
                if (file.FileName.ToLower().IndexOf(".xlsx") > 0)
                {
                    wk = new XSSFWorkbook(stream);
                }
                else if (file.FileName.ToLower().IndexOf(".xls") > 0)
                {
                    wk = new HSSFWorkbook(stream);
                }
                //importOrderList = new List<ImportOrderTemplate>();
                var loginer = HH.AutoBom.Core.FormsAuth.GetUserData<HH.AutoBom.Core.LoginerBase>();
                List<ImportTemplateEntity> iteList = new List<ImportTemplateEntity>();
                List<ErrorRowData> errorDataList = new List<ErrorRowData>();
 
                ISheet sheet = wk.GetSheetAt(0);   //读取当前表数据
 
                for (int j = 1; j <= sheet.LastRowNum; j++)  //LastRowNum 是当前表的总行数
                {
                    var excelRowIndex = (j + 1).ToString();
 
                    IRow row = sheet.GetRow(j);  //读取当前行数据
                    if (string.IsNullOrEmpty(Util.ToString(row.Cells[3]))) continue;
                    if (row != null)
                    {
                        try
                        {
                            ImportTemplateEntity ite = new ImportTemplateEntity();
                            ite.CN_S_STOCK_CODE = Util.ToString(Util.ToString(row.Cells[0]));//仓库编码
                            ite.CN_S_LOCATION_CODE = Util.ToString(row.Cells[1]);//货位编码
                            ite.CN_S_TRAY_CODE = Util.ToString(row.Cells[2]);//托盘编码
                            ite.CN_S_ITEM_CODE = Util.ToString(row.Cells[3]);//物料编码
                            ite.CN_F_QUANTITY = Convert.ToDecimal(row.Cells[4].ToString());//数量
                            ite.CN_S_PRODUCTION_BATCH = Util.ToString(row.Cells[5]);//生产批次
                            //ite.CN_S_LOT_CODE = Util.ToString(row.Cells[6]);//到货批次
                            ite.CN_S_ITEM_STATE = Util.ToString(row.Cells[6]);//物料状态
                            if (string.IsNullOrEmpty(ite.CN_S_ITEM_STATE))
                            {
                                ite.CN_S_ITEM_STATE = HH.WMS.Common.Constants.ItemState_Qualified;//没传物料状态的情况下,默认合格
                            }
 
 
 
                            if (!string.IsNullOrEmpty(ite.CN_S_STOCK_CODE))
                            {
                                var checkStock = lstStock.Where(t => t.CN_S_STOCK_CODE == ite.CN_S_STOCK_CODE).ToList();
                                if (!checkStock.Any())
                                {
                                    errorDataList.Add(new ErrorRowData { CN_S_RowIndex = excelRowIndex, CN_S_ErrorContent = "仓库号 " + ite.CN_S_STOCK_CODE + " 不存在!" });
                                }
                                var checkStockIn = iteList.Where(t => t.CN_S_STOCK_CODE == ite.CN_S_STOCK_CODE).ToList();
                                if (iteList.Count > 0 && (!checkStockIn.Any()))
                                {
                                    errorDataList.Add(new ErrorRowData { CN_S_RowIndex = excelRowIndex, CN_S_ErrorContent = "仓库号 " + ite.CN_S_STOCK_CODE + " 错误,一个Excel只能初始化一个仓库!" });
                                }
                            }
                            else
                            {
                                errorDataList.Add(new ErrorRowData { CN_S_RowIndex = excelRowIndex, CN_S_ErrorContent = "仓库号不可为空!" });
                            }
 
                            if (!string.IsNullOrEmpty(ite.CN_S_LOCATION_CODE))
                            {
                                var checkLocation = lstLocation.Where(t => t.CN_S_LOCATION_CODE.Trim() == ite.CN_S_LOCATION_CODE).ToList();
                                if (!checkLocation.Any())
                                {
                                    errorDataList.Add(new ErrorRowData { CN_S_RowIndex = excelRowIndex, CN_S_ErrorContent = "货位号 " + ite.CN_S_LOCATION_CODE + " 不存在!" });
                                }
                                else
                                {
                                    ite.CN_S_AREA_CODE = checkLocation[0].CN_S_AREA_CODE;
                                }
                            }
                            else
                            {
                                errorDataList.Add(new ErrorRowData { CN_S_RowIndex = excelRowIndex, CN_S_ErrorContent = "货位号不可为空!" });
                            }
 
                            if (!string.IsNullOrEmpty(ite.CN_S_TRAY_CODE))
                            {
                                var checkTray = lstTray.Where(t => t.CN_S_TRAY_CODE == ite.CN_S_TRAY_CODE).ToList();
                                if (!checkTray.Any())
                                {
                                    errorDataList.Add(new ErrorRowData { CN_S_RowIndex = excelRowIndex, CN_S_ErrorContent = "托盘号 " + ite.CN_S_TRAY_CODE + " 不存在!" });
                                }
                            }
 
                            if (!string.IsNullOrEmpty(ite.CN_S_ITEM_CODE))
                            {
                                var checkTray = lstItem.Where(t => t.CN_S_ITEM_CODE == ite.CN_S_ITEM_CODE).ToList();
                                if (!checkTray.Any())
                                {
                                    string jsonItem = HttpWMS_Get("api/Item/GetItemModel?itemCode=" + ite.CN_S_ITEM_CODE);
                                    OperateResult oresult = JsonConvert.DeserializeObject<OperateResult>(jsonItem);
                                    if (oresult.Success)
                                    {
                                        AutoBomItemEntity itemEntity = JsonConvert.DeserializeObject<AutoBomItemEntity>(oresult.Data.ToString());
 
                                        List<AutoBomItemEntity> lstItemEntity = new List<AutoBomItemEntity>();
                                        lstItemEntity.Add(itemEntity);
 
                                        if (!lstItemEntity.Any())
                                        {
                                            errorDataList.Add(new ErrorRowData { CN_S_RowIndex = excelRowIndex, CN_S_ErrorContent = "物料编码 " + ite.CN_S_ITEM_CODE + " 不存在!" });
                                        }
                                        else
                                        {
                                            ite.CN_S_ITEM_NAME = lstItemEntity[0].CN_S_ITEM_NAME;
                                            ite.CN_S_MEASURE_UNIT = lstItemEntity[0].CN_S_MEASURE_UNIT;
                                            ite.CN_S_FIGURE_NO = lstItemEntity[0].CN_S_FIGURE_NO;
                                            ite.CN_S_MODEL = lstItemEntity[0].CN_S_MODEL;
                                            lstItem.AddRange(lstItemEntity);
                                        }
                                    }
                                    else
                                    {
                                        errorDataList.Add(new ErrorRowData { CN_S_RowIndex = excelRowIndex, CN_S_ErrorContent = "物料编码 " + ite.CN_S_ITEM_CODE + " 不存在!" });
                                    }
                                }
                                else
                                {
                                    ite.CN_S_ITEM_NAME = checkTray[0].CN_S_ITEM_NAME;
                                    ite.CN_S_MEASURE_UNIT = checkTray[0].CN_S_MEASURE_UNIT;
                                    ite.CN_S_FIGURE_NO = checkTray[0].CN_S_FIGURE_NO;
                                    ite.CN_S_MODEL = checkTray[0].CN_S_MODEL;
                                }
                            }
                            else
                            {
                                errorDataList.Add(new ErrorRowData { CN_S_RowIndex = excelRowIndex, CN_S_ErrorContent = "物料编码不可为空!" });
                            }
 
 
                            if (!(string.IsNullOrEmpty(ite.CN_F_QUANTITY.ToString()) || ite.CN_F_QUANTITY.ToString() == "0"))
                            {
                                var qtyCheck = Regex.IsMatch(ite.CN_F_QUANTITY.ToString(), @"^[0-9]+\.{0,1}[0-9]{0,2}$");
                                if (!qtyCheck)
                                {
                                    errorDataList.Add(new ErrorRowData { CN_S_RowIndex = excelRowIndex, CN_S_ErrorContent = "数量格式不正确!" });
                                }
                            }
                            else
                            {
                                errorDataList.Add(new ErrorRowData { CN_S_RowIndex = excelRowIndex, CN_S_ErrorContent = "数量不可为空或0!" });
                            }
 
                            //if (!string.IsNullOrEmpty(ite.CN_S_LOT_CODE))
                            //{
                            //    var checkLot = iteList.Where(t => t.CN_S_LOT_CODE == ite.CN_S_LOT_CODE).ToList();
                            //    if (checkLot.Count > 1)
                            //    {
                            //        errorDataList.Add(new ErrorRowData { CN_S_RowIndex = excelRowIndex, CN_S_ErrorContent = "到货批次 " + ite.CN_S_LOT_CODE + " 在Excel中有重复!" });
                            //    }
 
                            //    var checkLotSys = lstLot.Where(t => t.CN_S_LOT_CODE == ite.CN_S_LOT_CODE).ToList();
                            //    if (checkLotSys.Any())
                            //    {
                            //        errorDataList.Add(new ErrorRowData { CN_S_RowIndex = excelRowIndex, CN_S_ErrorContent = "到货批次 " + ite.CN_S_LOT_CODE + " 在系统中已存在!" });
                            //    }
                            //}
 
 
                            iteList.Add(ite);
 
                        }
                        catch (Exception ex)
                        {
                            return JsonConvert.SerializeObject(OperateResult.Error("内部抛出异常!" + ex.ToString()));
                        }
                    }
                }
                var Group_List = iteList.GroupBy(x => new
                {
                    x.CN_S_LOCATION_CODE,
                    x.CN_S_TRAY_CODE,
                    x.CN_S_ITEM_CODE
                }).Select(g => new ImportTemplateEntity
                {
                    CN_S_LOCATION_CODE = g.FirstOrDefault().CN_S_LOCATION_CODE.Trim(),
                    CN_S_TRAY_CODE = g.FirstOrDefault().CN_S_TRAY_CODE.Trim(),
                    CN_S_ITEM_CODE = g.FirstOrDefault().CN_S_ITEM_CODE.Trim(),
                    CN_F_QUANTITY = g.Count()
                }).ToList();
 
                foreach (var item in Group_List)
                {
                    if (item.CN_F_QUANTITY > 1)
                    {
                        errorDataList.Add(new ErrorRowData { CN_S_RowIndex = "0", CN_S_ErrorContent = "货位 " + item.CN_S_LOCATION_CODE + " ,托盘 " + item.CN_S_TRAY_CODE + "  ,物料 " + item.CN_S_ITEM_CODE + " 存在重复!" });
                    }
                }
 
                var stockEntityList = lstStock.Where(x => x.CN_S_STOCK_CODE == iteList[0].CN_S_STOCK_CODE).ToList();
 
                if (stockEntityList.Any())
                {
                    if (stockEntityList[0].CN_C_IS_INIT !="N")
                    {
                        errorDataList.Add(new ErrorRowData { CN_S_RowIndex = "0", CN_S_ErrorContent = stockEntityList[0].CN_S_STOCK_CODE + " 号仓库必须是未初始化状态!" });
                    }
                }
 
                return
                        JsonConvert.SerializeObject(new
                        {
                            ImportData = iteList,
                            ErrorData = errorDataList
                        });
            }
            catch (Exception ex)
            {
                return JsonConvert.SerializeObject(OperateResult.Error("外部抛出异常!" + ex.ToString()));
            }
        }
 
        #region 初始化功能
        /// <summary>
        /// 初始化功能
        /// </summary>
        /// <returns></returns>
        public string ImportExcelData()
        {
            //XML文件流
            var sr = new StreamReader(Request.InputStream);
            var stream = sr.ReadToEnd();
 
            var loginer = FormsAuth.GetUserData<LoginerBase>();
            var TokenId = loginer.Extend1;//TokenId
 
            var streamStr = new
            {
                PostData = stream,
                TokenId = TokenId
            };
            return HttpWMS_Post("api/StockInit/SaveStockInit", JsonConvert.SerializeObject(streamStr));
        }
 
        #endregion
    }
 
 
}