122 lines
4.1 KiB
C#
122 lines
4.1 KiB
C#
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
|
|
{
|
|
/// <summary>
|
|
/// 历史搜索接口实现类Api
|
|
///
|
|
/// @author lwh
|
|
/// @date 2023-10-09
|
|
/// </summary>
|
|
[AppService(ServiceType = typeof(IHistorySearchServiceApi), ServiceLifetime = LifeTime.Transient)]
|
|
public class HistorySearchServiceImplApi : BaseService<HistorySearch>, IHistorySearchServiceApi
|
|
{
|
|
private readonly HistorySearchRepository _HistorySearchRepository;
|
|
|
|
public HistorySearchServiceImplApi(HistorySearchRepository HistorySearchRepository)
|
|
{
|
|
this._HistorySearchRepository = HistorySearchRepository;
|
|
}
|
|
|
|
#region Api接口代码
|
|
|
|
|
|
/// <summary>
|
|
/// 查询历史搜索列表(Api)
|
|
/// </summary>
|
|
/// <param name="parm"></param>
|
|
/// <returns></returns>
|
|
public async Task<List<HistorySearchVoApi>> GetHistorySearchListApi(HistorySearchQueryDtoApi parm)
|
|
{
|
|
//开始拼装查询条件d
|
|
var predicate = Expressionable.Create<HistorySearch>();
|
|
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();
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 获取热门搜索列表(Api)
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<List<SearchTermCount>> GetHotSearchListApi()
|
|
{
|
|
// 获取当天开始和结束时间
|
|
DateTime todayStart = DateTime.Today;
|
|
DateTime todayEnd = todayStart.AddDays(1).AddSeconds(-1);
|
|
|
|
var predicate = Expressionable.Create<HistorySearch>();
|
|
|
|
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;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 添加历史搜索
|
|
/// </summary>
|
|
public async Task<string> AddHistorySearch(HistorySearch model)
|
|
{
|
|
var response = await _HistorySearchRepository.InsertReturnSnowflakeIdAsync(model);
|
|
return "添加成功!";
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
}
|
|
}
|