Пример #1
0
        /// <summary>
        /// 积分事件触发
        /// </summary>
        /// <param name="websiteOwner">站点所有者</param>
        /// <param name="openId">openId</param>
        /// <param name="value">原始值</param>
        /// <param name="scoreEvent">事件名称</param>
        /// <param name="remark">备注</param>
        /// <param name="msg">提示信息</param>
        /// <param name="addScore">增加积分</param>
        /// <param name="showName">显示名称</param>
        /// <returns></returns>
        public bool EventUpdate(string websiteOwner, string openId, decimal value, string scoreEvent, string remark, out string msg, out int addScore, string showName = "", string serialNumber = "")
        {
            int score = 0;//最终添加的积分

            addScore = 0;
            msg      = "";//返回消息
            BLLApiLog       bllApiLog       = new BLLApiLog();
            BLLScoreDefine  bllScoreDefine  = new BLLScoreDefine();
            ScoreDefineInfo scoreDefineInfo = bllScoreDefine.GetScoreDefineInfoByScoreEvent(scoreEvent, WebsiteOwner);

            if (scoreDefineInfo == null)
            {
                msg = "积分规则不存在,请检查";
                return(false);
            }
            if (scoreDefineInfo.IsHide == 1)
            {
                msg = "积分规则已停用";
                return(false);
            }
            UserInfo userInfo = bllUser.GetUserInfoByOpenId(openId);

            if (userInfo == null && (websiteOwner == "dongwu" || websiteOwner == "dongwudev"))
            {
                userInfo = bllUser.CreateNewUser(websiteOwner, openId, showName);
            }
            if (userInfo == null)
            {
                msg = "openid不存在,请检查";
                return(false);
            }
            List <ScoreDefineInfoExt> scoreDefineEx = bllScoreDefine.GetScoreDefineExList(scoreDefineInfo.ScoreId);

            //优先级: 扩展->基本比例-一般
            if (scoreDefineEx != null && scoreDefineEx.Count > 0)
            {
                DateTime dtNow = DateTime.Now;
                var      item  = scoreDefineEx.FirstOrDefault(p => p.BeginTime <= DateTime.Now && p.EndTime >= DateTime.Now);
                if (item != null)
                {
                    //score = (double)Math.Round(value / (item.RateValue / item.RateScore), 2);
                    score = (int)Math.Ceiling(value / (item.RateValue / item.RateScore));
                }
            }
            if (score == 0)
            {
                if (scoreDefineInfo.BaseRateScore > 0 && scoreDefineInfo.BaseRateValue > 0)//基础比例
                {
                    //score = (double)Math.Round(value / (scoreDefineInfo.BaseRateValue / scoreDefineInfo.BaseRateScore), 2);
                    score = (int)Math.Ceiling(value / (scoreDefineInfo.BaseRateValue / scoreDefineInfo.BaseRateScore));
                }
            }
            if (score == 0)
            {
                score = (int)scoreDefineInfo.Score;
            }

            //if (score == 0)
            //{
            //    msg = "增加积分不能为0";
            //    return false;
            //}
            if (scoreDefineInfo.DayLimit > 0)
            {
                double nTotal = bllUser.GetUserDayScoreSUMEvent(userInfo.UserID, scoreEvent, true);
                if (scoreDefineInfo.DayLimit < nTotal + score)
                {
                    msg = scoreDefineInfo.ScoreEvent + "每日所得积分超限制";
                    return(false);
                }
            }
            if (scoreDefineInfo.TotalLimit > 0)
            {
                double nTotal = bllUser.GetUserDayScoreSUMEvent(userInfo.UserID, scoreEvent, false);
                if (scoreDefineInfo.TotalLimit < nTotal + score)
                {
                    msg = scoreDefineInfo.ScoreEvent + "所得总积分超限制";
                    return(false);
                }
            }

            //积分记录
            UserScoreDetailsInfo scoreModel = new UserScoreDetailsInfo();

            scoreModel.AddNote      = !string.IsNullOrEmpty(remark) ? remark : scoreDefineInfo.Description;
            scoreModel.AddTime      = DateTime.Now;
            scoreModel.Score        = score;
            scoreModel.UserID       = userInfo.UserID;
            scoreModel.OpenId       = userInfo.WXOpenId;
            scoreModel.ScoreType    = scoreDefineInfo.ScoreType;
            scoreModel.WebSiteOwner = WebsiteOwner;
            scoreModel.ScoreEvent   = scoreEvent;
            scoreModel.SerialNumber = serialNumber;
            BLLTransaction tran = new BLLTransaction();

            try
            {
                int historyTotalScore = 0;
                if (score > 0)
                {
                    historyTotalScore = score;
                }
                string sql = string.Format("TotalScore+={0},HistoryTotalScore+={1}", score, historyTotalScore);
                if (!string.IsNullOrEmpty(showName))
                {
                    sql += string.Format(",TrueName='{0}'", showName);
                }
                if (Update(userInfo, sql, string.Format(" AutoId={0}", userInfo.AutoID), tran) == 1 && Add(scoreModel, tran))
                {
                    #region 日志记录
                    if (!bllApiLog.Add(userInfo.WebsiteOwner, Enums.EnumApiModule.ScoreEvent, string.Format("积分事件,事件名称:{0}原始值{1}积分值:{2}", scoreDefineInfo.ScoreEvent, value, score), userInfo.WXOpenId, userInfo.UserID, serialNumber))
                    {
                        msg = "日志记录失败";
                        tran.Rollback();
                        return(false);
                    }

                    #endregion
                    tran.Commit();
                    addScore = score;
                    return(true);
                }
                else
                {
                    msg = "操作失败";
                    tran.Rollback();
                    return(false);
                }
            }
            catch (Exception ex)
            {
                msg = ex.ToString();
                tran.Rollback();
                return(false);
            }
        }