コード例 #1
0
ファイル: MemberManageCtrl.cs プロジェクト: jollitycn/JGNet
        private void DoImport()
        {
            try
            {
                List <Member> items            = new List <Member>();
                List <Member> incorrectItems   = new List <Member>();
                List <Member> checkTel         = new List <Member>();
                List <Member> checkMemberNo    = new List <Member>(); //导入时会员卡号不为数字或英文字母的集合
                List <int>    checkMemberNoRow = new List <int>();
                DataTable     dt = NPOIHelper.FormatToDatatable(importPath, 0);
                //20190223根据模板过滤第一行的注释和列标题
                for (int i = 1; i < dt.Rows.Count; i++)
                {
                    DataRow row  = dt.Rows[i];
                    Member  item = new Member();
                    try
                    {
                        if (!ImportValidate(row))
                        {
                            //20190218过滤第一列店铺列
                            int index = 1;
                            item.Index       = i + 2;
                            item.ShopID      = importShopID;
                            item.PhoneNumber = CommonGlobalUtil.ConvertToString(row[index++]);
                            item.Name        = CommonGlobalUtil.ConvertToString(row[index++]);
                            item.Phone       = CommonGlobalUtil.ConvertToString(row[index++]);
                            try
                            {
                                item.Sex = CommonGlobalUtil.ConvertToString(row[index++]) == "男";
                            }
                            catch
                            {
                                item.Sex = false;
                            }
                            try
                            {
                                item.Balance = Convert.ToDecimal(row[index++]);
                            }
                            catch
                            {
                                item.Balance = 0;
                            }
                            try
                            {
                                item.CurrentIntegration = Convert.ToInt32(row[index++]);
                            }
                            catch
                            {
                                item.CurrentIntegration = 0;
                            }
                            try
                            {
                                item.AccruedIntegration = Convert.ToInt32(row[index++]);
                            }
                            catch
                            {
                                item.AccruedIntegration = 0;
                            }
                            try
                            {
                                item.AccruedSpend = Convert.ToDecimal(row[index++]);
                            }
                            catch
                            {
                                item.AccruedSpend = 0;
                            }
                            try
                            {
                                item.BuyCount = Convert.ToInt32(row[index++]);
                            }
                            catch
                            {
                                item.BuyCount = 0;
                            }
                            try
                            {
                                item.QuantityOfBuy = Convert.ToInt32(row[index++]);
                            }
                            catch
                            {
                                item.BuyCount = 0;
                            }
                            item.GuideName   = CommonGlobalUtil.ConvertToString(row[index++]);
                            item.GuideID     = CommonGlobalCache.GetUserIDByUserName(item.GuideName);
                            item.CreatedTime = DateTimeUtil.ConvertToDateTime(CommonGlobalUtil.ConvertToString(row[index++]));
                            item.Birthday    = DateTimeUtil.ConvertToDateTime(CommonGlobalUtil.ConvertToString(row[index++]));
                            item.State       = 1;


                            if (!(String.IsNullOrEmpty(item.Name) && String.IsNullOrEmpty(item.PhoneNumber)))
                            {
                                items.Add(item);
                            }
                            if (checkPhoneNumber(item.PhoneNumber) == false)
                            {
                                checkMemberNo.Add(item);
                                checkMemberNoRow.Add(item.Index);
                            }
                            //else
                            //{
                            //    /*验证电话号码*/
                            //    if (!IstelNumber(item.PhoneNumber)) {
                            //        checkTel.Add(item);
                            //    }
                            //    incorrectItems.Add(item);
                            //}
                        }
                    }
                    catch
                    {
                        //   incorrectItems.Add(item);
                    }
                }

                if (incorrectItems.Count > 0)
                {
                    String str = string.Empty;
                    foreach (var item in incorrectItems)
                    {
                        str += item.Index + ",";
                    }
                    str = str.Substring(0, str.LastIndexOf(","));
                    ShowError("以下行数中,会员卡号/姓名为空,请重新检查!\r\n" + str);
                    ImportFormCancel();
                    return;
                }
                if (checkMemberNo.Count > 0)
                {
                    String rowStr = string.Empty;
                    for (int i = 0; i < checkMemberNoRow.Count; i++)
                    {
                        rowStr += checkMemberNoRow[i] + ",";
                    }
                    rowStr = rowStr.Substring(0, rowStr.LastIndexOf(","));
                    ShowError("以下有效数据行数中,会员卡号格式错误,请重新检查!\r\n" + rowStr);
                    ImportFormCancel();
                    return;
                }

                /*  if (checkTel.Count > 0)
                 * {
                 *    String telstr = string.Empty;
                 *    foreach (var item in checkTel)
                 *    {
                 *        telstr += item.Index + ",";
                 *    }
                 *    telstr = telstr.Substring(0, telstr.LastIndexOf(","));
                 *    ShowError("以下行数中,手机号码不正确,请重新检查!\r\n" + telstr);
                 *     ImportFormCancel();
                 *    return;
                 * }*/

                if (items != null && items.Count > 0)
                {
                }
                else
                {
                    ShowMessage("没有数据可以导入,请检查会员信息!");
                    ImportFormCancel();
                    return;
                }

                InteractResult result = CommonGlobalCache.ServerProxy.ImportMembers(items);
                switch (result.ExeResult)
                {
                case ExeResult.Success:
                    //导入
                    ImportFormDialogResult(DialogResult.OK);
                    ShowMessage("导入成功!");
                    //importForm.DialogResult = DialogResult.OK;
                    break;

                case ExeResult.Error:
                    ShowMessage(result.Msg);
                    // ImportFormDialogResult(DialogResult.OK);
                    ImportFormCancel();
                    break;

                default:
                    break;
                }
            }

            catch (Exception ex)
            {
                ImportFormCancel();
                ShowError(ex);
            }
            finally
            {
                UnLockPage();
            }
        }