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

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

    public partial class IShipmentApp : ApiApp
    {
        public IRepository<ShipmentHeader> _app;
        public IShipmentApp(IUnitWork unitWork, IAuth auth, BaseDBContext context, IRepository<ShipmentHeader> shipmentHeader) : base(unitWork, auth, context)
        {
            _app = shipmentHeader;
        }

        public string InsertShipment(IShipmentModel shipments)
        {
            IReceiptBackModel Response = new IReceiptBackModel();
            using (var tran = _unitWork.GetDbContext().Database.BeginTransaction())
            {
                try
                {
                    //判断出库站台是否正确
                    string WareCell = "";
                    StationRoadway Staroadway = _unitWork.Find<StationRoadway>(n => n.StationCode == shipments.LWHSL).FirstOrDefault();
                    if (Staroadway != null)
                    {
                        if (Staroadway.RoadWay == 1)
                        {
                            WareCell = "TB_WareCell";
                        }
                        else if (Staroadway.RoadWay == 2)
                        {
                            WareCell = "PP_WareCell";
                        }
                        else if (Staroadway.RoadWay == 3)
                        {
                            WareCell = "PJ_WareCell";
                        }
                    }
                    else
                    {
                        Response.Status = "E";
                        Response.ErrorMsg = "站台错误,未找到" + shipments.LWHSL + "站台,无法创建任务!";
                        return JsonHelper.Instance.Serialize(Response);
                    }
                    #region 保存出库单主表
                    ApiShipmentHeader apiShipmentHeader = _unitWork.Find<ApiShipmentHeader>(n => n.SourceCode == shipments.TaskNumber).FirstOrDefault();
                    if (apiShipmentHeader != null)
                    {
                        Response.Status = "E";
                        Response.ErrorMsg = "单据重复下发!";
                        return JsonHelper.Instance.Serialize(Response);
                    }
                    else
                    {
                        apiShipmentHeader = new ApiShipmentHeader
                        {
                            Organization = shipments.Organization,
                            SourceCode = shipments.TaskNumber,
                            WarehouseType = WareCell,
                            Type = shipments.TaskType.ToString(),
                            TotalQty = shipments.Quantity,
                            Code = _unitWork.GetTaskNo(shipments.TaskType.ToString()),
                            FirstStatus = ShipmentHeaderStatus.新建,
                            LastStatus = ShipmentHeaderStatus.新建,
                            TotalLines = 1,
                            ShipTo = shipments.Supllier,
                            Station = shipments.LWHSL,
                            CreateTime = DateTime.Now,
                            CreateBy = "Api",
                            ErpListType = shipments.ErpListType,
                            Status = 0
                        };
                        _unitWork.Add(apiShipmentHeader);
                    }
                    ApiInterfaceLog Apiinter = new ApiInterfaceLog
                    {
                        Type = "ApiLK_Out",
                        AllNum = 1,
                        ComNum = 0,
                        Request = JsonHelper.Instance.Serialize(shipments),
                        Status = 0,
                        TaskNo = shipments.TaskNumber,
                        CreateTime = DateTime.Now,
                        CreateBy = "Api"
                    };
                    _unitWork.Add(Apiinter);
                    #endregion
                    #region 保存出库单子表
                    ApiShipmentDetail apiShipmentDetail = new ApiShipmentDetail();
                    apiShipmentDetail = new ApiShipmentDetail
                    {
                        SourceCode = shipments.TaskNumber,
                        MaterialCode = shipments.PN,
                        InventoryStatus = "",
                        Qty = shipments.Quantity,
                        Lot = shipments.MO,
                        CreateTime = apiShipmentHeader.CreateTime,
                        CreateBy = apiShipmentHeader.CreateBy,
                        ShipmentId = apiShipmentHeader.Id,
                        ShipmentCode = apiShipmentHeader.Code,
                        QtyDivided = 0,
                        QtyCompleted = 0,
                        Price = 0,
                        Status = ShipmentHeaderStatus.新建,
                        WH = shipments.WH,
                        WHSL = shipments.WHSL,
                        LWH = shipments.LWH,
                        LWHSL = shipments.LWHSL,
                        Project = shipments.Barcode,
                    };
                    _unitWork.Add(apiShipmentDetail);
                    #endregion

                    Response.Status = "S";
                    Response.TaskNumber = apiShipmentHeader.SourceCode;
                    tran.Commit();
                }
                catch (Exception ex)
                {
                    Response.Status = "E";
                    Response.ErrorMsg = "MES出库创建失败,Message:" + ex.Message + ",StackTrace:" + ex.StackTrace + ",Source" + ex.Source + ",Data" + ex.Data;
                    tran.Rollback();
                }
            }

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