huangxiaoqiang
2025-11-17 b07472f884708a6bfdf63d999004bbf0bb5f00a8
ÏîÄ¿´úÂë/WMS/WIDESEA_WMSServer/WIDESEA_StorageBasicServices/Stock/StockInfoService.cs
@@ -4,12 +4,14 @@
using Masuit.Tools;
using NewLife.Reflection;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Math;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
using SqlSugar;
using System.Collections;
using System.Collections.Generic;
using System.Drawing.Printing;
using System.Linq.Expressions;
using System.Threading.Tasks;
using System.Xml.Linq;
using WIDESEA_Cache;
using WIDESEA_Common;
using WIDESEA_Core;
@@ -21,6 +23,7 @@
using WIDESEA_DTO.Stock;
using WIDESEA_DTO.WMS;
using WIDESEA_IOrderRepository;
using WIDESEA_IOrderServices;
using WIDESEA_Model.Models;
using WIDESEA_Model.Models.Order;
using WIDESEA_OrderRepository;
@@ -33,11 +36,13 @@
{
    private readonly ILocationStatusChangeRecordRepository _locationStatusChangeRecordRepository;
    private readonly IDt_InboundOrderRepository _inboundOrderRepository;
    private readonly IDt_InboundOrderService _inboundOrderService;
    private readonly IUnitOfWorkManage _unitOfWorkManage;
    private readonly IStockInfoDetailRepository _stockInfoDetailRepository;
    private readonly IDt_TaskService _taskService;
    private readonly ILocationInfoRepository _locationRepository;
    private readonly IDt_TaskRepository _taskRepository;
    private readonly IDt_Task_HtyRepository _task_HtyRepository;
    private readonly IDt_OrderOutDetailsRepository _outDetailsRepository;
    public StockInfoService(IStockInfoRepository BaseDal,
@@ -48,7 +53,9 @@
                                IDt_TaskService taskService,
                                ILocationInfoRepository locationRepository,
                                IDt_TaskRepository taskRepository,
                                IDt_OrderOutDetailsRepository outDetailsRepository) : base(BaseDal)
                                IDt_OrderOutDetailsRepository outDetailsRepository,
                                IDt_Task_HtyRepository task_HtyRepository,
                                IDt_InboundOrderService inboundOrderService) : base(BaseDal)
    {
        _locationStatusChangeRecordRepository = locationStatusChangeRecordRepository;
        _inboundOrderRepository = inboundOrderRepository;
@@ -58,6 +65,8 @@
        _locationRepository = locationRepository;
        _taskRepository = taskRepository;
        _outDetailsRepository = outDetailsRepository;
        _task_HtyRepository = task_HtyRepository;
        _inboundOrderService = inboundOrderService;
    }
    #region æ–¹æ³•重写
    /// <summary>
