/// <summary> /// 自动推送红包(公众号自动回复) /// </summary> /// <param name="gzhClient"></param> /// <param name="gzhSever"></param> /// <param name="autoKeyword"></param> /// <param name="dtNow"></param> /// <param name="timeStamp"></param> /// <returns></returns> public async Task <string> SendResponseRedPack(string gzhClient, string gzhSever, WxAutoKeywordShow autoKeyword, DateTime dtNow, int timeStamp) { // 红包只能8点之后领取 if (dtNow.Hour < 8) { return(await wxAutoComResponse.SendWxText(gzhSever, gzhClient, timeStamp, "亲:微信红包将于8点到24点之间才能领取")); } // 红包参数配置 金额|个数|祝福语|红包名称|红包备注 if (autoKeyword.RedAmount <= 0 || autoKeyword.RedCount <= 0) { logHelper.Error("SendMsgByPush:红包配置失败!autoID:" + autoKeyword.Id); return(await wxAutoComResponse.SendWxText(gzhClient, gzhSever, timeStamp, "获取红包失败,请稍候重试!")); } // 红包已发放个数 var sendCount = repo.SqlQuerySingle <int>($" SELECT COUNT(1) FROM WxAutoKeywordDetail WHERE AutoId = {autoKeyword.Id} "); if (sendCount >= autoKeyword.RedCount) { logHelper.Error("SendMsgByPush:红包已经抢完,autoID:" + autoKeyword.Id); return(await wxAutoComResponse.SendWxText(gzhClient, gzhSever, timeStamp, "客官,您来晚了,红包已被先到的小伙伴抢完鸟,下次记得早点来哦。")); } // 判断用户是否已经发放过红包 var exist = repo.FirstOrDefault <WxAutoKeywordDetail>(x => x.Id == autoKeyword.Id && x.Opend == gzhClient); if (exist != null) { logHelper.Error("SendMsgByPush:重复领取红包,autoID:" + autoKeyword.Id + " OpenID:" + gzhClient); return(await wxAutoComResponse.SendWxText(gzhClient, gzhSever, timeStamp, "你已经领取过此红包,请把机会留给更多的人!")); } // 发放红包 var payRes = RedpackApi.SendPack(gzhClient, autoKeyword.RedAmount, autoKeyword.RedAct, autoKeyword.RedWish, autoKeyword.RedRemark, ""); if (payRes.IsSucc) { //红包发送成功 WxAutoKeywordDetail redHis = await SaveAutoKeywordHis(autoKeyword.Id, gzhClient); logHelper.Debug("SendMsgByPush:发送红包成功:" + redHis.JsonSerialize()); return(await wxAutoComResponse.SendWxText(gzhClient, gzhSever, timeStamp, "红包发放成功,请注意查收~~")); } else { logHelper.Error("SendMsgByPush:红包发送失败,错误码:" + payRes.err_code + " 错误描述" + payRes.err_code_des); return(await wxAutoComResponse.SendWxText(gzhClient, gzhSever, timeStamp, "微信服务器暂忙,请稍候重试。")); } }
/// <summary> /// 保存关键字回复记录(关键字回复)(红包使用) /// </summary> /// <param name="autoId"></param> /// <param name="openId"></param> /// <returns></returns> public async Task <WxAutoKeywordDetail> SaveAutoKeywordHis(long autoId, string openId) { var dtNow = DateTime.Now; var autoHis = new WxAutoKeywordDetail(); autoHis.AutoId = autoId; autoHis.Opend = openId; autoHis.CreateTime = dtNow; autoHis.Creater = "AutoResp"; autoHis.Updater = "AutoResp"; autoHis.CreateTime = dtNow; autoHis.UpdateTime = dtNow; await repo.SaveAsync(autoHis); return(autoHis); }