wanshenmean
2024-11-03 a15d996584733d60d0adddcd791f029d608c4fae
Code Management/WMS/WIDESEA_WMSServer/WIDESEA_StorageTaskServices/Task/Dt_TaskService.cs
@@ -1,9 +1,9 @@
using Mapster;
using System.Collections.Generic;
using WIDESEA_DTO.MOM;
using WIDESEA_DTO.WMS;
using WIDESEA_IStoragIntegrationServices;
using WIDESEA_StoragIntegrationServices;
using WIDESEA_StorageBasicRepository;
namespace WIDESEA_StorageOutTaskServices; 
public class Dt_TaskService : ServiceBase<Dt_Task, IDt_TaskRepository>, IDt_TaskService
@@ -25,6 +25,7 @@
    private readonly IBoxingInfoRepository _boxingInfoRepository;
    private readonly ICellStateService _cellStateService;
    private readonly IProcessApplyService _processApplyService;
    private readonly IDt_AreaInfoRepository _areaInfoRepository;
    public Dt_TaskService(IDt_TaskRepository BaseDal,
                                IUnitOfWorkManage unitOfWorkManage,
@@ -42,7 +43,8 @@
                                ILocationStatusChangeRecordRepository locationStatusChangeRecordRepository,
                                IBoxingInfoRepository boxingInfoRepository,
                                ICellStateService cellStateService,
                                IProcessApplyService processApplyService) : base(BaseDal)
                                IProcessApplyService processApplyService,
                                IDt_AreaInfoRepository areaInfoRepository) : base(BaseDal)
    {
        _unitOfWorkManage = unitOfWorkManage;
        _outOrderRepository = outOrderRepository;
@@ -59,7 +61,8 @@
        _locationStatusChangeRecordRepository = locationStatusChangeRecordRepository;
        _boxingInfoRepository = boxingInfoRepository;
        _cellStateService = cellStateService;
        _processApplyService = processApplyService
        _processApplyService = processApplyService;
        _areaInfoRepository = areaInfoRepository;
    }
    #region 外部接口方法
@@ -195,7 +198,7 @@
            // 修改货位信息为有货
            var locationInf = await _locationRepository.QueryFirstAsync(x => x.LocationCode == task.TargetAddress);
            locationInf.LocationStatus = (int)LocationEnum.Lock;
            locationInf.LocationStatus = (int)LocationEnum.InStock;
            LogFactory.GetLog("任务完成").InfoFormat(true, "入库任务完成", $"货位地址:{task.TargetAddress},修改后货位数据:{locationInf}");
