Beispiel #1
0
        /// <summary>
        /// 导入数据到数据库
        /// </summary>
        /// <param name="informationList"></param>
        /// <param name="currentUser"></param>
        /// <param name="customItemList"></param>
        /// <param name="customItemValueList"></param>
        /// <returns></returns>
        public bool ExcelToDataBase(List <Information> informationList, Member currentUser, List <CustomItem> customItemList, List <CustomItemValue> customItemValueList)
        {
            using (var db = new DCSDBContext())
            {
                using (var trans = db.Database.BeginTransaction())
                {
                    try
                    {
                        List <Guid> guidList = new List <Guid>();
                        foreach (var item in informationList)
                        {
                            if (!_informationBLL.IsExsit(item.Phone, item.QQ, item.WebCat, currentUser.CompanyCode))
                            {
                                item.CompanyCode  = currentUser.CompanyCode;
                                item.InsertMember = currentUser.Account;
                                item.InsertTime   = item.UpdateTime = DateTime.Now;
                                item.UsageMember  = currentUser.Account;
                                item.State        = (int)InformatinState.Assigned;
                                item.IsDeleted    = false;
                                item.DataCode     = "DC" + currentUser.CompanyCode + TimeManager.GetTimeSpan() + RandomManager.GenerateRandom(5);

                                db.Set <Information>().Add(item);
                            }
                            else
                            {
                                guidList.Add(item.Id);
                            }
                        }

                        foreach (var item in customItemList)
                        {
                            if (db.Set <CustomItem>().Where(n => n.Id == item.Id).SingleOrDefault() == null)
                            {
                                db.Set <CustomItem>().Add(item);
                            }
                        }

                        foreach (var item in customItemValueList)
                        {
                            if (!guidList.Contains(item.InforId))
                            {
                                db.Set <CustomItemValue>().Add(item);
                            }
                        }

                        db.SaveChanges();
                        trans.Commit();

                        return(true);
                    }
                    catch (Exception ex)
                    {
                        LogHelper.writeLog_error(ex.Message);
                        LogHelper.writeLog_error(ex.StackTrace);

                        trans.Rollback();

                        return(false);
                    }
                }
            }
        }
Beispiel #2
0
        public ActionResult Import(Information InformationModel, List <FormModel> CustomItemModel)
        {
            AjaxResult ar = new AjaxResult();

            if (InformationModel == null)
            {
                ar.state   = ResultType.error.ToString();
                ar.message = "提交数据有误,添加新纪录失败";
                return(Json(ar, JsonRequestBehavior.AllowGet));
            }

            try
            {
                var currentUser = LoginManager.GetCurrentUser();
                if (!_informationBLL.IsExsit(InformationModel.Phone,
                                             InformationModel.QQ, InformationModel.WebCat, currentUser.CompanyCode))
                {
                    #region 注释
                    //var state = _informationBLL.AddInformation(InformationModel, currentUser.Account, currentUser.CompanyCode);
                    //if (state == OperatorState.empty)
                    //{
                    //    ar.state = ResultType.error.ToString();
                    //    ar.message = "提交的数据为空,添加新纪录失败";
                    //}
                    //else if (state == OperatorState.error)
                    //{
                    //    ar.state = ResultType.error.ToString();
                    //    ar.message = "添加新纪录失败";
                    //}
                    //else if (state == OperatorState.success)
                    //{
                    //    //ar.state = ResultType.success.ToString();
                    //    //ar.message = "添加新纪录成功";
                    //}
                    #endregion

                    using (var db = new DCSDBContext())
                    {
                        #region 开始一个事务
                        using (var trans = db.Database.BeginTransaction())
                        {
                            try
                            {
                                InformationModel.InsertMember = InformationModel.UsageMember = currentUser.Account;
                                InformationModel.State        = (int)InformatinState.UnAssigned;
                                InformationModel.InsertTime   = DateTime.Now;
                                InformationModel.UpdateTime   = DateTime.Now;
                                InformationModel.CompanyCode  = currentUser.CompanyCode;
                                InformationModel.DataCode     = "DC" + currentUser.CompanyCode + TimeManager.GetTimeSpan() + RandomManager.GenerateRandom(5);

                                db.Informations.Add(InformationModel);

                                List <CustomItem> customItemList = new List <CustomItem>();
                                _customItemBLL.GetCustomItems(currentUser.Account, ref customItemList);

                                foreach (var item in customItemList)
                                {
                                    var             cm = CustomItemModel.Where(n => n.name == item.ItemName).SingleOrDefault();
                                    CustomItemValue cv = new CustomItemValue();

                                    cv.InforId      = InformationModel.Id;
                                    cv.InsertTime   = cv.UpdateTime = DateTime.Now;
                                    cv.IsDeleted    = false;
                                    cv.ItemValue    = cm.value ?? "";
                                    cv.CustomItemId = item.Id;
                                    cv.ItemName     = item.ItemName;

                                    db.CustomItemValues.Add(cv);
                                }

                                db.SaveChanges();

                                // 修改用户的以收集数据的数量  + 1
                                currentUser.Cocount += 1;
                                db.Set <Member>().Attach(currentUser);
                                db.Entry(currentUser).State = System.Data.Entity.EntityState.Modified;

                                db.SaveChanges();

                                trans.Commit();

                                ar.state   = ResultType.success.ToString();
                                ar.message = "添加成功";
                            }
                            catch (Exception ex)
                            {
                                LogHelper.writeLog_error(ex.Message);
                                LogHelper.writeLog_error(ex.StackTrace);

                                trans.Rollback();
                                ar.state   = ResultType.error.ToString();
                                ar.message = "添加失败,数据已回滚";
                            }
                        }
                        #endregion
                    }
                }
                else
                {
                    ar.state   = ResultType.error.ToString();
                    ar.message = "已存在相同记录,添加新纪录失败";
                }
            }
            catch (Exception ex)
            {
                LogHelper.writeLog_error(ex.Message);
                LogHelper.writeLog_error(ex.StackTrace);

                ar.state   = ResultType.error.ToString();
                ar.message = "系统错误,添加新纪录失败";
            }
            return(Json(ar, JsonRequestBehavior.AllowGet));
        }