using JNPF.Common.Enums;
using JNPF.Common.Extension;
using JNPF.Common.Filter;
using JNPF.Common.Security;
using JNPF.DependencyInjection;
using JNPF.DynamicApiController;
using JNPF.Extend.Entitys;
using JNPF.Extend.Entitys.Dto.BigData;
using JNPF.FriendlyException;
using JNPF.LinqBuilder;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
namespace JNPF.Extend;
///
/// 大数据测试
/// 版 本:V3.2
/// 版 权:拓通智联科技有限公司(http://www.tuotong-tech.com)
/// 日 期:2021-06-01 .
///
[ApiDescriptionSettings(Tag = "Extend", Name = "BigData", Order = 600)]
[Route("api/extend/[controller]")]
public class BigDataService : IDynamicApiController, ITransient
{
private readonly ISqlSugarRepository _repository;
///
/// 初始化一个类型的新实例.
///
public BigDataService(ISqlSugarRepository repository)
{
_repository = repository;
}
#region GET
///
/// 列表
///
/// 请求参数
///
[HttpGet("")]
public async Task GetList([FromQuery] PageInputBase input)
{
var queryWhere = LinqExpression.And();
if (!string.IsNullOrEmpty(input.keyword))
queryWhere = queryWhere.And(m => m.FullName.Contains(input.keyword) || m.EnCode.Contains(input.keyword));
var list = await _repository.AsQueryable().Where(queryWhere).OrderBy(x => x.CreatorTime, OrderByType.Desc).ToPagedListAsync(input.currentPage, input.pageSize);
var pageList = new SqlSugarPagedList()
{
list = list.list.Adapt>(),
pagination = list.pagination
};
return PageResult.SqlSugarPageResult(pageList);
}
#endregion
#region POST
///
/// 新建
///
///
[HttpPost("")]
public async Task Create()
{
var list = await _repository.GetListAsync();
var code = 0;
if (list.Count > 0)
{
code = list.Select(x => x.EnCode).ToList().Max().ParseToInt();
}
var index = code == 0 ? 10000001 : code;
if (index > 11500001)
throw Oops.Oh(ErrorCode.Ex0001);
List entityList = new List();
for (int i = 0; i < 10000; i++)
{
entityList.Add(new BigDataEntity
{
Id = SnowflakeIdHelper.NextId(),
EnCode = index.ToString(),
FullName = "测试大数据" + index,
CreatorTime = DateTime.Now,
});
index++;
}
Blukcopy(entityList);
}
#endregion
#region PrivateMethod
///
/// 大数据批量插入.
///
///
private void Blukcopy(List entityList)
{
try
{
var storageable = _repository.AsSugarClient().Storageable(entityList).SplitInsert(x => true).ToStorage();
switch (_repository.AsSugarClient().CurrentConnectionConfig.DbType)
{
case DbType.Dm:
case DbType.Kdbndp:
storageable.AsInsertable.ExecuteCommand();
break;
case DbType.Oracle:
_repository.AsSugarClient().Storageable(entityList).ToStorage().BulkCopy();
break;
default:
_repository.AsSugarClient().Fastest().BulkCopy(entityList);
break;
}
}
catch (Exception ex)
{
throw;
}
}
#endregion
}