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.DbModels;
using WCS.BLL.Services.IService;
using WCS.DAL.Db;
using WCS.Model;
using WCS.BLL.DbModels;
using WCS.Model.ApiModel;
using WCS.Model.ApiModel.InOutRecord;
using WCS.Model.ApiModel.MatInventoryDetail;
using WCS.Model.ApiModel.User;
using WCS.DAL.DbModels;

namespace WCS.BLL.Services.Service
{
    public class InOutRecordService : IInOutRecordService
    {
        public async Task<PageQueryResponse<InOutRecord>> getInOutRecord(GetInOutRecordRequest request)
        {
            try
            {
                var recordsQueryable = DbHelp.db.Queryable<InOutRecord>()
                                                   .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.Direction != null, (id, si) => id.Direction == request.Direction)
                                                   .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))
                                                   ;

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

                return new PageQueryResponse<InOutRecord>()
                {
                    Code = 200,
                    Message = $"success",
                    Data = new PageQueryResponseData<InOutRecord>()
                    {
                        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<InOutRecord>()
                {
                    Code = 300,
                    Message = $"操作失败:{ex.Message}",
                };
            }
        }

        public async Task<PageQueryResponse<InOutRecord>> exportInOutRecord(GetInOutRecordRequest request)
        {
            try
            {
                var recordsQueryable = DbHelp.db.Queryable<InOutRecord>()
                                         .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.StoreId != 0, (id, si) => id.StoreId == request.StoreId)
                                         .WhereIF(!string.IsNullOrEmpty(request.StoreCode), (id, si) => id.StoreCode.Contains(request.StoreCode))
                                         ;
                var records = await recordsQueryable
                                    .OrderByDescending((id, si) => id.Id)
                                    .Select<InOutRecord>()
                                    .ToListAsync();
                //生成序号
                var index = 1;
                records.ForEach(r =>
                {
                    r.RowNumber = index++;
                });
                return new PageQueryResponse<InOutRecord>()
                {
                    Code = 200,
                    Message = $"success",
                    Data = new PageQueryResponseData<InOutRecord>()
                    {
                        Lists = records
                    }
                };
            }
            catch (Exception ex)
            {
                return new PageQueryResponse<InOutRecord>()
                {
                    Code = 300,
                    Message = $"操作失败:{ex.Message}",
                };
            }
        }
    }
}
