Пример #1
0
        /// <summary>
        /// 出发特定的积分事件
        /// </summary>
        /// <param name="ruleCode">规则代码</param>
        /// <param name="userId">用户id</param>
        /// <param name="tenantId">租户Id</param>
        /// <returns></returns>
        public int FireIntegrationEvent(IntegrationEvents ruleCode, int userId, int tenantId)
        {
            var code = ruleCode.ToString();

            if (!IntegrationRules.Instance.IsExistsCode(code))
            {
                //规则代码不匹配
                throw new Exception("规则代码不匹配");
            }
            //找到规则
            var rule = _dataAccess.GetList <IntegralRule>(string.Format("IsEnabled=1 AND RuleCode='{0}' AND TenantId={1}", ruleCode, tenantId)).FirstOrDefault();

            if (rule == null)
            {
                //该租户木有配置规则
                return(0);
            }
            //判断规则周期
            var startTime = DateTime.Now;

            switch (rule.TimeRange)
            {
            case -1:
                startTime     = new DateTime(1901, 1, 1);
                rule.MaxTimes = 1;
                break;

            case 0:
                AddIntegration(rule.RuleId, rule.Integral, userId, tenantId);
                return(rule.Integral);

            case 2:
                startTime = startTime.Accuracy("h");
                break;

            case 3:
                startTime = startTime.Accuracy("d");
                break;
            }
            //查找本周期内,积分奖惩次数
            var count = _dataAccess.GetCount <UserIntegral>(string.Format("Int_UserIntegral.RuleId={0} AND Int_UserIntegral.TimeToGet>'{1}' AND Int_UserIntegral.UserId={2}", rule.RuleId, startTime.ToString("yyyy-MM-dd HH:mm:00"), userId));

            if (count >= rule.MaxTimes)
            {
                return(0);
            }
            AddIntegration(rule.RuleId, rule.Integral, userId, tenantId, rule.RuleDesc);
            return(rule.Integral);
        }