Exemplo n.º 1
0
        private void IntegralRecordsExpense(UserIntegral ui, int Points)
        {
            var ps   = -Points;
            var list = _context.IntegralRecords.Where(t => t.UserIntegral.ID == ui.ID && t.ValidPoints > 0 && ui.IsValid == true).OrderBy(t => t.ExpiredTime).ThenBy(t => t.RecordTime);

            foreach (var r in list)
            {
                if (r.ValidPoints >= ps)
                {
                    r.ValidPoints   = r.ValidPoints - ps;
                    r.ExpensePoints = r.ExpensePoints + ps;
                    ps = 0;
                    break;
                }
                else
                {
                    ps = ps - r.ValidPoints;
                    r.ExpensePoints = r.ExpensePoints + r.ValidPoints;
                    r.ValidPoints   = 0;
                }
            }

            if (ps > 0)
            {
                _isSave  = false;
                _error   = "积分数据异常";
                _message = "积分兑换失败";
            }
        }
Exemplo n.º 2
0
        public IntegralRecord IntegralProcess(UserIntegral ui, string activityname, dynamic extdata)
        {
            try
            {
                var activity = _context.IntegralActivitys.FirstOrDefault(t => t.Name == activityname);

                if (activity != null)
                {
                    //var rule = activity.IntegralRule;

                    var useractivity = ui.IntegralUserActivitys.FirstOrDefault(t => t.IntegralActivityID == activity.ID);

                    if (useractivity == null)
                    {
                        //_context.IntegralUserActivitys.

                        ui.IntegralUserActivitys.Add(new IntegralUserActivity
                        {
                            IntegralActivity = activity
                        });
                    }
                }

                var mark = activityname;

                if (string.IsNullOrEmpty(mark))
                {
                    mark = extdata.ShortMark;
                }

                IntegralRecord res = new IntegralRecord
                {
                    IntegralActivity = activity,
                    UserIntegral     = ui,
                    ShortMark        = mark, //activityname,
                    Points           = GetPoints(ui, activity, extdata)
                                             //Content = BuildRecordContent(activityname, extdata)
                };

                return(SaveRecord(res));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Exemplo n.º 3
0
        private int GetPoints(UserIntegral ui, IntegralActivity activity, dynamic extdata)
        {
            int res = 0;

            if (activity == null)
            {
                return(extdata.Points);
            }

            if (activity.Name == "每日签到")
            {
                return(GetDailySignInPoints(ui, activity));
            }
            else
            {
                var rule = activity.IntegralRule;
                switch (rule.Type)
                {
                case 1:    //单次获得积分
                    res = rule.Points.Value;
                    break;

                case 2:    //外部获取积分
                    res = extdata.Points;
                    break;

                case 3:    //持续获得积分
                    break;

                case 4:    //外部消耗积分
                    res = extdata.Points;
                    break;

                default:
                    break;
                }
            }

            return(res);
        }
Exemplo n.º 4
0
        public UserDto GetUser(dynamic wxuser)
        {
            if (((IDictionary <string, object>)wxuser).ContainsKey("openid") && !string.IsNullOrEmpty(wxuser.openid))
            {
                using (var db = base.NewDB())
                {
                    string openid = wxuser.openid;

                    var dbitem = db.UserAuths.FirstOrDefault(t => t.IdentityType == "wx" && t.Identifier == openid);

                    if (dbitem == null)
                    {
                        User user = new User();
                        user = db.Users.Add(user);
                        //db.SaveChanges();

                        UserProfile userpf = new UserProfile {
                            ID        = user.ID,
                            NickName  = wxuser.userinfo["nickName"].ToString(),
                            AvatarUrl = wxuser.userinfo["avatarUrl"].ToString()
                        };
                        db.UserProfiles.Add(userpf);
                        //db.SaveChanges();


                        UserAuth ua = new UserAuth
                        {
                            IdentityType = "wx",
                            Identifier   = openid,
                            User         = user
                        };

                        db.UserAuths.Add(ua);


                        Agent agent = new Agent
                        {
                            User = user
                        };
                        db.Agents.Add(agent);

                        UserIntegral ui = new UserIntegral
                        {
                            User = user
                        };
                        db.UserIntegrals.Add(ui);

                        UserAccount uc = new UserAccount
                        {
                            User = user
                        };
                        db.UserAccounts.Add(uc);

                        db.SaveChanges();

                        try
                        {
                            if (!string.IsNullOrEmpty(wxuser.sharecode))
                            {
                                Guid authid = ConvertHelper.StrToGuid(wxuser.sharecode, default(Guid));

                                if (ShareRewardType == 2)
                                {
                                    var useracc = db.UserAccounts.FirstOrDefault(t => t.User.AuthID == authid);
                                    var bus     = new AccountBus(db);
                                    bus.ShareReward(useracc, user);
                                }
                                else
                                {
                                    var shareui = db.UserIntegrals.FirstOrDefault(t => t.User.AuthID == authid);
                                    if (shareui != null)
                                    {
                                        var bus = new IntegralBus(db);
                                        bus.IntegralProcess(shareui, "分享有礼", user);

                                        //分享直接成为上级
                                        var shareagent = shareui.User.Agent;
                                        if (shareagent.IsValid ?? false)
                                        {
                                            agent.ParentAgent = shareui.User.Agent;
                                        }
                                    }
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                        }

                        db.SaveChanges();

                        dbitem = db.UserAuths.FirstOrDefault(t => t.IdentityType == "wx" && t.Identifier == openid);
                    }

                    dbitem.LastActiveTime = DateTime.Now;
                    db.SaveChanges();
                    //dbitem = db.UserAuths.FirstOrDefault(t => t.IdentityType == "wx" && t.Identifier == openid);

                    UserDto userdto = new UserDto
                    {
                        //id = dbitem.User.ID,
                        authid = dbitem.User.AuthID,
                        //agentcode = dbitem.User.UserProfile.AgentCode,
                        //agentcode = dbitem.User.Agent.Code,
                        //isagentvalid = dbitem.User.Agent.IsValid,
                        isvalid    = dbitem.User.IsValid,
                        isverified = dbitem.User.UserProfile.IsVerified
                    };

                    if (dbitem.User.UserProfile.NickName != wxuser.userinfo["nickName"].ToString())
                    {
                        dbitem.User.UserProfile.NickName = wxuser.userinfo["nickName"].ToString();
                    }

                    if (dbitem.User.UserProfile.AvatarUrl != wxuser.userinfo["avatarUrl"].ToString())
                    {
                        dbitem.User.UserProfile.AvatarUrl = wxuser.userinfo["avatarUrl"].ToString();
                    }

                    if (db.Entry(dbitem.User.UserProfile).State == EntityState.Modified)
                    {
                        db.SaveChanges();
                    }

                    //if (dbitem.User.Agent == null)
                    //{
                    //    Agent agent = new Agent
                    //    {
                    //        User = dbitem.User
                    //    };
                    //    dbitem.User.Agent = db.Agents.Add(agent);

                    //    db.SaveChanges();
                    //}

                    if (dbitem.User.Agent != null && (dbitem.User.Agent.IsValid ?? false))
                    {
                        userdto.agentcode    = dbitem.User.Agent.Code;
                        userdto.isagentvalid = dbitem.User.Agent.IsValid;
                    }

                    var signin = db.IntegralSignIns.FirstOrDefault(t => t.UserIntegral.User.ID == dbitem.User.ID);

                    if (signin != null && signin.LastTime != null && signin.LastTime.Value.Date == DateTime.Now.Date)
                    {
                        userdto.issignin = true;
                    }

                    //return new ArticleDto { content = dbitem == null ? null : dbitem.Content };
                    return(userdto);
                }
            }
            else
            {
                return(null);
            }
        }
        public async Task <ContentResult> New([FromBody] dynamic json)
        {
            if (json == null)
            {
                return new ContentResult()
                       {
                           StatusCode = 404
                       }
            }
            ;

            string openid = json.oid.ToString();
            string salt   = json.s.ToString();
            int    color  = Convert.ToInt32(json.color.ToString());
            string map    = json.map.ToString();

            if (string.IsNullOrEmpty(openid) ||
                string.IsNullOrEmpty(salt) ||
                color < 0 ||
                string.IsNullOrEmpty(map))
            {
                return new ContentResult()
                       {
                           StatusCode = 404
                       }
            }
            ;
            else
            {
                var user = await DbEntityManager.SelectOne <WxUser>("openid", openid);

                if (user.salt != salt)
                {
                    return new ContentResult()
                           {
                               StatusCode = 401
                           }
                }
                ;
                else
                {
                    string name = $"{user.nickname}({HashManager.Md5(openid).Substring(12, 4)})";

                    //init integral
                    UserIntegral userIntegral = null;
                    if (await DbEntityManager.Exist <UserIntegral>("wx_openid", openid))
                    {
                        userIntegral = await DbEntityManager.SelectOne <UserIntegral>("wx_openid", openid);
                    }
                    else
                    {
                        userIntegral = new UserIntegral()
                        {
                            wx_openid      = openid,
                            mc_integral    = 0,
                            total_integral = 0
                        };

                        await DbEntityManager.Insert(userIntegral);
                    }

                    try
                    {
                        //join or creat a game
                        bool   isMatch = userIntegral.mc_integral >= HelperConfig.Current.IntegralToJoin;
                        string match   = isMatch ? "true" : "false";
                        var    result  = await GameManager.JoinOneGame(
                            isMatch, map, name, color, openid);

                        return(new ContentResult()
                        {
                            StatusCode = 200,
                            Content = $"{{\"gid\":\"{result.gid}\",\"pid\":\"{result.pid}\",\"pidx\":{result.pidx},\"match\":{match}}}"
                        });
                    }
                    catch (GameJoinedException)
                    {
                        return(new ContentResult()
                        {
                            StatusCode = 400, Content = "请等待上局结束"
                        });
                    }
                }
            }
        }
Exemplo n.º 6
0
        public int GetDailySignInPoints(UserIntegral ui, IntegralActivity activity)
        {
            int pointmark = 0;
            int points    = 0;

            var rule = activity.IntegralRule;

            var usersign = _context.IntegralSignIns.FirstOrDefault(t => t.UserIntegral.ID == ui.ID);

            if (usersign == null)
            {
                usersign = new IntegralSignIn
                {
                    UserIntegral = ui
                };

                usersign = _context.IntegralSignIns.Add(usersign);
            }

            if (usersign.LastTime == null)
            {
                usersign.DurationDays = 1;
                pointmark             = 1;
            }
            else
            {
                if (usersign.LastTime.Value.Date == DateTime.Now.Date)
                {
                    _message  = "请勿重复签到";
                    _isSave   = false;
                    pointmark = 0;
                    return(pointmark);
                }
                else
                {
                    if (rule.CycleType == "Month" && DateTime.Now.Day == 1)
                    {
                        usersign.DurationDays = 1;
                        pointmark             = 1;
                    }
                    else
                    {
                        if (usersign.LastTime.Value.AddDays(1).Date == DateTime.Now.Date)
                        {
                            usersign.DurationDays += 1;
                            pointmark              = usersign.DurationDays;
                        }
                        else
                        {
                            usersign.DurationDays = 1;
                            pointmark             = 1;
                        }
                    }
                }
            }

            usersign.LastTime = DateTime.Now;

            if (pointmark != 0)
            {
                points = rule.StepPoints.Value * pointmark;

                if (rule.MaxPoints != null)
                {
                    points = points > rule.MaxPoints.Value ? rule.MaxPoints.Value : points;
                }
            }

            return(points);
        }