BigDataService.cs
4.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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
{
/// <summary>
/// 大数据测试
/// 版 本:V1.20.15
/// 版 权:Wesley(https://www.NCCsoft.com)
/// 作 者:NCC开发平台组
/// 日 期:2022-03-16
/// </summary>
[ApiDescriptionSettings(Tag = "Extend", Name = "BigData", Order = 600)]
[Route("api/extend/[controller]")]
public class BigDataService: IDynamicApiController, ITransient
{
private readonly ISqlSugarRepository<BigDataEntity> _bigDataRepository;
private readonly SqlSugarScope db;// 核心对象:拥有完整的SqlSugar全部功能
/// <summary>
///
/// </summary>
/// <param name="bigDataRepository"></param>
public BigDataService(ISqlSugarRepository<BigDataEntity> bigDataRepository)
{
_bigDataRepository = bigDataRepository;
db = bigDataRepository.Context;
}
#region GET
/// <summary>
/// 列表
/// </summary>
/// <param name="input">请求参数</param>
/// <returns></returns>
[HttpGet("")]
public async Task<dynamic> GetList([FromQuery] PageInputBase input)
{
var queryWhere = LinqExpression.And<BigDataEntity>();
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<BigDataListOutput>()
{
list = list.list.Adapt<List<BigDataListOutput>>(),
pagination = list.pagination
};
return PageResult<BigDataListOutput>.SqlSugarPageResult(pageList);
}
#endregion
#region POST
/// <summary>
/// 新建
/// </summary>
/// <returns></returns>
[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<BigDataEntity> entityList = new List<BigDataEntity>();
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
/// <summary>
/// 大数据批量插入
/// </summary>
/// <param name="entityList"></param>
private void Blukcopy(List<BigDataEntity> 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
}
}