Exemple #1
0
        public static async Task <bool> PushArticleMessage(string userId, int batchId, int articleid, string userName)
        {
            try
            {
                using (var client = new Tuhu.Service.Push.TemplatePushClient())
                {
                    var result = await client.PushByUserIDAndBatchIDAsync(new List <string> {
                        userId
                    }, batchId, new Service.Push.Models.Push.PushTemplateLog()
                    {
                        Replacement = Newtonsoft.Json.JsonConvert.SerializeObject(new Dictionary <string, string> {
                            { "{{push_title}}", "" },
                            { "{{iOS_Nickname}}", userName },
                            { "{{Android_Nickname}}", userName },
                            { "{{App_Nickname}}", userName },
                            { "{{articleid}}", $"{articleid}" },
                        }),
                        DeviceType = Service.Push.Models.Push.DeviceType.MessageBox
                    });

                    result.ThrowIfException(true);
                    return(result.Result);
                }
            }
            catch (Exception ex)
            {
                WebLog.LogException(ex);
            }
            return(false);
        }
        public static async Task <List <PushTemplate> > SelectPushTemplatesAsync()
        {
            try
            {
                using (var client = new Tuhu.Service.Push.TemplatePushClient())
                {
                    var result = await client.SelectTemplateAsync();

                    result.ThrowIfException(true);
                    var datas = result.Result;
                    //var list = from template in datas
                    //           group template by template.BatchID
                    //           into g
                    //           select new
                    //           {
                    //               batchid = g.Key,
                    //               datas = g
                    //           };
                    return(datas);
                }
            }
            catch (System.Exception ex)
            {
                throw;
                return(null);
            }
        }
        public static async Task PushOrderFinish(Guid userId, string productName, decimal money, string orderId)
        {
            List <string> target = new List <string> {
                userId.ToString()
            };


            using (var client = new Tuhu.Service.Push.TemplatePushClient())
            {
                var result = await client.PushByUserIDAndBatchIDAsync(target, 798, new Service.Push.Models.Push.PushTemplateLog()
                {
                    Replacement = Newtonsoft.Json.JsonConvert.SerializeObject(new Dictionary <string, string>()
                    {
                        { "{{keyword1.DATA}}", productName },
                        { "{{keyword2.DATA}}", string.Format("{0:f}", money) },
                        { "{{keyword3.DATA}}", orderId },
                        { "{{keyword4.DATA}}", "\"恭喜您拼团成功,我们正在火速为您安排发货~\"" },
                        { "{{orderid}}", orderId }
                    }),
                    DeviceType = Service.Push.Models.Push.DeviceType.WeChat
                });

                result.ThrowIfException(true);
            }
        }
        public static async Task <PushTemplate> SelectTemplateByPKIDAsync(int pkid)
        {
            using (var client = new Tuhu.Service.Push.TemplatePushClient())
            {
                var result = await client.SelectTemplateByPKIDAsync(pkid);

                result.ThrowIfException(true);
                return(result.Result);
            }
        }
        public static async Task <bool> PushByRegidAsync(IEnumerable <string> target, PushTemplate template)
        {
            using (var client = new Tuhu.Service.Push.TemplatePushClient())
            {
                var result = await client.PushByRegidAsync(target, template);

                result.ThrowIfException(true);
                return(result.Result);
            }
        }
        public static async Task <TemplateQueryResult> SelectPushTemplatesAsync(TemplateQuery query)
        {
            using (var client = new Tuhu.Service.Push.TemplatePushClient())
            {
                var result = await client.QueryTemplatesAsync(query);

                result.ThrowIfException(true);
                var datas = result.Result;
                return(datas);
            }
        }
        public static async Task <int> UpdateTemplatePushStatusAsync(int pkid, PushStatus status)
        {
            using (var client = new Tuhu.Service.Push.TemplatePushClient())
            {
                var result = await client.UpdateTemplatePushStatusAsync(pkid, status);

                result.ThrowIfException(true);
                var data = result.Result;
                return(data);
            }
        }
        public static async Task <int> UpdateTemplatePlanInfoAsync(int batchid, string planname, Dictionary <int, PushStatus> status)
        {
            using (var client = new Tuhu.Service.Push.TemplatePushClient())
            {
                var result = await client.UpdatePlanInfoAsync(batchid, planname, status);

                result.ThrowIfException(true);
                var datas = result.Result;
                return(datas);
            }
        }
        public static async Task <int> UpdateTemplateBatchIDAsync(int templateid, int batchid)
        {
            using (var client = new Tuhu.Service.Push.TemplatePushClient())
            {
                var result = await client.UpdatePushTemplateBatchIDAsync(templateid, batchid);

                result.ThrowIfException(true);
                var datas = result.Result;
                return(datas);
            }
        }
        public static async Task <int> CreateOrUpdateTemplateAsync(PushTemplate template)
        {
            try
            {
#if DEBUG
                template.CreateUser = "******";
#endif
                if (template.PKID != 0)
                {
                    using (var client = new Tuhu.Service.Push.TemplatePushClient())
                    {
                        var result = await client.UpdatePushTemplateAsync(template);

                        result.ThrowIfException(true);
                        var datas = result.Result;

                        return(datas);
                    }
                }
                else
                {
                    if (template.BatchID != 0)
                    {
                        var temp = await SelectPushTemplateByBatchIDAndDeviceTypeAsync(template.BatchID, template.DeviceType);

                        if (temp != null)
                        {
                            template.PKID = temp.PKID;
                            return(await CreateOrUpdateTemplateAsync(template));
                        }
                    }
                    if (string.IsNullOrEmpty(template.PlanName))
                    {
                        template.PlanName = "计划名称";
                    }
                    using (var client = new Tuhu.Service.Push.TemplatePushClient())
                    {
                        var result = await client.CreateTemplateAsync(template);

                        result.ThrowIfException(true);
                        var datas = result.Result;

                        return(datas);
                    }
                }
            }
            catch (System.Exception ex)
            {
                WebLog.LogException(ex);
                return(0);
            }
        }
