using Infrastructure.Attribute;
using Microsoft.AspNetCore.Http;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ARW.Model;
using ARW.Repository;
using ARW.Repository.Business.Custom.HistorySearchs;
using ARW.Service.Api.IBusinessService.Custom.HistorySearchs;
using ARW.Model.Dto.Api.Custom.HistorySearchs;
using ARW.Model.Models.Business.Custom.HistorySearchs;
using ARW.Model.Vo.Api.Custom.HistorySearchs;
namespace ARW.Service.Api.BusinessService.Custom.HistorySearchs
{
///
/// 历史搜索接口实现类Api
///
/// @author lwh
/// @date 2023-10-09
///
[AppService(ServiceType = typeof(IHistorySearchServiceApi), ServiceLifetime = LifeTime.Transient)]
public class HistorySearchServiceImplApi : BaseService, IHistorySearchServiceApi
{
private readonly HistorySearchRepository _HistorySearchRepository;
public HistorySearchServiceImplApi(HistorySearchRepository HistorySearchRepository)
{
this._HistorySearchRepository = HistorySearchRepository;
}
#region Api接口代码
///
/// 查询历史搜索列表(Api)
///
///
///
public async Task> GetHistorySearchListApi(HistorySearchQueryDtoApi parm)
{
//开始拼装查询条件d
var predicate = Expressionable.Create();
predicate = predicate.AndIF(parm.CustomerGuid != 0, s => s.CustomerGuid == parm.CustomerGuid);
var query = _HistorySearchRepository
.Queryable()
.Where(predicate.ToExpression())
.OrderBy(s => s.Create_time, OrderByType.Desc)
.Select(s => new HistorySearchVoApi
{
HistorySearchId = s.HistorySearchId,
HistorySearchGuid = s.HistorySearchGuid,
CustomerGuid = s.CustomerGuid,
HistorySearchContent = s.HistorySearchContent,
});
return await query.ToListAsync();
}
///
/// 获取热门搜索列表(Api)
///
///
public async Task> GetHotSearchListApi()
{
// 获取当天开始和结束时间
DateTime todayStart = DateTime.Today;
DateTime todayEnd = todayStart.AddDays(1).AddSeconds(-1);
var predicate = Expressionable.Create();
var query = _HistorySearchRepository
.Queryable()
//.Where(s => s.Create_time >= todayStart && s.Create_time <= todayEnd) // 仅选择当天的搜索记录
.Where(predicate.ToExpression())
.OrderBy(s => s.Create_time, OrderByType.Desc)
.Select(s => new HistorySearchVoApi
{
HistorySearchId = s.HistorySearchId,
HistorySearchGuid = s.HistorySearchGuid,
CustomerGuid = s.CustomerGuid,
HistorySearchContent = s.HistorySearchContent,
});
var searchTerms = await query.ToListAsync();
var searchTermCounts = searchTerms
.GroupBy(s => s.HistorySearchContent)
.Select(g => new SearchTermCount
{
SearchTerm = g.Key,
Count = g.Count()
})
.OrderByDescending(g => g.Count) // 按照搜索次数降序排列
.Take(20)
.ToList();
return searchTermCounts;
}
///
/// 添加历史搜索
///
public async Task AddHistorySearch(HistorySearch model)
{
var response = await _HistorySearchRepository.InsertReturnSnowflakeIdAsync(model);
return "添加成功!";
}
#endregion
}
}