using NCC.Common.Enum;
using NCC.Common.Extension;
using NCC.Common.Filter;
using NCC.Dependency;
using NCC.DynamicApiController;
using NCC.Extend.Entitys.common_extend;
using NCC.Extend.Entitys.Dto.BigData;
using NCC.FriendlyException;
using NCC.LinqBuilder;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Yitter.IdGenerator;
namespace NCC.Extend
{
///
/// 大数据测试
/// 版 本:V1.20.15
/// 版 权:Wesley(https://www.NCCsoft.com)
/// 作 者:NCC开发平台组
/// 日 期:2022-03-16
///
[ApiDescriptionSettings(Tag = "Extend", Name = "BigData", Order = 600)]
[Route("api/extend/[controller]")]
public class BigDataService: IDynamicApiController, ITransient
{
private readonly ISqlSugarRepository _bigDataRepository;
private readonly SqlSugarScope db;// 核心对象:拥有完整的SqlSugar全部功能
///
///
///
///
public BigDataService(ISqlSugarRepository bigDataRepository)
{
_bigDataRepository = bigDataRepository;
db = bigDataRepository.Context;
}
#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 _bigDataRepository.Entities.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 void Create()
{
var list = _bigDataRepository.Entities.ToList();
var code = 0;
if (list.Count>0)
{
code = list.Select(x => x.EnCode).ToList().Max().ToInt();
}
var index = code == 0 ? 10000001 : code;
if (index > 11500001)
throw NCCException.Oh(ErrorCode.Ex0001);
List entityList = new List();
for (int i = 0; i < 1000000; i++)
{
entityList.Add(new BigDataEntity
{
Id = YitIdHelper.NextId().ToString(),
EnCode = index.ToString(),
FullName = "测试大数据" + index,
CreatorTime = DateTime.Now,
});
index++;
}
Blukcopy(entityList);
}
#endregion
#region PrivateMethod
///
/// 大数据批量插入
///
///
private void Blukcopy(List entityList)
{
try
{
var storageable = db.Storageable(entityList).SplitInsert(x => true).ToStorage();
switch (db.CurrentConnectionConfig.DbType)
{
case DbType.SqlServer:
storageable.AsInsertable.UseSqlServer().ExecuteBulkCopy();
break;
case DbType.MySql:
storageable.AsInsertable.UseMySql().ExecuteBulkCopy();
break;
case DbType.Oracle:
//db.Utilities.PageEach(entityList, 100, pageList =>
//{
// storageable.AsInsertable.ExecuteCommand(); //执行插入
//});
storageable.AsInsertable.UseOracle().ExecuteBulkCopy();
break;
default:
db.Utilities.PageEach(entityList, 1000, pageList =>
{
storageable.AsInsertable.ExecuteCommand(); //执行插入
});
break;
}
}
catch (Exception ex)
{
throw;
}
}
#endregion
}
}