using Microsoft.Extensions.Hosting; using Serilog; using SlnMesnac.Common; using SlnMesnac.Model.domain; using SlnMesnac.Repository.service; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ReadService { public class ReadBusiness : IHostedService { private IMesProductReadInfoService _mesProductReadInfoService; public ReadBusiness(IMesProductReadInfoService mesProductReadInfoService) { _mesProductReadInfoService = mesProductReadInfoService; } public Task StartAsync(CancellationToken cancellationToken) { // 启动你的监听逻辑 ListenRead(); return Task.CompletedTask; } public Task StopAsync(CancellationToken cancellationToken) { // 对于清理工作,可以在这里实现 return Task.CompletedTask; } public void ListenRead() { SpeechStr.Instance.Speak("开始监听朗读业务"); Console.WriteLine("开始监听朗读业务"); Task.Run(() => { while (true) { try { // Log.Information("读取业务"); MesProductReadInfo? mesProductReadInfo = _mesProductReadInfoService.Query(x => x.UseFlag == 1 && x.ReadStatus != 2).OrderBy(x => x.ReadId).FirstOrDefault(); if (mesProductReadInfo != null) { Log.Information($"总次数:{mesProductReadInfo.ReadTotal} 朗读第次数{mesProductReadInfo.ReadCount} 开始朗读:====>{mesProductReadInfo.ReadInfo}"); SpeechStr.Instance.Speak(mesProductReadInfo.ReadInfo); mesProductReadInfo.ReadCount++; mesProductReadInfo.ReadStatus = 1; mesProductReadInfo.UpdateTime = DateTime.Now; if (mesProductReadInfo.ReadCount >= mesProductReadInfo.ReadTotal) { mesProductReadInfo.ReadStatus = 2; mesProductReadInfo.EndTime = DateTime.Now; } _mesProductReadInfoService.Update(mesProductReadInfo); Thread.Sleep(1500); } else { Thread.Sleep(1000 * 10); } } catch (Exception ex) { Log.Error(ex.Message); } } }); } } }