Пример #1
0
        /// <summary>
        /// 生成会员卡
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public OpResult GenerateMemberCard(string ids)
        {
            try
            {
                var datas = _service.GetMakingMembershipCardByIds(ids);

                if (datas == null || datas.Count == 0)
                {
                    return(new OpResult()
                    {
                        Successed = false, Message = "找不到原数据!"
                    });
                }
                //var sw = new Stopwatch();

                var securityCodes = BaseService <MembershipCard> .CurrentRepository.Entities.Where(o => o.CompanyId == CommonService.CompanyId).Select(o => o.SecurityCode).ToList();

                var cardTypeIds  = datas.Select(o => o.CardTypeId).ToList();
                var cardTypeInfo = BaseService <CardInfo> .CurrentRepository.Entities.Where(o => cardTypeIds.Contains(o.CardTypeId)).ToDictionary(o => o.CardTypeId, o => o);

                var areaCode = string.Empty;

                //统计当前的所有批次
                var batchs = string.Empty;
                //生成相应会员卡信息
                foreach (var item in datas)
                {
                    //查询当前cid区号
                    if (string.IsNullOrEmpty(areaCode))
                    {
                        var companyEntity = BaseService <OMS_CompanyAuthorize> .CurrentRepository.Entities.FirstOrDefault(o => o.Code == item.CompanyId);

                        if (companyEntity == null)
                        {
                            areaCode = "0000";
                        }
                        else
                        {
                            if (!string.IsNullOrEmpty(companyEntity.AreaCode))
                            {
                                areaCode = companyEntity.AreaCode;
                            }
                            else
                            {
                                areaCode = "0000";
                            }
                        }
                    }
                    //判断状态 0=未制卡
                    if (item.State == 0)
                    {
                        batchs += "," + item.BatchSN;
                        //生成会员卡数据
                        var membershipCards = new List <MembershipCard>();

                        var         resetSwiftNumber = 0;
                        SwiftNumber swiftNumber      = new SwiftNumber(string.Format("MembershipCard_{0}", item.CompanyId), SwiftNumberMode.Normal);
                        var         swiftNumbers     = swiftNumber.GetNumberRanges(item.MakeNumber, out resetSwiftNumber);
                        //sw.Start();
                        var indexCard = string.Empty;
                        for (int i = 0; i < item.MakeNumber; i++)
                        {
                            var securitycode = ExtendHelper.GetRandomStr(4, (o) => { return(securityCodes.Contains(o)); });
                            securityCodes.Add(securitycode);
                            //1位卡类型   4位区号  7位商户号  7位随机数
                            StringBuilder cardSn = new StringBuilder();
                            if (cardTypeInfo[item.CardTypeId].CardType.Contains(","))
                            {
                                indexCard = "3";
                            }
                            else
                            {
                                indexCard = cardTypeInfo[item.CardTypeId].CardType;
                            }
                            cardSn.Append(indexCard);
                            cardSn.Append(areaCode);
                            cardSn.Append(item.CompanyId.ToString("0000000"));
                            cardSn.Append(swiftNumbers[i].ToString("0000000"));
                            MembershipCard membershipCard = new MembershipCard()
                            {
                                CompanyId     = item.CompanyId,
                                BatchSN       = item.BatchSN,
                                CardSN        = cardSn.ToString(),
                                CardTypeId    = item.CardTypeId,
                                ReChargeTotal = 0m,
                                GiveTotal     = 0m,
                                Balance       = cardTypeInfo[item.CardTypeId].DefaultPrice,
                                Deposit       = 0m,
                                State         = 0,
                                ExpiryStart   = item.ExpiryStart,
                                ExpiryEnd     = item.ExpiryEnd,
                                CreateDT      = DateTime.Now,
                                CreateUID     = Sys.CurrentUser.UID,
                                SecurityCode  = securitycode,
                            };
                            membershipCards.Add(membershipCard);
                        }
                        //更新批次状态
                        _service.UpdateState(1, item.Id.ToString());

                        BluckHelper.BulkInsertAll <MembershipCard>(membershipCards);//5000 0.9767s
                        //BaseService<MembershipCard>.CurrentRepository.AddRange(membershipCards);//5000条1.08分钟

                        swiftNumber.Reset(resetSwiftNumber);
                        //sw.Stop();
                    }
                }
                //var date = sw.Elapsed;
                return(new OpResult()
                {
                    Successed = true, Message = "操作成功!"
                });
            }
            catch (Exception e)
            {
                return(new OpResult()
                {
                    Successed = false, Message = e.Message
                });
            }
        }
Пример #2
0
        /// <summary>
        /// 生成优惠券
        /// </summary>
        /// <param name="id">优惠券Id</param>
        /// <param name="batchSN">批次号</param>
        /// <param name="num">生成数量</param>
        /// <returns>生成结果</returns>
        public OpResult GenerateCoupon(string id, string batchSN, int num)
        {
            OpResult result = new OpResult()
            {
                Successed = false, Message = "生成失败!"
            };

            if ((!string.IsNullOrEmpty(batchSN)) && num > 0)
            {
                TransactionOptions transactionoptions = new TransactionOptions();
                transactionoptions.Timeout = new TimeSpan(0, 10, 0);//事务超时:10分钟
                try
                {
                    using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, transactionoptions))
                    {
                        var ranArray = GetRandString(3, num);//3位不重复的随机字符串数组,数组长度为num
                        if (ranArray[0] == "错误")
                        {
                            return(result);
                        }
                        object obj = new object();
                        lock (obj)
                        {
                            var couponList = new List <CouponCardDetail>();
                            for (int i = 0; i < num; i++)
                            {
                                CouponCardDetail model = new CouponCardDetail();
                                model.CreateDT  = DateTime.Now;
                                model.CreateUID = Sys.CurrentUser.UID;
                                model.CompanyId = CommonService.CompanyId;
                                model.BatchSN   = batchSN;
                                var serialNo = (i + 1).ToString().PadLeft(5, '0');
                                model.TicketNo     = batchSN + serialNo; //券号:8位批次号 + 5位序号,示例:1608160100001
                                model.SecurityCode = ranArray[i];        //防伪码:3位英文字母随机组合,同一批次内唯一
                                model.State        = 0;
                                couponList.Add(model);
                            }
                            BluckHelper.BulkInsertAll <CouponCardDetail>(couponList);//往数据库插入5000条数据约耗时:0.9s
                            result = SetCouponState(id, 1);
                            //result = BaseService<CouponCardDetail>.AddRange(couponList);//往数据库插入5000条数据约耗时:90s
                            //if (result.Successed)
                            //{
                            //    result = SetCouponState(id, 1);
                            //}
                        }
                        scope.Complete();
                    }
                }
                catch (Exception)
                {
                    return(result);
                }
                if (result.Successed)
                {
                    return(new OpResult()
                    {
                        Successed = true, Message = "生成成功!"
                    });
                }
                else
                {
                    return(new OpResult()
                    {
                        Successed = false, Message = "生成失败!"
                    });
                }
            }
            else
            {
                return(result);
            }
        }