using AutoMapper; using Newtonsoft.Json; using Newtonsoft.Json.Serialization; using SqlSugar; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using WIDESEA_Core; using WIDESEA_Core.BaseRepository; using WIDESEA_Core.BaseServices; using WIDESEA_Core.Enums; using WIDESEA_Core.Helper; using WIDESEA_Core.Utilities; using WIDESEA_DTO.Inbound; using WIDESEA_DTO.Outbound; using WIDESEA_IBasicService; using WIDESEA_IOutboundRepository; using WIDESEA_IOutboundService; using WIDESEA_IStockService; using WIDESEA_ITaskInfoRepository; using WIDESEA_Model.Models; namespace WIDESEA_OutboundService { public partial class OutboundOrderService : ServiceBase, IOutboundOrderService { private readonly IMapper _mapper; private readonly IMaterielInfoService _materielInfoService; private readonly IStockInfoService _stockService; private readonly IStockInfoDetailService _stockDetailService; private readonly IUnitOfWorkManage _unitOfWorkManage; private readonly ITaskRepository _taskRepository; private readonly SqlSugarClient _dbBase; public IOutboundOrderRepository Repository => BaseDal; public OutboundOrderService(IOutboundOrderRepository BaseDal, IMapper mapper, IMaterielInfoService materielInfoService, IStockInfoDetailService stockDetailService, IStockInfoService stockInfoService, IUnitOfWorkManage unitOfWorkManage,ITaskRepository taskRepository) : base(BaseDal) { _mapper = mapper; _materielInfoService = materielInfoService; _stockDetailService = stockDetailService; _stockService = stockInfoService; _dbBase = unitOfWorkManage.GetDbClient(); _taskRepository = taskRepository; } public override WebResponseContent AddData(SaveModel saveModel) { OutboundOrderAddDTO outboundOrder = saveModel.MainData.DicToModel(); List orderDetailAddDTOs = saveModel.DetailData.DicToIEnumerable(); #region 根据物料编号合并出库数量 //outboundOrder.Details = orderDetailAddDTOs.GroupBy(x => x.MaterielCode).Select(x => new OutboundOrderDetailAddDTO //{ // BatchNo = x.FirstOrDefault()?.BatchNo ?? "", // MaterielCode = x.Key, // OrderQuantity = x.Sum(x => x.OrderQuantity), // Remark = x.FirstOrDefault(v => !string.IsNullOrEmpty(v.Remark))?.Remark ?? "" //}).ToList(); #endregion outboundOrder.Details = new List(); outboundOrder.Details.AddRange(orderDetailAddDTOs); return AddOutboundOrder(outboundOrder); } public override WebResponseContent UpdateData(SaveModel saveModel) { List outboundOrderDetails = saveModel.DetailData.DicToIEnumerable(); if (outboundOrderDetails.GroupBy(x => x.MaterielCode).Select(x => x.Count()).Any(x => x > 1)) { return WebResponseContent.Instance.Error("物料重复"); } outboundOrderDetails = outboundOrderDetails.Where(x => (x.Id > 0 && x.OrderDetailStatus == OrderDetailStatusEnum.New.ObjToInt()) || x.Id == 0).ToList(); List> dics = new List>(); JsonSerializerSettings settings = new JsonSerializerSettings(); settings.ContractResolver = new CamelCasePropertyNamesContractResolver(); foreach (var item in outboundOrderDetails) { string str = JsonConvert.SerializeObject(item, settings); Dictionary? dic = JsonConvert.DeserializeObject>(str); if (dic != null) dics.Add(dic); } saveModel.DetailData = dics; return base.UpdateData(saveModel); } public WebResponseContent AddOutboundOrder(OutboundOrderAddDTO orderAddDTO) { WebResponseContent content = new(); try { #region 验证数据 (bool, string, object?) result = CheckOutboundOrderAddData(orderAddDTO); if (!result.Item1) return content = WebResponseContent.Instance.Error(result.Item2); #endregion Dt_OutboundOrder outboundOrder = _mapper.Map(orderAddDTO); outboundOrder.OrderNo = DateTime.Now.ToString("yyMMddHHmmss"); outboundOrder.OrderStatus = InboundStatusEnum.未开始.ObjToInt(); bool a = BaseDal.Db.InsertNav(outboundOrder).Include(x => x.Details).ExecuteCommand(); content = WebResponseContent.Instance.OK(); } catch (Exception ex) { content = WebResponseContent.Instance.Error(ex.Message); } finally { } return content; } private (bool, string, object?) CheckOutboundOrderAddData(OutboundOrderAddDTO outboundOrderAddDTO) { (bool, string, object?) result1 = ModelValidate.ValidateModelData(outboundOrderAddDTO); if (!result1.Item1) return result1; (bool, string, object?) result2 = ModelValidate.ValidateModelData(outboundOrderAddDTO.Details); if (!result2.Item1) return result2; IEnumerable inOrderTypes = Enum.GetValues().Cast(); if (!inOrderTypes.Contains(outboundOrderAddDTO.OrderType)) { return (false, "未找到该单据类型", outboundOrderAddDTO); } List materielCodes = outboundOrderAddDTO.Details.Select(x => x.MaterielCode).ToList(); if (!_materielInfoService.ExsitMateriels(materielCodes)) { return (false, "有物料信息未录入,请录入物料信息", outboundOrderAddDTO); } if (BaseDal.QueryFirst(x => x.UpperOrderNo == outboundOrderAddDTO.UpperOrderNo && !string.IsNullOrEmpty(x.UpperOrderNo)) != null) { return (false, "单据已存在", outboundOrderAddDTO); } return (true, "成功", outboundOrderAddDTO); } public WebResponseContent ReleaseOutOrder(int orderId) { WebResponseContent content = new WebResponseContent(); try { } catch (Exception ex) { } return content; } } }