예제 #1
0
        /// <summary>
        /// 获取有效的 投票主表 数据列表.
        /// </summary>
        /// <param name="typeCode"></param>
        /// <returns></returns>
        public BallotMaster GetActiveBallotMaster(string typeCode)
        {
            try
            {
                using (MyBallotContext context = new MyBallotContext())
                {
                    // 先检查 typeCode 是否存在.
                    var ballotType = context.BallotTypes.Find(typeCode);
                    if (ballotType == null)
                    {
                        logger.WarnFormat("外部传入了无法识别的类型代码 : {0}", typeCode);
                        return(null);
                    }


                    var query =
                        from data in context.BallotMasters.Include("BallotDetailList").Include("BallotTypeData")
                        where
                        // 类型.
                        data.BallotTypeCode == typeCode
                        // 今天的数据.
                        && data.BallotDate == DateTime.Today
                        select
                        data;
                    BallotMaster result = query.FirstOrDefault();


                    if (result == null)
                    {
                        // 当日数据不存在, 创建.
                        result = new BallotMaster()
                        {
                            // 类型代码.
                            BallotTypeCode = typeCode,
                            BallotTypeData = ballotType,
                            // 投票日期.
                            BallotDate = DateTime.Today,
                            // 投票明细.
                            BallotDetailList = new List <BallotDetail>(),
                        };


                        // 遍历明细.
                        List <BallotTypeDetail> typeDetailList = ballotType.BallotTypeDetailList;
                        foreach (var typeDetail in typeDetailList)
                        {
                            BallotDetail detail = new BallotDetail()
                            {
                                // 投票明细代码
                                BallotTypeDetailCode = typeDetail.BallotTypeDetailCode,
                                // 投票数.
                                BallotCount = typeDetail.DefaultBallotValue,
                            };

                            result.BallotDetailList.Add(detail);
                        }

                        context.BallotMasters.Add(result);
                        context.SaveChanges();
                    }


                    // 加载分类明细.
                    foreach (var detail in result.BallotDetailList)
                    {
                        detail.BallotTypeDetailData = context.BallotTypeDetails.Find(detail.BallotTypeDetailCode);
                    }


                    // 计算投票百分比.
                    result.CalculateBallotPercent();

                    // 返回.
                    return(result);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message, ex);
                return(null);
            }
        }
        /// <summary>
        /// 用户投票
        /// </summary>
        /// <param name="userBallot"></param>
        /// <returns></returns>
        public bool TakeUserBallot(UserBallot userBallot)
        {
            // 预期结果.
            bool result = false;

            try
            {
                using (MyBallotContext context = new MyBallotContext())
                {
                    // 先检查. 是否存在 重复提交.
                    var existQuery =
                        from data in context.UserBallots
                        where
                        // 同一个 投票主数据.
                        data.BallotMasterID == userBallot.BallotMasterID
                        // ip 一致.
                        && data.UserIp == userBallot.UserIp
                        // cookie 一致.
                        && data.UserCookie == userBallot.UserCookie
                        // 今天.
                        && data.BallotTime >= DateTime.Today
                        select
                        data;

                    if (existQuery.Count() > 0)
                    {
                        // 存在 重复提交.
                        ResultMessage = "一天只能投票一次!";
                        return(false);
                    }


                    // 检查, 避免传入 不存在的  投票主表  与 投票明细.
                    BallotMaster master = context.BallotMasters.Find(userBallot.BallotMasterID);
                    if (master == null)
                    {
                        ResultMessage = "投票主数据不存在!";
                        return(false);
                    }

                    BallotDetail detail = context.BallotDetails.Find(userBallot.BallotDetailID);
                    if (detail == null)
                    {
                        ResultMessage = "投票明细数据不存在!";
                        return(false);
                    }



                    // 数据检查完毕.
                    // 投票明细的 投票数 + 1.
                    detail.BallotCount = detail.BallotCount + 1;


                    // 插入用户投票数据.
                    context.UserBallots.Add(userBallot);


                    // 物理保存.
                    context.SaveChanges();
                }

                result = true;
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException dbErr)
            {
                logger.Info(userBallot);
                foreach (var errItem in dbErr.EntityValidationErrors)
                {
                    foreach (var err in errItem.ValidationErrors)
                    {
                        logger.InfoFormat("{0} : {1}", err.PropertyName, err.ErrorMessage);
                    }
                }
                logger.Error(dbErr.Message, dbErr);
                result        = false;
                ResultMessage = dbErr.Message;
            }
            catch (Exception ex)
            {
                result        = false;
                ResultMessage = ex.Message;
            }
            return(result);
        }