コード例 #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="httpFiles"></param>
        /// <param name="codeCol">条码列</param>
        /// <param name="countCol">实盘列</param>
        /// <param name="minRow">起始行</param>
        /// <param name="maxRow">截止行</param>
        /// <returns></returns>
        public static OpResult Import(TreasuryLocks obj, System.Web.HttpFileCollectionBase httpFiles, char codeCol, char countCol, int minRow, int maxRow, string checkUID)
        {
            var op = new OpResult();

            try
            {
                if (httpFiles.Count <= 0 || httpFiles[0].ContentLength <= 0)
                {
                    op.Message = "请先选择Excel文件";
                    return(op);
                }
                var stream = httpFiles[0].InputStream;
                var ext    = httpFiles[0].FileName.Substring(httpFiles[0].FileName.LastIndexOf("."));
                if (!(ext.Equals(".xls", StringComparison.CurrentCultureIgnoreCase) ||
                      ext.Equals(".xlsx", StringComparison.CurrentCultureIgnoreCase)))
                {
                    op.Message = "请先选择Excel文件";
                    return(op);
                }
                var over     = HttpContext.Current.Request["over"].ToType <short?>();
                int?checkCol = null;
                if (!HttpContext.Current.Request["CheckCol"].IsNullOrEmpty())
                {
                    checkCol = (int)Convert.ToChar(HttpContext.Current.Request["CheckCol"]);
                }
                obj.CompanyId = CommonService.CompanyId;
                var dt       = new ExportExcel().ToDataTable(stream, minRow: minRow, maxRow: maxRow);
                var codeIdx  = Convert.ToInt32(codeCol) - 65;
                var countIdx = Convert.ToInt32(countCol) - 65;
                var users    = new List <Sys.Entity.SysUserInfo>();
                if (checkCol.HasValue)
                {
                    var codes = dt.AsEnumerable().Select(o => o[checkCol.Value - 65].ToString()).Distinct().ToList();
                    users = UserInfoService.FindList(o => o.CompanyId == CommonService.CompanyId && codes.Contains(o.UserCode));
                }
                var stocks = new List <StockTakingLog>();
                var errLs  = new Dictionary <int, string>();
                int idx    = 1;
                foreach (DataRow dr in dt.Rows)
                {
                    idx++;
                    string barcode = "", number = "";
                    try
                    {
                        barcode = dr[codeIdx].ToString();
                        number  = dr[countIdx].ToString();
                        if (checkCol.HasValue)
                        {
                            var code = dr[checkCol.Value - 65].ToString();
                            if (!code.IsNullOrEmpty())
                            {
                                var user = users.FirstOrDefault(o => o.UserCode == code);
                                if (user != null)
                                {
                                    checkUID = user.UID;
                                }
                                else
                                {
                                    errLs.Add(idx, barcode + "&nbsp;&nbsp;盘点员工号不存在!");
                                    continue;
                                }
                            }
                        }
                    }
                    catch (Exception)
                    {
                        throw new Exception("列选择超过范围!");
                    }
                    if (barcode.IsNullOrEmpty())
                    {
                        continue;
                    }
                    if (number.IsNullOrEmpty() && !barcode.IsNullOrEmpty())
                    {
                        errLs.Add(idx, barcode + "&nbsp;&nbsp;实盘数量为空");
                        continue;
                    }
                    decimal num = 0;
                    if (!decimal.TryParse(number, out num) || num < 0)
                    {
                        errLs.Add(idx, barcode + "&nbsp;&nbsp;实盘数量小于零");
                        continue;
                    }

                    var st = stocks.FirstOrDefault(o => o.Barcode == barcode);
                    if (st != null)
                    {
                        st.Number += num;
                    }
                    else
                    {
                        stocks.Add(new StockTakingLog()
                        {
                            Id         = idx,
                            Barcode    = barcode,
                            Number     = num,
                            CheckBatch = obj.CheckBatch,
                            CheckUID   = checkUID,
                            CreateDT   = DateTime.Now,
                            State      = over.GetValueOrDefault(),
                            SysPrice   = 0,
                            CompanyId  = obj.CompanyId,
                            Source     = 3
                        });
                    }
                }
                op = SaveOrUpdate(obj, "[]", stocks.ToJson(), "[]", HttpContext.Current.Request["ActualDate"], 1);
                var errs = op.Data as Dictionary <int, string>;
                if (errs == null)
                {
                    errs = new Dictionary <int, string>();
                }
                foreach (var de in errs)
                {
                    errLs.Add(de.Key, de.Value);
                }
                if (errLs.Any())
                {
                    var html = "<ul><li>成功导入{0}条数据,余{1}条导入失败!</li><li><a href=\"javascript:void(0)\" onclick=\"viewErr()\">查看失败记录!</a></li></ul>";
                    op.Message  = string.Format(html, stocks.Count - errs.Count, errLs.Count);
                    op.Descript = "<dl><dt>以下数据导入失败:</dt>{0}</dl>";
                    string str = "";
                    foreach (var de in errLs)
                    {
                        str += "<dd>行" + de.Key + ":" + de.Value + "</dd>";
                    }
                    op.Descript = string.Format(op.Descript, str);
                }
                else
                {
                    op.Message = "<ul><li>成功导入" + stocks.Count + "条数据!</li></ul>";
                }
                op.Message  = System.Web.HttpUtility.UrlEncode(op.Message);
                op.Descript = System.Web.HttpUtility.UrlEncode(op.Descript);
                Log.WriteInsert("盘点导入", Sys.LogModule.库存管理);
            }
            catch (Exception ex)
            {
                op.Message = ex.Message;
                Log.WriteError(ex);
            }
            return(op);
        }