using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using TouchSocket.Core;
using WCS.BLL.Config;
using WCS.BLL.DbModels;
using WCS.BLL.Services.IService;
using WCS.DAL.Db;
using WCS.DAL.DbModels;
using WCS.Model;
using WCS.Model.ApiModel;
using WCS.Model.ApiModel.MatInventoryDetail;
using WCS.Model.ApiModel.User;

namespace WCS.BLL.Services.Service
{
    public class MatInventoryDetailService : IMatInventoryDetailService
    {
        public async Task<PageQueryResponse<InventoryDetail>> getMatInventoryDetail(GetMatInventoryDetailRequest request)
        {
            try
            {
                var recordsQueryable = DbHelp.db.Queryable<InventoryDetail>()
                                                   .LeftJoin<StoreInfo>((id, si) => id.StoreId == si.Id)
                                                   .WhereIF(!string.IsNullOrEmpty(request.MatSN), (id, si) => id.MatSN.Contains(request.MatSN))
                                                   .WhereIF(!string.IsNullOrEmpty(request.MatCode), (id, si) => id.MatCode.Contains(request.MatCode))
                                                   .WhereIF(!string.IsNullOrEmpty(request.MatName), (id, si) => id.MatName.Contains(request.MatName))
                                                   .WhereIF(!string.IsNullOrEmpty(request.MatBatch), (id, si) => id.MatBatch.Contains(request.MatBatch))
                                                   .WhereIF(!string.IsNullOrEmpty(request.MatSpec), (id, si) => id.MatSpec.Contains(request.MatSpec))
                                                   .WhereIF(!string.IsNullOrEmpty(request.MatSupplier), (id, si) => id.MatSpec.Contains(request.MatSupplier))
                                                   .WhereIF(!string.IsNullOrEmpty(request.MatCustomer), (id, si) => id.MatSpec.Contains(request.MatCustomer))
                                                   .WhereIF(request.ShelfTypeId != 0, (id, si) => si.ShelfTypeId == request.ShelfTypeId)

                                                   .WhereIF(request.StoreId != 0, (id, si) => id.StoreId == request.StoreId)
                                                   .WhereIF(!string.IsNullOrEmpty(request.StoreCode), (id, si) => id.StoreCode.Contains(request.StoreCode))
                                                   .WhereIF(!string.IsNullOrEmpty(request.ShelfCode), (id, si) => si.ShelfCode.Contains(request.ShelfCode))
                                                   ;

                var totalCount = await recordsQueryable.CountAsync();
                var totalQty = await recordsQueryable.SumAsync((id, si) => id.MatQty);
                var records = await recordsQueryable
                                    .OrderByDescending((id, si) => id.Id)
                                    .Skip((request.PageNumber - 1) * request.PageSize).Take(request.PageSize)
                                    .Select<InventoryDetail>()
                                    .ToListAsync();
                //生成序号
                for (int i = 0; i < records.Count; i++)
                {
                    records[i].RowNumber = (request.PageNumber - 1) * request.PageSize + i + 1;
                }

                return new PageQueryResponse<InventoryDetail>()
                {
                    Code = 200,
                    Message = $"{totalQty}",
                    Data = new PageQueryResponseData<InventoryDetail>()
                    {
                        TotalCount = totalCount,
                        MaxPage = request.PageSize == 0 ? 0 : (int)Math.Ceiling((decimal)totalCount / request.PageSize),
                        Count = records.Count,
                        Lists = records.ToList()
                    }
                };

            }
            catch (Exception ex)
            {
                return new PageQueryResponse<InventoryDetail>()
                {
                    Code = 300,
                    Message = $"操作失败:{ex.Message}",
                };
            }
        }

