using Microsoft.AspNetCore.Mvc; using MiniExcelLibs; using System.Linq.Expressions; using WCS.BLL.DbModels; using WCS.BLL.Services.IService; using WCS.BLL.Services.Service; using WCS.DAL.Db; using WCS.DAL.DbModels; using WCS.Model; using WCS.Model.ApiModel; using WCS.Model.ApiModel.BatchBindMatDetail; using WCS.Model.ApiModel.Home; using WCS.Model.ApiModel.MatBaseInfo; using WCS.Model.ApiModel.PDAMatBind; using WCS.Model.ApiModel.StoreInfo; using WCS.Model.ApiModel.User; using WCS.Model.WebSocketModel; namespace WCS.WebApi.Controllers { /// /// PDA物料绑定相关接口 /// [ApiController] [Route("[controller]")] public class BatchBindMatDetailController : ControllerBase { public IBatchBindMatDetailService _batchBindMatDetailService { get; set; } public BatchBindMatDetailController(IBatchBindMatDetailService batchBindMatDetailService) { _batchBindMatDetailService = batchBindMatDetailService; } [Route("getOrderTypes")] [HttpPost(Name = "getOrderTypes")] public async Task getOrderTypes(RequestBase request) { try { //直接获取当前所有货架类型并返回 var OrderTypes = DbHelp.db.Queryable() .Select(t => new OrderTypeModel() { Id = t.Id, OrderTypeName = t.OrderTypeName }) .ToList(); return new PageQueryResponse() { Code = 200, Message = "success", Data = new PageQueryResponseData { Lists = OrderTypes, Count = OrderTypes.Count } }; } catch (Exception ex) { return new PageQueryResponse() { Code = 300, Message = $"查询失败:{ex.Message}", }; } } [Route("getMatDetailCurrentInfosByStationCode")] [HttpPost(Name = "getMatDetailCurrentInfosByStationCode")] public async Task GetMatDetailCurrentInfosByStationCode(GetMatDetailCurrentInfosByStationCodeRequest request) { ////判断参数 //if (string.IsNullOrEmpty(request.StationCode)) //{ // return new ResponseCommon() // { // Code = 201, // Message = "工位编码为空,请联系相关人员进行配置!", // Data = null, // }; //} ////判断工位是否存在 //var isExsistLocation = await DbHelp.db.Queryable() // .Where(t => t.LocationCode == request.StationCode) // .AnyAsync(); ////不存在工位 //if (!isExsistLocation) //{ // return new ResponseCommon() // { // Code = 201, // Message = $"工位[{request.StationCode}]不存在,暂时无法使用此功能!", // Data = null, // }; //} return await _batchBindMatDetailService.GetMatDetailCurrentInfosByStationCode(request); } [HttpPost("updateMatDetailCurrentInfoForBind")] public async Task> updateMatDetailCurrentInfoForBind(AddLocaionInfoRequest request) { return await _batchBindMatDetailService.updateMatDetailCurrentInfo(request); } [HttpPost("deleteMatDetailCurrentInfoForBind")] public async Task> deleteMatDetailCurrentInfoForBind(DeleteInfosRequest request) { //校验 if (request.needDeleteIds == null || request.needDeleteIds.Count == 0) { return new ResponseCommon() { Code = 201, Message = "操作失败:参数校验失败(ID)." }; } return await _batchBindMatDetailService.deleteMatDetailCurrentInfo(request); } [HttpPost("importMatDetailCurrentInfos")] public async Task importMatDetailCurrentInfos([FromForm] IFormFile excelFile, [FromForm] string userName , [FromForm] string deviceType, [FromForm] string stationCode) { //文件校验 if (excelFile == null || excelFile.Length == 0) { return new ResponseCommon() { Code = 201, Message = "导入失败:文件无有效内容!" }; } //输入数据校验 if (string.IsNullOrEmpty(userName)) { return new ResponseCommon() { Code = 201, Message = "导入失败:参数[用户名]无效,请登录后重试!" }; } if (string.IsNullOrEmpty(deviceType)) { return new ResponseCommon() { Code = 201, Message = "导入失败:参数[设备类型]无效!" }; } if (string.IsNullOrEmpty(stationCode)) { return new ResponseCommon() { Code = 201, Message = "导入失败:参数[工位]无效,请联系相关人员维护工位编码!" }; } using (var stream = new MemoryStream()) { await excelFile.CopyToAsync(stream); stream.Position = 0; var list = MiniExcelLibs.MiniExcel.Query(stream, "物料绑定", ExcelType.XLSX).ToList(); //去除空白行 //infoList.RemoveAll(x => string.IsNullOrWhiteSpace(x.物料编码)); if (list == null || list.Count == 0) { return new ResponseCommon() { Code = 201, Message = "导入失败:上传的文件中不存在有效的数据!" }; } return await _batchBindMatDetailService.importMatDetailCurrentInfo(list, userName, deviceType, stationCode); } } /// /// 导入旧系统绑定关系 /// /// /// /// /// /// [HttpPost("importOldSystem")] public async Task importOldSystem([FromForm] IFormFile excelFile, [FromForm] string userName , [FromForm] string deviceType) { try { //文件校验 if (excelFile == null || excelFile.Length == 0) { return new ResponseCommon() { Code = 201, Message = "导入失败:文件无有效内容!" }; } //输入数据校验 if (string.IsNullOrEmpty(userName)) { return new ResponseCommon() { Code = 201, Message = "导入失败:参数[用户名]无效,请登录后重试!" }; } if (string.IsNullOrEmpty(deviceType)) { return new ResponseCommon() { Code = 201, Message = "导入失败:参数[设备类型]无效!" }; } using (var stream = new MemoryStream()) { await excelFile.CopyToAsync(stream); stream.Position = 0; var infoList = MiniExcelLibs.MiniExcel.Query(stream, "货架管理", ExcelType.XLSX).ToList(); //去除空白行 infoList.RemoveAll(x => string.IsNullOrWhiteSpace(x.物料批次号)); if (infoList == null || infoList.Count == 0) { return new ResponseCommon() { Code = 201, Message = "导入失败:上传的文件中不存在有效的数据!" }; } var shelfAlreadyBind = DbHelp.db.Queryable().Select(t => t.ShelfId).Distinct().ToList(); var shelfCodes = infoList.Select(t => t.货架编号).Distinct().ToList(); //获取系统中的货架 var shelfs = DbHelp.db.Queryable() .LeftJoin((si, mci) => mci.ShelfId == si.Id) .Where((si, mci) => shelfCodes.Contains(si.ShelfCode) && !shelfAlreadyBind.Contains(si.Id)) .Select((si, mci) => si) .Distinct() .ToList(); //var shelfCodesInDb = shelfs.Select(t => t.ShelfCode).Distinct().ToList(); var matCurrentInfos = new List(); foreach (var info in infoList) { var shelf = shelfs.Where(t => t.ShelfCode == info.货架编号).FirstOrDefault(); //此货架已在新系统绑定的情况 if (shelf == null) { continue; } var matCurrentInfo = new MatDetailCurrentInfo() { ShelfId = shelf.Id, ShelfCode = shelf.ShelfCode, ShelfType = shelf.ShelfTypeName, MatCode = info.物料批次号 + "(旧系统)", MatName = info.物料批次号 + "(旧系统)", MatBatch = "", MatQty = 1, ModifyUser = "system" }; matCurrentInfos.Add(matCurrentInfo); } //删除上一次导入的信息 DbHelp.db.Deleteable().Where(t => t.MatCode.Contains("(旧系统)")).ExecuteCommand(); DbHelp.db.Insertable(matCurrentInfos).ExecuteCommand(); return new ResponseCommon() { Code = 200, Message = "Success" }; } } catch (Exception ex) { return new ResponseCommon() { Code = 201, Message = "失败" + ex.Message }; } } } }