Exemplo n.º 1
0
        public ResResultModel SaveAssetInStore(AssetInStoreFmModel model)
        {
            try
            {
                if (model == null)
                {
                    return(ResResult.Response(false, "未获取到任何可保存的数据", ""));
                }

                var modelInfo = new AssetInStoreInfo();

                Guid gId = Guid.Empty;
                if (model.CategoryId != null)
                {
                    Guid.TryParse(model.CategoryId.ToString(), out gId);
                }
                modelInfo.CategoryId = gId;
                if (model.UseCompanyId != null)
                {
                    Guid.TryParse(model.UseCompanyId.ToString(), out gId);
                }
                modelInfo.UseCompanyId = gId;
                if (model.UseDepmtId != null)
                {
                    Guid.TryParse(model.UseDepmtId.ToString(), out gId);
                }
                modelInfo.UseDepmtId = gId;
                if (model.RegionId != null)
                {
                    Guid.TryParse(model.RegionId.ToString(), out gId);
                }
                modelInfo.RegionId = gId;
                if (model.OwnedCompanyId != null)
                {
                    Guid.TryParse(model.OwnedCompanyId.ToString(), out gId);
                }
                modelInfo.OwnedCompanyId = gId;
                if (model.PictureId != null)
                {
                    Guid.TryParse(model.PictureId.ToString(), out gId);
                }
                modelInfo.PictureId = gId;

                DateTime time = DateTime.MinValue;
                DateTime.TryParse(model.SBuyDate, out time);

                modelInfo.Barcode         = model.Barcode;
                modelInfo.Named           = model.Named;
                modelInfo.SpecModel       = model.SpecModel;
                modelInfo.SNCode          = model.SNCode;
                modelInfo.Unit            = model.Unit;
                modelInfo.Price           = model.Price;
                modelInfo.BuyDate         = time == DateTime.MinValue ? DateTime.Parse("1754-01-01") : time;
                modelInfo.UsePerson       = model.UsePerson;
                modelInfo.Manager         = model.Manager;
                modelInfo.StoreLocation   = model.StoreLocation;
                modelInfo.UseExpireMonth  = model.UseExpireMonth;
                modelInfo.Supplier        = model.Supplier;
                modelInfo.Remark          = model.Remark;
                modelInfo.LastUpdatedDate = DateTime.Now;
                modelInfo.UserId          = Guid.Parse(SecurityService.GetUserId().ToString());

                if (model.Id != null)
                {
                    Guid.TryParse(model.Id.ToString(), out gId);
                }
                modelInfo.Id = gId;

                var bll    = new AssetInStore();
                int effect = -1;

                if (modelInfo.Id.Equals(Guid.Empty))
                {
                    effect = bll.Insert(modelInfo);
                }
                else
                {
                    effect = bll.Update(modelInfo);
                }
                if (effect < 1)
                {
                    return(ResResult.Response(false, "操作失败,数据库操作异常,请稍后再重试", ""));
                }

                return(ResResult.Response(true, "操作成功", ""));
            }
            catch (Exception ex)
            {
                return(ResResult.Response(false, "操作异常:" + ex.Message + "", ""));
            }
        }
