Files
wcs/WCS.BLL/Services/Service/MXL4Service.cs
2025-03-21 14:46:11 +08:00

300 lines
12 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Security.Policy;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Xml.Xsl;
using TouchSocket.Core;
using WCS.BLL.DbModels;
using WCS.BLL.HardWare;
using WCS.BLL.Manager;
using WCS.BLL.Services.IService;
using WCS.BLL.Tool;
using WCS.DAL;
using WCS.DAL.Db;
using WCS.DAL.Db.AuthDb;
using WCS.DAL.DbModels;
using WCS.Model;
using WCS.Model.ApiModel;
using WCS.Model.ApiModel.MXL4;
using WCS.Model.ApiModel.SingleLight;
using WCS.Model.ApiModel.User;
using WCS.Model.WebSocketModel;
using static System.Formats.Asn1.AsnWriter;
using static WCS.BLL.Tool.Helper;
namespace WCS.BLL.Services.Service
{
public class MXL4Service : IMXL4Service
{
public async Task<ResponseCommon<object>> sysOrderMXL4(SysOrderMXL4Request request)
{
try
{
//第一步:获取所有库位码 校验库位在数据库中是否都存在
var storeCodeList = request.List.Select(t => t.StoreCode)
.Distinct()
.ToList();
var moduleInfo = DbHelp.db.Queryable<ModuleInfo>()
.LeftJoin<ShelfTypeInfo>((si, sti) => si.ShelfTypeId == sti.Id)
.Where((si, sti) => sti.ShelfTypeName == "液晶标签货架")
.Where((si, sti) => storeCodeList.Contains(si.ModuleCode))
.Select((si, sti) => si)
.ToList();
if (moduleInfo.Count < storeCodeList.Count)
{
var storeCodesInDB = moduleInfo.Select(t => t.ModuleCode).ToList();
storeCodeList.RemoveAll(t => storeCodesInDB.Contains(t));
return new ResponseCommon<object>
{
Code = 201,
Message = $"操作失败:库位【{string.Join(",", storeCodeList)}】不存在!",
};
}
//第二步 获取库位正在进行的任务
//模式相同视为可以继续发送任务
var currenTasks = DbHelp.db.Queryable<CurrentTask>()
.Where(t => storeCodeList.Contains(t.StoreCode))
.ToList();
var currenTasksNotThisMode = currenTasks.Where(t => t.TaskMode != request.OrderType).ToList(); ;
if (currenTasksNotThisMode.Any())
{
var task = currenTasksNotThisMode.First();
return new ResponseCommon<object>
{
Code = 201,
Message = $"操作失败:库位【{task.StoreCode}】当前在{task.TaskMode}",
};
}
//获取taskId
var list = new List<int>() { 1, 2, 3, 4, 5, 6, 7 };
//已存在的taskID
var exsitIds = currenTasks.Select(t => t.TaskID)
.ToList();
exsitIds.ForEach(t =>
{
list.RemoveAll(l => l == t);
});
var tasks = new List<CurrentTask>();
//生成任务数据
foreach (var orderDetail in request.List)
{
var taskId = list.First();
list.RemoveAll(l => l == taskId);
var module = moduleInfo.Where(t => t.ModuleCode == orderDetail.StoreCode).First();
var task = new CurrentTask()
{
ModuleId = module.Id,
ModuleCode = module.ModuleCode,
ShelfId = module.ShelfId,
ShelfCode = module.ShelfCode,
Guid = orderDetail.Guid,
ItemNo = orderDetail.ItemNo,
TaskMode = request.OrderType,
OrderNumber = request.OrderNumber,
TaskID = taskId,
ButtonColor = DbModels.Task.ButtonColorEnum.绿,
MatCode = orderDetail.MatCode,
MatName = orderDetail.MatName,
MatSpec = string.IsNullOrEmpty(orderDetail.MatSpec) ? "-" : orderDetail.MatSpec,
MatBatch = orderDetail.MatBatch,
MatSN = orderDetail.MatSN,
Qty = orderDetail.Qty
};
tasks.Add(task);
}
try
{
DbHelp.db.BeginTran();
DbHelp.db.Insertable(tasks).ExecuteCommand();
DbHelp.db.CommitTran();
}
catch (Exception ex)
{
DbHelp.db.RollbackTran();
return new ResponseCommon<object>
{
Code = 300,
Message = $"操作失败:{ex.Message}"
};
}
//发送任务至各个标签 交给后台线程来做
return new ResponseCommon<object>
{
Code = 200,
Message = "success"
};
}
catch (Exception ex)
{
return new ResponseCommon<object>
{
Code = 201,
Message = "操作失败:" + ex.Message,
};
}
}
public async Task<ResponseCommon> refreshInventoryRequest(RefreshInventoryRequest request)
{
try
{
//第一步:校验库位在数据库中是否都存在
var storeCodeList = request.StoreCodes.Distinct()
.ToList();
var modules = DbHelp.db.Queryable<ModuleInfo>()
.LeftJoin<ShelfTypeInfo>((si, sti) => si.ShelfTypeId == sti.Id)
.Where((si, sti) => sti.ShelfTypeName == "液晶标签货架")
.Where((si, sti) => storeCodeList.Contains(si.ModuleCode))
.Select((si, sti) => si)
.ToList();
if (modules.Count < storeCodeList.Count)
{
var storeCodesInDB = modules.Select(t => t.ModuleCode).ToList();
storeCodeList.RemoveAll(t => storeCodesInDB.Contains(t));
return new ResponseCommon
{
Code = 201,
Message = $"操作失败:库位【{string.Join(",", storeCodeList)}】不存在!",
};
}
//第二步:获取到对应货架
var shelfs = modules.Select(t => new
{
ShelfId = t.ShelfId,
ModuId = t.Id
})
.GroupBy(t => t.ShelfId)
.Select(g => new
{
ShelfId = g.Key,
ModuIds = g.Select(t => t.ModuId).ToList()
})
.ToList();
//标记对应的模组为需要刷新
foreach (var shelf in shelfs)
{
var shelfInMemory = ShelfManager.Shelves.Where(t => t.ShelfTypeName == "液晶标签货架")
.Where(t => t.ShelfId == shelf.ShelfId)
.FirstOrDefault();
if (shelfInMemory == null)
{
Logs.Write($"[后台APi refreshInventoryRequest]通过{shelf.ShelfId}在内存缓存中查找货架失败!");
continue;
}
//将内存中对应模组置为需要刷新!
var modulesInMemory = shelfInMemory.MXL4Modules.Where(t => shelf.ModuIds.Contains(t.ModuleId))
.ToList();
modulesInMemory.ForEach(t =>
{
t.IsNeedRefresh = true;
});
}
return new ResponseCommon
{
Code = 200,
Message = "success"
};
}
catch (Exception ex)
{
return new ResponseCommon
{
Code = 201,
Message = "操作失败:" + ex.Message,
};
}
}
public async Task<ResponseCommon> reSetAll()
{
try
{
//1.获取所有液晶标签货架
var shelfs = ShelfManager.Shelves.Where(t => t.ShelfTypeName == "液晶标签货架")
.Select(t => t as MXL4Shelf)
.ToList();
shelfs.ForEach(t =>
{
t.Reset();
});
return new ResponseCommon
{
Code = 200,
Message = "success"
};
}
catch (Exception ex)
{
return new ResponseCommon
{
Code = 201,
Message = "操作失败:" + ex.Message,
};
}
}
public async Task<ResponseCommon> defaultDisplay()
{
try
{
//1.获取所有液晶标签货架
var shelfs = ShelfManager.Shelves.Where(t => t.ShelfTypeName == "液晶标签货架")
.Select(t => t as MXL4Shelf)
.ToList();
shelfs.ForEach(t =>
{
t.MXL4Modules.ForEach(m =>
{
//发送进入入库模式
m.GoInOutstoreMode(t.TcpCleint,DbModels.Task.ButtonColorEnum.);
//任务ID
m.SendTaskId(7, t.TcpCleint);
m.SendMatCode("A7100200300", t.TcpCleint);
m.SendMatName("A7SESSSSSS", t.TcpCleint);
m.SendMatSpec("7GUIGE", t.TcpCleint);
m.SendMatBatch("202412097", t.TcpCleint);
m.SendMatQty(999, t.TcpCleint);
m.SendTaskId(6, t.TcpCleint);
m.SendMatCode("A6666666", t.TcpCleint);
m.SendMatName("ASESSSSSS6", t.TcpCleint);
m.SendMatSpec("GUIGE6", t.TcpCleint);
m.SendMatBatch("202412096", t.TcpCleint);
m.SendMatQty(222, t.TcpCleint);
m.SendTaskId(3, t.TcpCleint);
m.SendMatCode("A33333", t.TcpCleint);
m.SendMatName("3ASESSSSS", t.TcpCleint);
m.SendMatSpec("3GUIGE3", t.TcpCleint);
m.SendMatBatch("202412093", t.TcpCleint);
m.SendMatQty(333, t.TcpCleint);
});
});
return new ResponseCommon
{
Code = 200,
Message = "success"
};
}
catch (Exception ex)
{
return new ResponseCommon
{
Code = 200,
Message = "操作失败:" + ex.Message,
};
}
}
}
}