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
 
using JiepeiWMS.IServices;
using JiepeiWMS.Model.Models;
using JiepeiWMS.Services.BASE;
using JiepeiWMS.IRepository.Base;
using System.Threading.Tasks;
using JiepeiWMS.Model;
using JiepeiWMS.Common.Enums;
using JiepeiWMS.Common.Helper;
using SqlSugar;
using System.Linq.Expressions;
using System;
using System.Linq;
using JiepeiWMS.Extends;
using JiepeiWMS.IRepository;
using JiepeiWMS.Common.HttpContextUser;
 
namespace JiepeiWMS.Services
{
    public class WMStockLogServices : BaseServices<WMStockLog>, IWMStockLogServices
    {
        private readonly IBaseRepository<WMStockLog> _dal;
        private readonly IWMProductListRepository _wMProductListRepository;
        private readonly IWMProductSpecRepository _wMProductSpecRepository;
        private readonly IWMPurchaseInfoRepository _wMPurchaseInfoRepository;
        private readonly IWMQuoteInfoRepository _wMQuoteInfoRepository;
        private readonly IWMSaleInfoRepository _wMSaleInfoRepository;
        private readonly IWMWareHouseRepository _wMWareHouseRepository;
        private readonly IUser _user;
        public WMStockLogServices(IBaseRepository<WMStockLog> dal,
            IWMProductListRepository WMProductListRepository,
            IWMProductSpecRepository WMProductSpecRepository,
            IWMPurchaseInfoRepository WMPurchaseInfoRepository,
            IWMQuoteInfoRepository WMQuoteInfoRepository,
            IWMSaleInfoRepository WMSaleInfoRepository,
            IWMWareHouseRepository WMWareHouseRepository,
            IUser user)
        {
            this._dal = dal;
            base.BaseDal = dal;
            _wMProductListRepository = WMProductListRepository;
            _wMProductSpecRepository = WMProductSpecRepository;
            _wMPurchaseInfoRepository = WMPurchaseInfoRepository;
            _wMQuoteInfoRepository = WMQuoteInfoRepository;
            _wMSaleInfoRepository = WMSaleInfoRepository;
            _wMWareHouseRepository = WMWareHouseRepository;
            _user = user;
        }
 
