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.IBusiness.MES;
using Tiger.Model.Entitys.MES.U9C;
using System.Reflection;
using Dm.filter;
using System.Dynamic;
namespace Tiger.Business.MES
{
///
/// 工单计划导入Excel
///
public class ImportWoPlan : IImportWoPlan
{
///
/// 导入
///
///
///
///
///
public async Task Import(T newEntity, ImportInput input) where T : class, new()
{
var result = new ApiAction();
try
{
List list = JsonConvert.DeserializeObject>(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;
}
///
/// 导入Excel前验证
///
///
///
///
///
public async Task>> ValidateTableImport(T newEntity, ImportValidateInput input) {
var result = new ApiAction>();
try
{
List list = JsonConvert.DeserializeObject>(input.EntityJson);
List chkColumns = JsonConvert.DeserializeObject>(input.CheckJson);
List wheres = JsonConvert.DeserializeObject>(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().Where(where).AnyAsync())
{
typeInfo.SetValue(item, "修改");
retInfo.SetValue(item, "数据已经存在,使用更新");
}
}
result.Data = list;
}
catch (Exception ex)
{
result.CatchException(ex, $"验证导入异常");
}
return result;
}
}
}