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
 
using JiepeiWMS.IServices;
using JiepeiWMS.Model.Models;
using JiepeiWMS.Services.BASE;
using JiepeiWMS.IRepository.Base;
using System.Threading.Tasks;
using JiepeiWMS.Common.HttpContextUser;
using JiepeiWMS.Common.Enums;
using JiepeiWMS.Common.Helper;
using System;
using JiepeiWMS.Model.ViewModels;
using System.Linq;
using System.Collections.Generic;
using JiepeiWMS.Model;
using System.Linq.Expressions;
using System.Security.Claims;
using JiepeiWMS.Extends;
using SqlSugar;
using JiepeiWMS.Common.Config;
using JiepeiWMS.IRepository;
using JiepeiWMS.Common;
using JiepeiWMS.Common.HttpRestSharp;
using JiepeiWMS.Model.Result;
using System.Threading;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
 
namespace JiepeiWMS.Services
{
    public class WMSaleInfoServices : BaseServices<WMSaleInfo>, IWMSaleInfoServices
    {
        private readonly IBaseRepository<WMSaleInfo> _dal;
        private readonly IWMSaleDetailRepository _wMSaleDetailRepository;
        private readonly IWMProductListRepository _wMProductListRepository;
        private readonly IWMWareHouseRepository _wMWareHouseRepository;
        private readonly IWMProductSpecRepository _wMProductSpecRepository;
        private readonly IWMProductUnitRepository _wMProductUnitRepository;
        private readonly IWMCheckInfoRepository _wMCheckInfo;
        private readonly IDepartmentRepository _departmentRepository;
        private readonly IBaseRepository<sysUserInfo> _sysUserInfoRepository;
        private readonly IUser _user;
        private readonly IBaseRepository<UserRole> _userRoleRepository;
        private readonly IBaseRepository<Role> _roleRepository;
        private readonly ILogger<WMSaleInfoServices> _logger;
        public WMSaleInfoServices(IBaseRepository<WMSaleInfo> dal,
            IWMSaleDetailRepository WMSaleDetailRepository,
            IWMProductListRepository WMProductListRepository,
            IWMWareHouseRepository WMWareHouseRepository,
            IWMProductSpecRepository WMProductSpecRepository,
            IWMProductUnitRepository WMProductUnitRepository,
            IWMCheckInfoRepository WMCheckInfo,
            IDepartmentRepository DepartmentRepository,
            IBaseRepository<sysUserInfo> SysUserInfoRepository,
            IUser user,
            IBaseRepository<Role> RoleRepository,
            IBaseRepository<UserRole> UserRoleRepository,
            ILogger<WMSaleInfoServices> logger)
        {
            this._dal = dal;
            base.BaseDal = dal;
            _wMSaleDetailRepository = WMSaleDetailRepository;
            _wMProductListRepository = WMProductListRepository;
            _wMWareHouseRepository = WMWareHouseRepository;
            _wMProductSpecRepository = WMProductSpecRepository;
            _wMProductUnitRepository = WMProductUnitRepository;
            _wMCheckInfo = WMCheckInfo;
            _departmentRepository = DepartmentRepository;
            _sysUserInfoRepository = SysUserInfoRepository;
            _user = user;
            _roleRepository = RoleRepository;
            _userRoleRepository = UserRoleRepository;
            _logger = logger;
        }
 
        private string materialoutUrl = "/uapws/rest/ic/manage/materialout/add";
 