        public async Task<PageQueryResponse<InventoryDetail>> exportMatInventoryDetail(GetMatInventoryDetailRequest request)
        {
            try
            {
                var recordsQueryable = DbHelp.db.Queryable<InventoryDetail>()
                                         .LeftJoin<StoreInfo>((id, si) => id.StoreId == si.Id)
                                         .WhereIF(!string.IsNullOrEmpty(request.MatSN), (id, si) => id.MatSN.Contains(request.MatSN))
                                         .WhereIF(!string.IsNullOrEmpty(request.MatCode), (id, si) => id.MatCode.Contains(request.MatCode))
                                         .WhereIF(!string.IsNullOrEmpty(request.MatName), (id, si) => id.MatName.Contains(request.MatName))
                                         .WhereIF(!string.IsNullOrEmpty(request.MatBatch), (id, si) => id.MatBatch.Contains(request.MatBatch))
                                         .WhereIF(!string.IsNullOrEmpty(request.MatSpec), (id, si) => id.MatSpec.Contains(request.MatSpec))
                                         .WhereIF(!string.IsNullOrEmpty(request.MatSupplier), (id, si) => id.MatSpec.Contains(request.MatSupplier))
                                         .WhereIF(!string.IsNullOrEmpty(request.MatCustomer), (id, si) => id.MatSpec.Contains(request.MatCustomer))
                                         .WhereIF(request.ShelfTypeId != 0, (id, si) => si.ShelfTypeId == request.ShelfTypeId)

                                         .WhereIF(request.StoreId != 0, (id, si) => id.StoreId == request.StoreId)
                                         .WhereIF(!string.IsNullOrEmpty(request.StoreCode), (id, si) => id.StoreCode.Contains(request.StoreCode))
                                         .WhereIF(!string.IsNullOrEmpty(request.ShelfCode), (id, si) => si.ShelfCode.Contains(request.ShelfCode))
                                         ;
                var records = await recordsQueryable
                                    .OrderByDescending((id, si) => id.Id)
                                    .Skip((request.PageNumber - 1) * request.PageSize).Take(request.PageSize)
                                    .Select<InventoryDetail>()
                                    .ToListAsync();
                //生成序号
                var index = 1;
                records.ForEach(r =>
                {
                    r.RowNumber = index++;
                });
                return new PageQueryResponse<InventoryDetail>()
                {
                    Code = 200,
                    Message = $"success",
                    Data = new PageQueryResponseData<InventoryDetail>()
                    {
                        Lists = records
                    }
                };
            }
            catch (Exception ex)
            {
                return new PageQueryResponse<InventoryDetail>()
                {
                    Code = 300,
                    Message = $"操作失败:{ex.Message}",
                };
            }
        }

        public async Task<ResponseCommon<List<MatInventorySummaryModel>>> getMatInventorySummary(GetMatInventorySummaryRequest request)
        {
            var inventortyDetails = await DbHelp.db.Queryable<InventoryDetail>()
                                             .WhereIF(!string.IsNullOrEmpty(request.MatName), t => t.MatName.Contains(request.MatName))
                                             .WhereIF(!string.IsNullOrEmpty(request.MatCode), t => t.MatCode.Contains(request.MatCode))
                                             .WhereIF(!string.IsNullOrEmpty(request.MatBatch), t => t.MatBatch.Contains(request.MatBatch))
                                             .Where(t => t.StoreInfo.ShelfTypeId == request.ShelfTypeId)
                                             .GroupBy(t => t.MatCode)
                                             .GroupBy(t => t.MatBatch)
                                             .Select(t => new MatInventorySummaryModel
                                             {
                                                 MatCode = t.MatCode,
                                                 MatName = t.MatName,
                                                 MatSpec = t.MatSpec,
                                                 MatBatch = t.MatBatch,
                                                 TotalQty = SqlFunc.AggregateSum(t.MatQty)
                                             })
                                             .ToListAsync();

            return new ResponseCommon<List<MatInventorySummaryModel>>()
            {
                Code = 200,
                Message = inventortyDetails.Count().ToString(),
                Data = inventortyDetails
            };
        }

        public async Task<PageQueryResponse<InventoryDetail>> compareMatInventoryDetail(CompareMatInventoryDetailRequest request)
        {
            try
            {
                //查询 本分组中 是否含有所传SN的物料
                var recordsQueryable = DbHelp.db.Queryable<InventoryDetail>()
                                                .Where(t => request.MatSns.Contains(t.MatSN))
                                                .OrderBy(t => t.MatCode)
                                                .WhereIF(!string.IsNullOrEmpty(LocalFile.Config.GroupName), t => t.StoreInfo.GroupName == LocalFile.Config.GroupName);
                var totalCount = await recordsQueryable.CountAsync();
                var records = await recordsQueryable.ToListAsync();
                //生成序号 选中
                for (int i = 0; i < records.Count; i++)
                {
                    records[i].RowNumber = i + 1;
                    records[i].IsSelected = true;
                }
                return new PageQueryResponse<InventoryDetail>()
                {
                    Code = 200,
                    Message = $"success",
                    Data = new PageQueryResponseData<InventoryDetail>()
                    {
                        TotalCount = totalCount,
                        Count = records.Count,
                        Lists = records.ToList()
                    }
                };
            }
            catch
            {
                return new PageQueryResponse<InventoryDetail>()
                {
                    Code = 200,
                    Message = $"success",
                    Data = new PageQueryResponseData<InventoryDetail>()
                    {
                        Lists = new List<InventoryDetail>()
                    }
                };
            }
        }
    }
}
