Ejemplo n.º 1
0
        /// <summary>
        /// Inserts or updates subscription information.
        /// </summary>
        /// <param name="conn"></param>
        /// <param name="subscription"></param>
        /// <returns></returns>
        public DemandSubscription InsertOrUpdateSubscription(SqlConnection conn, DemandSubscription subscription)
        {
            ParameterChecker.Check(subscription, "DemandSubscription");

            SqlParameter[] parameters = new SqlParameter[]
            {
                new SqlParameter(Parameter_SubscriptionId, subscription.SubscriptionId),
                new SqlParameter(Parameter_WeChatUserId, subscription.WeChatUserId),
                new SqlParameter(Parameter_IsSubscribed, subscription.IsSubscribed),
                new SqlParameter(Parameter_LastPushTimestamp, subscription.LastPushTimestamp ?? subscription.LastUpdatedTimestamp),
                new SqlParameter(Parameter_LastUpdatedTimestamp, subscription.LastUpdatedTimestamp),
                new SqlParameter(Parameter_InsertedTimestamp, subscription.LastUpdatedTimestamp),
                new SqlParameter(Parameter_IsEnableEmailSubscription, subscription.IsEnableEmailSubscription),
                new SqlParameter(Parameter_EmailAddress, subscription.EmailAddress)
            };

            using (SqlDataReader reader = DBHelper.RunProcedure(conn, SP_InsertOrUpdateDemandSubscription, parameters))
            {
                while(reader.Read())
                {
                    subscription.SubscriptionId = reader[0].DBToInt32();
                }
            }

            return subscription;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 添加默认的需求订阅信息当新用户关注时
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public DemandSubscription AddDefautlSubcription(int userId)
        {
            DemandSubscription subscription = null;
            var conn = DBHelper.GetSqlConnection();

            try
            {
                var demandCatogoryRepository = new DemandCategoryRepository();
                var catogroies = demandCatogoryRepository.FindAll();

                using (TransactionScope scope = new TransactionScope())
                {
                    conn.Open();

                    var currentTime = DateTime.Now;
                    subscription = new DemandSubscription()
                    {
                        InsertedTimestamp = currentTime,
                        IsSubscribed = true,
                        LastPushTimestamp = currentTime,
                        LastUpdatedTimestamp = currentTime,
                        WeChatUserId = userId,
                        IsEnableEmailSubscription = false,
                        EmailAddress = string.Empty
                    };

                    subscription = subscriptionDao.InsertOrUpdateSubscription(conn, subscription);

                    if (subscription.IsNotNull() && catogroies.HasItem())
                    {
                        foreach (var item in catogroies)
                        {
                            var subscriptionDetail = new DemandSubscriptionDetail()
                            {
                                InsertedTimestamp = currentTime,
                                SubscriptionId = subscription.SubscriptionId,
                                SubscriptionType = DemandSubscriptionType.Category.ToString(),
                                SubscriptionValue = item.Id.ToString()
                            };

                            subscriptionDao.InsertSubscriptionDetail(conn, subscriptionDetail);
                        }
                    }

                    scope.Complete();
                }
            }
            catch (Exception ex)
            {
                LogService.LogWexin("添加默认的需求订阅信息当新用户关注时", ex.ToString());
            }
            finally
            {
                conn.Close();
            }

            return subscription;
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 根据用户ID获取用户需求订阅信息
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public DemandSubscription GetSubscription(int userId)
        {
            var subscription = new DemandSubscription();
            var conn = DBHelper.GetSqlConnection();

            try
            {
                conn.Open();
                subscription = subscriptionDao.SelectSubscriptionByUserId(conn, userId);
                if (subscription == null)
                {
                    subscription = AddDefautlSubcription(userId);
                }
            }
            catch (Exception ex)
            {
                LogService.LogWexin("根据用户ID获取用户需求订阅信息", ex.ToString());
            }
            finally
            {
                conn.Close();
            }

            return subscription;
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 更新用户需求订阅信息
        /// </summary>
        /// <param name="subcription"></param>
        /// <returns>True: 更新成功,否则失败</returns>
        public bool UpdateSubscription(DemandSubscription subscription)
        {
            ParameterChecker.Check(subscription, "DemandSubscription");
            var isSuccessful = false;
            var conn = DBHelper.GetSqlConnection();

            try
            {
                using (TransactionScope scope = new TransactionScope())
                {
                    conn.Open();
                    subscription.LastUpdatedTimestamp = DateTime.Now;
                    subscriptionDao.InsertOrUpdateSubscription(conn, subscription);

                    // Deletes old subscription details first
                    subscriptionDao.DeleteSubscriptionDetails(conn, subscription.SubscriptionId);
                    isSuccessful = true;

                    if (subscription.SubscriptionDetails.HasItem())
                    {
                        foreach (var item in subscription.SubscriptionDetails)
                        {
                            if (item.IsNotNull())
                            {
                                item.SubscriptionId = subscription.SubscriptionId;
                                item.InsertedTimestamp = DateTime.Now;

                                isSuccessful = subscriptionDao.InsertSubscriptionDetail(conn, item);
                                if (!isSuccessful)
                                {
                                    throw new Exception("InsertSubscriptionDetail failed.");
                                }
                            }
                        }
                    }

                    if (isSuccessful)
                    {
                        scope.Complete();
                    }
                }
            }
            catch (Exception ex)
            {
                LogService.LogWexin("更新用户需求订阅信息并返回更新后的结果", ex.ToString());
            }
            finally
            {
                conn.Close();
            }

            return isSuccessful;
        }
Ejemplo n.º 5
0
        private bool IsDemandMatchWithSubscription(DemandSubscription subscription, Demand demand)
        {
            bool isLocationMatch = false;
            bool isDemandTypeMatch = false;
            bool isKeywordMatch = false;

            if (subscription == null || demand == null)
            {
                return false;
            }

            List<Location> subscribedLocations = new List<Location>();
            List<DemandType> subscribedTypes = new List<DemandType>();
            List<Keyword> subscribedKeywords = new List<Keyword>();

            foreach (DemandSubscriptionDetail item in subscription.SubscriptionDetails)
            {
                if (item.SubscriptionType.Equals(DemandSubscriptionType.Area.ToString(), StringComparison.CurrentCultureIgnoreCase))
                {
                    subscribedLocations.Add(new Location(item.SubscriptionValue));
                }
                else if (item.SubscriptionType.Equals(DemandSubscriptionType.Category.ToString(), StringComparison.CurrentCultureIgnoreCase))
                {
                    subscribedTypes.Add(new DemandType(item.SubscriptionValue));
                }
                else
                {
                    subscribedKeywords.Add(new Keyword(item.SubscriptionValue));
                }
            }

            if (subscribedLocations.Count == 0)
            {
                isLocationMatch = true;
            }
            else
            {
                foreach (var location in subscribedLocations)
                {
                    if (location.IsMatch(demand.Province, demand.City, demand.Area))
                    {
                        isLocationMatch = true;
                        break;
                    }
                }
            }

            if (isLocationMatch)
            {

                if (subscribedTypes.Count == 0)
                {
                    isDemandTypeMatch = true;
                }
                else
                {
                    foreach (var type in subscribedTypes)
                    {
                        if (type.IsMatch(demand.ResourceType, demand.ResourceTypeId))
                        {
                            isDemandTypeMatch = true;
                            break;
                        }
                    }
                }
            }

            if (isLocationMatch && isDemandTypeMatch)
            {
                if (subscribedKeywords.Count == 0)
                {
                    isKeywordMatch = true;
                }
                else
                {
                    foreach (var keyword in subscribedKeywords)
                    {
                        if (keyword.IsMatch(demand.Title))
                        {
                            isKeywordMatch = true;
                            break;
                        }
                    }
                }
            }

            return isLocationMatch && isDemandTypeMatch && isKeywordMatch;
        }