239 lines
8.5 KiB
C#
239 lines
8.5 KiB
C#
using Admin.NET.Core.Service;
|
||
using Microsoft.AspNetCore.Http;
|
||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||
namespace Admin.NET.Application;
|
||
/// <summary>
|
||
/// 基础批次表服务
|
||
/// </summary>
|
||
[ApiDescriptionSettings(ApplicationConst.ZYGroupName, Order = 100)]
|
||
public class BusBatchBaseService : IDynamicApiController, ITransient
|
||
{
|
||
private readonly SqlSugarRepository<BusBatchBase> _rep;
|
||
public BusBatchBaseService(SqlSugarRepository<BusBatchBase> rep)
|
||
{
|
||
_rep = rep;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 分页查询基础批次表
|
||
/// </summary>
|
||
/// <param name="input"></param>
|
||
/// <returns></returns>
|
||
[HttpPost]
|
||
[ApiDescriptionSettings(Name = "Page")]
|
||
public async Task<SqlSugarPagedList<BusBatchBaseOutput>> Page(BusBatchBaseInput input)
|
||
{
|
||
var query = _rep.AsQueryable()
|
||
.WhereIF(!string.IsNullOrWhiteSpace(input.SearchKey), u =>
|
||
u.ProvinceCode.Contains(input.SearchKey.Trim())
|
||
|| u.ProvinceName.Contains(input.SearchKey.Trim())
|
||
|| u.Batch.Contains(input.SearchKey.Trim())
|
||
|| u.Course.Contains(input.SearchKey.Trim())
|
||
|| u.BatchType.Contains(input.SearchKey.Trim())
|
||
|| u.PressureRange.Contains(input.SearchKey.Trim())
|
||
)
|
||
.WhereIF(!string.IsNullOrWhiteSpace(input.ProvinceCode), u => u.ProvinceCode.Contains(input.ProvinceCode.Trim()))
|
||
.WhereIF(!string.IsNullOrWhiteSpace(input.ProvinceName), u => u.ProvinceName.Contains(input.ProvinceName.Trim()))
|
||
.WhereIF(input.Year > 0, u => u.Year == input.Year)
|
||
.WhereIF(!string.IsNullOrWhiteSpace(input.Batch), u => u.Batch.Contains(input.Batch.Trim()))
|
||
.WhereIF(!string.IsNullOrWhiteSpace(input.Course), u => u.Course.Contains(input.Course.Trim()))
|
||
.WhereIF(!string.IsNullOrWhiteSpace(input.BatchType), u => u.BatchType.Contains(input.BatchType.Trim()))
|
||
.WhereIF(input.Score > 0, u => u.Score == input.Score)
|
||
.WhereIF(input.PressureScore > 0, u => u.PressureScore == input.PressureScore)
|
||
.WhereIF(!string.IsNullOrWhiteSpace(input.PressureRange), u => u.PressureRange.Contains(input.PressureRange.Trim()))
|
||
|
||
// 只进行一次 LEFT JOIN,避免重复
|
||
.LeftJoin<BusBatchBase>((u, p) => u.ProvinceCode == p.ProvinceCode)
|
||
|
||
// 去重,避免重复数据
|
||
.Distinct()
|
||
|
||
// 按创建时间排序
|
||
.OrderBy(u => u.CreateTime)
|
||
|
||
// 选择需要的字段
|
||
.Select((u, p) => new BusBatchBaseOutput
|
||
{
|
||
Id = u.Id,
|
||
ProvinceCode = u.ProvinceCode,
|
||
ProvinceCodeProvinceCode = p.ProvinceCode,
|
||
ProvinceName = u.ProvinceName,
|
||
ProvinceNameProvinceName = p.ProvinceName,
|
||
Year = u.Year,
|
||
Batch = u.Batch,
|
||
Course = u.Course,
|
||
BatchType = u.BatchType,
|
||
Score = u.Score,
|
||
PressureScore = u.PressureScore,
|
||
PressureRange = u.PressureRange,
|
||
CreateTime = u.CreateTime,
|
||
UpdateTime = u.UpdateTime,
|
||
CreateUserId = u.CreateUserId,
|
||
CreateUserName = u.CreateUserName,
|
||
UpdateUserId = u.UpdateUserId,
|
||
UpdateUserName = u.UpdateUserName,
|
||
IsDelete = u.IsDelete,
|
||
});
|
||
|
||
return await query.OrderBuilder(input).ToPagedListAsync(input.Page, input.PageSize);
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 增加基础批次表
|
||
/// </summary>
|
||
/// <param name="input"></param>
|
||
/// <returns></returns>
|
||
[HttpPost]
|
||
[ApiDescriptionSettings(Name = "Add")]
|
||
public async Task<long> Add(AddBusBatchBaseInput input)
|
||
{
|
||
var entity = input.Adapt<BusBatchBase>();
|
||
await _rep.InsertAsync(entity);
|
||
return entity.Id;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 删除基础批次表
|
||
/// </summary>
|
||
/// <param name="input"></param>
|
||
/// <returns></returns>
|
||
[HttpPost]
|
||
[ApiDescriptionSettings(Name = "Delete")]
|
||
public async Task Delete(DeleteBusBatchBaseInput input)
|
||
{
|
||
var entity = await _rep.GetFirstAsync(u => u.Id == input.Id) ?? throw Oops.Oh(ErrorCodeEnum.D1002);
|
||
await _rep.FakeDeleteAsync(entity); //假删除
|
||
//await _rep.DeleteAsync(entity); //真删除
|
||
}
|
||
|
||
/// <summary>
|
||
/// 更新基础批次表
|
||
/// </summary>
|
||
/// <param name="input"></param>
|
||
/// <returns></returns>
|
||
[HttpPost]
|
||
[ApiDescriptionSettings(Name = "Update")]
|
||
public async Task Update(UpdateBusBatchBaseInput input)
|
||
{
|
||
var entity = input.Adapt<BusBatchBase>();
|
||
await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取基础批次表
|
||
/// </summary>
|
||
/// <param name="input"></param>
|
||
/// <returns></returns>
|
||
[HttpGet]
|
||
[ApiDescriptionSettings(Name = "Detail")]
|
||
public async Task<BusBatchBase> Detail([FromQuery] QueryByIdBusBatchBaseInput input)
|
||
{
|
||
return await _rep.GetFirstAsync(u => u.Id == input.Id);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取基础批次表列表
|
||
/// </summary>
|
||
/// <param name="input"></param>
|
||
/// <returns></returns>
|
||
[HttpGet]
|
||
[ApiDescriptionSettings(Name = "List")]
|
||
public async Task<List<BusBatchBaseOutput>> List([FromQuery] BusBatchBaseInput input)
|
||
{
|
||
return await _rep.AsQueryable().Select<BusBatchBaseOutput>().ToListAsync();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取省份Code列表
|
||
/// </summary>
|
||
/// <param name="input"></param>
|
||
/// <returns></returns>
|
||
[ApiDescriptionSettings(Name = "BusBatchBaseProvinceCodeDropdown"), HttpGet]
|
||
public async Task<dynamic> BusBatchBaseProvinceCodeDropdown()
|
||
{
|
||
return await _rep.Context.Queryable<BusProvince>()
|
||
.Select(u => new
|
||
{
|
||
Label = u.Code,
|
||
Value = u.Code
|
||
}
|
||
).ToListAsync();
|
||
}
|
||
/// <summary>
|
||
/// 获取省份列表
|
||
/// </summary>
|
||
/// <param name="input"></param>
|
||
/// <returns></returns>
|
||
[ApiDescriptionSettings(Name = "BusBatchBaseProvinceNameDropdown"), HttpGet]
|
||
public async Task<dynamic> BusBatchBaseProvinceNameDropdown()
|
||
{
|
||
return await _rep.Context.Queryable<BusProvince>()
|
||
.Select(u => new
|
||
{
|
||
Label = u.Name,
|
||
Value = u.Name
|
||
}
|
||
).ToListAsync();
|
||
}
|
||
|
||
|
||
|
||
/// <summary>
|
||
/// 根据省份code和批次类型获取批次信息
|
||
/// </summary>
|
||
/// <param name="requestDTO"></param>
|
||
/// <returns></returns>
|
||
[HttpGet]
|
||
[AllowAnonymous]
|
||
[ApiDescriptionSettings(Name = "batch")]
|
||
public async Task<ProvinceBatchResultDto> Batch([FromQuery] BusBatchRequestDTO requestDTO)
|
||
{
|
||
if (string.IsNullOrWhiteSpace(requestDTO.LocationCode))
|
||
throw Oops.Oh("locationCode 不能为空");
|
||
|
||
if (string.IsNullOrWhiteSpace(requestDTO.Course))
|
||
{
|
||
requestDTO.Course = "综合";
|
||
}
|
||
else
|
||
{
|
||
if (requestDTO.LocationCode == "510000" || requestDTO.LocationCode == "410000")
|
||
{
|
||
requestDTO.Course = requestDTO.Course.Contains("物") ? "理科" : "文科";
|
||
}
|
||
else
|
||
{
|
||
requestDTO.Course = requestDTO.Course.Contains("物") ? "物理类" : "历史类";
|
||
}
|
||
}
|
||
var result = new ProvinceBatchResultDto();
|
||
var query = await _rep.AsQueryable().Where(e => e.ProvinceCode == requestDTO.LocationCode)
|
||
.Where(e => e.Course == requestDTO.Course)
|
||
.Select<BusBatchBaseOutput>().ToListAsync();
|
||
|
||
result.Year = query.FirstOrDefault() != null ? query.FirstOrDefault().Year : 0;
|
||
result.ProvinceCode = query.FirstOrDefault() != null ? query.FirstOrDefault().ProvinceCode : "";
|
||
result.ProvinceName = query.FirstOrDefault() != null ? query.FirstOrDefault().ProvinceName : "";
|
||
result.MinScore = query.FirstOrDefault() != null ? query.FirstOrDefault().MinScore : 0;
|
||
result.MaxScore = query.FirstOrDefault() != null ? query.FirstOrDefault().MaxScore : 0;
|
||
result.batches = query.Select(s => new ProvinceBatchItem()
|
||
{
|
||
Batch = s.Batch,
|
||
Year = s.Year,
|
||
BatchType = s.BatchType,
|
||
Course = s.Course,
|
||
Score = s.Score,
|
||
PressureScore = s.PressureScore,
|
||
PressureRange = s.PressureRange,
|
||
}).ToList();
|
||
|
||
return result;
|
||
}
|
||
|
||
|
||
|
||
|
||
}
|
||
|