        /// <summary>
        /// 获取领料分页
        /// </summary>
        /// <returns></returns>
        public async Task<PageModel<WMSaleInfo>> GetWMSaleInfoPage(int page, int intPageSize, string key = "", string options = "", string typeoptions = "", DateTime? startdate = null, DateTime? enddate = null, string statusoptions = "")
        {
            Expression<Func<WMSaleInfo, bool>> whereExpression = SaleInfo => SaleInfo.Status > 0;
 
            //获取角色信息,判断是否数据展示
            var role = string.Join(",", _user.GetClaimValueByType(ClaimTypes.Role));
            if (role.Contains("SuperAdmin") || role.Contains("超级管理员") || role.Contains("仓库管理员"))
            {
 
            }
            else
            {
                whereExpression = whereExpression.And(SaleInfo => SaleInfo.CheckAdminId == _user.ID || SaleInfo.EmployeeId == _user.ID);
            }
            //筛选主体Id
            var userInfoModel = await _sysUserInfoRepository.QueryById(_user.ID);
            if (userInfoModel != null)
            {
                whereExpression = whereExpression.And(SaleInfo => SaleInfo.SysOrgId == userInfoModel.SysOrgId);
            }
            if (!string.IsNullOrWhiteSpace(key))
            {
                //筛选商品名称、商品编号
                var ProductListList = (await _wMProductListRepository.Query(s => s.Name.Contains(key.Trim()) || s.Code.Contains(key.Trim()))).Select(c => c.Id);
                var SaleDetailList = (await _wMSaleDetailRepository.Query(x => ProductListList.Contains(x.ProductId))).Select(c => c.SaleId).Distinct();
 
                whereExpression = whereExpression.And(SaleInfo => SaleInfo.SaleCode.Contains(key.Trim()) || SaleDetailList.Contains(SaleInfo.Id));
            }
            if (!string.IsNullOrWhiteSpace(options))
            {
                whereExpression = whereExpression.And(SaleInfo => SaleInfo.WareHouseId == options.ObjToInt());
            }
            if (!string.IsNullOrWhiteSpace(typeoptions))
            {
                whereExpression = whereExpression.And(SaleInfo => SaleInfo.PayType == typeoptions.ObjToInt());
            }
            if (startdate != null)
            {
                whereExpression = whereExpression.And(SaleInfo => SaleInfo.CreateTime >= startdate);
            }
            if (enddate != null)
            {
                whereExpression = whereExpression.And(SaleInfo => SaleInfo.CreateTime <= enddate);
            }
            if (!string.IsNullOrWhiteSpace(statusoptions))
            {
                whereExpression = whereExpression.And(SaleInfo => SaleInfo.Status == statusoptions.ObjToInt());
            }
 
            var PageList = await base.QueryTabsPage<WMSaleInfo, WMWareHouse, sysUserInfo, sysUserInfo, WMSaleInfo>(
                 (SaleInfo, WareHouse, UserInfo, UserInfo2) => new object[]
                 {
                    JoinType.Left,SaleInfo.WareHouseId==WareHouse.Id,
                    JoinType.Left,SaleInfo.EmployeeId==UserInfo.uID,
                    JoinType.Left,SaleInfo.CheckAdminId==UserInfo2.uID
                 },
                 (SaleInfo, WareHouse, UserInfo, UserInfo2) => new WMSaleInfo()
                 {
                     Id = SaleInfo.Id,
                     Status = SaleInfo.Status,
                     CheckKey = SaleInfo.CheckKey,
                     EmployeeId = SaleInfo.EmployeeId,
                     PayType = SaleInfo.PayType,
                     Remark = SaleInfo.Remark,
                     PayAmount = SaleInfo.PayAmount,
                     CheckRank = SaleInfo.CheckRank,
                     CreateTime = SaleInfo.CreateTime,
                     SaleCode = SaleInfo.SaleCode,
                     UpdateTime = SaleInfo.UpdateTime,
                     WareHouseName = WareHouse.Name,
                     EmployeeName = UserInfo.uRealName,
                     CheckAdminName = UserInfo2.uRealName,
                     DepartmentId = UserInfo.DepartmentId.Value
                 },
                 whereExpression,
                 page,
                 intPageSize,
                 " SaleInfo.Id desc"
                 );
 
            foreach (var item in PageList.data)
            {
                //领料状态
                var dicStatus = typeof(EnumWMSaleStatus)._GetValueDescriptionDicFromEnumType<EnumWMSaleStatus>();
                string nameStatus;
                item.SaleStatus = dicStatus.TryGetValue((EnumWMSaleStatus)item.Status, out nameStatus) ? nameStatus : string.Empty;
 
                //领料类型
                var dicType = typeof(EnumWMSaleType)._GetValueDescriptionDicFromEnumType<EnumWMSaleType>();
                string nameType;
                item.PayTypeName = dicType.TryGetValue((EnumWMSaleType)item.PayType, out nameType) ? nameType : string.Empty;
 
                //部门信息
                if (item.DepartmentId > 0)
                {
                    var DepartmentModel = await _departmentRepository.QueryById(item.DepartmentId.Value);
                    if (DepartmentModel != null)
                        item.DepartmentName = DepartmentModel.Name;
                }
 
                //商品信息
                var SaleDetailModel = await _wMSaleDetailRepository.GetModel(x => x.SaleId == item.Id);
                if (SaleDetailModel != null)
                {
                    var ProductListModel = await _wMProductListRepository.QueryById(SaleDetailModel.ProductId);
                    if (ProductListModel != null)
                    {
                        item.ProductName = ProductListModel.Name;
                        item.ProductCode = ProductListModel.Code;
                    }
                }
 
                var userrolemodel = await _userRoleRepository.GetModel(r => r.UserId == _user.ID);
                if (userrolemodel != null)
                {
                    var rolemodel = await _roleRepository.QueryById(userrolemodel.RoleId);
                    if (rolemodel != null && rolemodel.Name == RoleConfig.SaleCangKuStaff)
                    {
                        item.MayOut = true;//显示出库按钮
                    }
                }
            }
 
            return PageList;
        }
 