@@ -433,4 +442,170 @@
    }
    #endregion
    #region åº“存数据展示
    public async Task<WebResponseContent> GetStockData()
    {
        WebResponseContent content = new WebResponseContent();
        try
        {
            var stocks = await _stockInfoDetailRepository.QueryDataAsync(x => true);
            var orderdetail = SqlSugarHelper.DbWMS.Queryable<Dt_OrderOutDetails_Hty>().Where(x => true).ToList();
            var task = _taskRepository.QueryData(x => true).ToList();
            // èŽ·å–UTC当前时间
            DateTime utcNow = DateTime.UtcNow;
            // è®¡ç®—当月时间范围
            DateTime firstDayOfMonth = new DateTime(utcNow.Year, utcNow.Month, 1);
            DateTime lastDayOfMonth = firstDayOfMonth.AddMonths(1).AddSeconds(-1);
            // è®¡ç®—上月时间范围
            DateTime firstDayOfLastMonth = new DateTime(utcNow.Year, utcNow.Month, 1).AddMonths(-1);
            DateTime lastDayOfLastMonth = firstDayOfLastMonth.AddMonths(1).AddSeconds(-1);
            // è®¡ç®—当天时间范围
            DateTime todayStart = utcNow.Date;
            DateTime todayEnd = todayStart.AddDays(1).AddTicks(-1);
            // è®¡ç®—昨日时间范围
            DateTime startOfPreviousDay = utcNow.AddDays(-1).Date;  // å‰ä¸€å¤©çš„ 00:00:00
            DateTime endOfPreviousDay = startOfPreviousDay.AddDays(1).AddSeconds(-1);
            // æŸ¥è¯¢å½“月数据
            var currentMonthInStocks = stocks.Where(x => x.CreateDate >= firstDayOfMonth && x.CreateDate <= lastDayOfMonth).Sum(x => x.Quantity);
            var currentMonthOutStocks = orderdetail.Where(x => x.CreateDate >= firstDayOfMonth && x.CreateDate <= lastDayOfMonth).Sum(x => x.OutboundQuantity);
            //查询上月数据
            var currentMonthInStocksLastMonth = stocks.Where(x => x.CreateDate >= firstDayOfLastMonth && x.CreateDate <= lastDayOfLastMonth).Sum(x => x.Quantity);
            var currentMonthOutStocksLastMonth = orderdetail.Where(x => x.CreateDate >= firstDayOfLastMonth && x.CreateDate <= lastDayOfLastMonth).Sum(x => x.OutboundQuantity);
            // æŸ¥è¯¢ä»Šæ—¥æ•°æ®
            var currentDayInStocks = stocks.Where(x => x.CreateDate >= todayStart && x.CreateDate <= todayEnd).Sum(x => x.Quantity);
            var currentDayOutStocks = orderdetail.Where(x => x.CreateDate >= todayStart && x.CreateDate <= todayEnd).Sum(x => x.OutboundQuantity);
            // æŸ¥è¯¢æ˜¨å¤©æ—¥æ•°æ®
            var currentDayInStocksLast = stocks.Where(x => x.CreateDate >= startOfPreviousDay && x.CreateDate <= endOfPreviousDay).Sum(x => x.Quantity);
            var currentDayOutStocksLast = orderdetail.Where(x => x.CreateDate >= startOfPreviousDay && x.CreateDate <= endOfPreviousDay).Sum(x => x.OutboundQuantity);
            // ç”Ÿæˆæœ€è¿‘7天的出入库数据
            var last7Days = Enumerable.Range(0, 7)
                            .Select(i => utcNow.Date.AddDays(-i))
                            .OrderBy(x => x)
                            .ToList();
            var monthDays = Enumerable.Range(1, DateTime.DaysInMonth(utcNow.Year, utcNow.Month))
                            .Select(day => new DateTime(utcNow.Year, utcNow.Month, day))
                            .Where(date => date <= utcNow.Date) // åªåŒ…含到今天为止的日期
                            .ToList();
            var inboundData = last7Days.Select(date => new
            {
                Date = date.ToString("MM-dd"),
                Value = stocks.Where(x => x.CreateDate.Date == date).Sum(x => x.Quantity),
            }).ToList();
            var outboundData = last7Days.Select(date => new
            {
                Date = date.ToString("MM-dd"),
                Value = orderdetail.Where(x => x.CreateDate.Date == date).Sum(x => x.OutboundQuantity)
            }).ToList();
            var MonthSumData = monthDays.Select(date => new
            {
                Date = date.ToString("MM-dd"),
                InValue = stocks.Where(x => x.CreateDate.Date == date).Sum(x => x.Quantity),
                OutValue = orderdetail.Where(x => x.CreateDate.Date == date).Sum(x => x.OutboundQuantity)
            }).ToList();
            object obj = new
            {
                metrics = new List<object>{
                new {
                    name = "今日进库量",
                    value = currentDayInStocks,
                    compare =currentDayInStocks-currentDayInStocksLast,
                },
                new {
                    name = "今日出库量",
                    value = currentDayOutStocks,
                    compare =currentDayOutStocks-currentDayOutStocksLast,
                },
                new {
                    name = "本月进库量",
                    value = currentMonthInStocks,
                    compare =currentMonthInStocks-currentMonthInStocksLastMonth,
                },
                new {
                    name = "本月出库量",
                    value = currentMonthOutStocks,
                    compare = currentMonthOutStocks - currentMonthOutStocksLastMonth,
                },
                new {
                    name = "库存总量",
                    value = stocks.Sum(x => x.Quantity)
                }
            },
                outbound = new
                {
                    dates = outboundData.Select(x => x.Date).ToArray(),
                    values = outboundData.Select(x => x.Value).ToArray()
                },
                inbound = new
                {
                    dates = inboundData.Select(x => x.Date).ToArray(),
                    values = inboundData.Select(x => x.Value).ToArray()
                },
                monthData = new
                {
                    dates = MonthSumData.Select(x => x.Date).ToArray(),
                    inValue = MonthSumData.Select(x => x.InValue).ToArray(),
                    outValue = MonthSumData.Select(x => x.OutValue).ToArray(),
                },
                newTask = task.Select(x => new
                {
                    x.PalletCode,
                    x.Roadway,
                    x.SourceAddress,
                    x.TargetAddress,
                    x.ErrorMessage,
                    x.CreateDate,
                    TaskType = EnumHelper.GetDescriptionFromEnums(x.TaskType, typeof(TaskOutboundTypeEnum), typeof(TaskInboundTypeEnum), typeof(TaskAcrossFloorTypeEnum), typeof(TaskRelocationTypeEnum), typeof(TaskAGVCarryTypeEnum)),
                    TaskState = EnumHelper.GetDescriptionFromEnums(x.TaskState, typeof(TaskInStatusEnum), typeof(TaskOutStatusEnum), typeof(TaskAcrossFloorStatusEnum), typeof(TaskRelocationStatusEnum), typeof(TaskAGVCarryStatusEnum))
                }).ToList()
            };
            return content.OK(data: obj);
        }
        catch (Exception ex)
        {
            return content.Error("系统错误,请联系管理员");
        }
    }
    #endregion
    #region æ‰“印库存单个物料二维码
    public async Task<WebResponseContent> PrintOrder(object[] key)
    {
        WebResponseContent content = new WebResponseContent();
        try
        {
            List<Dt_InboundOrder> inboundOrders = new List<Dt_InboundOrder>();
            foreach (var item in key)
            {
                var order = _inboundOrderRepository.QueryFirst(x => x.OrderNo == item.ToString());
                if (order != null)
                {
                    inboundOrders.Add(order);
                }
            }
            return _inboundOrderService.PrintInbound(inboundOrders); ;
        }
        catch (Exception ex)
        {
            return content.Error("未知错误,请联系管理员");
        }
    }
    #endregion
}