示例#1
0
        public ActionResult List(HttpPostedFileBase file)
        {
            try
            {
                Tools.Config.UploadConfig config = UploadProvice.Instance();
                SiteOption option   = config.SiteOptions["local"];
                string     filePath = option.Folder + config.Settings["file"].FilePath + DateTime.Now.ToString("yyyyMMddHHmmss")
                                      + (file.FileName.IndexOf(".xlsx") > 0 ? ".xlsx" : ".xls");

                string result = FileHelper.ValicationAndSaveFileToPath(file, filePath);
                if (result != "0")
                {
                    return(MessageBoxAndJump($"上传失败,{result}", "list"));
                }

                var    sheet = ExcelHelper.ReadExcel(filePath);
                string msg   = ExcelToList(sheet, 5);

                if (msg != "0")
                {
                    return(MessageBoxAndJump("导入失败," + GetMsg(msg), "list"));
                }
            }
            catch (Exception ex)
            {
                return(MessageBoxAndJump("导入失败:" + ex.Message.ToString(), "list"));
            }

            return(Redirect("list"));
        }
示例#2
0
        public ActionResult BatchImportExcel(HttpPostedFileBase file)
        {
            try
            {
                Tools.Config.UploadConfig config = UploadProvice.Instance();
                SiteOption option   = config.SiteOptions["local"];
                string     filePath = option.Folder + config.Settings["file"].FilePath + DateTime.Now.ToString("yyyyMMddHHmmss")
                                      + (file.FileName.IndexOf(".xlsx") > 0 ? ".xlsx" : ".xls");

                string result = FileHelper.ValicationAndSaveFileToPath(file, filePath);
                if (result != "0")
                {
                    return(MessageBoxAndJump($"上传失败,{result}", "list"));
                }

                var sheet     = ExcelHelper.ReadExcel(filePath);
                var modelList = BatchExcelToList(sheet, 1);

                if (modelList == null || modelList.Count < 1)
                {
                    return(MessageBoxAndJump("数据为空,导入失败!", "list"));
                }

                if (Bll.BllSupplier_List.Insert(modelList) < 1)
                {
                    return(MessageBoxAndJump("导入失败", "list"));
                }
            }
            catch (Exception ex)
            {
                return(MessageBoxAndJump("导入失败:" + ex.Message.ToString(), "list"));
            }

            return(Redirect("list"));
        }
示例#3
0
        public ActionResult SingleImportExcel(HttpPostedFileBase file)
        {
            try
            {
                Tools.Config.UploadConfig config = UploadProvice.Instance();
                SiteOption option   = config.SiteOptions["local"];
                string     filePath = option.Folder + config.Settings["file"].FilePath + DateTime.Now.ToString("yyyyMMddHHmmss")
                                      + (file.FileName.IndexOf(".xlsx") > 0 ? ".xlsx" : ".xls");

                string result = FileHelper.ValicationAndSaveFileToPath(file, filePath);
                if (result != "0")
                {
                    return(MessageBoxAndJump($"上传失败,{result}", "list"));
                }

                var sheet     = Tools.Tool.ExcelHelper.ReadExcel(filePath);
                var modelList = SingleExcelToList(sheet, 1, ref result);

                if (modelList == null)
                {
                    return(MessageBoxAndJump($"导入失败,{result}!", "list"));
                }

                result = Bll.BllSupplier_VendorInfo.AddVendorInfo(modelList);
                if (result != "0")
                {
                    return(MessageBoxAndJump("导入失败," + result, "list"));
                }
            }
            catch (Exception)
            {
                return(MessageBoxAndJump("导入失败:请联系管理员", "list"));
            }

            return(Redirect("list"));
        }
