public virtual bool Edit(ref ValidationErrors errors, WMS_Feed_ListModel model)
        {
            try
            {
                WMS_Feed_List entity = m_Rep.GetById(model.Id);
                if (entity == null)
                {
                    errors.Add(Resource.Disable);
                    return(false);
                }
                entity.Id                = model.Id;
                entity.FeedBillNum       = model.FeedBillNum;
                entity.ReleaseBillNum    = model.ReleaseBillNum;
                entity.Department        = model.Department;
                entity.AssemblyPartId    = model.AssemblyPartId;
                entity.SubAssemblyPartId = model.SubAssemblyPartId;
                entity.Lot               = model.Lot;
                entity.FeedQty           = model.FeedQty;
                entity.BoxQty            = model.BoxQty;
                entity.Capacity          = model.Capacity;
                entity.InvId             = model.InvId;
                entity.SubInvId          = model.SubInvId;
                entity.Remark            = model.Remark;
                entity.PrintStaus        = model.PrintStaus;
                entity.PrintDate         = model.PrintDate;
                entity.PrintMan          = model.PrintMan;
                entity.ConfirmStatus     = model.ConfirmStatus;
                entity.ConfirmMan        = model.ConfirmMan;
                entity.ConfirmDate       = model.ConfirmDate;
                entity.Attr1             = model.Attr1;
                entity.Attr2             = model.Attr2;
                entity.Attr3             = model.Attr3;
                entity.Attr4             = model.Attr4;
                entity.Attr5             = model.Attr5;
                entity.CreatePerson      = model.CreatePerson;
                entity.CreateTime        = model.CreateTime;
                entity.ModifyPerson      = model.ModifyPerson;
                entity.ModifyTime        = model.ModifyTime;
                entity.ConfirmMessage    = model.ConfirmMessage;



                if (m_Rep.Edit(entity))
                {
                    return(true);
                }
                else
                {
                    errors.Add(Resource.NoDataChange);
                    return(false);
                }
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return(false);
            }
        }
        /// <summary>
        /// 保存数据
        /// </summary>
        public virtual void SaveImportData(IEnumerable <WMS_Feed_ListModel> list)
        {
            try
            {
                using (DBContainer db = new DBContainer())
                {
                    foreach (var model in list)
                    {
                        WMS_Feed_List entity = new WMS_Feed_List();
                        entity.Id                = 0;
                        entity.FeedBillNum       = model.FeedBillNum;
                        entity.ReleaseBillNum    = model.ReleaseBillNum;
                        entity.Department        = model.Department;
                        entity.AssemblyPartId    = model.AssemblyPartId;
                        entity.SubAssemblyPartId = model.SubAssemblyPartId;
                        entity.Lot               = model.Lot;
                        entity.FeedQty           = model.FeedQty;
                        entity.BoxQty            = model.BoxQty;
                        entity.Capacity          = model.Capacity;
                        entity.InvId             = model.InvId;
                        entity.SubInvId          = model.SubInvId;
                        entity.Remark            = model.Remark;
                        entity.PrintStaus        = model.PrintStaus;
                        entity.PrintDate         = model.PrintDate;
                        entity.PrintMan          = model.PrintMan;
                        entity.ConfirmStatus     = model.ConfirmStatus;
                        entity.ConfirmMan        = model.ConfirmMan;
                        entity.ConfirmDate       = model.ConfirmDate;
                        entity.Attr1             = model.Attr1;
                        entity.Attr2             = model.Attr2;
                        entity.Attr3             = model.Attr3;
                        entity.Attr4             = model.Attr4;
                        entity.Attr5             = model.Attr5;
                        entity.CreatePerson      = model.CreatePerson;
                        entity.CreateTime        = ResultHelper.NowTime;
                        entity.ModifyPerson      = model.ModifyPerson;
                        entity.ModifyTime        = model.ModifyTime;
                        entity.ConfirmMessage    = model.ConfirmMessage;

                        db.WMS_Feed_List.Add(entity);
                    }
                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        public virtual WMS_Feed_ListModel GetById(object id)
        {
            if (IsExists(id))
            {
                WMS_Feed_List      entity = m_Rep.GetById(id);
                WMS_Feed_ListModel model  = new WMS_Feed_ListModel();
                model.Id                = entity.Id;
                model.FeedBillNum       = entity.FeedBillNum;
                model.ReleaseBillNum    = entity.ReleaseBillNum;
                model.Department        = entity.Department;
                model.AssemblyPartId    = entity.AssemblyPartId;
                model.SubAssemblyPartId = entity.SubAssemblyPartId;
                model.Lot               = entity.Lot;
                model.FeedQty           = entity.FeedQty;
                model.BoxQty            = entity.BoxQty;
                model.Capacity          = entity.Capacity;
                model.InvId             = entity.InvId;
                model.SubInvId          = entity.SubInvId;
                model.Remark            = entity.Remark;
                model.PrintStaus        = entity.PrintStaus;
                model.PrintDate         = entity.PrintDate;
                model.PrintMan          = entity.PrintMan;
                model.ConfirmStatus     = entity.ConfirmStatus;
                model.ConfirmMan        = entity.ConfirmMan;
                model.ConfirmDate       = entity.ConfirmDate;
                model.Attr1             = entity.Attr1;
                model.Attr2             = entity.Attr2;
                model.Attr3             = entity.Attr3;
                model.Attr4             = entity.Attr4;
                model.Attr5             = entity.Attr5;
                model.CreatePerson      = entity.CreatePerson;
                model.CreateTime        = entity.CreateTime;
                model.ModifyPerson      = entity.ModifyPerson;
                model.ModifyTime        = entity.ModifyTime;
                model.ConfirmMessage    = entity.ConfirmMessage;

                return(model);
            }
            else
            {
                return(null);
            }
        }
Exemplo n.º 4
0
        public bool ImportExcelData(string oper, string filePath, ref ValidationErrors errors)
        {
            bool rtn = true;

            var targetFile = new FileInfo(filePath);

            if (!targetFile.Exists)
            {
                errors.Add("导入的数据文件不存在");
                return(false);
            }

            var excelFile = new ExcelQueryFactory(filePath);

            using (XLWorkbook wb = new XLWorkbook(filePath))
            {
                //第一个Sheet
                using (IXLWorksheet wws = wb.Worksheets.First())
                {
                    //对应列头
                    excelFile.AddMapping <WMS_Feed_ListModel>(x => x.FeedBillNum, "投料单号(业务)");
                    //excelFile.AddMapping<WMS_Feed_ListModel>(x => x.ReleaseBillNum, "投料单号(系统)");
                    excelFile.AddMapping <WMS_Feed_ListModel>(x => x.Department, "投料部门");
                    excelFile.AddMapping <WMS_Feed_ListModel>(x => x.AssemblyPartCode, "总成物料");
                    excelFile.AddMapping <WMS_Feed_ListModel>(x => x.SubAssemblyPartCode, "投料物料");
                    excelFile.AddMapping <WMS_Feed_ListModel>(x => x.Lot, "批次号");
                    excelFile.AddMapping <WMS_Feed_ListModel>(x => x.FeedQty, "投料数量");
                    excelFile.AddMapping <WMS_Feed_ListModel>(x => x.BoxQty, "箱数");
                    excelFile.AddMapping <WMS_Feed_ListModel>(x => x.Capacity, "体积");
                    excelFile.AddMapping <WMS_Feed_ListModel>(x => x.InvName, "库房");
                    //excelFile.AddMapping<WMS_Feed_ListModel>(x => x.SubInvId, "子库存");
                    excelFile.AddMapping <WMS_Feed_ListModel>(x => x.Remark, "备注");
                    //excelFile.AddMapping<WMS_Feed_ListModel>(x => x.PrintStaus, "打印状态");
                    //excelFile.AddMapping<WMS_Feed_ListModel>(x => x.PrintDate, "打印时间");
                    //excelFile.AddMapping<WMS_Feed_ListModel>(x => x.PrintMan, "打印人");
                    //excelFile.AddMapping<WMS_Feed_ListModel>(x => x.ConfirmStatus, "确认状态");
                    //excelFile.AddMapping<WMS_Feed_ListModel>(x => x.ConfirmMan, "确认人");
                    //excelFile.AddMapping<WMS_Feed_ListModel>(x => x.ConfirmDate, "确认时间");
                    //excelFile.AddMapping<WMS_Feed_ListModel>(x => x.Attr1, "");
                    //excelFile.AddMapping<WMS_Feed_ListModel>(x => x.Attr2, "");
                    //excelFile.AddMapping<WMS_Feed_ListModel>(x => x.Attr3, "");
                    //excelFile.AddMapping<WMS_Feed_ListModel>(x => x.Attr4, "");
                    //excelFile.AddMapping<WMS_Feed_ListModel>(x => x.Attr5, "");
                    //excelFile.AddMapping<WMS_Feed_ListModel>(x => x.CreatePerson, "创建人");
                    //excelFile.AddMapping<WMS_Feed_ListModel>(x => x.CreateTime, "创建时间");
                    //excelFile.AddMapping<WMS_Feed_ListModel>(x => x.ModifyPerson, "修改人");
                    //excelFile.AddMapping<WMS_Feed_ListModel>(x => x.ModifyTime, "修改时间");

                    //SheetName,第一个Sheet
                    var excelContent = excelFile.Worksheet <WMS_Feed_ListModel>(0);

                    //开启事务
                    using (DBContainer db = new DBContainer())
                    {
                        var tran     = db.Database.BeginTransaction();                      //开启事务
                        int rowIndex = 0;

                        //检查数据正确性
                        foreach (var row in excelContent)
                        {
                            rowIndex += 1;
                            string errorMessage = String.Empty;
                            var    model        = new WMS_Feed_ListModel();
                            model.Id          = row.Id;
                            model.FeedBillNum = row.FeedBillNum;
                            //model.ReleaseBillNum = row.ReleaseBillNum;
                            model.Department          = row.Department;
                            model.AssemblyPartCode    = row.AssemblyPartCode;
                            model.SubAssemblyPartCode = row.SubAssemblyPartCode;
                            model.FeedQty             = row.FeedQty;
                            model.BoxQty   = row.BoxQty;
                            model.Capacity = row.Capacity;
                            model.InvName  = row.InvName;
                            model.Lot      = row.Lot;
                            //model.SubInvId = row.SubInvId;
                            model.Remark = row.Remark;
                            //model.PrintStaus = row.PrintStaus;
                            //model.PrintDate = row.PrintDate;
                            //model.PrintMan = row.PrintMan;
                            //model.ConfirmStatus = row.ConfirmStatus;
                            //model.ConfirmMan = row.ConfirmMan;
                            //model.ConfirmDate = row.ConfirmDate;
                            //model.Attr1 = row.Attr1;
                            //model.Attr2 = row.Attr2;
                            //model.Attr3 = row.Attr3;
                            //model.Attr4 = row.Attr4;
                            //model.Attr5 = row.Attr5;
                            //model.CreatePerson = row.CreatePerson;
                            //model.CreateTime = row.CreateTime;
                            //model.ModifyPerson = row.ModifyPerson;
                            //model.ModifyTime = row.ModifyTime;

                            if (!String.IsNullOrEmpty(errorMessage))
                            {
                                rtn = false;
                                errors.Add(string.Format("第 {0} 列发现错误:{1}{2}", rowIndex, errorMessage, "<br/>"));
                                wws.Cell(rowIndex + 1, excelFile.GetColumnNames("Sheet1").Count()).Value = errorMessage;
                                continue;
                            }

                            //执行额外的数据校验
                            try
                            {
                                AdditionalCheckExcelData(db, ref model);
                            }
                            catch (Exception ex)
                            {
                                rtn          = false;
                                errorMessage = ex.Message;
                                errors.Add(string.Format("第 {0} 列发现错误:{1}{2}", rowIndex, errorMessage, "<br/>"));
                                wws.Cell(rowIndex + 1, excelFile.GetColumnNames("Sheet1").Count()).Value = errorMessage;
                                continue;
                            }

                            //写入数据库
                            WMS_Feed_List entity = new WMS_Feed_List();
                            entity.Id          = model.Id;
                            entity.FeedBillNum = model.FeedBillNum;
                            //entity.ReleaseBillNum = model.ReleaseBillNum;
                            //entity.ReleaseBillNum = "TL" + DateTime.Now.ToString("yyyyMMddHHmmssff");打印时生成
                            entity.Department        = model.Department;
                            entity.AssemblyPartId    = model.AssemblyPartId;
                            entity.SubAssemblyPartId = model.SubAssemblyPartId;
                            entity.FeedQty           = model.FeedQty;
                            entity.BoxQty            = model.BoxQty;
                            entity.Capacity          = model.Capacity;
                            entity.InvId             = model.InvId;
                            entity.Lot = model.Lot;
                            //entity.SubInvId = model.SubInvId;
                            entity.Remark     = model.Remark;
                            entity.PrintStaus = "未打印";
                            //entity.PrintDate = model.PrintDate;
                            //entity.PrintMan = model.PrintMan;
                            entity.ConfirmStatus = "未确认";
                            //entity.ConfirmMan = model.ConfirmMan;
                            //entity.ConfirmDate = model.ConfirmDate;
                            //entity.Attr1 = model.Attr1;
                            //entity.Attr2 = model.Attr2;
                            //entity.Attr3 = model.Attr3;
                            //entity.Attr4 = model.Attr4;
                            //entity.Attr5 = model.Attr5;
                            //entity.CreatePerson = model.CreatePerson;
                            //entity.CreateTime = model.CreateTime;
                            //entity.ModifyPerson = model.ModifyPerson;
                            //entity.ModifyTime = model.ModifyTime;
                            entity.CreatePerson = oper;
                            entity.CreateTime   = DateTime.Now;
                            entity.ModifyPerson = oper;
                            entity.ModifyTime   = DateTime.Now;

                            db.WMS_Feed_List.Add(entity);
                            try
                            {
                                db.SaveChanges();
                            }
                            catch (Exception ex)
                            {
                                rtn = false;
                                //将当前报错的entity状态改为分离,类似EF的回滚(忽略之前的Add操作)
                                db.Entry(entity).State = System.Data.Entity.EntityState.Detached;
                                errorMessage           = ex.InnerException.InnerException.Message;
                                errors.Add(string.Format("第 {0} 列发现错误:{1}{2}", rowIndex, errorMessage, "<br/>"));
                                wws.Cell(rowIndex + 1, excelFile.GetColumnNames("Sheet1").Count()).Value = errorMessage;
                            }
                        }

                        if (rtn)
                        {
                            tran.Commit();                                      //必须调用Commit(),不然数据不会保存
                        }
                        else
                        {
                            tran.Rollback();                                        //出错就回滚
                        }
                    }
                }
                wb.Save();
            }

            return(rtn);
        }