IShipmentApp.cs 10.9 KB
using System;
using System.Linq;
using Infrastructure;
using WebRepository;

namespace WebApp
{
    /// <summary>
    /// 出库单下发接口App
    /// </summary>

    public partial class IShipmentApp : ApiApp
    {

        public IShipmentApp(IUnitWork unitWork, IAuth auth, BaseDBContext context) : base(unitWork, auth, context)
        {

        }

        public string InsertShipment(IShipmentModel shipments)
        {
            Response Response = new Response();

            if (!CheckLogin())
            {
                Response.Code = 500;
                Response.Status = false;
                Response.Message = "请先登录!";
                return JsonHelper.Instance.Serialize(Response);
            }

            try
            {
                #region 保存出库单接口主表
                IshipmentHeader headdata = _unitWork.FindSingle<IshipmentHeader>(u => u.SourceCode.Equals(shipments.shipmentHeader.SourceCode));
                shipments.shipmentHeader.ReceiveTime = DateTime.Now;
                shipments.shipmentHeader.Id = headdata?.Id;
                if (shipments.shipmentHeader.Id == null)
                {
                    if (shipments.shipmentHeader.CreateBy == null)
                    {
                        shipments.shipmentHeader.CreateBy = "wms";
                        shipments.shipmentHeader.CreateTime = DateTime.Now;
                    }
                    _unitWork.Add(shipments.shipmentHeader);
                }
                else
                {
                    if (shipments.shipmentHeader.UpdateBy == null)
                    {
                        shipments.shipmentHeader.UpdateBy = "wms";
                        shipments.shipmentHeader.UpdateTime = DateTime.Now;
                    }
                    _unitWork.Update(shipments.shipmentHeader);
                }
                #endregion

                #region 保存出库单主表
                ShipmentHeader shipmentHeader = _unitWork.FindSingle<ShipmentHeader>(u => u.SourceCode.Equals(shipments.shipmentHeader.SourceCode));
                if (shipmentHeader != null)
                {
                    if (shipmentHeader.FirstStatus != ShipmentHeaderStatus.新建)
                    {
                        throw new Exception("单据进入订单池后,不允许修改!");
                    }
                }
                else
                {
                    shipmentHeader = new ShipmentHeader();
                }

                shipmentHeader.Type = shipments.shipmentHeader.Type;
                shipmentHeader.SourceCode = shipments.shipmentHeader.SourceCode;
                shipmentHeader.WarehouseType = shipments.shipmentHeader.WarehouseType;

                shipmentHeader.Priority = (shipments.shipmentHeader.Priority == null ? 0 : shipments.shipmentHeader.Priority);
                shipmentHeader.Station = shipments.shipmentHeader.Station;
                shipmentHeader.CreateTime = shipments.shipmentHeader.CreateTime;
                shipmentHeader.CreateBy = shipments.shipmentHeader.CreateBy;
                shipmentHeader.UpdateTime = shipments.shipmentHeader.UpdateTime;
                shipmentHeader.UpdateBy = shipments.shipmentHeader.UpdateBy;

                shipmentHeader.Code = string.IsNullOrEmpty(shipmentHeader.Code) ? _unitWork.GetTaskNo(shipments.shipmentHeader.Type) : shipmentHeader.Code;
                shipmentHeader.UploadStatus = 0;
                shipmentHeader.FirstStatus = ShipmentHeaderStatus.新建;
                shipmentHeader.LastStatus = ShipmentHeaderStatus.新建;
                shipmentHeader.TotalLines = 0;
                shipmentHeader.TotalQty = 0;

                if (shipmentHeader.Id == null)
                {
                    if (shipmentHeader.CreateBy == null)
                    {
                        shipmentHeader.CreateBy = "wms";
                        shipmentHeader.CreateTime = DateTime.Now;
                    }
                    _unitWork.Add(shipmentHeader);
                }
                else
                {
                    if (shipmentHeader.UpdateBy == null)
                    {
                        shipmentHeader.UpdateBy = "wms";
                        shipmentHeader.UpdateTime = DateTime.Now;
                    }
                    _unitWork.Update(shipmentHeader);
                }
                #endregion

                short? totalLines = 0;
                decimal? qty = 0;
                foreach (var item in shipments.shipmentDetails)
                {
                    try
                    {
                        #region 保存出库单接口子表
                        IshipmentDetail data = _context.Set<IshipmentDetail>().AsQueryable().Where(u => u.SourceCode.Equals(item.SourceCode) && u.MaterialCode.Equals(item.MaterialCode)).SingleOrDefault();
                        item.ReceiveTime = DateTime.Now;
                        item.Id = data?.Id;

                        if (item.Id == null)
                        {
                            if (item.CreateBy == null)
                            {
                                item.CreateBy = "wms";
                                item.CreateTime = DateTime.Now;
                            }
                            _unitWork.Add(item);
                        }
                        else
                        {
                            if (item.UpdateBy == null)
                            {
                                item.UpdateBy = "wms";
                                item.UpdateTime = DateTime.Now;
                            }
                            _unitWork.Update(item);
                        }
                        #endregion

                        #region 保存出库单子表
                        ShipmentDetail shipmentDetail = _context.Set<ShipmentDetail>().AsQueryable().Where(u => u.SourceCode.Equals(item.SourceCode) && u.MaterialCode.Equals(item.MaterialCode)).SingleOrDefault();
                        if (shipmentDetail == null)
                        {
                            shipmentDetail = new ShipmentDetail();
                        }

                        shipmentDetail.SourceCode = item.SourceCode;
                        shipmentDetail.MaterialCode = item.MaterialCode;
                        shipmentDetail.Lot = item.Lot;
                        shipmentDetail.InventoryStatus = item.InventoryStatus;
                        shipmentDetail.Qty = item.Qty;
                        shipmentDetail.CreateTime = item.CreateTime;
                        shipmentDetail.CreateBy = item.CreateBy;
                        shipmentDetail.UpdateTime = item.UpdateTime;
                        shipmentDetail.UpdateBy = item.UpdateBy;

                        shipmentDetail.ShipmentId = shipmentHeader.Id;
                        shipmentDetail.ShipmentCode = shipmentHeader.Code;
                        shipmentDetail.QtyDivided = 0;
                        shipmentDetail.QtyCompleted = 0;
                        shipmentDetail.Price = 0;
                        shipmentDetail.Status = ShipmentHeaderStatus.新建;

                        if (shipmentDetail.Id == null)
                        {
                            if (shipmentDetail.CreateBy == null)
                            {
                                shipmentDetail.CreateBy = "wms";
                                shipmentDetail.CreateTime = DateTime.Now;
                            }
                            _unitWork.Add(shipmentDetail);
                        }
                        else
                        {
                            if (shipmentDetail.UpdateBy == null)
                            {
                                shipmentDetail.UpdateBy = "wms";
                                shipmentDetail.UpdateTime = DateTime.Now;
                            }
                            _unitWork.Update(shipmentDetail);
                        }
                        #endregion

                        totalLines += 1;
                        qty += item.Qty;
                    }
                    catch (Exception ex)
                    {
                        #region 记录出库单接口子表错误信息
                        IshipmentDetail data = _context.Set<IshipmentDetail>().AsQueryable().Where(u => u.SourceCode.Equals(item.SourceCode) && u.MaterialCode.Equals(item.MaterialCode)).SingleOrDefault();
                        if (data != null)
                        {
                            data.ErrorMessage = ex.Message;
                            if (data.UpdateBy == null)
                            {
                                data.UpdateBy = "wms";
                                data.UpdateTime = DateTime.Now;
                            }
                            _unitWork.Update(data);
                        }
                        #endregion

                        Response.Code = 500;
                        Response.Status = false;
                        Response.Message = (Response.Message == "操作成功" ? "" : Response.Message) + "\r\n" + "SourceCode:" + item.SourceCode + ",MaterialCode:" + item.MaterialCode + "同步失败:" + ex.Message;
                    }
                }

                #region 更新出库单主表汇总信息
                shipmentHeader.TotalLines += (short)_unitWork.Find<ShipmentDetail>(n => n.ShipmentCode == shipmentHeader.Code).ToList().Count; ;
                shipmentHeader.TotalQty += _unitWork.Find<ShipmentDetail>(n => n.ShipmentCode == shipmentHeader.Code).Sum(x => x.Qty); ;
                if (shipmentHeader.UpdateBy == null)
                {
                    shipmentHeader.UpdateBy = "wms";
                    shipmentHeader.UpdateTime = DateTime.Now;
                }
                _unitWork.Update(shipmentHeader);
                #endregion
            }
            catch (Exception ex)
            {
                #region 记录出库单接口子表错误信息
                try
                {
                    IshipmentHeader data = _context.Set<IshipmentHeader>().AsQueryable().Where(u => u.SourceCode.Equals(shipments.shipmentHeader.SourceCode)).SingleOrDefault();
                    if (data != null)
                    {
                        data.ErrorMessage = ex.Message;
                        if (data.UpdateBy == null)
                        {
                            data.UpdateBy = "wms";
                            data.UpdateTime = DateTime.Now;
                        }
                        _unitWork.Update(data);
                    }
                }
                catch (Exception)
                {
                }
                #endregion

                Response.Code = 500;
                Response.Status = false;
                Response.Message = (Response.Message == "操作成功" ? "" : Response.Message) + "\r\n" + "同步失败:" + ex.Message;
            }

            return JsonHelper.Instance.Serialize(Response);
        }
    }
}