Exemple #11
0
        public static async Task <List <PushTemplate> > SelectPushTemplatesAsync()
        {
            try
            {
                using (var client = new Tuhu.Service.Push.TemplatePushClient())
                {
                    var result = await client.SelectTemplateAsync();

                    result.ThrowIfException(true);
                    var datas = result.Result;
                    return(datas.Cast <List <PushTemplate> >());
                }
            }
            catch (System.Exception ex)
            {
                throw;
            }
        }
        /// <summary>
        /// 发送优惠券
        /// </summary>
        /// <param name="fightGroupsIdentity"></param>
        /// <returns></returns>
        public static async Task <FightGroupsPacketsProvideResponse> CreateFightGroupsPacketByPromotion(Guid fightGroupsIdentity)
        {
            using (var client = CacheHelper.CreateCacheClient(FightGroupPacketsListRedisKey))
            {
                List <FightGroupsPacketsLogModel> list = null;
                var result = await client.GetOrSetAsync <List <FightGroupsPacketsLogModel> >(fightGroupsIdentity.ToString(), async() => await DalFightGroupsPacketsLog.GetFightGroupsPacketsList(fightGroupsIdentity), TimeSpan.FromDays(1));

                list = result?.Value;
                if (list == null)
                {
                    list = await DalFightGroupsPacketsLog.GetFightGroupsPacketsList(fightGroupsIdentity);
                }
                if (list?.Where(_ => _.UserId == null)?.Count() <= 0 && list?.Where(_ => _.PromotionPKID != null)?.Count() <= 0)
                {
                    using (var memberClient = new Tuhu.Service.Member.PromotionClient())
                    {
                        List <CreatePromotionModel> promotionModelList = new List <CreatePromotionModel>();
                        foreach (var item in list)
                        {
                            promotionModelList.Add(new CreatePromotionModel()
                            {
                                Author         = item.UserId.ToString(),
                                GetRuleGUID    = item.GetRuleGuid,
                                UserID         = item.UserId,
                                Channel        = "小程序分享红包",
                                DeviceID       = "",
                                Operation      = "小程序分享红包",
                                IssueChannle   = "小程序分享红包",
                                IssueChannleId = fightGroupsIdentity.ToString(),
                                Issuer         = "李维童"
                            });
                        }
                        var memberResult = await memberClient.CreatePromotionsNewAsync(promotionModelList);

                        if (memberResult.Result.IsSuccess)
                        {
                            for (int i = 0; i < list?.Count; i++)
                            {
                                if (memberResult?.Result?.promotionIds?.Count > i)
                                {
                                    list[i].PromotionPKID = memberResult?.Result?.promotionIds[i];
                                }
                            }
                            await DalFightGroupsPacketsLog.UpdateCreatePromotionPKID(list);

                            // 先注释掉,因为读写库原因,删掉之后,下次从数据库读取不到
                            //using (var reditClient = CacheHelper.CreateCacheClient(FightGroupPacketsListRedisKey))
                            //{
                            //    reditClient.Remove(list?.Where(_ => _.IsLeader == true)?.FirstOrDefault()?.UserId.ToString());
                            //    reditClient.Remove(fightGroupsIdentity.ToString());
                            //}


                            using (var pushClient = new Tuhu.Service.Push.TemplatePushClient())
                            {
                                foreach (var item in list)
                                {
                                    using (var userClient = new Tuhu.Service.UserAccount.UserAccountClient())
                                    {
                                        var userResult = await userClient.GetUserByIdAsync(item.UserId.Value);

                                        var pushResult = await pushClient.PushByUserIDAndBatchIDAsync(new List <string>() { item.UserId.Value.ToString() }, 1643, new Service.Push.Models.Push.PushTemplateLog()
                                        {
                                            Replacement = Newtonsoft.Json.JsonConvert.SerializeObject(new Dictionary <string, string>()
                                            {
                                                { "{{nickname}}", userResult.Result.Profile.NickName },
                                                { "{{packetgroupno}}", item.FightGroupsIdentity.ToString() }
                                            })
                                        });

                                        if (!pushResult.Success || !pushResult.Result)
                                        {
                                            Logger.ErrorException("拆红包推送失败", pushResult.Exception);
                                        }
                                    }
                                }
                            }

                            return(new FightGroupsPacketsProvideResponse()
                            {
                                IsSuccess = memberResult.Result.IsSuccess, Msg = "优惠券领取成功"
                            });
                        }
                        else
                        {
                            return(new FightGroupsPacketsProvideResponse()
                            {
                                IsSuccess = memberResult.Result.IsSuccess, Msg = memberResult?.Result?.ErrorMessage
                            });
                        }
                    }
                }
                else
                {
                    return new FightGroupsPacketsProvideResponse()
                           {
                               IsSuccess = false, Msg = string.Format("分享红包组没有拼组完成还差{0}个或者已经发放过", list?.Where(_ => _.UserId == null)?.Count())
                           }
                };
            }
        }
        public void Execute(IJobExecutionContext context)
        {
            string jobname     = "SubscribeAllUserTokens";
            var    checkresult = HandleExpiredMessageBoxJob.CheckIsOpenWithDescription(jobname);

            if (!checkresult.Item1)
            {
                Logger.Info("开关已关 return");
            }
            int count = 0;

            while (true)
            {
                count++;
                Logger.Info($"开始刷新第{count}批次");
                checkresult = HandleExpiredMessageBoxJob.CheckIsOpenWithDescription(jobname);
                if (!checkresult.Item1)
                {
                    Logger.Info("开关已关 return");
                }
                int maxpkid = 0;
                int.TryParse(checkresult.Item2, out maxpkid);
                var results    = SelectDeviceInfos(maxpkid);
                var topicnames = SelectAllTopicNames();
                if (results != null && results.Any())
                {
                    var tokens = ParseTargets(results, 900);
                    Parallel.ForEach(tokens, new ParallelOptions()
                    {
                        MaxDegreeOfParallelism = 4,
                        TaskScheduler          = TaskScheduler.Default
                    }, t =>
                    {
                        try
                        {
                            var iostokens = t.Where(x =>
                                                    string.Equals(x.Platform, "ios", StringComparison.OrdinalIgnoreCase));
                            var androidtokens = t.Where(x =>
                                                        string.Equals(x.Platform, "android", StringComparison.OrdinalIgnoreCase));

                            foreach (var topicname in topicnames)
                            {
                                if (iostokens.Any())
                                {
                                    using (var client = new Tuhu.Service.Push.TemplatePushClient())
                                    {
                                        var result = client.SubscribeByRegids(iostokens.Select(x => x.Token), topicname, DeviceType.iOS);
                                        Logger.Info($"第{count}批次 ios结果{result.Result}");
                                    }
                                }
                                if (androidtokens.Any())
                                {
                                    using (var client = new Tuhu.Service.Push.TemplatePushClient())
                                    {
                                        var result = client.SubscribeByRegids(androidtokens.Select(x => x.Token), topicname, DeviceType.Android);
                                        Logger.Info($"第{count}批次 android结果{result.Result}");
                                    }
                                }
                            }
                        }
                        catch (System.Exception ex)
                        {
                            Logger.Warn("HandleUserMessageSettingJob ex=>" + ex);
                        }
                    });
                    HandleExpiredMessageBoxJob.UpdateRunTimeSwitchDescription(jobname, results.Max(x => x.PKID).ToString());
                    Logger.Info($"结束刷新第{count}批次");
                }
                else
                {
                    break;
                }
            }
            Logger.Info($"刷新结束");
        }