public LotteryTicketModel SellLottery(LotteryBuyer lotteryBuyer, LotteryType lotteryType)
        {
            var issue = GetlatestIssue();

            if (issue == null)
            {
                throw new Exception("没有找到本期数据。");
            }
            if (issue.Stat != (int)IssueStat.Establish)
            {
                throw new Exception("本期已经结束。");
            }
            var buyerIssue = _buyerIssueRepository.Table.Where(bi => bi.BuyerId == lotteryBuyer.Id && bi.IssueId == issue.Id).FirstOrDefault();

            if (buyerIssue == null)
            {
                buyerIssue = new BuyerIssue()
                {
                    BuyerId          = lotteryBuyer.Id,
                    IssueId          = issue.Id,
                    PurchaseQuantity = 0
                };
                _buyerIssueRepository.Insert(buyerIssue);
            }
            buyerIssue.PurchaseQuantity = buyerIssue.PurchaseQuantity + 1;
            LotteryTicketModel model = SendLotteryTicket(buyerIssue, issue, (int)lotteryType);

            return(model);
        }
示例#2
0
        /// <summary>
        /// 活的用户已经持有的当前期彩票
        /// </summary>
        /// <param name="lotteryBuyer"></param>
        /// <returns></returns>
        public IList <LotteryTicketModel> GetCurrentLotteysByBuyer(LotteryBuyer lotteryBuyer)
        {
            var issue = _issueRepository.Table.OrderByDescending(i => i.CreatedOn).FirstOrDefault();

            if (issue.Stat == (int)IssueStat.Accomplish)
            {
                throw new Exception("当前期没有生成。");
            }
            List <LotteryTicketModel> models = new List <LotteryTicketModel>();

            if (issue.PoolCmd == 0)
            {
                var lp0s = _pool0Repository.Table.Where(pl0 => pl0.BuyerId == lotteryBuyer.Id).ToList();
                foreach (var lp0 in lp0s)
                {
                    LotteryTicketModel model = new LotteryTicketModel()
                    {
                        A       = lp0.A,
                        B       = lp0.B,
                        C       = lp0.C,
                        D       = lp0.D,
                        BuyerId = lp0.BuyerId,
                        Id      = lp0.Id,
                        IssueId = issue.Id,
                        OrderOn = lp0.OrderTime,
                        PoolCmd = issue.PoolCmd,
                        Stat    = lp0.Stat,
                        Type    = lp0.Type
                    };
                    models.Add(model);
                }
            }
            else if (issue.PoolCmd == 1)
            {
                var lp1s = _pool1Repository.Table.Where(pl1 => pl1.BuyerId == lotteryBuyer.Id).ToList();
                foreach (var lp1 in lp1s)
                {
                    LotteryTicketModel model = new LotteryTicketModel()
                    {
                        A       = lp1.A,
                        B       = lp1.B,
                        C       = lp1.C,
                        D       = lp1.D,
                        BuyerId = lp1.BuyerId,
                        Id      = lp1.Id,
                        IssueId = issue.Id,
                        OrderOn = lp1.OrderTime,
                        PoolCmd = issue.PoolCmd,
                        Stat    = lp1.Stat,
                        Type    = lp1.Type
                    };
                    models.Add(model);
                }
            }
            else
            {
                throw new Exception("奖池命令错误!");
            }
            return(models);
        }
        private LotteryTicketModel SendLotteryTicket(BuyerIssue buyerIssue, LotteryIssue lotteryIssue, int sellType)
        {
            if (lotteryIssue.Stat != (int)IssueStat.Establish)
            {
                throw new Exception("当期已经结束,不再接受订单。");
            }
            Random rd0 = new Random();
            int    a0  = rd0.Next(100) % 52;
            int    b0  = rd0.Next(100) % 52;
            int    c0  = rd0.Next(100) % 52;
            int    d0  = rd0.Next(100) % 52;

            if (lotteryIssue.PoolCmd == 0)
            {
                var lp0 = GetTicketInLotteryPool0(a0, b0, c0, d0, buyerIssue.BuyerId, sellType);
                LotteryTicketModel model = new LotteryTicketModel()
                {
                    A       = lp0.A,
                    B       = lp0.B,
                    C       = lp0.C,
                    D       = lp0.D,
                    BuyerId = lp0.BuyerId,
                    PoolCmd = 0,
                    Stat    = lp0.Stat,
                    Type    = lp0.Type,
                    OrderOn = DateTime.Now
                };
                return(model);
            }
            else if (lotteryIssue.PoolCmd == 1)
            {
                var lp1 = GetTicketInLotteryPool1(a0, b0, c0, d0, buyerIssue.BuyerId, sellType);
                LotteryTicketModel model = new LotteryTicketModel()
                {
                    A       = lp1.A,
                    B       = lp1.B,
                    C       = lp1.C,
                    D       = lp1.D,
                    BuyerId = lp1.BuyerId,
                    PoolCmd = 1,
                    Stat    = lp1.Stat,
                    Type    = lp1.Type,
                    OrderOn = DateTime.Now
                };
                return(model);
            }
            throw new Exception("数据命令不匹配500244,不接受订单。");
        }