Exemplo n.º 2
0
        private void ImportByAssetInStore(HttpContext context, HttpPostedFile file)
        {
            //NpoiHelper npoi = new NpoiHelper();
            //var dt = npoi.GetExcelData(Path.GetExtension(file.FileName), file.InputStream);
            var dt  = new DataTable();
            var drc = dt.Rows;

            if (drc.Count == 0)
            {
                context.Response.Write(ResResult.ResJsonString(false, "导入的数据不能为空字符串", ""));
                return;
            }
            Category cBll  = new Category();
            OrgDepmt odBll = new OrgDepmt();
            Region   rBll  = new Region();
            decimal  d     = decimal.MinValue;
            DateTime time  = DateTime.MinValue;
            var      list  = new List <AssetInStoreInfo>();

            foreach (DataRow dr in drc)
            {
                if (string.IsNullOrWhiteSpace(dr["资产条码"].ToString()) || string.IsNullOrWhiteSpace(dr["资产名称"].ToString()) || string.IsNullOrWhiteSpace(dr["资产类别编码"].ToString()) || string.IsNullOrWhiteSpace(dr["使用公司编码"].ToString()) || string.IsNullOrWhiteSpace(dr["存放地点"].ToString()) || string.IsNullOrWhiteSpace(dr["所属公司编码"].ToString()) || string.IsNullOrWhiteSpace(dr["购入时间"].ToString()))
                {
                    throw new ArgumentException("带有“*”的列为必填项,请正确操作");
                }
                var model = new AssetInStoreInfo();
                model.Barcode = dr["资产条码"].ToString();
                model.Named   = dr["资产名称"].ToString();
                var categoryModel = cBll.GetModelByCode(dr["资产类别编码"].ToString().Trim());
                if (categoryModel == null)
                {
                    throw new ArgumentException("资产类别编码“" + dr["资产类别编码"].ToString().Trim() + "”对应的数据不存在或已被删除");
                }
                model.CategoryId = categoryModel.Id;

                if (!decimal.TryParse(dr["金额"].ToString(), out d))
                {
                    throw new ArgumentException("金额“" + dr["金额"].ToString() + "”不正确");
                }
                model.Price     = d;
                model.SpecModel = dr["规格型号"].ToString();
                model.Unit      = dr["计量单位"].ToString();

                var useCompanyModel = odBll.GetModelByCode(dr["使用公司编码"].ToString());
                if (useCompanyModel == null)
                {
                    throw new ArgumentException("使用公司编码“" + dr["使用公司编码"].ToString() + "”对应的数据不存在或已被删除");
                }
                var orgdModel = odBll.GetModelByCode(dr["使用部门编码"].ToString());
                if (orgdModel == null)
                {
                    throw new ArgumentException("使用部门编码“" + dr["使用部门编码"].ToString() + "”对应的数据不存在或已被删除");
                }

                model.UseCompanyId = useCompanyModel.Id;
                model.UseDepmtId   = orgdModel.Id;

                var rModel = rBll.GetModelByCode(dr["区域编码"].ToString());
                if (rModel == null)
                {
                    throw new ArgumentException("区域编码“" + dr["区域编码"].ToString() + "”对应的数据不存在或已被删除");
                }
                model.RegionId = rModel.Id;

                var ownedCompanyModel = odBll.GetModelByCode(dr["所属公司编码"].ToString());
                if (ownedCompanyModel == null)
                {
                    throw new ArgumentException("所属公司编码“" + dr["所属公司编码"].ToString() + "”对应的数据不存在或已被删除");
                }
                model.OwnedCompanyId = ownedCompanyModel.Id;

                model.UsePerson     = dr["使用人"].ToString();
                model.StoreLocation = dr["存放地点"].ToString();
                model.Manager       = dr["管理员姓名"].ToString();
                if (!DateTime.TryParse(dr["购入时间"].ToString(), out time))
                {
                    throw new ArgumentException("购入时间“" + dr["购入时间"].ToString() + "”不正确");
                }
                model.BuyDate  = time;
                model.Supplier = dr["供应商"].ToString();
                model.Remark   = dr["备注"].ToString();
                model.SNCode   = dr["SN号"].ToString();

                model.UseExpireMonth  = 1200;
                model.PictureId       = Guid.Empty;
                model.LastUpdatedDate = DateTime.Now;

                list.Add(model);
            }

            AssetInStore aisBll = new AssetInStore();
            var          index  = 0;
            var          userId = WebCommon.GetUserId();

            foreach (var model in list)
            {
                model.UserId = userId;
                if (aisBll.Insert(model) < 1)
                {
                    throw new ArgumentException(string.Format("{0}", index > 0 ? "部分数据已经成功导入,但是执行到第“" + index + "”行时发生异常" : "数据导入失败,行“" + index + "”发生异常"));
                }
                index++;
            }
            context.Response.Write(ResResult.ResJsonString(true, "导入成功", ""));
        }