Example #1
0
        public static OpResult Push(Push model, List <int> typeList)
        {
            var saveResult = Save(model, typeList);

            if (!typeList.Any())
            {
                return(OpResult.Fail("必须选择至少一种推送方式!"));
            }
            if (typeList.Any(a => a == 0))  //移除“全部”项
            {
                typeList.RemoveAll(a => a == 0);
            }

            var members = MembersService.GetList();

            try
            {
                foreach (var type in typeList)
                {
                    var membersOfType = members;
                    switch (type)
                    {
                    case 153:     //手机号
                        membersOfType = membersOfType.Where(a => a.MobilePhone != null && a.MobilePhone != "").ToList();
                        break;

                    case 154:     //Email
                        membersOfType = membersOfType.Where(a => a.Email != null && a.Email != "").ToList();
                        break;
                        //case 155:
                        //    membersOfType = membersOfType.Where(a => a.Weixin != null && a.Weixin != "").ToList();
                        //    break;
                    }

                    if (membersOfType.Any())
                    {
                        List <PushWithMember> pwms = new List <PushWithMember>();
                        foreach (var member in membersOfType)
                        {
                            var pwm = new PushWithMember()
                            {
                                PushId   = model.PushId,
                                MemberId = member.MemberId,
                                Channel  = Convert.ToInt16(type),
                                State    = 0 //待推送
                            };
                            pwms.Add(pwm);
                        }
                        var count = pushDal.InsertPushWithMemberMappings(pwms);
                    }
                }
            }
            catch (Exception e)
            {
                return(OpResult.Fail("处理异常!" + e.Message));
            }

            return(saveResult);
        }
        public static OpResult Import(ImportSet obj, System.Web.HttpFileCollectionBase httpFiles, string fieldName, string columnName)
        {
            var op    = new OpResult();
            var errLs = new List <string>();
            int count = 0;

            try
            {
                //防伪码在同商户下唯一,防止重复
                SecurityCodes = BaseService <MembershipCard> .CurrentRepository.Entities.Where(o => o.CompanyId == CommonService.CompanyId).Select(o => o.SecurityCode).ToList();

                Dictionary <string, char> fieldCols = null;
                DataTable dt = null;
                op = ImportSetService.ImportSet(obj, httpFiles, fieldName, columnName, ref fieldCols, ref dt);
                if (!op.Successed)
                {
                    return(op);
                }

                var cardsnIdx  = fieldCols.GetValue("CardSN").ToType <int>() - 65;
                var balanceIdx = fieldCols.GetValue("Balance").ToType <int>() - 65;
                var nameIdx    = fieldCols.GetValue("MemberId").ToType <int>() - 65;
                var mobileIdx  = fieldCols.GetValue("MobilePhone").ToType <int>() - 65;
                count = dt.Rows.Count;
                if (cardsnIdx < 0 || balanceIdx < 0)
                {
                    errLs.Add("卡号或余额列不能为空!");
                    dt.Rows.Clear();
                    count = errLs.Count;
                }
                var cardsns = new List <string>();
                var names   = new List <string>();
                var mobiles = new List <string>();
                foreach (DataRow dr in dt.Rows)
                {
                    var cardsn = dr.GetValue(cardsnIdx).ToString();
                    var name   = dr.GetValue(nameIdx).ToString();
                    var mobile = dr.GetValue(mobileIdx).ToString();
                    if (!cardsn.IsNullOrEmpty() && !cardsns.Contains(cardsn))
                    {
                        cardsns.Add(cardsn);
                    }
                    if (!name.IsNullOrEmpty() && !names.Contains(name))
                    {
                        names.Add(name);
                    }
                    if (!mobile.IsNullOrEmpty() && !mobiles.Contains(mobile))
                    {
                        mobiles.Add(mobile);
                    }
                }

                var cards   = new List <MembershipCard>();
                var members = new List <Members>();
                if (cardsns.Any())
                {
                    cards = CurrentRepository.QueryEntity.Where(o => o.CompanyId == CommonService.CompanyId && cardsns.Contains(o.CardSN)).ToList();
                }
                if (names.Any() || mobiles.Any())
                {
                    members = MembersService.FindList(o => names.Contains(o.RealName) || mobiles.Contains(o.MobilePhone));
                }

                var storeId = System.Web.HttpContext.Current.Request["StoreId"];
                for (int i = dt.Rows.Count - 1; i >= 0; i--)
                {
                    var     dr   = dt.Rows[i];
                    var     text = dr.GetValue(cardsnIdx).ToString().Trim();
                    decimal val  = 0;
                    if (text.IsNullOrEmpty())
                    {
                        dt.Rows.RemoveAt(i);//去除不导入
                        continue;
                    }
                    else if (!decimal.TryParse(text, out val))
                    {
                        errLs.Add("卡号[" + text + "]不合法!");
                        dt.Rows.RemoveAt(i);//去除不导入
                        continue;
                    }
                    else if (cards.Any(o => o.CardSN == text) && System.Web.HttpContext.Current.Request["updateBalance"] != "on")
                    {
                        errLs.Add("卡号[" + text + "]已存在!");
                        dt.Rows.RemoveAt(i);//去除不导入
                        continue;
                    }
                    else
                    {
                        cards.Add(new MembershipCard()
                        {
                            CardSN = text
                        });
                    }
                    var name   = dr.GetValue(nameIdx).ToString();
                    var mobile = dr.GetValue(mobileIdx).ToString();
                    if (!(name.IsNullOrEmpty() || mobile.IsNullOrEmpty()))
                    {
                        var mem = members.FirstOrDefault(o => o.RealName == name && o.MobilePhone == mobile);
                        if (mem != null)
                        {
                            dr[nameIdx] = mem.MemberId;
                        }
                        else
                        {
                            if (obj.RefCreate)
                            {
                                mem = new Members()
                                {
                                    CreateDT    = DateTime.Now,
                                    CreateUID   = CurrentUser.UID,
                                    RealName    = name,
                                    MobilePhone = mobile,
                                    Sex         = -1,
                                    Status      = 1,
                                    StoreId     = storeId.IsNullOrEmpty() ? "00" : storeId,
                                    CompanyId   = obj.CompanyId,
                                    MemberNo    = CommonRules.MemberNum("00"),
                                    MemberId    = CommonRules.GUID
                                };
                                MembersService.Add(mem);
                                dr[nameIdx] = mem.MemberId;
                            }
                        }
                    }
                }
                fieldCols.Remove("MobilePhone");
                var cardTypeId = System.Web.HttpContext.Current.Request["CardTypeId"];
                var prefix     = DateTime.Now.ToString("yyMMdd");
                var maxsn      = CurrentRepository.QueryEntity.Where(o => o.CompanyId == obj.CompanyId && o.BatchSN.StartsWith(prefix)).Select(o => o.BatchSN).Max();
                int sn         = 1;
                if (!maxsn.IsNullOrEmpty())
                {
                    sn = int.Parse(maxsn.Substring(maxsn.Length - 2, 2)) + 1;
                    if (sn >= 100)
                    {
                        errLs.Add("当天制卡批次号已使用完!");
                        dt.Rows.Clear();
                    }
                }
                prefix = prefix + sn.ToString("00");
                StringBuilder sb = new StringBuilder();
                sb.Append("begin tran ");
                foreach (DataRow dr in dt.Rows)
                {
                    //判断卡号是否操作
                    var cardSn = dr.GetValue(cardsnIdx).ToString().Trim();
                    //var isExit = CurrentRepository.Entities.Any(o => o.CompanyId == CommonService.CompanyId && o.CardSN == cardSn);
                    //判断是否只做更新动作 重复的卡号做余额更新动作
                    if (cards.Any(o => o.CardSN == cardSn) && System.Web.HttpContext.Current.Request["updateBalance"] == "on")
                    {
                        sb.Append("UPDATE " + obj.TableName);
                        sb.Append(" SET ");
                        sb.Append("Balance= ");
                        sb.Append(dr.GetValue(balanceIdx).ToString().Trim());
                        sb.Append(" WHERE ");
                        sb.Append(" CardSN= ");
                        sb.Append("'" + cardSn + "'");
                        sb.Append(" AND ");
                        sb.Append(" CompanyId= ");
                        sb.Append(CommonService.CompanyId);
                        sb.Append(";");
                    }
                    else
                    {
                        sb.Append("insert into ");
                        sb.Append(obj.TableName);
                        sb.Append("(CompanyId,BatchSN,CardTypeId,State,CreateUID,SecurityCode,");
                        sb.Append(string.Join(",", fieldCols.Keys));
                        sb.Append(") values(");
                        sb.AppendFormat("{0},", obj.CompanyId);
                        sb.AppendFormat("'{0}',", prefix);
                        sb.AppendFormat("'{0}',", cardTypeId);
                        sb.AppendFormat("1,");
                        sb.AppendFormat("'{0}',", CurrentUser.UID);
                        sb.AppendFormat("'{0}',", GetSecurityCode);
                        foreach (var de in fieldCols)
                        {
                            var index = Convert.ToInt32(de.Value) - 65;
                            try
                            {
                                var text = dr[index].ToString();
                                sb.Append("'" + text + "',");
                            }
                            catch (Exception e)
                            {
                                throw new Exception("列选择超过范围!", e);
                            }
                        }
                        sb = sb.Remove(sb.Length - 1, 1);
                        sb.Append(");");
                    }
                    sn++;
                }
                if (dt.Rows.Count > 0)
                {
                    sb.Append("insert into ");
                    sb.Append("MakingMembershipCard");
                    sb.Append("(CompanyId,BatchSN,CardTypeId,State,CreateUID,MakeNumber");
                    sb.Append(") values(");
                    sb.AppendFormat("{0},'{1}','{2}',{3},'{4}','{5}')", obj.CompanyId, prefix, cardTypeId, 1, CurrentUser.UID, dt.Rows.Count);

                    sb.Append(" commit tran");
                    op.Successed = new Pharos.Logic.DAL.CommonDAL()._db.ExecuteNonQueryText(sb.ToString(), null) > 0;
                    if (op.Successed)
                    {
                        Log.WriteInsert("会员卡导入", Sys.LogModule.会员管理);
                    }
                    ("会员卡导入:" + sb.ToString()).ToLog();
                }
            }
            catch (Exception ex)
            {
                op.Message   = ex.Message;
                op.Successed = false;
                Log.WriteError(ex);
                errLs.Add("导入出现异常!");
            }
            return(CommonService.GenerateImportHtml(errLs, count));
        }