Ejemplo n.º 1
0
        public async Task <List <RecordDto> > GetRandomPrizeRecords(int prizeID, int numberOfSpinAreas)
        {
            var flag       = true;
            var currentQty = 0;
            var result     = new List <RecordDto>();
            var models     = new List <Record>();

            while (flag)
            {
                var emp = await _empRepository.FindAll(x => x.EmpCode != null).OrderBy(x => Guid.NewGuid()).FirstOrDefaultAsync();

                if (emp == null)
                {
                    continue;
                }

                var isWinner = await _recordRepository.FindAll().AnyAsync(x => x.EmpCode == emp.EmpCode);

                if (isWinner)
                {
                    continue;
                }

                var model = new Record
                {
                    EmpCode = emp.EmpCode,
                    PrizeID = prizeID,
                    Visible = true
                };
                models.Add(model);
                currentQty += 1;

                var record = new RecordDto
                {
                    EmpCode = emp.EmpCode,
                    PrizeID = prizeID,
                    Visible = true,
                    EmpDept = emp.EmpDept,
                    EmpName = emp.EmpName
                };
                result.Add(record);

                if (currentQty == numberOfSpinAreas)
                {
                    flag = false;
                }
            }

            _recordRepository.AddMultiple(models);
            try
            {
                await _recordRepository.Save();

                return(result);
            }
            catch (System.Exception)
            {
                return(new List <RecordDto>());
            }
        }