        /// <summary>
        /// 获取库存流水日志分页
        /// </summary>
        /// <returns></returns>
        public async Task<PageModel<WMStockLog>> GetWMStockLogPage(int page, int intPageSize, string key = "", string typeoptions = "", string warehouseoptions = "", DateTime? startdate = null, DateTime? enddate = null)
        {
            Expression<Func<WMStockLog, bool>> whereExpression = StockLog => StockLog.Id > 0;
 
            if (!string.IsNullOrWhiteSpace(key))
            {
                //筛选采购订单编号
                var PurchaseCode = (await _wMPurchaseInfoRepository.Query(s => s.PurchaseCode.Contains(key))).Select(c => c.Id);
                //筛选报价订单编号
                var QuoteCode = (await _wMQuoteInfoRepository.Query(s => s.QuoteCode.Contains(key))).Select(c => c.Id);
                //筛选领料订单编号
                var SaleCode = (await _wMSaleInfoRepository.Query(s => s.SaleCode.Contains(key))).Select(c => c.Id);
                //筛选商品编号
                var ProductCode = (await _wMProductListRepository.Query(s => s.Code.Contains(key))).Select(c => c.Id);
                //筛选商品名称
                var ProductName = (await _wMProductListRepository.Query(s => s.Name.Contains(key))).Select(c => c.Id);
                //筛选商品规格
                var ProductSpec = (await _wMProductSpecRepository.Query(s => s.Name.Contains(key))).Select(c => c.Id);
 
                whereExpression = whereExpression.And(StockLog => PurchaseCode.Contains(StockLog.OrderId.Value) || QuoteCode.Contains(StockLog.OrderId.Value) || SaleCode.Contains(StockLog.OrderId.Value) || ProductCode.Contains(StockLog.ProductListId) || ProductName.Contains(StockLog.ProductListId) || ProductSpec.Contains(StockLog.ProductSpecId));
            }
            if (!string.IsNullOrWhiteSpace(typeoptions))
            {
                whereExpression = whereExpression.And(StockLog => StockLog.Type == typeoptions.ObjToInt());
            }
            if (!string.IsNullOrWhiteSpace(warehouseoptions))
            {
                whereExpression = whereExpression.And(StockLog => StockLog.WareHouseId == warehouseoptions.ObjToInt());
            }
            else
            {
                var WareHouseIdList = (await _wMWareHouseRepository.Query(x => x.SysOrgId == _user.SysOrgId)).Select(x => x.Id).ToList();
                whereExpression = whereExpression.And(StockLog => WareHouseIdList.Contains(StockLog.WareHouseId));
            }
            if (startdate != null)
            {
                whereExpression = whereExpression.And(StockLog => StockLog.CreateTime >= startdate);
            }
            if (enddate != null)
            {
                whereExpression = whereExpression.And(StockLog => StockLog.CreateTime <= enddate);
            }
 
            var PageList = await base.QueryTabsPage<WMStockLog, WMProductList, WMProductSpec, WMWareHouse, sysUserInfo, WMStockLog>(
             (StockLog, ProductList, ProductSpec, WareHouse, UserInfo) => new object[]
             {
                    JoinType.Left,StockLog.ProductListId==ProductList.Id,
                    JoinType.Left,StockLog.ProductSpecId==ProductSpec.Id,
                    JoinType.Left,StockLog.WareHouseId==WareHouse.Id,
                    JoinType.Left,StockLog.AdminId==UserInfo.uID,
             },
             (StockLog, ProductList, ProductSpec, WareHouse, UserInfo) => new WMStockLog()
             {
                 Id = StockLog.Id,
                 Code = ProductList.Code,
                 ProductName = ProductList.Name,
                 ProductSpec = ProductSpec.Name,
                 WareHouseName = WareHouse.Name,
                 Price = ProductList.Price,
                 Type = StockLog.Type,
                 TotalPrice = StockLog.TotalPrice,
                 RemainQuantity = StockLog.RemainQuantity,
                 RemainPrice = StockLog.RemainPrice,
                 TotalRemainPrice = StockLog.TotalRemainPrice,
                 QuantitySign = StockLog.Quantity > 0 ? ((StockLog.Operation == 1 ? "+" : "-") + StockLog.Quantity.ToString()) : "",
                 OrderId = StockLog.OrderId,
                 CreateTime = StockLog.CreateTime,
                 AdminName = UserInfo.uRealName,
             },
             whereExpression,
             page,
             intPageSize,
             " StockLog.Id desc"
             );
 
            foreach (var item in PageList.data)
            {
                var dicType = typeof(EnumWMStockType)._GetValueDescriptionDicFromEnumType<EnumWMStockType>();
                string name;
                item.TypeName = dicType.TryGetValue((EnumWMStockType)item.Type, out name) ? name : string.Empty;
 
                if (item.Type == 1)
                {
                    var PurchaseInfoModel = await _wMPurchaseInfoRepository.QueryById(item.OrderId);
                    if (PurchaseInfoModel != null)
                        item.CodeNo = PurchaseInfoModel.PurchaseCode;
                }
                else if (item.Type == 2)
                {
                    var SaleInfoModel = await _wMSaleInfoRepository.QueryById(item.OrderId);
                    if (SaleInfoModel != null)
                        item.CodeNo = SaleInfoModel.SaleCode;
                }
                else if (item.Type == 4)
                {
                    var QuoteInfoModel = await _wMQuoteInfoRepository.QueryById(item.OrderId);
                    if (QuoteInfoModel != null)
                        item.CodeNo = QuoteInfoModel.QuoteCode;
                }
            }
 
            return PageList;
        }
 
 
        /// <summary>
        /// 获取最近几天得库存
        /// </summary>
        /// <param name="ProductListId">商品列表id</param>
        /// <param name="Id">商品id</param>
        /// <param name="Day">天数</param>
        /// <param name="Type">库存流水类型</param>
        /// <returns></returns>
        public async Task<decimal> GetStockNumByDateDay(int ProductListId, int Id, int Day, int Type)
        {
            var sql = string.Format(@"  select isnull((SELECT SUM(Quantity) FROM dbo.WMStockLog WHERE ProductListId = {0} AND ProductId = {1} AND Type = {2} AND CreateTime >= CONVERT(VARCHAR(10),GETDATE() - {3},120)),0) ", ProductListId, Id, Type, Day);
            var dt_count = await BaseDal.QueryTable(sql.ToString());
            return (decimal)dt_count.Rows[0][0];
        }
    }
}