using Apache.NMS.ActiveMQ.Commands;
|
using Rhea.Common;
|
using System;
|
using System.Collections.Generic;
|
using Newtonsoft.Json;
|
using System.Diagnostics;
|
using System.IO;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
using Tiger.IBusiness;
|
using Tiger.Model.Minsun;
|
using Tiger.Model;
|
using Tiger.Model.Entitys.MES.U9C;
|
using System.Reflection;
|
using Dm.filter;
|
using System.Dynamic;
|
|
namespace Tiger.Business
|
{
|
/// <summary>
|
/// 工单计划导入Excel
|
/// </summary>
|
public class ImportWoPlan : IImportWoPlan
|
{
|
/// <summary>
|
/// 导入
|
/// </summary>
|
/// <typeparam name="T"></typeparam>
|
/// <param name="newEntity"></param>
|
/// <param name="jsonStr"></param>
|
/// <returns></returns>
|
public async Task<ApiAction> Import<T>(T newEntity, ImportInput input) where T : class, new()
|
{
|
var result = new ApiAction();
|
try
|
{
|
|
List<T> list = JsonConvert.DeserializeObject<List<T>>(input.EntityJson);
|
DbClient db = Biz.Db;
|
if (list.Any())
|
{
|
if (list.Count > 100)
|
{
|
db.Utilities.PageEach(list, 100, pageList =>
|
{
|
var y = db.Storageable(pageList)
|
.ToStorage();
|
y.AsInsertable.ExecuteCommand();
|
y.AsUpdateable.ExecuteCommand();
|
});
|
}
|
else
|
{
|
var s = db.Storageable(list)
|
.ToStorage();
|
s.AsInsertable.ExecuteCommand();
|
s.AsUpdateable.ExecuteCommand();
|
}
|
}
|
|
|
}
|
catch (Exception ex)
|
{
|
result.CatchException(ex, $"验证导入异常");
|
}
|
return result;
|
}
|
|
/// <summary>
|
/// 导入Excel前验证
|
/// </summary>
|
/// <typeparam name="T"></typeparam>
|
/// <param name="newEntity"></param>
|
/// <param name="input"></param>
|
/// <returns></returns>
|
public async Task<ApiAction<List<T>>> ValidateTableImport<T>(T newEntity, ImportValidateInput input) {
|
var result = new ApiAction<List<T>>();
|
try
|
{
|
List<T> list = JsonConvert.DeserializeObject<List<T>>(input.EntityJson);
|
List<string> chkColumns = JsonConvert.DeserializeObject<List<string>>(input.CheckJson);
|
List<string> wheres = JsonConvert.DeserializeObject<List<string>>(input.where); //目前只有=,以后可以增加其他的查询条件
|
Type entityType = typeof(T); //T泛型
|
//通过属性名获取属性值 “VALIDATION_TYPE”属性名称
|
PropertyInfo typeInfo = entityType.GetProperty("VALIDATION_TYPE");
|
//通过属性名获取属性值 “VALIDATION_RESULT”属性名称
|
PropertyInfo retInfo = entityType.GetProperty("VALIDATION_RESULT");
|
foreach (var item in list)
|
{
|
string where = " 1=1 ";
|
typeInfo.SetValue(item, "新增");
|
retInfo.SetValue(item, "");
|
foreach (var col in chkColumns)
|
{
|
PropertyInfo colInfo = entityType.GetProperty(col);
|
if (!colInfo.IsNullOrEmpty() && colInfo.GetValue(item).IsNullOrEmpty()) {
|
typeInfo.SetValue(item, "数据异常");
|
retInfo.SetValue(item, "值为空或不存在");
|
}
|
}
|
foreach (var w in wheres)
|
{
|
PropertyInfo wInfo = entityType.GetProperty(w);
|
where += $"AND {w} = '{wInfo.GetValue(item)}'";
|
}
|
if (await Biz.Db.Queryable<T>().Where(where).AnyAsync())
|
{
|
typeInfo.SetValue(item, "修改");
|
retInfo.SetValue(item, "数据已经存在,使用更新");
|
}
|
}
|
result.Data = list;
|
}
|
catch (Exception ex)
|
{
|
result.CatchException(ex, $"验证导入异常");
|
}
|
return result;
|
}
|
}
|
}
|