using FactorySystemCommon;
using FactorySystemModel.SqlSugarModel;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using FactorySystemModel.EnumModel;
using System.Data;
namespace FactorySystemBll
{
public class MaterialBll
{
private readonly string[] NoSetField = "FID,FAddDate,FEditDate".Split(',');
///
/// 物料主数据比对
///
public int DockingRecipeData(List mainList, List mainProp, int userId)
{
int result = 0;
if (mainList.Count == 0) return result;
SqlSugarClient db = AppSettingsHelper.GetSqlSugar();
List factorys = db.Queryable().OrderBy(s => s.FDeleted).ToList();
//已存在
List codes = mainList.GroupBy(g => g.FCode).Select(ss => ss.Key).ToList();
List mainsList = db.Queryable().Where(s => s.FDeleted != (int)Constant.DeleteCode.已删除 && codes.Contains(s.FCode)).ToList();
//增改
List updateList = new List();
List insertList = new List();
//根据物料号分组
foreach (var item in mainList.GroupBy(s => s.FCode))
{
TFS_Material temp = mainsList.Find(d => d.FCode == item.Key);
if (temp == null)
{
temp = item.First();
if (!string.IsNullOrEmpty(temp.FFactoryCode))
{
TFS_Factory factory = factorys.Find(s => s.FCode == temp.FFactoryCode);
if (factory == null) factory = factorys.Find(s => s.FName == temp.FFactoryCode);
if (factory == null) temp.FFactoryID = -1;
else temp.FFactoryID = factory.FID;
}
temp.FAddDate = temp.FEditDate = DateTime.Now;
temp.FEditUser = userId;
insertList.Add(temp);
}
else
{
temp.FEditDate = DateTime.Now;
temp.FEditUser = userId;
string value;
foreach (PropertyInfo mProp in mainProp)
{
if (!NoSetField.Contains(mProp.Name) && mProp.PropertyType.Name != "Boolean")
{
object mPV = mProp.GetValue(item.First());
if (mPV != null && !string.IsNullOrEmpty(value = mPV.ToString()) && value != "null" && value != "0")
{
if (mProp.PropertyType.Name == "Int32")
{
if (int.TryParse(value, out int val)) mProp.SetValue(temp, val);
}
else
{
mProp.SetValue(temp, value);
}
}
}
}
updateList.Add(temp);
}
}
try
{
db.BeginTran();
if (updateList.Count > 0)
{
foreach (var item in updateList)
{
result += db.Updateable(item).IgnoreColumns(true).ExecuteCommand();
}
}
if (insertList.Count > 0)
{
foreach (var item in insertList)
{
result += db.Insertable(item).IgnoreColumns(true).ExecuteCommand();
}
}
db.CommitTran();
}
catch (Exception)
{
db.RollbackTran();
}
return result;
}
public List GetMaterial(string fMaterialFormulaIDs)
{
return AppSettingsHelper.GetSqlSugar()
.Queryable((a, b) => new JoinQueryInfos(JoinType.Left, a.FID == b.FMaterialID))
.Where((a, b) => fMaterialFormulaIDs.Contains(a.FID.ToString())).Select((a, b) => a).ToList();
}
///
/// 更具试验号获取物料
///
///
///
public List GetMaterialListByFTestCode(string fTestCode)
{
return AppSettingsHelper.GetSqlSugar().Queryable().Where(m => m.FTestCode == fTestCode).ToList();
}
public TFS_Material GetMaterialById(int id)
{
return BaseBll.GetTempModel("FID=" + id);
}
///
/// 物料子数据比对
///
public int DockingRecipeData(List childList, List childProp, int userId)
{
int result = 0;
if (childList.Count == 0) return result;
SqlSugarClient db = AppSettingsHelper.GetSqlSugar();
//主数据
List codes = childList.GroupBy(g => g.FCode).Select(ss => ss.Key).ToList();
List mainList = db.Queryable().Where(s => s.FDeleted != (int)Constant.DeleteCode.已删除 && codes.Contains(s.FCode)).ToList();
if (mainList == null || mainList.Count == 0) return result;
//已存在子数据
codes = mainList.Select(ss => ss.FID.ToString()).ToList();
List childsList = db.Queryable().Where(s => s.FDeleted != (int)Constant.DeleteCode.已删除
&& codes.Contains(s.FMaterialID.ToString())).ToList();
List factorys = db.Queryable().OrderBy(s => s.FDeleted).ToList();
List updateList = new List();
List insertList = new List();
foreach (TFS_Material main in mainList)
{
List childs = childList.Where(s => s.FCode.Equals(main.FCode)).ToList();
if (childs != null && childs.Count() > 0)
{
foreach (TFS_ViewMaterial child in childs)
{
TFS_ViewMaterial temp = childsList.Find(s => s.FMaterialID == main.FID && s.FFactoryCode.Equals(child.FFactoryCode));
if (temp == null)
{
temp = child;
temp.FAddDate = temp.FEditDate = DateTime.Now;
temp.FEditUser = userId;
insertList.Add(temp);
}
else
{
temp.FEditDate = DateTime.Now;
temp.FEditUser = userId;
foreach (PropertyInfo mProp in childProp)
{
if (!NoSetField.Contains(mProp.Name))
{
string value = mProp.GetValue(child).ToString();
if (!string.IsNullOrEmpty(value) && value != "null" && value != "0")
{
if (mProp.PropertyType.Name == "Int32")
{
if (int.TryParse(value, out int val)) mProp.SetValue(temp, val);
}
else
{
mProp.SetValue(temp, value);
}
}
}
}
updateList.Add(temp);
}
temp.FMaterialID = main.FID;
if (!string.IsNullOrEmpty(temp.FFactoryCode))
{
TFS_Factory factory = factorys.Find(s => s.FCode == temp.FFactoryCode);
if (factory == null) factory = factorys.Find(s => s.FName == temp.FFactoryCode);
if (factory == null) temp.FFactoryID = -1;
else temp.FFactoryID = factory.FID;
}
}
}
}
try
{
db.BeginTran();
if (updateList.Count > 0)
{
foreach (var item in updateList)
{
result += db.Updateable(item).IgnoreColumns(true).ExecuteCommand();
}
}
if (insertList.Count > 0)
{
foreach (var item in insertList)
{
result += db.Insertable(item).IgnoreColumns(true).ExecuteCommand();
}
}
db.CommitTran();
}
catch (Exception)
{
db.RollbackTran();
}
return result;
}
///
/// 物料查询接口--弃用
///
public object SearchMaterialData(Dictionary inParam)
{
List