@@ -241,7 +244,7 @@
        if (stock == null)
        {
            LogFactory.GetLog("任务完成").InfoFormat(true, "库存不存在存在,判断是否是入库任务", $"{task.TaskType}");
            if (task.TaskType == (int)TaskTypeEnum.Inbound)
            if (task.TaskType == (int)TaskInboundTypeEnum.Inbound)
            {
                LogFactory.GetLog("任务完成").InfoFormat(true, "入库任务", "");
                return await CompleteInboundTaskAsync(task);
@@ -254,7 +257,7 @@
        }
        LogFactory.GetLog("任务完成").InfoFormat(true, "验证库存是否存在", JsonConvert.SerializeObject(stock));
        if (task.TaskType == (int)TaskTypeEnum.Outbound)
        if (task.TaskType == (int)TaskOutboundTypeEnum.Outbound)
        {
            LogFactory.GetLog("任务完成").InfoFormat(true, "出库任务", "");
            if (task.TaskState == TaskOutStatusEnum.SC_OutExecuting.ObjToInt())
@@ -291,22 +294,63 @@
    /// <returns>包含任务信息的响应内容</returns>
    public async Task<WebResponseContent> RequestTaskAsync(RequestTaskDto input)
    {
        // 创建一个WebResponseContent对象
        WebResponseContent content = new WebResponseContent();
        try
        {
            // 根据托盘获取库存信息
            //var stockInfo = await _stockInfoRepository.QueryFirstAsync(x => x.PalletCode == input.PalletCode);
            // 创建一个TrayCellsStatusDto对象,并赋值
            TrayCellsStatusDto trayCells = new TrayCellsStatusDto()
            {
                TrayBarcode = input.PalletCode
            };
            content = await _cellStateService.GetTrayCellStatusAsync(trayCells);
            if (content.Status)
            {
                ResultTrayCellsStatus result = JsonConvert.DeserializeObject<ResultTrayCellsStatus>(content.Data.ToString());
                ProcessApplyDto process = new ProcessApplyDto()
            // 调用GetTrayCellStatusAsync方法,获取整盘电芯
            content = await GetTrayCellStatusAsync(trayCells);
            // 如果状态为false,则返回content
            if (!content.Status) return content;
            // 调用GetProcessApplyAsync方法,获取工艺申请
            ProcessApplyDto process = await GetProcessApplyAsync(content);
            // 如果process为null,则返回content
            if (process == null) return content;
            // 调用GetProcessResponseAsync方法,获取工艺响应
            var processResponse = await GetProcessResponseAsync(process, input.Position);
            // 调用BaseDal.QueryFirstAsync方法,查询任务
            var task = await BaseDal.QueryFirstAsync(x => x.PalletCode == input.PalletCode);
            // 如果task不为null,则调用UpdateExistingTask方法,更新任务;否则调用CreateNewTask方法,创建新任务
            content = task != null ? await UpdateExistingTask(input, task) : await CreateNewTask(processResponse, input);
        }
        catch (Exception err)
        {
            // 如果发生异常,则调用content.Error方法,记录错误信息,并输出错误信息
            content.Error(err.Message);
            Console.WriteLine(err.Message);
        }
        // 返回content
        return content;
    }
    // 获取托盘单元格状态
    private async Task<WebResponseContent> GetTrayCellStatusAsync(TrayCellsStatusDto trayCells)
    {
        // 调用_cellStateService.GetTrayCellStatusAsync方法,获取托盘单元格状态
        return await _cellStateService.GetTrayCellStatusAsync(trayCells);
    }
    // 获取工艺申请
    private async Task<ProcessApplyDto> GetProcessApplyAsync(WebResponseContent content)
    {
        // 如果状态为false,则返回null
        if (!content.Status) return null;
        // 将content.Data转换为ResultTrayCellsStatus对象
        var result = JsonConvert.DeserializeObject<ResultTrayCellsStatus>(content.Data.ToString());
        // 创建一个ProcessApplyDto对象,并赋值
        return new ProcessApplyDto()
                {
                    WipOrderNo = result.BindCode,
                    SerialNos = result.SerialNos.Select(item => new SerialNos
@@ -314,42 +358,92 @@
                        SerialNo = item.SerialNo
                    }).ToList()
                };
                content = await _processApplyService.GetProcessApplyAsync(process);
                if (content.Status)
    }
    // 获取工艺响应
    private async Task<List<Dt_EquipmentProcess>> GetProcessResponseAsync(ProcessApplyDto process, string position)
                {
                    var X = SqlSugarHelper.Db.Queryable<Dt_EquipmentProcess>().Where(x => x.EquipmentType == "CH").ToList();
                }
        // 如果process为null,则返回null
        if (process == null) return null;
            }
        // 调用_processApplyService.GetProcessApplyAsync方法,获取工艺申请
        WebResponseContent content = await _processApplyService.GetProcessApplyAsync(process);
        // 如果状态为false,则返回null
        if (!content.Status) return null;
            // 获取仓库区域信息
            var areaInfo = await _wareAreaInfoRepository.QueryFirstAsync(x => x.WareAreaCode == input.AreaCode);
            // 查询是否已有任务
            var task = await BaseDal.QueryFirstAsync(x => x.PalletCode == input.PalletCode);
            if (task != null)
        // 将content.Data转换为ResultProcessApply对象
        var apply = JsonConvert.DeserializeObject<ResultProcessApply>(content.Data.ToString());
        // 根据position的值,返回不同的Dt_EquipmentProcess列表
        switch (position)
            {
                // 更新现有任务
                content = await UpdateExistingTask(input, areaInfo.AreaID, task);
            }
            else
            {
                // 创建新任务
                content = await CreateNewTask(input, areaInfo.AreaID);
            }
        }
        catch (Exception err)
        {
            // 错误处理
            content.Error(err.Message);
            Console.WriteLine(err.Message);
        }
            case "1088":
                return SqlSugarHelper.Db.Queryable<Dt_EquipmentProcess>()
                    .Where(x => x.EquipmentType == "陈化" && x.WipOrderNo == apply.WipOrderNo && x.ProductDesc == apply.ProductNo)
                    .ToList();
        return content;
            default:
                return new List<Dt_EquipmentProcess>();
        }
    }
    #endregion 请求任务
    #region 请求空托盘
    public async Task<WebResponseContent> RequestTrayTaskAsync(string position)
    {
        WebResponseContent content = new WebResponseContent();
        try
        {
            var area = _areaInfoRepository.QueryFirst(x => x.AreaCode == "CH");
            var stockInfo = _stockInfoRepository.QueryData(x => x.Remark == area.AreaCode && x.StockInfoDetails.Any(z => z.MaterielCode == "空托盘")).OrderBy(x => x.CreateDate).FirstOrDefault();
            // 创建新任务实例
            var task = new Dt_Task
            {
                CurrentAddress = stockInfo.LocationCode,
                Grade = 1,
                Roadway = stockInfo.LocationInfo.RoadwayNo,
                TargetAddress = position,
                Dispatchertime = DateTime.Now,
                MaterialNo = "",
                NextAddress = position,
                OrderNo = null,
                PalletCode = stockInfo.PalletCode,
                SourceAddress = stockInfo.LocationCode,
                TaskState = (int)TaskOutStatusEnum.OutNew,
                TaskType = (int)TaskOutboundTypeEnum.OutTray,
                TaskNum = await BaseDal.GetTaskNo(),
                Creater = "Systeam"
            };
            var taskId = await BaseDal.AddDataAsync(task);
            bool isResult = taskId > 0;
            if (isResult)
            {
                WMSTaskDTO taskDTO = new WMSTaskDTO()
                {
                    TaskNum = task.TaskNum.Value,
                    Grade = 1,
                    PalletCode = DateTime.Now.ToString("MMddHHmmss"),
                    RoadWay = task.Roadway,
                    SourceAddress = stockInfo.LocationCode,
                    TargetAddress = task.Roadway,
                    TaskState = (int)TaskOutStatusEnum.OutNew,
                    Id = 0,
                    TaskType = (int)TaskOutboundTypeEnum.OutTray
                };
                content.OK(data: taskDTO);
            }
        }
        catch (Exception ex)
        {
            throw;
        }
        return content;
    }
    #endregion 请求空托盘
    #endregion 外部接口方法
