示例#1
0
        public async Task <IHttpActionResult> LotteryAsync([Required] LotteryRequest request)
        {
            //中奖名单
            var wonEmployee = await _prizeService.GetListAsync(a => a.EmployeeId, a => a.Disable == (short)BaseModel.DisableEnum.Normal);

            var notWonEmployee = await _employeeService.GetListAsync(a => a.Id, a => !wonEmployee.Contains(a.Id));

            var prizeType = await _prizeTypeService.FirstOrDefaultAsync(a => a.Id == request.Type);

            //当前类型奖品的中奖人数
            var typeCount = await _prizeService.CountAsync(a => a.PrizeTypeId == request.Type);

            if (typeCount >= prizeType.Count)
            {
                return(Fail("奖品已经抽完"));
            }
            int xunhuanr = 0;

            if ((prizeType.Count - typeCount) <= prizeType.Num)
            {
                xunhuanr = (prizeType.Count - typeCount);
            }
            else
            {
                xunhuanr = prizeType.Num;
            }
            Random     rand  = new Random();
            List <int> plist = new List <int>();

            for (int i = 0; i < xunhuanr; i++)
            {
                var employeeId = Suiji(rand, plist, notWonEmployee);
                plist.Add(employeeId);
            }
            var result = await _prizeService.AddAsync(plist.Select(a => new Prize
            {
                EmployeeId = a,
                PrizeTypeId = prizeType.Id
            }).ToList());

            return(Succeed(new LotteryResponse
            {
                EmployeeIds = plist,
                Rounds = (prizeType.Count % prizeType.Num) == 0 ? (prizeType.Count / prizeType.Num) : (prizeType.Count / prizeType.Num + 1),
                RoundsIndex = (typeCount % prizeType.Num) == 0 ? (typeCount / prizeType.Num) : (typeCount / prizeType.Num + 1)
            }, "抽取成功"));
        }