WYB
2021-03-22 91b8cdad021ab052e4991f3d41834a6f0ddc36b8
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
using JiepeiWMS.Common;
using JiepeiWMS.Common.Helper;
using JiepeiWMS.Common.HttpContextUser;
using JiepeiWMS.IRepository.UnitOfWork;
using JiepeiWMS.IServices;
using JiepeiWMS.Model;
using JiepeiWMS.Model.Models;
using JiepeiWMS.Model.ViewModels;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
 
namespace JiepeiWMS.Api.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize(Permissions.Name)]
    public class WMProductListController : ControllerBase
    {
        /// <summary>
        /// 商品
        /// </summary>
        private readonly IWMProductListServices _wMProductListServices;
        private readonly IWMProductSpecServices _wMProductSpecServices;
        private readonly IWMProductUnitServices _wMProductUnitServices;
        private readonly IWMProductClassServices _wMProductClassServices;
        private readonly IWMProductInfoServices _wMProductInfoServices;
        private readonly IWMPurchaseQuoteDetailServices _wMPurchaseQuoteDetailServices;
        private readonly IWMSaleDetailServices _wMSaleDetailServices;
        private readonly ISysUserInfoServices _sysUserInfoServices;
        private readonly IWMWareHouseServices _wMWareHouseServices;
        private readonly ISysOrgServices _BllSysOrg;
        private readonly IUser _user;
        private readonly IUnitOfWork _unitOfWork;
        private readonly IUserRoleServices _userRole;
 
        public WMProductListController(IWMProductListServices WMProductListServices, IWMProductSpecServices WMProductSpecServices, IWMProductUnitServices WMProductUnitServices, IWMProductClassServices WMProductClassServices, IWMProductInfoServices WMProductInfoServices, IWMPurchaseQuoteDetailServices WMPurchaseQuoteDetailServices, IWMSaleDetailServices WMSaleDetailServices, ISysUserInfoServices SysUserInfoServices, IWMWareHouseServices WMWareHouseServices, ISysOrgServices BllSysOrg, IUser user, IUnitOfWork unitOfWork, IUserRoleServices userRole)
        {
            _wMProductListServices = WMProductListServices;
            _wMProductSpecServices = WMProductSpecServices;
            _wMProductUnitServices = WMProductUnitServices;
            _wMProductClassServices = WMProductClassServices;
            _wMProductInfoServices = WMProductInfoServices;
            _wMPurchaseQuoteDetailServices = WMPurchaseQuoteDetailServices;
            _wMSaleDetailServices = WMSaleDetailServices;
            _sysUserInfoServices = SysUserInfoServices;
            _wMWareHouseServices = WMWareHouseServices;
            _BllSysOrg = BllSysOrg;
            _user = user;
            _unitOfWork = unitOfWork;
            _userRole = userRole;
        }
 
        /// <summary>
        /// 查询列表
        /// </summary>
        /// <param name="key">关键字</param>
        /// <param name="options">分类筛选</param>
        /// <param name="startdate">开始时间筛选</param>
        /// <param name="enddate">结束时间筛选</param>
        /// <param name="wareHouseId">仓库Id</param>
        /// <param name="page">页数</param>
        /// <param name="intPageSize">条数</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<MessageModel<PageModel<WMProductList>>> Index(string key = "", string options = "", DateTime? startdate = null, DateTime? enddate = null, int wareHouseId = 0, int page = 1, int intPageSize = 20)
        {
            if (string.IsNullOrWhiteSpace(key))
                key = "";
            if (string.IsNullOrWhiteSpace(options))
                options = "";
 
            var PageList = await _wMProductListServices.GetWMProductListPage(page, intPageSize, key, options, startdate, enddate, wareHouseId);
 
            return new MessageModel<PageModel<WMProductList>>()
            {
                msg = "获取成功",
                success = PageList.dataCount >= 0,
                response = PageList
            };
        }
 
        /// <summary>
        /// 获取列表
        /// </summary>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpGet]
        public async Task<MessageModel<object>> Get(int id = 0, int wareHouseId = 0)
        {
            decimal quantity = 0;
 
            var listList = await _wMProductListServices.Query(x => x.IsUse == true);
 
            //listList = listList.Where(x => x.SysOrgId == _user.SysOrgId).ToList();
 
            if (wareHouseId > 0)
            {
                var ProductInfoModel = await _wMProductInfoServices.GetModel(x => x.ProductListId == id && x.WareHouseId == wareHouseId);
                if (ProductInfoModel == null)
                    quantity = 0;
                else
                    quantity = ProductInfoModel.Quantity ?? 0;
            }
 
            return new MessageModel<object>()
            {
                msg = "获取成功",
                success = true,
                response = new
                {
                    listList,
                    quantity
                }
            };
        }
 
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<MessageModel<string>> Add([FromBody] WMProductList request)
        {
            var data = new MessageModel<string>();
 
            var sysUserInfoModel = await _sysUserInfoServices.QueryById(_user.ID);
            if (sysUserInfoModel == null || sysUserInfoModel.tdIsDelete == true)
            {
                data.msg = "当前用户已被禁用无法进行导入";
                data.response = 0.ObjToString();
                data.status = 500;
                return data;
            }
            var userRoleList = await _userRole.Query(x => x.UserId == sysUserInfoModel.uID);
            if (userRoleList == null && userRoleList.Count() == 0)
            {
                data.msg = "当前用户还未配置角色无法进行导入";
                data.response = 0.ObjToString();
                data.status = 500;
                return data;
            }
            //var productListModel = await _wMProductListServices.GetModel(x => x.Code == request.Code);
            //if (productListModel != null)
            //{
            //    data.msg = "已有相同商品编码,请联系管理员";
            //    return data;
            //}
 
            if (request.Price < 0)
            {
                data.msg = "零售价价格不能小于0";
                return data;
            }
            if (request.PurchasePrice < 0)
            {
                data.msg = "采购价价格不能小于0";
                return data;
            }
            var ProductClassMdoel = await _wMProductClassServices.QueryById(request.ClassId);
            if (ProductClassMdoel != null)
            {
                var ProductClassMdoel1 = await _wMProductClassServices.GetModel(x => x.ParentId == ProductClassMdoel.Id);
                if (ProductClassMdoel1 != null)
                {
                    data.msg = "商品分类不是末级分类";
                    return data;
                }
            }
            else
            {
                data.msg = "未获取到商品分类";
                return data;
            }
 
            request.AdminId = _user.ID;
            request.CreateTime = DateTime.Now;
            request.SysOrgId = _user.SysOrgId;
 
            var result2 = new Tuple<string, bool, string>("", true, "");
            //根据配置是否调用NC接口
            var NCInterfaceSecret = Appsettings.app("AppSettings", "NCInterfaceSecret").ToString();
            if (NCInterfaceSecret == "True")
                //调NC新增物料接口
                result2 = await _wMProductListServices.BeginMaterialInNC(request);
            if (result2.Item2)
            {
                if (!string.IsNullOrWhiteSpace(result2.Item3))
                    request.Code = result2.Item3;
                var result = await _wMProductListServices.Add(request);
                if (result > 0)
                {
                    var wareHouseModel = await _wMWareHouseServices.GetModel(a => a.SysOrgId == _user.SysOrgId);
                    WMProductInfo model = new WMProductInfo()
                    {
                        ProductListId = result,
                        ProductSpecId = request.SpecIds,
                        PurchasePrice = request.PurchasePrice,
                        Quantity = 0,
                        AdminId = _user.ID,
                        CreateTime = DateTime.Now,
                        SysOrgId = _user.SysOrgId,
                        WareHouseId = wareHouseModel == null ? 0 : wareHouseModel.Id
                    };
                    var result1 = await _wMProductInfoServices.Add(model);
                    data.success = result1 > 0;
                    if (data.success)
                    {
                        data.msg = "添加成功";
                        data.response = result.ObjToString();
                    }
                }
            }
            else if (!result2.Item2 && !string.IsNullOrWhiteSpace(result2.Item1))
            {
                data.msg = result2.Item1;
                data.response = 1.ObjToString();
            }
            return data;
        }
 
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        //[HttpPut]
        //public async Task<MessageModel<string>> Update([FromBody] WMProductList request)
        //{
        //    var data = new MessageModel<string>();
        //    if (request.Id > 0)
        //    {
        //        //var productListModel = await _wMProductListServices.GetModel(x => x.Code == request.Code && x.Id != request.Id);
        //        //if (productListModel != null)
        //        //{
        //        //    data.msg = "已有相同商品编码,请联系管理员";
        //        //    return data;
        //        //}
        //        if (request.Price < 0)
        //        {
        //            data.msg = "零售价价格不能小于0";
        //            return data;
        //        }
        //        var ProductClassMdoel = await _wMProductClassServices.QueryById(request.ClassId);
        //        if (ProductClassMdoel != null)
        //        {
        //            var ProductClassMdoel1 = await _wMProductClassServices.GetModel(x => x.ParentId == ProductClassMdoel.Id);
        //            if (ProductClassMdoel1 != null)
        //            {
        //                data.msg = "商品分类不是末级分类";
        //                return data;
        //            }
        //        }
        //        else
        //        {
        //            data.msg = "未获取到商品分类";
        //            return data;
        //        }
 
        //        var result = new Tuple<string, bool, string>("", true, "");
        //        //根据配置是否调用NC接口
        //        var NCInterfaceSecret = Appsettings.app("AppSettings", "NCInterfaceSecret").ToString();
        //        if (NCInterfaceSecret == "True")
        //            //调NC新增物料接口
        //            result = await _wMProductListServices.BeginMaterialInNC(request);
        //        if (result.Item2)
        //        {
        //            if (!string.IsNullOrWhiteSpace(result.Item3))
        //                request.Code = result.Item3;
        //            data.success = await _wMProductListServices.Update(request);
        //            if (data.success)
        //            {
        //                data.msg = "更新成功";
        //                data.response = request?.Id.ObjToString();
        //            }
        //        }
        //        else if (!result.Item2 && !string.IsNullOrWhiteSpace(result.Item1))
        //        {
        //            data.msg = result.Item1;
        //            data.response = request?.Id.ObjToString();
        //        }
        //    }
        //    return data;
        //}
 
        /// <summary>
        /// 设置商品
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<MessageModel<string>> SetProduct([FromBody] WMProductList request)
        {
            var data = new MessageModel<string>();
            if (request.Id > 0)
            {
                var wareHouseModel = await _wMWareHouseServices.GetModel(x => x.Name == request.WareHouseName || x.Id == request.WareHouseName.ObjToInt());
                var ProductInfoModel = await _wMProductInfoServices.GetModel(x => x.ProductListId == request.Id && x.WareHouseId == wareHouseModel.Id);
                if (ProductInfoModel != null)
                {
                    ProductInfoModel.PurchasePrice = request.PurchasePrice;
                    ProductInfoModel.Quantity = request.Quantity;
                    ProductInfoModel.UpdateTime = DateTime.Now;
                    ProductInfoModel.WareHouseId = wareHouseModel.Id;
                    await _wMProductInfoServices.Update(ProductInfoModel);
                }
                else
                {
                    WMProductInfo model = new WMProductInfo()
                    {
                        ProductListId = request.Id,
                        ProductSpecId = request.SpecIds,
                        PurchasePrice = request.PurchasePrice,
                        Quantity = request.Quantity,
                        AdminId = _user.ID,
                        CreateTime = DateTime.Now,
                        SysOrgId = _user.SysOrgId,
                        WareHouseId = wareHouseModel.Id
                    };
                    await _wMProductInfoServices.Add(model);
                }
 
                if (string.IsNullOrWhiteSpace(request.Price.ToString()) && request.Price <= 0)
                {
                    request.Price = request.PurchasePrice;
                }
                request.UpdateTime = DateTime.Now;
 
                data.success = await _wMProductListServices.Update(request);
                if (data.success)
                {
                    data.msg = "设置成功";
                    data.response = request?.Id.ObjToString();
                }
            }
            return data;
        }
 
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        //[HttpDelete]
        //public async Task<MessageModel<string>> Delete(int id = 0)
        //{
        //    var data = new MessageModel<string>();
        //    if (id > 0)
        //    {
        //        var productListModel = await _wMProductListServices.QueryById(id);
        //        if (productListModel != null)
        //        {
        //            //var purchaseQuoteDetailModel = await _wMPurchaseQuoteDetailServices.GetModel(x => x.ProductId == productListModel.Id);
        //            //if (purchaseQuoteDetailModel != null)
        //            //{
        //            //    data.msg = productListModel.Name + "无法删除,该商品在采购申请使用中";
        //            //    return data;
        //            //}
        //            //var saleDetailModel = await _wMSaleDetailServices.GetModel(x => x.ProductId == productListModel.Id);
        //            //if (saleDetailModel != null)
        //            //{
        //            //    data.msg = productListModel.Name + "无法删除,该商品在领料申请使用中";
        //            //    return data;
        //            //}
        //            productListModel.IsUse = false;
 
        //            var result = new Tuple<string, bool, string>("", true, "");
        //            //根据配置是否调用NC接口
        //            var NCInterfaceSecret = Appsettings.app("AppSettings", "NCInterfaceSecret").ToString();
        //            if (NCInterfaceSecret == "True")
        //                //调NC新增物料接口
        //                result = await _wMProductListServices.BeginMaterialInNC(productListModel);
        //            if (result.Item2)
        //            {
        //                if (!string.IsNullOrWhiteSpace(result.Item3))
        //                    productListModel.Code = result.Item3;
        //                data.success = await _wMProductListServices.Update(productListModel);
        //                if (data.success)
        //                {
        //                    data.msg = "删除成功";
        //                    data.response = productListModel?.Id.ObjToString();
        //                }
        //            }
        //            else if (!result.Item2 && !string.IsNullOrWhiteSpace(result.Item1))
        //            {
        //                data.msg = result.Item1;
        //                data.response = productListModel?.Id.ObjToString();
        //            }
        //        }
        //    }
        //    return data;
        //}
 
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        //[HttpDelete]
        //public async Task<MessageModel<string>> BatchDelete(string ids = "")
        //{
        //    //var err = "";
        //    var data = new MessageModel<string>();
        //    var ProductListList = new List<WMProductList>();
 
        //    if (!string.IsNullOrWhiteSpace(ids))
        //    {
        //        foreach (var item in ids.Split(','))
        //        {
        //            var productListModel = await _wMProductListServices.QueryById(item.ObjToInt());
        //            if (productListModel != null)
        //            {
        //                //var purchaseQuoteDetailModel = await _wMPurchaseQuoteDetailServices.GetModel(x => x.ProductId == productListModel.Id);
        //                //var saleDetailModel = await _wMSaleDetailServices.GetModel(x => x.ProductId == productListModel.Id);
        //                //if (purchaseQuoteDetailModel != null || saleDetailModel != null)
        //                //    err += productListModel.Name + ",";
        //                //else
        //                ProductListList.Add(productListModel);
        //            }
        //        }
 
        //        //if (string.IsNullOrWhiteSpace(err))
        //        //{
        //        foreach (var item in ProductListList)
        //        {
        //            item.IsUse = false;
 
        //            var result = new Tuple<string, bool, string>("", true, "");
        //            //根据配置是否调用NC接口
        //            var NCInterfaceSecret = Appsettings.app("AppSettings", "NCInterfaceSecret").ToString();
        //            if (NCInterfaceSecret == "True")
        //                //调NC新增物料接口
        //                result = await _wMProductListServices.BeginMaterialInNC(item);
        //            if (result.Item2)
        //            {
        //                if (!string.IsNullOrWhiteSpace(result.Item3))
        //                    item.Code = result.Item3;
        //                data.success = await _wMProductListServices.Update(item);
        //                if (data.success)
        //                {
        //                    data.msg = "删除成功";
        //                    data.response = item?.Id.ObjToString();
        //                }
        //            }
        //            else if (!result.Item2 && !string.IsNullOrWhiteSpace(result.Item1))
        //            {
        //                data.msg = result.Item1;
        //                data.response = item?.Id.ObjToString();
        //                return data;
        //            }
        //        }
        //        //}
        //        //else
        //        //{
        //        //    data.msg = err.Trim(',') + "无法删除,有商品在领料申请使用中";
        //        //}
        //    }
        //    return data;
        //}
 
        /// <summary>
        /// 导出商品表格
        /// </summary>
        [HttpGet]
        public async Task<IActionResult> ExportProductList()
        {
            var sqlString = "";
            var sysUserInfoModel = new sysUserInfo();
            var role = string.Join(",", _user.GetClaimValueByType(ClaimTypes.Role));
            if (role.Contains("仓库管理员"))
            {
                sysUserInfoModel = await _sysUserInfoServices.QueryById(_user.ID);
                sqlString += " and pf.SysOrgId=" + sysUserInfoModel.SysOrgId;
            }
            //获取角色信息,判断是否数据展示
            if (role.Contains("超级管理员"))
            {
 
            }
            else
            {
                sqlString += " and pl.IsUse=1";
            }
            var productList = await _wMProductListServices.QuerySql("select pl.Id,Name,(select Name from WMProductClass where id=ClassId)ClassName,(select Name from WMProductSpec where id = SpecIds)SpecName, (select Unit from WMProductUnit where id = pl.Unit)UnitName,Code,BarCode,Price,pf.PurchasePrice,pf.Quantity,(select Name from WMWareHouse where id = pf.WareHouseId)WareHouseName,OddDays,OddFloatNum,StoreBaseline,StoreLimit,Sort,case when isuse = 1 then '启用' else '停用' end IsUseName,case when IsChecked = 1 then '采购申请时需要审核' else '采购申请时不需要审核' end IsCheckedName from WMProductList pl left join WMProductInfo pf on pl.id = pf.ProductListId where 1=1" + sqlString);
 
            if (productList != null && productList.Any())
            {
                var PList = productList.Select(c => new { c.Id, c.Name, c.ClassName, c.SpecName, c.UnitName, c.Code, c.BarCode, c.Price, c.PurchasePrice, c.Quantity, c.WareHouseName, c.OddDays, c.OddFloatNum, c.StoreBaseline, c.StoreLimit, c.Sort, c.IsUseName, c.IsCheckedName }).OrderBy(x => x.Id).ToList();
                var heads = new List<string>() { "编号", "商品名称", "商品分类", "商品单位", "商品规格", "物料编码", "商品条码", "零售价", "采购价", "库存", "所属仓库", "异常浮动天数", "异常浮动倍数", "库存预警下限", "库存预警上限", "排序", "状态", "是否需要审核" };
 
                var stream = ExcelHelper.CreateExcelStreamFromList(PList, heads);
                return File(stream, "application/octet-stream", "商品.xlsx");
            }
            return null;
        }
 
        /// <summary>
        /// 导入商品表格
        /// </summary>
        [HttpPost]
        public async Task<MessageModel<string>> ImportProductList([FromForm] IFormCollection files)
        {
            var data = new MessageModel<string>();
            var importProductListModel = new ImportProductListViewModels();
            var importProductListList = new List<ImportProductListViewModels>();
 
            try
            {
                var sysUserInfoModel = await _sysUserInfoServices.QueryById(_user.ID);
                if (sysUserInfoModel == null || sysUserInfoModel.tdIsDelete == true)
                {
                    data.msg = "当前用户已被禁用无法进行导入";
                    data.response = 0.ObjToString();
                    data.status = 500;
                    return data;
                }
                var userRoleList = await _userRole.Query(x => x.UserId == sysUserInfoModel.uID);
                if (userRoleList == null && userRoleList.Count() == 0)
                {
                    data.msg = "当前用户还未配置角色无法进行导入";
                    data.response = 0.ObjToString();
                    data.status = 500;
                    return data;
                }
 
                //创建事务
                _unitOfWork.BeginTran();
                var path = Directory.GetCurrentDirectory();
                var fileFolder = Path.Combine(path, "ImportFile");
                if (!Directory.Exists(fileFolder))
                    Directory.CreateDirectory(fileFolder);
 
                FormFileCollection fileCollection = (FormFileCollection)files.Files;
                foreach (var file in fileCollection)
                {
                    var fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + Path.GetExtension(file.FileName);
                    var filePath = Path.Combine(fileFolder, fileName);
 
                    //using (var stream = new FileStream(filePath, FileMode.Create))
                    //{
                    //    await file.CopyToAsync(stream);
                    //}
 
                    //var dtExcel = ExcelHelper.Read(filePath);
                    using (var stream = new FileStream(filePath, FileMode.Create))
                    {
                        file.CopyTo(stream);
                    }
 
                    var dtExcel = ExcelHelper.ImportExcel(filePath);
                    foreach (DataRow myRow in dtExcel.Rows)
                    {
                        if (string.IsNullOrWhiteSpace(myRow[0].ToString()))
                            continue;
                        if (string.IsNullOrWhiteSpace(myRow[1].ToString()))
                            continue;
                        if (string.IsNullOrWhiteSpace(myRow[2].ToString()))
                            continue;
                        if (string.IsNullOrWhiteSpace(myRow[3].ToString()))
                            continue;
 
                        importProductListModel = new ImportProductListViewModels()
                        {
                            OrgName = myRow[0].ToString(),
                            WareHouse = myRow[1].ToString(),
                            ListCode = myRow[2].ToString(),
                            ListName = myRow[3].ToString(),
                            Class = myRow[4].ToString(),
                            Spec = myRow[5].ToString(),
                            Unit = myRow[6].ToString(),
                            Quantity = myRow[7].ToString(),
                        };
                        importProductListList.Add(importProductListModel);
                    }
 
                    foreach (var item in importProductListList)
                    {
                        var warehouseId = 0;//仓库ID
                        var classId = 0;//分类ID
                        var unitId = 0;//单元ID
                        var specId = 0;//规格ID
                        var sysOrgId = 0;//主体ID
                        //var userId = 0;//用户ID
 
                        #region 获取分类、规格、单元的参数
                        var code = item.ListCode.Substring(0, item.ListCode.Trim().Length - 5);//编码
                        var Level = ExcelHelper.GradeForSort("一级");//等级
                        var productClassList = await _wMProductClassServices.GetClassCodeList(code, Level);
                        var sysOrgModel = await _BllSysOrg.GetShortName(item.OrgName.Trim());
                        if (sysOrgModel != null)
                        {
                            sysOrgId = sysOrgModel.Id;
                            //userId = sysOrgModel.UserId.Value;
                        }
 
                        //获取仓库
                        if (!string.IsNullOrWhiteSpace(item.WareHouse))
                        {
                            var wareHouseModel = await _wMWareHouseServices.GetModel(a => a.Name == item.WareHouse.Trim() && a.SysOrgId == sysOrgId);
                            if (wareHouseModel == null)
                            {
                                //添加单位表
                                WMWareHouse modelWareHouse = new WMWareHouse()
                                {
                                    Name = item.WareHouse.Trim(),
                                    IsUse = true,
                                    Type = 1,
                                    CreateTime = DateTime.Now,
                                    AdminId = _user.ID,
                                    Sort = 0,
                                    Remark = "",
                                    SysOrgId = sysOrgId
                                };
                                warehouseId = await _wMWareHouseServices.Add(modelWareHouse);
                            }
                            else
                            {
                                warehouseId = wareHouseModel.Id;
                            }
                        }
 
                        if (string.IsNullOrWhiteSpace(item.Class))
                            item.Class = "无";
                        if (string.IsNullOrWhiteSpace(code))
                            code = "0";
                        //获取分类
                        if (!string.IsNullOrWhiteSpace(code) && !string.IsNullOrWhiteSpace(item.Class))
                        {
                            var productClassModel = await _wMProductClassServices.GetModel(a => a.ClassCode == code);
                            if (productClassModel == null)
                            {
                                var productClassParentId = productClassList.Any() ? productClassList.OrderBy(x => x.Id).FirstOrDefault().Id : 0;
                                var productClassSort = await _wMProductClassServices.Query(x => x.ParentId == productClassParentId);
                                //添加分类表
                                WMProductClass modelClass = new WMProductClass()
                                {
                                    ClassName = item.Class.Trim(),
                                    ClassCode = code,
                                    Sort = productClassSort.Count(),
                                    ParentId = productClassParentId,
                                    LevelId = Level,
                                    SysOrgId = sysOrgId
                                };
                                classId = await _wMProductClassServices.Add(modelClass);
                            }
                            else
                            {
                                classId = productClassModel.Id;
                            }
                        }
 
                        //获取单位
                        if (!string.IsNullOrWhiteSpace(item.Unit))
                        {
                            var productUnitModel = await _wMProductUnitServices.GetModel(a => a.Unit == item.Unit.Trim());
                            if (productUnitModel == null)
                            {
                                //添加单位表
                                WMProductUnit modelUnit = new WMProductUnit()
                                {
                                    Unit = item.Unit.Trim(),
                                    SysOrgId = sysOrgId
                                };
                                unitId = await _wMProductUnitServices.Add(modelUnit);
                            }
                            else
                            {
                                unitId = productUnitModel.Id;
                            }
                        }
 
                        //获取规格
                        if (!string.IsNullOrWhiteSpace(item.Spec))
                        {
                            var productSpecModel = await _wMProductSpecServices.GetModel(a => a.Name == item.Spec.Trim());
                            if (productSpecModel == null)
                            {
                                //添加规格表
                                WMProductSpec modelSpec = new WMProductSpec()
                                {
                                    Name = item.Spec.Trim(),
                                    AdminId = _user.ID,
                                    CreateTime = DateTime.Now,
                                    Remark = "",
                                    SysOrgId = sysOrgId
                                };
                                specId = await _wMProductSpecServices.Add(modelSpec);
                            }
                            else
                            {
                                specId = productSpecModel.Id;
                            }
                        }
                        #endregion
 
                        //匹配商品表的分类编号
                        var productListModel = await _wMProductListServices.GetModel(x => x.Code == item.ListCode);
                        if (productListModel != null)
                        {
                            //更新商品表
                            //productListModel.Name = item.ListName.Trim();
                            //productListModel.ClassId = classId;
                            //productListModel.SpecIds = specId;
                            //productListModel.Unit = unitId;
                            //await _wMProductListServices.Update(productListModel);
 
                            //匹配商品关联表的仓库
                            var productInfoModel = await _wMProductInfoServices.GetModel(x => x.ProductListId == productListModel.Id && x.WareHouseId == warehouseId);
                            if (productInfoModel != null)
                            {
                                //更新商品关联表
                                //productInfoModel.ProductSpecId = specId;
                                productInfoModel.Quantity = item.Quantity.Trim().ObjToDecimal();
                                await _wMProductInfoServices.Update(productInfoModel);
                            }
                            else
                            {
                                //添加商品关联表
                                WMProductInfo model = new WMProductInfo()
                                {
                                    ProductListId = productListModel.Id,
                                    ProductSpecId = specId,
                                    PurchasePrice = 0,
                                    Quantity = item.Quantity.Trim().ObjToDecimal(),
                                    AdminId = _user.ID,
                                    CreateTime = DateTime.Now,
                                    SysOrgId = sysOrgId,
                                    WareHouseId = warehouseId
                                };
                                await _wMProductInfoServices.Add(model);
                            }
                        }
                        else
                        {
                            //添加商品表
                            var productList = new WMProductList
                            {
                                ClassId = classId,
                                Name = item.ListName.Trim(),
                                Code = item.ListCode,
                                BarCode = "",
                                Unit = unitId,
                                StoreLimit = 100,
                                StoreBaseline = 10,
                                Price = 0,
                                PurchasePrice = 0,
                                IsUse = true,
                                AdminId = _user.ID,
                                CreateTime = DateTime.Now,
                                SpecIds = specId,
                                Sort = 0,
                                OddDays = 15,
                                OddFloatNum = 2,
                                IsChecked = true,
                                SimpleName = null,
                                SysOrgId = sysOrgId
                            };
                            var result = await _wMProductListServices.Add(productList);
                            if (result > 0)
                            {
                                //添加商品关联表
                                WMProductInfo model = new WMProductInfo()
                                {
                                    ProductListId = result,
                                    ProductSpecId = specId,
                                    PurchasePrice = 0,
                                    Quantity = item.Quantity.Trim().ObjToDecimal(),
                                    AdminId = _user.ID,
                                    CreateTime = DateTime.Now,
                                    SysOrgId = sysOrgId,
                                    WareHouseId = warehouseId
                                };
                                await _wMProductInfoServices.Add(model);
                            }
                        }
                    }
                }
 
                _unitOfWork.CommitTran();
                data.response = 1.ObjToString();
                data.msg = "导入商品成功";
                data.success = true;
                return data;
            }
            catch (Exception ex)
            {
                _unitOfWork.RollbackTran();
                data.msg = ex.Message;
                data.response = 0.ObjToString();
                data.status = 500;
                return data;
            }
        }
    }
}