IReceiptApp.cs 9.23 KB
using System;
using System.Collections.Generic;
using System.Linq;
using Infrastructure;
using WebRepository;

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

    public partial class IReceiptApp : ApiApp
    {
        public IRepository<ReceiptHeader> _app;
        public IReceiptApp(IUnitWork unitWork, IAuth auth, BaseDBContext context, IRepository<ReceiptHeader> repository) : base(unitWork, auth, context)
        {
            _app = repository;
        }

        public string InsertReceipt(IReceiptModel receipts)
        {
            IReceiptBackModel Response = new IReceiptBackModel();
            using (var tran = _unitWork.GetDbContext().Database.BeginTransaction())
            {
                try
                {
                    #region 保存入库单主表
                    ReceiptHeader receiptHeader = _unitWork.FindSingle<ReceiptHeader>(u => u.SourceCode == receipts.TaskNumber);
                    if (receiptHeader != null)
                    {
                        Response.Status = "E";
                        Response.ErrorMsg = "单据重复下发!";
                        return JsonHelper.Instance.Serialize(Response);
                    }
                    else
                    {
                        receiptHeader = new ReceiptHeader
                        {
                            Type = receipts.TaskType.ToString(),
                            SourceCode = receipts.TaskNumber,
                            SupplierCode = receipts.Supllier,
                            CreateTime = DateTime.Now,
                            CreateBy = "Api",
                            UploadStatus = 0,
                            Code = _app.GetTaskNo(receipts.TaskType.ToString()),
                            FirstStatus = ReceiptHeaderStatus.新建,
                            LastStatus = ReceiptHeaderStatus.新建,
                            TotalLines = 0,
                            TotalQty = 0,
                            WH = receipts.WH,
                            WHSL = receipts.WHSL,
                            LWH = receipts.LWH,
                            LWHSL = receipts.LWH,
                            ContainerCode = receipts.Tray,
                            ErpListType = receipts.ErpListType,
                            Status = 0,
                            Organization = receipts.Organization
                        };
                        _unitWork.Add(receiptHeader);
                    }
                    //总行数
                    short? totalLines = 0;
                    //物料总数
                    decimal? qty = 0;
                    #endregion

                    #region 保存入库单子表
                    foreach (var item in receipts.stockInDetails)
                    {
                        ReceiptDetail receiptDetail = new ReceiptDetail();
                        receiptDetail.SourceCode = receipts.TaskNumber;
                        receiptDetail.MaterialCode = receipts.PN;
                        receiptDetail.Lot = receipts.MO;
                        receiptDetail.Qty = item.Quantity;
                        receiptDetail.CreateTime = receiptHeader.CreateTime;
                        receiptDetail.CreateBy = receiptHeader.CreateBy;
                        receiptDetail.UpdateTime = receiptHeader.UpdateTime;
                        receiptDetail.UpdateBy = receiptHeader.UpdateBy;
                        receiptDetail.Project = item.Barcode;
                        receiptDetail.Batch = item.Lot;
                        receiptDetail.Grade = receipts.Grade;
                        receiptDetail.Result = receipts.Result;
                        receiptDetail.ReceiptId = receiptHeader.Id;
                        receiptDetail.ReceiptCode = receiptHeader.Code;
                        receiptDetail.QtyDivided = 0;
                        receiptDetail.QtyCompleted = 0;
                        receiptDetail.Price = 0;
                        receiptDetail.Status = ReceiptHeaderStatus.新建;
                        receiptDetail.Unit = item.Uom;
                        receiptDetail.BLot = item.Lot;

                        _unitWork.Add(receiptDetail);

                        totalLines += 1;
                        qty += item.Quantity;
                    }
                    #endregion

                    #region 更新入库单主表汇总信息
                    receiptHeader.TotalLines += totalLines;
                    receiptHeader.TotalQty += qty;
                    _unitWork.Update(receiptHeader);
                    #endregion

                    ApiInterfaceLog Apiinter = new ApiInterfaceLog
                    {
                        Type = "ApiLK_In",
                        AllNum = totalLines,
                        ComNum = 0,
                        Request = JsonHelper.Instance.Serialize(receipts),
                        Status = 0,
                        TaskNo = receipts.TaskNumber,
                        CreateTime = DateTime.Now,
                        CreateBy = "Api"
                    };
                    _unitWork.Add(Apiinter);

                    Response.Status = "S";
                    Response.TaskNumber = receiptHeader.SourceCode;
                    tran.Commit();
                }
                catch (Exception ex)
                {
                    tran.Rollback();
                    #region 记录入库单接口错误信息
                    ApiInterfaceLog Apiinter = new ApiInterfaceLog
                    {
                        Type = "ApiLK_InErr",
                        AllNum = 0,
                        ComNum = 0,
                        Request = JsonHelper.Instance.Serialize(receipts),
                        Status = 4,
                        TaskNo = receipts.TaskNumber,
                        CreateTime = DateTime.Now,
                        CreateBy = "Api"
                    };
                    #endregion

                    Response.Status = "E";
                    Response.ErrorMsg = "入库单据错误,Message:" + ex.Message + ",StackTrace:" + ex.StackTrace + ",Source:" + ex.Source + ",Data:" + ex.Data;
                    return JsonHelper.Instance.Serialize(Response);
                    
                }
            }
            return JsonHelper.Instance.Serialize(Response);
        }

        public string UpInvinspect(InspectionResultRequest UpInvinspect)
        {
            MesResponse mesResponse = new MesResponse();
            using (var tran = _unitWork.GetDbContext().Database.BeginTransaction())
            {
                try
                {
                    #region 查找并更新库存
                    List<Inventory> inventorys = _unitWork.Find<Inventory>(n => n.Lot == UpInvinspect.MO).ToList();
                    if (inventorys.Count() > 0)
                    {
                        foreach (var inventory in inventorys)
                        {
                            inventory.Result = UpInvinspect.Result;
                            inventory.Grade = UpInvinspect.Grade;
                            _unitWork.Update(inventory);
                        }

                    }
                    else
                    {
                        mesResponse.Status = "E";
                        mesResponse.ErrorMsg = "库存中未找到单号为" + UpInvinspect.MO + "的物料";
                        return JsonHelper.Instance.Serialize(mesResponse);
                    }
                    #endregion
                    //添加更改库存记录
                    ApiInterfaceLog Apiinter = new ApiInterfaceLog
                    {
                        AllNum = 0,
                        ComNum = 0,
                        Request = JsonHelper.Instance.Serialize(UpInvinspect),
                        Status = 1,
                        TaskNo = "ApiLK_Up",
                        CreateTime = DateTime.Now,
                        CreateBy = "Api",
                        Type = "更改检验成功",
                    };
                    _unitWork.Add(Apiinter);
                    mesResponse.Status = "S";
                    tran.Commit();
                }
                catch (Exception ex)
                {
                    tran.Rollback();
                    #region 记录入库单接口错误信息
                    ApiInterfaceLog Apiinter = new ApiInterfaceLog
                    {
                        AllNum = 0,
                        ComNum = 0,
                        Message = ex.Message,
                        Request = JsonHelper.Instance.Serialize(UpInvinspect),
                        Status = 1,
                        TaskNo = "",
                        CreateTime = DateTime.Now,
                        CreateBy = "Api",
                        Type = "更改检验失败",
                    };
                    _unitWork.Add(Apiinter);
                    #endregion

                    mesResponse.Status = "E";
                    mesResponse.ErrorMsg = "出库单据错误,Message:" + ex.Message + ",StackTrace:" + ex.StackTrace + ",Source:" + ex.Source + ",Data:" + ex.Data;
                    return JsonHelper.Instance.Serialize(mesResponse);

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