        /// <summary>
        /// 获取领料或出库详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<WMSaleInfo> GetWMSaleInfo(int Id)
        {
            var model = await base.GetModel(w => w.Id == Id);
            model.SaleDetailList = await _wMSaleDetailRepository.Query(w => w.SaleId == Id);
 
            //仓库名称
            var WMWareHouseModel = await _wMWareHouseRepository.GetModel(w => w.Id == model.WareHouseId);
            if (WMWareHouseModel != null)
                model.WareHouseName = WMWareHouseModel.Name;
 
 
            //用户模型
            var UserModel = await _sysUserInfoRepository.GetModel(u => u.uID == model.EmployeeId);
            if (UserModel != null)
            {
                model.DepartmentId = UserModel.DepartmentId.Value;
                model.EmployeeName = UserModel.uRealName;
                //部门名称
                var DepartmentModel = await _departmentRepository.GetModel(d => d.Id == UserModel.DepartmentId);
                if (DepartmentModel != null)
                    model.DepartmentName = DepartmentModel.Name;
            }
 
            //var UserModel1 = await _sysUserInfoRepository.GetModel(u => u.uID == model.CheckAdminId);
            //if (UserModel1 != null)
            //    model.CheckAdminName = UserModel1.uRealName;
 
            var roleMdoel = await _roleRepository.GetModel(x => x.Name == RoleConfig.SaleCangKuStaff);
            if (roleMdoel != null && model.Status >= EnumWMSaleStatus.OutWareHouse.GetHashCode())
            {
                var userRoleMdoel = await _userRoleRepository.GetModel(x => x.RoleId == roleMdoel.Id);
                if (userRoleMdoel != null)
                {
                    var UserModel2 = await _sysUserInfoRepository.GetModel(u => u.uID == userRoleMdoel.UserId);
                    if (UserModel2 != null)
                        model.IssueName = UserModel2.uRealName;
                }
            }
 
            //领料状态
            var dicSaleStatus = typeof(EnumWMSaleStatus)._GetValueDescriptionDicFromEnumType<EnumWMSaleStatus>();
            string nameSaleStatus;
            model.SaleStatus = dicSaleStatus.TryGetValue((EnumWMSaleStatus)model.Status, out nameSaleStatus) ? nameSaleStatus : string.Empty;
 
            //领料类型
            var dicType = typeof(EnumWMSaleType)._GetValueDescriptionDicFromEnumType<EnumWMSaleType>();
            string nameType;
            model.PayTypeName = dicType.TryGetValue((EnumWMSaleType)model.PayType, out nameType) ? nameType : string.Empty;
 
            //获取审批流
            var checkModel = await _wMCheckInfo.GetModel(w => w.SysOrgId == _user.SysOrgId && w.Key == model.CheckKey);
            if (checkModel != null)
            {
                var rankList = JsonHelper.DeserializeObject<CheckRankList>(checkModel.Json);
                var checkarr = rankList.checkRankList.Select(w => w.AdminName).ToArray();
 
                for (int i = 0; i < checkarr.Length; i++)
                {
                    model.CheckAdminName = checkarr[i] + "  ";
                }
                model.CheckStrStep = checkarr.ToList();
                //model.CheckRankStep = model.CheckRank == 0 ? (checkarr.Count() - 1) : (model.CheckRank - 2);
                model.CheckRankStep = model.CheckRank == 0 ? checkarr.Count() : (model.CheckRank - 1);
            }
            else
            {
                model.CheckStrStep = new List<string>();
                model.CheckRankStep = 0;
            }
 
            model.AdminId = _user.ID;
 
            foreach (var item in model.SaleDetailList)
            {
                //商品信息
                var ProductListModel = await _wMProductListRepository.GetModel(w => w.Id == item.ProductId);
                if (ProductListModel != null)
                {
                    item.ProductCode = ProductListModel.Code;
                    item.ProductName = ProductListModel.Name;
                    if (ProductListModel.SpecIds > 0)
                    {
                        //商品规格
                        var ProductSpecModel = await _wMProductSpecRepository.QueryById(ProductListModel.SpecIds);
                        if (ProductSpecModel != null)
                        {
                            item.ProductSpec = ProductSpecModel.Name;
                        }
                    }
                    if (ProductListModel.Unit > 0)
                    {
                        //商品单位
                        var ProductUnitModel = await _wMProductUnitRepository.QueryById(ProductListModel.Unit);
                        if (ProductUnitModel != null)
                        {
                            item.ProductUnit = ProductUnitModel.Unit;
                        }
                    }
                }
 
                item.CheckNum = item.Quantity;
                item.GiveNum = item.Quantity;
                if (item.Checked <= 0)
                {
                    item.Checked = 1;
                }
 
                //商品状态
                var dicProductStatus = typeof(EnumWMSaleStatus)._GetValueDescriptionDicFromEnumType<EnumWMSaleStatus>();
                string nameProductStatus;
                item.ProductStatus = dicProductStatus.TryGetValue((EnumWMSaleStatus)item.Status, out nameProductStatus) ? nameProductStatus : string.Empty;
            }
 
            return model;
        }
 