@@ -640,7 +734,7 @@
    /// <param name="task">任务实例</param>
    /// <param name="content">响应内容</param>
    /// <returns></returns>
    private async Task<WebResponseContent> UpdateExistingTask(RequestTaskDto input, int areaId, Dt_Task task)
    private async Task<WebResponseContent> UpdateExistingTask(RequestTaskDto input, Dt_Task task)
    {
        // 创建WebResponseContent对象
        WebResponseContent content = new WebResponseContent();
@@ -652,26 +746,26 @@
        int beforeStatus = 0;
        // 根据任务类型判断是出库任务还是入库任务
        if (input.Type == (int)TaskTypeEnum.Outbound)
        {
            // 处理出库任务
            toAddress = await GetRoadWayAsync(areaId, task.Roadway, input.Direction, input.Area, input.Type);
            taskState = (int)TaskOutStatusEnum.SC_OutFinish;
        }
        else
        {
        //if (input.Type == (int)TaskTypeEnum.Outbound)
        //{
        //    // 处理出库任务
        //    toAddress = await GetRoadWayAsync(areaId, task.Roadway, input.Direction, input.Area, input.Type);
        //    taskState = (int)TaskOutStatusEnum.SC_OutFinish;
        //}
        //else
        //{
            // 处理入库任务
            location = await GetLocationDistributeAsync(areaId, task.Roadway);
        location = await GetLocationDistributeAsync(task.Roadway);
            toAddress = location.LocationCode;
            taskState = (int)TaskInStatusEnum.Line_InFinish;
            beforeStatus = location.LocationStatus;
            // 更新货位信息
            location.LocationStatus = (int)LocationEnum.Lock;
        }
        //}
        // 更新任务信息
        task.SourceAddress = input.Position;
        //task.SourceAddress = input.Position;
        task.CurrentAddress = input.Position;
        task.TargetAddress = toAddress;
        task.NextAddress = toAddress;
@@ -686,8 +780,7 @@
        // 尝试更新任务
        bool isResult = await BaseDal.UpdateDataAsync(task);
        bool isTaskDetail = await _taskExecuteDetailRepository.AddDetailAsync(task, false, TaskDescription.GetTaskUpdateDescription(task.PalletCode, original, input.Position, TaskInStatusEnum.Line_InFinish.GetIntegralRuleTypeEnumDesc()));
        if (input.Type != (int)TaskTypeEnum.Outbound)
        {
            // 创建LocationChangeRecordDto对象
            LocationChangeRecordDto changeRecordDto = new LocationChangeRecordDto()
            {
@@ -702,7 +795,6 @@
            // 更新位置状态
            isUpdateChange = _locationStatusChangeRecordRepository.AddStatusChangeRecord(changeRecordDto);
            isUpdateLo = await _locationRepository.UpdateDataAsync(location);
        }
        if (isResult && isUpdateLo && isTaskDetail)
        {
@@ -726,11 +818,12 @@
    /// <param name="areaId">区域ID</param>
    /// <param name="content">响应内容</param>
    /// <returns></returns>
    private async Task<WebResponseContent> CreateNewTask(RequestTaskDto input, int areaId)
    private async Task<WebResponseContent> CreateNewTask(List<Dt_EquipmentProcess> process, RequestTaskDto input)
    {
        WebResponseContent content = new WebResponseContent();
        List<string> strings = process.Select(x => x.EquipmentName).ToList();
        // 获取目标地址
        string ToAddress = await GetRoadWayAsync(areaId, input.Position, input.Direction, input.Area, input.Type);
        string ToAddress = await GetRoadWayAsync(strings);
        // 创建新任务实例
        var task = new Dt_Task
@@ -765,7 +858,7 @@
                    Grade = 1,
                    PalletCode = DateTime.Now.ToString("MMddHHmmss"),
                    RoadWay = task.Roadway,
                    SourceAddress = "001-001-001",
                    SourceAddress = input.Position,
                    TargetAddress = task.Roadway,
                    TaskState = (int)TaskInStatusEnum.InNew,
                    Id = 0,
@@ -787,13 +880,13 @@
    /// <param name="areaId">区域主键</param>
    /// <param name="roadWay">巷道</param>
    /// <returns></returns>
    public async Task<DtLocationInfo> GetLocationDistributeAsync(int areaId, string roadWay)
    public async Task<DtLocationInfo> GetLocationDistributeAsync(string roadWay)
    {
        #region 获取货位
        try
        {
            var locations = await _locationRepository.QueryDataAsync(x => x.AreaId == areaId && x.LocationStatus == (int)LocationEnum.Free && x.RoadwayNo == roadWay);
            var locations = await _locationRepository.QueryDataAsync(x => x.LocationStatus == (int)LocationEnum.Free && x.RoadwayNo == roadWay);
            if (locations == null)
            {
                return null;
@@ -818,36 +911,16 @@
    /// <param name="Direction">方向</param>
    /// <param name="area">关系区域</param>
    /// <returns></returns>
    public async Task<string> GetRoadWayAsync(int areaId, string position, string Direction, string area, int type)
    public async Task<string> GetRoadWayAsync(List<string> process)
    {
        var point = new PointStackerRelation();
        if (type == (int)TaskTypeEnum.Inbound)
        {
            point = await _pointStackerRelationRepository.QueryFirstAsync(x => x.Direction == Direction && x.PointCode == position && x.Area == area);
            if (point == null) { return null; }
            var locationInfos = await _locationRepository.QueryDataAsync(x => x.AreaId == areaId && point.StackerCode.Contains(x.RoadwayNo));
            var groupWithMinCount = locationInfos.GroupBy(x => x.RoadwayNo).OrderBy(x => x.Count()).FirstOrDefault();
        var minGroup = _locationRepository.QueryData(x => process.Contains(x.RoadwayNo) && x.LocationStatus == (int)LocationEnum.Free)
            .GroupBy(x => x.RoadwayNo)
             .OrderBy(g => g.Count()) // 根据每个组的元素数量排序
             .First(); // 取出数量最少的组
            if (groupWithMinCount != null)
                return groupWithMinCount.Key;
            else
                return null;
        }
        else if (type == (int)TaskTypeEnum.Outbound)
        {
            point = await _pointStackerRelationRepository.QueryFirstAsync(x => x.Direction == Direction && position == x.StackerCode && x.Area == area);
            if (point == null) { return null; }
        string minRoadwayNo = minGroup.Key; // 数量最少的组的Key
            string station = point.PointCodeList[0];
            var rList = point.PointCodeList;
            rList.Remove(station);
            rList.Add(station);
            point.PointCodeList = rList;
            return station;
        }
        else { return null; }
        return minRoadwayNo;
    }
    #endregion 任务请求方法