using MES.Service.DB;
|
using MES.Service.Dto.webApi;
|
using MES.Service.Modes;
|
using SqlSugar;
|
|
namespace MES.Service.service;
|
|
public class OrganizeManager : Repository<Organize>
|
{
|
|
public bool Save(ErpOrganize item)
|
{
|
var entity = GetOrganize(item);
|
|
return UseTransaction(db =>
|
{
|
switch (item.Type)
|
{
|
case "0":
|
if (UpdateOrganizetatus(db, entity.Id, "A")) //√
|
return 1;
|
break;
|
case "1":
|
if (UpdateOrganizetatus(db, entity.Id, "B")) //√
|
return 1;
|
break;
|
case "2":
|
if (InsertItem(db, entity)) //√
|
return 1;
|
break;
|
case "3":
|
if (UpdateOrganizetatus(db, entity.Id, "B")) //√
|
return 1;
|
break;
|
case "4":
|
if (InsertOrUpdate(db, entity)) //√
|
return 1;
|
break;
|
default:
|
throw new ArgumentNullException($"type没有{item.Type}这个类型的参数");
|
}
|
|
throw new NotImplementedException("操作失败");
|
}) > 0;
|
}
|
|
public bool SaveList(List<ErpOrganize> Organize)
|
{
|
var list = Organize.Select(GetOrganize).ToList();
|
var groupBy = list.GroupBy(s => s.Type).ToDictionary(g => g.Key, g => g.ToList());
|
|
return UseTransaction(db =>
|
{
|
foreach (var itemGroup in groupBy)
|
switch (itemGroup.Key)
|
{
|
case "0":
|
if (!UpdateOrganizetatusBatch(db, itemGroup.Value, "A"))
|
throw new NotImplementedException("启用失败");
|
break;
|
case "1":
|
if (!UpdateOrganizetatusBatch(db, itemGroup.Value, "B"))
|
throw new NotImplementedException("禁用失败");
|
break;
|
case "2":
|
if (!InsertItemBatch(db, itemGroup.Value))
|
throw new NotImplementedException("插入失败");
|
break;
|
case "3":
|
if (!UpdateOrganizetatusBatch(db, itemGroup.Value, "B"))
|
throw new NotImplementedException("删除失败");
|
break;
|
case "4":
|
if (!InsertOrUpdateBatch(db, itemGroup.Value))
|
throw new NotImplementedException("同步失败");
|
break;
|
default:
|
throw new ArgumentNullException($"type没有{itemGroup.Key}这个类型的参数");
|
}
|
|
return 1;
|
}) > 0;
|
}
|
|
private bool UpdateOrganizetatus(SqlSugarScope db, decimal itemId,string status)
|
{
|
var result = db.Updateable<Organize>().SetColumns(s => s.Fforbidstatus == status).Where(s => s.Id == itemId).ExecuteCommand();
|
return true;
|
}
|
|
private bool InsertItem(SqlSugarScope db, Organize entity)
|
{
|
var exists = db.Queryable<Organize>().Any(e => e.Id == entity.Id);
|
if (exists)
|
{
|
var result = db.Updateable<Organize>().ExecuteCommand();
|
return true;
|
}
|
else
|
{
|
var insert = db.Insertable(entity).ExecuteCommand();
|
if (insert > 0)
|
{
|
return true;
|
}
|
else
|
{
|
throw new NotImplementedException("插入失败");
|
}
|
}
|
}
|
|
private bool InsertOrUpdate(SqlSugarScope db, Organize entity)
|
{
|
var exists = db.Queryable<Organize>().Any(e => e.Id == entity.Id);
|
if (exists)
|
{
|
var update = db.Updateable(entity).ExecuteCommand();
|
return true;
|
}
|
else
|
{
|
var insert = db.Insertable(entity).ExecuteCommand();
|
if (insert > 0)
|
{
|
return true;
|
}
|
else
|
{
|
return false;
|
}
|
}
|
}
|
|
private Organize GetOrganize(ErpOrganize Organize)
|
{
|
return new Organize
|
{
|
|
Id = Convert.ToDecimal(Organize.Id),
|
Fnumber = Organize.FNumber,
|
Fname = Organize.FName,
|
Fforbidstatus = Organize.FForbidStatus,
|
Type = Organize.Type,
|
Fparentid=Organize.FParentID,
|
Fcreatedate=DateTime.Now
|
};
|
}
|
|
private bool UpdateOrganizetatusBatch(SqlSugarScope db,List<Organize> itemList, string status)
|
{
|
var ids = itemList.Select(it => it.Id).ToArray();
|
var result = db.Updateable<Organize>().SetColumns(s => s.Fforbidstatus == status).Where(s => ids.Contains(s.Id)).ExecuteCommand();
|
return true;
|
}
|
|
private bool InsertItemBatch(SqlSugarScope db, List<Organize> itemList)
|
{
|
foreach (var entity in itemList)
|
{
|
if (!InsertItem(db, entity))
|
{
|
return false;
|
}
|
}
|
return true;
|
}
|
|
private bool InsertOrUpdateBatch(SqlSugarScope db, List<Organize> itemList)
|
{
|
foreach (var entity in itemList)
|
{
|
if (!InsertOrUpdate(db, entity))
|
{
|
return false;
|
}
|
}
|
return true;
|
}
|
|
}
|