        /// <summary>
        /// 获取领料最大的Id
        /// </summary>
        /// <returns></returns>
        public async Task<int> GetSaleInfoMaxId()
        {
            var db = await base.QueryTable("SELECT ISNULL(MAX(Id),0) FROM dbo.WMSaleInfo");
            return db.Rows[0][0].ObjToInt();
        }
 
        /// <summary>
        /// 调用友物料出库接口
        /// </summary>
        /// <returns></returns>
        public async Task<Tuple<string, bool>> BeginMaterialOutInNC(WMSaleInfo infoModel, List<WMSaleDetail> detailList)
        {
            try
            {
                #region
                NCMaterialOutViewModels pos = new NCMaterialOutViewModels();
                NCMaterialOutViewModels.Ufinterface uface = new NCMaterialOutViewModels.Ufinterface();
                NCMaterialOutViewModels.Bill bill = new NCMaterialOutViewModels.Bill();
                NCMaterialOutViewModels.Billhead head = new NCMaterialOutViewModels.Billhead();
                NCMaterialOutViewModels.Cgeneralbid cgeneralbid = new NCMaterialOutViewModels.Cgeneralbid();
                List<NCMaterialOutViewModels.Item> item = new List<NCMaterialOutViewModels.Item>();
 
                var UnitCode = "";
                var SupplierModel = new WMSupplier();
                var ProductListModel = new WMProductList();
 
                //赋uface
                uface.account = "01";
                uface.billtype = "4D";//物料出库
                uface.isexchange = "Y";
                uface.replace = "Y";
                uface.sender = "JPKJ001";
                uface.groupcode = "JPKJ";
 
                //赋head
                head.pk_group = "JPKJ";
                head.pk_org = "1001";
                head.corpoid = "1001";
                head.corpvid = "1001";
                head.pk_org = "1001";
                head.pk_org_v = "1001";
                head.cwarehouseid = "01";
                head.vtrantypecode = "4D-01";
                head.cdrawcalbodyoid = "1001";
                head.cdrawcalbodyvid = "1001";
                head.fbillflag = "2";
 
                //赋item
                foreach (var detailItem in detailList)
                {
                    if (detailItem.ProductId > 0)
                    {
                        ProductListModel = await _wMProductListRepository.QueryById(detailItem.ProductId);
                        if (ProductListModel != null)
                        {
                            var qt = await QueryTable("select UnitCode from WMProductUnit where id=" + ProductListModel.Unit);
                            UnitCode = qt.Rows._Select(t => t["UnitCode"]._ToDbStr()).FirstOrDefault();
                        }
                    }
 
                    var ItemModel = new NCMaterialOutViewModels.Item();
                    ItemModel.cmaterialoid = ProductListModel.Code;
                    ItemModel.cmaterialvid = ProductListModel.Code;
                    ItemModel.cunitid = UnitCode;
                    ItemModel.castunitid = UnitCode;
                    ItemModel.vchangerate = "1/1";//换算率
                    ItemModel.nshouldnum = detailItem.GiveNum.ToString();
                    ItemModel.nshouldassistnum = detailItem.GiveNum.ToString();
                    ItemModel.nnum = detailItem.GiveNum.ToString();
                    ItemModel.nassistnum = detailItem.GiveNum.ToString();
                    item.Add(ItemModel);
                }
 
                //所有集合赋值
                cgeneralbid.item = item;
                head.cgeneralbid = cgeneralbid;
                bill.billhead = head;
                uface.bill = bill;
                pos.ufinterface = uface;
                var json = JsonConvert.SerializeObject(pos);
 
                //物料出库接口
                var apinchost = Appsettings.app("AppSettings", "ApiNCHost");
                NCJsonResult jsonResult = HttpHelper.PostApi<NCJsonResult>(apinchost + materialoutUrl, pos);
                Thread.Sleep(300);
                #endregion
 
                var resultdescription = jsonResult.data.ufinterface.sendresult[0].resultdescription;
                if (jsonResult.data.ufinterface.successful == "Y")
                {
                    return new Tuple<string, bool>("用友物料出库接口成功", true);
                }
                else
                {
                    string[] strArray = resultdescription.Split("异常信息:");
                    return new Tuple<string, bool>("用友物料出库接口失败:"+strArray[1], false);
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, ex.ToString());
                return new Tuple<string, bool>(ex.ToString(), false);
            }
        }
    }
}