示例#4
0
        public ActionResult ImportProduct(HttpPostedFileBase file)
        {
            try
            {
                int contractId = RequestInt("contractId");
                if (contractId == 0)
                {
                    return(LayerAlertErrorAndReturn("请选择一个合同!"));
                }
                var contract = Bll.BllOrder_Contract.First(o => o.Id == contractId);
                if (contract == null)
                {
                    return(LayerAlertErrorAndClose("合同不存在!"));
                }

                Tools.Config.UploadConfig config = UploadProvice.Instance();
                SiteOption option   = config.SiteOptions["local"];
                string     filePath = option.Folder + config.Settings["file"].FilePath + DateTime.Now.ToString("yyyyMMddHHmmss")
                                      + (file.FileName.IndexOf(".xlsx") > 0 ? ".xlsx" : ".xls");

                string result = FileHelper.ValicationAndSaveFileToPath(file, filePath);
                if (result != "0")
                {
                    return(MessageBoxAndReturn($"上传失败,{result}!"));
                }

                var sheet = Tools.Tool.ExcelHelper.ReadExcel(filePath);
                if (sheet == null)
                {
                    return(MessageBoxAndReturn($"Excel读取失败,请检查格式!"));
                }

                int updateCount = 0;
                List <Order_ContractProduct> list = new List <Order_ContractProduct>();
                IRow row;
                for (int i = 1; i <= sheet.LastRowNum; i++) //从第二行开始读取
                {
                    row = sheet.GetRow(i);                  //第i行数据
                    if (row != null)
                    {
                        var name = row.GetCell(0).ToString();
                        if (!string.IsNullOrEmpty(name))
                        {
                            var price    = row.GetCell(1).ToString().ToDecimal();
                            var quantity = row.GetCell(2).ToString().ToInt32();
                            var model    = Bll.BllOrder_ContractProduct.First(o => o.Name == name && o.ContractId == contractId);
                            if (model != null)
                            {
                                model.Quantity  += quantity;
                                model.Price     += price;
                                model.TotalPrice = model.Quantity * model.Price;
                                if (Bll.BllOrder_ContractProduct.Update(model) > 0)
                                {
                                    updateCount++;
                                }
                            }
                            else
                            {
                                list.Add(new Order_ContractProduct()
                                {
                                    ContractId = contractId,
                                    Name       = name,
                                    Price      = price,
                                    Quantity   = quantity,
                                    TotalPrice = price * quantity,
                                    AddTime    = DateTime.Now,
                                    AdminId    = MyInfo.Id
                                });
                            }
                        }
                    }
                }
                if (Bll.BllOrder_ContractProduct.Insert(list) > 0 || updateCount > 0)
                {
                    return(MessageBoxAndReturn("导入成功!"));
                }
                else
                {
                    return(MessageBoxAndReturn("导入失败,请联系管理员!"));
                }
            }
            catch (Exception ex)
            {
                return(MessageBoxAndReturn("导入失败,请联系管理员!"));
            }
        }
        public ActionResult ImportProduct(HttpPostedFileBase file)
        {
            try
            {
                int classId = RequestInt("classId");
                if (classId == 0)
                {
                    return(LayerAlertErrorAndReturn("请选择一个分类!"));
                }
                var classModel = Bll.BllProduct_Class.First(o => o.Id == classId);
                if (classModel == null)
                {
                    return(LayerAlertErrorAndClose("分类不存在!"));
                }
                if (classModel.ChildNum > 0)
                {
                    return(LayerAlertErrorAndReturn("请选择一个最底层分类!"));
                }

                Tools.Config.UploadConfig config = UploadProvice.Instance();
                SiteOption option   = config.SiteOptions["local"];
                string     filePath = option.Folder + config.Settings["file"].FilePath + DateTime.Now.ToString("yyyyMMddHHmmss")
                                      + (file.FileName.IndexOf(".xlsx") > 0 ? ".xlsx" : ".xls");

                string result = FileHelper.ValicationAndSaveFileToPath(file, filePath);
                if (result != "0")
                {
                    return(MessageBoxAndReturn($"上传失败,{result}!"));
                }

                var sheet = Tools.Tool.ExcelHelper.ReadExcel(filePath);
                if (sheet == null)
                {
                    return(MessageBoxAndReturn($"Excel读取失败,请检查格式!"));
                }
                if (sheet.LastRowNum < 1)
                {
                    return(MessageBoxAndReturn($"Excel无数据!"));
                }

                int succuessCount        = 0;
                List <Product_Info> list = new List <Product_Info>();
                IRow row;
                for (int i = 1; i <= sheet.LastRowNum; i++) //从第二行开始读取
                {
                    row = sheet.GetRow(i);                  //第i行数据
                    if (row != null)
                    {
                        string title = row.GetCell(0)?.ToString();
                        if (string.IsNullOrEmpty(title))
                        {
                            continue;                              //标题为空,跳过
                        }
                        if (Bll.BllProduct_Info.Any(o => o.Title == title))
                        {
                            continue;                                                 //标题和老SPU已存在,跳过
                        }
                        //产品Spu
                        var    classIds = classModel.ParPath.TrimEnd(',').Split(',');
                        string spu      = Bll.BllProduct_Class.GetSpuCode(classId);
                        if (string.IsNullOrEmpty(spu))
                        {
                            continue;                            //spu编码生成失败,跳过
                        }
                        if (Bll.BllProduct_Info.IsExistsSpu(spu))
                        {
                            continue;                                       //Spu重复,跳过
                        }
                        string titleEn       = row.GetCell(1)?.ToString();
                        string oldSpu        = row.GetCell(2)?.ToString();
                        string supplierCode  = row.GetCell(3)?.ToString();
                        string supplierName  = row.GetCell(4)?.ToString();
                        string specification = row.GetCell(5)?.ToString();
                        string priceRemark   = row.GetCell(6)?.ToString();
                        string hsCode        = row.GetCell(7)?.ToString();
                        string InvoiceName   = row.GetCell(8)?.ToString();
                        string firstPic      = row.GetCell(9)?.ToString();
                        var    model         = new Product_Info()
                        {
                            ClassId_1           = classIds.Length > 0 ? classIds[0].ToInt32() : 0,
                            ClassId_2           = classIds.Length > 1 ? classIds[1].ToInt32() : 0,
                            ClassId             = classModel.Id,
                            ClassParPath        = classModel.ParPath,
                            Title               = title,
                            Title_En            = titleEn,
                            IsShow              = true,
                            Old_Spu             = oldSpu,
                            Spu                 = spu,
                            SupplierProductCode = supplierCode,
                            SupplierName        = supplierName,
                            Specification       = specification,
                            PriceRemark         = priceRemark,
                            HS_Code             = hsCode,
                            InvoiceName         = InvoiceName,
                            AdminId             = MyInfo.Id,
                            CreateDate          = DateTime.Now,
                            PriceUnit           = "$",
                            Currency            = "USD",
                            Price               = 0,
                            TaxPrice            = 0,
                            Moq                 = 1,
                            FirstPic            = ImgConfig.GetProductUrl(firstPic)
                        };
                        if (Bll.BllProduct_Info.Insert(model) > 0)
                        {
                            succuessCount++;
                        }
                    }
                }
                if (succuessCount > 0)
                {
                    return(MessageBoxAndReturn("导入成功!"));
                }
                else
                {
                    return(MessageBoxAndReturn("导入失败,请联系管理员!"));
                }
            }
            catch (Exception ex)
            {
                Tools.Tool.LogHelper.WriteLog(typeof(ProductController), ex, MyInfo.Id, MyInfo.RealName);
                return(MessageBoxAndReturn("导入失败,请联系管理员!" + ex));
            }
        }