using DotNetCore.CAP;
using Furion.DependencyInjection;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YBDevice.Entity;
namespace YBDevice.WorkerService.Services
{
///
/// 消息处理
///
public class MessageService : IMessageService, ITransient
{
private readonly ISqlSugarRepository repository;
private readonly SqlSugarClient dbClient;
private readonly ICapPublisher _capBus;
public MessageService(ISqlSugarRepository sqlSugarRepository, ICapPublisher capPublishe)
{
repository = sqlSugarRepository;
dbClient = repository.Context;
_capBus = capPublishe;
}
///
/// 获取消息列表
///
///
public async Task GetMessageListAsync()
{
DateTime nowtime = DateTime.Now.Date;
DateTime weektime = nowtime.AddDays(-7);
DateTime monthtime = nowtime.AddMonths(-1);
DateTime halfyeartime = nowtime.AddMonths(-6);
DateTime yeartime = nowtime.AddYears(-1);
//查询到需要发送的消息
var list = await dbClient.Queryable()
.Where(x => (x.CreateTime == weektime && x.Type == SubscribeTypeConst.Week)
|| (x.CreateTime == monthtime && x.Type == SubscribeTypeConst.Month)
|| (x.CreateTime == halfyeartime && x.Type == SubscribeTypeConst.HalfYear)
|| (x.CreateTime == yeartime && x.Type == SubscribeTypeConst.Year)
)
.ToListAsync();
foreach (var item in list)
{
//发布到消息队列
await _capBus.PublishAsync("system.service.sendsubscribemessage", new
{
UserId = item.UserId,
AppId = item.AppId,
OpenId = item.OpenId,
TplId = item.TplId,
IsAlWays = item.IsAlWays,
Id = item.Id
});
}
}
}
}