using JetBrains.Annotations; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using NUglify.Helpers; using Seyounth.Auto.Hs.Runtime.Plc; using Seyounth.Auto.Hs.Runtime.Printer; using Syc.Abp.Application.Contracts; using Syc.Basic.Web.WMS.Dto; using Syc.Basic.Web.WMS.Entitys; using Syc.Basic.Web.WMS.IService; using System; using System.Collections.Generic; using System.Linq; using System.Linq.Dynamic.Core; using System.Text; using System.Threading.Tasks; using Volo.Abp.DependencyInjection; using Volo.Abp.Domain.Repositories; namespace Syc.Basic.Web.WMS.Service { [AllowAnonymous] public class BoxService : ApiService, IBoxService, ITransientDependency { private readonly IRepository boxRepository; private readonly IRepository produceRepository; private readonly IRepository silkRepository; private readonly IPlcService plcService; private readonly ILogger logger; private readonly IPrinterService printerService; private readonly IRepository autoRepository; public BoxService(IRepository boxRepository, IRepository produceRepository, IRepository silkRepository, IPlcService plcService, ILogger logger, IPrinterService printerService, IRepository autoRepository) { this.boxRepository = boxRepository; this.produceRepository = produceRepository; this.silkRepository = silkRepository; this.plcService = plcService; this.logger = logger; this.printerService = printerService; this.autoRepository = autoRepository; } /// /// 查询纸箱 /// /// /// [HttpPost] public async Task> GetBoxList(BoxInput input) { var boxlist = await boxRepository.GetQueryableAsync(); boxlist = boxlist.Where(x => x.IsDelete == 0); if (input.Lot_No != null) boxlist = boxlist.Where(x => x.Lot_No.Contains(input.Lot_No)); if (input.Spec != null) boxlist = boxlist.Where(x => x.Spec.Contains(input.Spec)); var result = boxlist.OrderByDescending(x => x.Id).PageResult(input.Page, input.PageSize); var data = result.Queryable.Select(e => new BoxDto() { Length = e.Length, Net_Weight = e.Net_Weight, Qty = e.Qty, Id = e.Id, Code = e.Code, Dom_Time = DateTime.Parse(e.Dom_Time.ToString()).ToString("yyyy-MM-dd HH:mm:ss"), DataType = e.DataType, Mark = e.Mark, Create_Time = e.Create_Time, Exp_Time = DateTime.Parse(e.Exp_Time.ToString()).ToString("yyyy-MM-dd HH:mm:ss"), Lot_No = e.Lot_No, Spec = e.Spec, Gross_Weight = e.Gross_Weight, Type = e.Type }).ToList(); PageOutput pageOutput = new PageOutput(); pageOutput.Total = boxlist.Count(); pageOutput.Data = data; pageOutput.PageIndex = input.Page; pageOutput.PageSize = input.PageSize; return pageOutput; } /// /// 查询数量纸箱 /// /// /// [HttpGet] public async Task> GetBoxByNum() { var produce = await produceRepository.FirstOrDefaultAsync(x => x.IfUse == 1); if (produce == null) throw Oops.Oh("没有生产设置"); //var silks = await silkRepository.GetListAsync(x=>x.Status==1||x.Status==0); var boxlist = await boxRepository.GetQueryableAsync(); boxlist = boxlist.Where(x => x.IsDelete == 0 && x.Spec == produce.Spec && x.Type == produce.Type && x.Lot_No == produce.Lot_No); var data = boxlist.Where(x => x.DataType == 0).Select(e => new BoxDto() { Net_Weight = e.Net_Weight, Id = e.Id, Code = e.Code, DataType = e.DataType, Mark = e.Mark }).ToList(); return data.OrderByDescending(x => x.Id).Take(15).ToList(); } /// /// 添加纸箱 /// /// /// [HttpPost] public async Task InsertBox(BoxDto input) { var produce = await produceRepository.FirstOrDefaultAsync(x => x.IfUse == 1); var silksQuery = await silkRepository.GetQueryableAsync(); var silks = silksQuery.Where(x => x.IsDelete == 0 && x.Status == (int)SilkStatus.手动添加称重).OrderBy(x => x.Id).Take((int)produce.Qty).ToList(); if (produce == null) throw Oops.Oh("没有生产设置"); if (silks.Count < produce.Qty) { logger.LogWarning($"没有{produce.Qty}条{produce.Type}的单品的信息,只有{silks.Count}条,无法创建码垛信息"); throw Oops.Oh($"没有{produce.Qty}条{produce.Type}的单品的信息,只有{silks.Count}条,无法创建码垛信息"); //return; } var netWeight = silks.Sum(x => x.Net_Weight); var weight = silks.Average(x => x.Net_Weight); var date = DateOnly.Parse(DateTime.Now.ToString("yyyy-MM-dd")); var autolabel = await autoRepository.FirstOrDefaultAsync(x => x.Date == date && x.Type == (int)AutoLabelType.成箱码垛流水号); var timestr = DateTime.Now.ToString("yyyyMMddHHmmss"); if (autolabel == null) { autolabel = await autoRepository.InsertAsync(new AutoLabel() { Date = date, Sort = 0, Mark = timestr, Type = (int)AutoLabelType.成箱码垛流水号 }, true); } autolabel.Sort += 1; logger.LogInformation("手动添加成箱信息" + timestr + autolabel.Sort.ToString().PadLeft(4, '0')); await autoRepository.UpdateAsync(autolabel); var box = new Box() { Dom_Time = DateTime.Now, Exp_Time = produce.Exp_Time, Qty = produce.Qty, Length = produce.Length, Lot_No = produce.Lot_No, Code = timestr + autolabel.Sort.ToString().PadLeft(4, '0'), Net_Weight = netWeight, Spec = produce.Spec, Create_Time = DateTime.Now, Type = produce.Type, IsUse = true, IsDelete = 0, DataType = (int)BoxDataType.手动装箱, Mark = "已经手动添加装箱信息" }; var entity = await boxRepository.InsertAsync(box, true); logger.LogInformation($"已手动添加装箱信息,装箱编号:{box.Code},包含单品数量:{silks.Count}"); List list = new List(); foreach (var silk in silks) { silk.Status = (int)SilkStatus.已手动装箱; silk.BoxId = entity.Id; silk.Status_Details = "已手动装箱"; list.Add(silk); } await silkRepository.UpdateManyAsync(list); } /// /// 添加纸箱 /// /// /// [HttpPost] public async Task AddInsertBox(BoxDto input) { var date = DateOnly.Parse(DateTime.Now.ToString("yyyy-MM-dd")); var autolabel = await autoRepository.FirstOrDefaultAsync(x => x.Date == date && x.Type == (int)AutoLabelType.成箱码垛流水号); var timestr = DateTime.Now.ToString("yyyyMMddHHmmss"); if (autolabel == null) { autolabel = await autoRepository.InsertAsync(new AutoLabel() { Date = date, Sort = 0, Mark = timestr, Type = (int)AutoLabelType.成箱码垛流水号 }, true); } autolabel.Sort += 1; logger.LogInformation("手动添加成箱信息" + timestr + autolabel.Sort.ToString().PadLeft(4, '0')); await autoRepository.UpdateAsync(autolabel); var box = new Box() { Dom_Time = DateTime.Now, Exp_Time = DateTime.Parse(input.Exp_Time), Qty = input.Qty, Length = input.Length, Lot_No = input.Lot_No, Code = timestr + autolabel.Sort.ToString().PadLeft(4, '0'), Net_Weight = input.Net_Weight, Spec = input.Spec, Create_Time = DateTime.Now, Type = input.Type, IsUse = true, IsDelete = 0, DataType = (int)BoxDataType.手动成箱未包含单品信息, Mark = "手动添加的成箱信息数据库未绑定单品信息" }; var entity = await boxRepository.InsertAsync(box, true); logger.LogInformation($"手动添加的成箱信息未绑定单品信息:{box.Code},未含单品数量"); BarTenderHelper.BoxPrint(box); logger.LogInformation($"打印条码{box.Code},成箱标签打印成功"); } /// /// 打印纸箱 /// /// /// [HttpPost] public async Task PrintBox(DelInput input) { var box = await boxRepository.FirstOrDefaultAsync(x => x.Id == input.id); BarTenderHelper.BoxPrint(box); logger.LogInformation($"打印条码{box.Code},成箱标签打印成功"); //throw Oops.Oh("成箱标签打印成功"); } /// /// 修改纸箱 /// /// /// [HttpPost] public async Task UpdateBox(BoxDto input) { var box = await boxRepository.FirstOrDefaultAsync(x => x.Id == input.Id); box.Spec = input.Spec; box.Length = input.Length; //box.Code = input.Code; box.Lot_No = input.Lot_No; box.Net_Weight = input.Net_Weight; box.Dom_Time = DateTime.Now; box.Type = input.Type; box.Qty = input.Qty; box.Exp_Time = string.IsNullOrWhiteSpace(input.Exp_Time) ? null : Convert.ToDateTime(input.Exp_Time); await boxRepository.UpdateAsync(box); } /// /// 删除纸箱 /// /// /// [HttpPost] public async Task DeleteBox(DelInput input) { var box = await boxRepository.FirstOrDefaultAsync(x => x.Id == input.id); if (box == null) throw Oops.Oh("删除失败,数据为空"); box.IsDelete = 1; await boxRepository.UpdateAsync(box); } /// /// 批量删除纸箱 /// /// /// [HttpPost] public async Task DeletesBoxs(DelInput input) { var boxs = await boxRepository.GetListAsync(x => input.ids.Contains(x.Id)); if (boxs.Count == 0) throw Oops.Oh("删除失败,数据为空"); for (var i = 0; i < boxs.Count; i++) { boxs[i].IsDelete = 1; } await boxRepository.UpdateManyAsync(boxs); } /// /// 设置热缩机温度 /// /// public async Task SetTemperature(ByIdInput input) { await plcService.SetTemperatureAsync(input.Id); } /// /// 复位 /// /// [HttpGet] public async Task ClearData() { await plcService.ClearData(); } /// /// 设置热缩机温度 /// /// public async Task GetTemperature() { return await plcService.GetTemperatureAsync(); } /// /// 整箱码垛打印标签 /// /// /// [HttpPost] public async Task BoxsPalletPrint(FullPalletInput input) { var produce = await produceRepository.FirstOrDefaultAsync(x => x.IfUse == 1); var boxQuery = await boxRepository.GetQueryableAsync(); var boxs = boxQuery.Where(x => x.DataType == 0 && x.IsDelete == 0).OrderBy(x => x.Id).Take(input.Num).ToList(); if (produce == null) throw Oops.Oh("没有生产设置"); if (boxs.Count < input.Num) { logger.LogWarning($"没有{input.Num}条{produce.Type}的整箱的信息,只有{boxs.Count}条,无法创建码垛信息"); throw Oops.Oh($"没有{input.Num}条{produce.Type}的整箱的信息,只有{boxs.Count}条,无法创建码垛信息"); //return; } #region 生成自定义Code var netWeight = boxs.Sum(x => x.Net_Weight); var date = DateOnly.Parse(DateTime.Now.ToString("yyyy-MM-dd")); var autolabel = await autoRepository.FirstOrDefaultAsync(x => x.Date == date && x.Type == (int)AutoLabelType.成箱码垛流水号); var timestr = DateTime.Now.ToString("yyyyMMddHHmmss"); if (autolabel == null) { autolabel = await autoRepository.InsertAsync(new AutoLabel() { Date = date, Sort = 0, Mark = timestr, Type = (int)AutoLabelType.成箱码垛流水号 }, true); } autolabel.Sort += 1; logger.LogInformation("整箱码垛生成Code" + timestr + autolabel.Sort.ToString().PadLeft(4, '0')); await autoRepository.UpdateAsync(autolabel); #endregion var box = new Box() { Dom_Time = DateTime.Now, Type = produce.Type, Exp_Time = produce.Exp_Time, Qty = input.Num, Length = produce.Length, Lot_No = produce.Lot_No, //Code = Guid.NewGuid().ToString().Substring(9, 18), Code = timestr + autolabel.Sort.ToString().PadLeft(4, '0'), Net_Weight = netWeight, Spec = produce.Spec, IsUse = true, IsDelete = 0, DataType = (int)BoxDataType.成箱码垛, Create_Time = DateTime.Now, Mark = "整箱手动码垛,码垛整箱数量" + input.Num }; var Box = await boxRepository.InsertAsync(box, true); logger.LogInformation($"已添加码垛信息,码垛编号:{box.Code},包含整箱数量:{boxs.Count}"); List list = new List(); foreach (var item in boxs) { item.DataType = (int)BoxDataType.已码垛; item.Mark = "已码垛到编号" + Box.Code + "中,码垛数据编号ID" + Box.Id; list.Add(item); } await boxRepository.UpdateManyAsync(list); #region 打印标签 //打印标签 BarTenderHelper.BoxPrint(box); logger.LogInformation($"打印条码{box.Code},成箱码垛标签打印成功"); //throw Oops.Oh("成箱码垛标签打印成功"); #endregion } /// /// 单品码垛打印标签 /// /// /// [HttpPost] public async Task SilksPalletPrint(FullPalletInput input) { var produce = await produceRepository.FirstOrDefaultAsync(x => x.IfUse == 1); var silksQuery = await silkRepository.GetQueryableAsync(); var silks = silksQuery.Where(x => x.IsDelete == 0 && x.Status == (int)SilkStatus.已称重).OrderBy(x => x.Id).Take(input.Num).ToList(); if (produce == null) throw Oops.Oh("没有生产设置"); if (silks.Count < input.Num) { logger.LogWarning($"没有{input.Num}条{produce.Type}的单品的信息,只有{silks.Count}条,无法创建码垛信息"); throw Oops.Oh($"没有{input.Num}条{produce.Type}的单品的信息,只有{silks.Count}条,无法创建码垛信息"); //return; } var netWeight = silks.Sum(x => x.Net_Weight); var weight = silks.Average(x => x.Net_Weight); #region 自动生成成箱码垛流水号 var date = DateOnly.Parse(DateTime.Now.ToString("yyyy-MM-dd")); var autolabel = await autoRepository.FirstOrDefaultAsync(x => x.Date == date && x.Type == (int)AutoLabelType.成箱码垛流水号); var timestr = DateTime.Now.ToString("yyyyMMddHHmmss"); if (autolabel == null) { autolabel = await autoRepository.InsertAsync(new AutoLabel() { Date = date, Sort = 0, Mark = timestr, Type = (int)AutoLabelType.成箱码垛流水号 }, true); } autolabel.Sort += 1; logger.LogInformation("丝锭码垛生成Code" + timestr + autolabel.Sort.ToString().PadLeft(4, '0')); await autoRepository.UpdateAsync(autolabel); #endregion var box = new Box() { Dom_Time = DateTime.Now, Type = produce.Type, Exp_Time = produce.Exp_Time, Qty = input.Num, Length = produce.Length, Lot_No = produce.Lot_No, //Code = Guid.NewGuid().ToString().Substring(9, 18), Code = timestr + autolabel.Sort.ToString().PadLeft(4, '0'), Net_Weight = netWeight, Spec = produce.Spec, IsUse = true, IsDelete = 0, DataType = (int)BoxDataType.单品码垛, Create_Time = DateTime.Now, Mark = "单品手动码垛,码垛单品数量" + input.Num }; var Box = await boxRepository.InsertAsync(box, true); logger.LogInformation($"已添加码垛信息,码垛编号:{box.Code},包含单品数量:{silks.Count}"); List list = new List(); foreach (var silk in silks) { silk.Status = 3; silk.BoxId = Box.Id; silk.Status_Details = "已码垛"; list.Add(silk); } await silkRepository.UpdateManyAsync(list); #region 打印标签 //打印标签 BarTenderHelper.BoxPrint(box); logger.LogInformation($"打印条码{box.Code},丝锭码垛标签打印成功"); //throw Oops.Oh("丝锭码垛标签打印成功"); #endregion } } }