using MES.Service.DB;
|
using MES.Service.Dto.webApi;
|
using MES.Service.Modes;
|
using MES.Service.util;
|
using SqlSugar;
|
|
namespace MES.Service.service.BasicData;
|
|
public class MesSupplierManager : Repository<MesSupplier>
|
{
|
public bool Save(ErpSupplier unit)
|
{
|
var entity = GetMesSupplier(unit);
|
|
return UseTransaction(db =>
|
{
|
switch (unit.Type)
|
{
|
case "0":
|
if (UpdateSupplierStatus(db, entity, "A"))
|
return 1;
|
break;
|
case "1":
|
if (UpdateSupplierStatus(db, entity, "B"))
|
return 1;
|
break;
|
case "3":
|
if (DeleteSupplier(entity))
|
return 1;
|
break;
|
case "2":
|
case "4":
|
if (InsertOrUpdate(db, entity))
|
return 1;
|
break;
|
default:
|
throw new ArgumentNullException(
|
$"type没有{unit.Type}这个类型的参数");
|
}
|
|
throw new NotImplementedException("操作失败");
|
}) > 0;
|
}
|
|
public bool SaveList(List<ErpSupplier> suppliers)
|
{
|
var result = suppliers.Select(Save).ToList();
|
return result.All(b => b);
|
}
|
|
private bool UpdateSupplierStatus(SqlSugarScope db,
|
List<MesSupplier> supplierId,
|
string status)
|
{
|
var ids = supplierId.Select(it => it.Id).ToArray();
|
|
var result = db.Updateable<MesSupplier>()
|
.SetColumns(s => s.Fforbidstatus == status)
|
.Where(s => ids.Contains(s.Id)).ExecuteCommand();
|
|
if (result > 0)
|
return true;
|
|
throw new NotImplementedException(status == "A" ? "启用失败" : "禁用失败");
|
}
|
|
private bool DeleteSupplier(List<MesSupplier> supplierId)
|
{
|
// 提取所有需要的 MesId 组合
|
var ids = supplierId.Select(customer => customer.Id.ToString())
|
.ToList();
|
|
// 使用这些 MesId 去查询 MesLinkU9 表,找到对应的 U9Id
|
var u9Ids = Db.Queryable<MesLinkU9>()
|
.Where(u =>
|
ids.Contains(u.MesId) && u.TableType == "MES_SUPPLIER")
|
.Select(u => u.U9Id)
|
.Distinct() // 应用 Distinct 去重
|
.ToList();
|
|
// 根据 U9Id 找到所有对应的 MesId,进行删除
|
// 注意:这里假设您确实想要删除这些记录。如果不确定,请先查询以验证结果。
|
var idsToDelete = Db.Queryable<MesLinkU9>()
|
.Where(u => u9Ids.Contains(u.U9Id) && u.TableType == "MES_SUPPLIER")
|
.Select(u => u.MesId)
|
.Distinct() // 应用 Distinct 去重
|
.ToList();
|
|
|
//以上方法可以考虑优化,将U9id找个新建字段存在MES_CUSTOMER中,就可以少一步查询了。
|
|
var deleteByIds = Db.Deleteable<MesSupplier>().In(idsToDelete)
|
.ExecuteCommand();
|
// if (deleteByIds > 0)
|
// return true;
|
return true;
|
//throw new NotImplementedException("删除失败");
|
}
|
|
private List<MesSupplier> GetMesSupplier(ErpSupplier supplier)
|
{
|
List<MesSupplier> list = new();
|
foreach (var se in supplier.FUseOrgId)
|
{
|
var exists = Db.Queryable<MesLinkU9>().Any(u =>
|
u.U9Id == supplier.Id && u.OrgId == se.FUseOrgId &&
|
u.TableType == "MES_SUPPLIER");
|
|
decimal mesId = 0;
|
if (exists)
|
{
|
//获取mesid
|
mesId = Convert.ToDecimal(Db.Queryable<MesLinkU9>()
|
.Where(u =>
|
u.U9Id == supplier.Id && u.OrgId == se.FUseOrgId &&
|
u.TableType == "MES_SUPPLIER")
|
.Select(u => u.MesId) // 选择 MesId 字段
|
.First());
|
}
|
else
|
{
|
mesId = GetSeqUtil.GetSeq("SEQ_BASEINFO_ID");
|
var entity = new MesLinkU9
|
{
|
CreateDate = DateTime.Now,
|
MesId = mesId.ToString(),
|
U9Id = supplier.Id,
|
OrgId = se.FUseOrgId,
|
TableType = "MES_SUPPLIER"
|
};
|
Db.Insertable(entity).ExecuteCommand();
|
}
|
|
var mesSupplier = new MesSupplier
|
{
|
Id = mesId,
|
SuppNo = supplier.FNumber,
|
SuppSname = supplier.FShortName,
|
SuppName = supplier.FName,
|
Lxr = supplier.FContact,
|
Telf1 = supplier.FTel,
|
Fstaffid = supplier.FStaffId,
|
/// <summary>
|
/// 接口字段调整:标准版接口新增 ERP单据状态 字段。
|
/// </summary>
|
/// <remarks>
|
/// 修改人:<池南骏>
|
/// 修改日期:<2024-12-28>
|
/// 修改说明:
|
/// - 原代码:空
|
/// - 修改后:
|
///FDOCUMENTSTATUS = supplier.FDocumentStatus,
|
/// </remarks>
|
DocumentStatus = supplier.FDocumentStatus,
|
Fforbidstatus = supplier.FForbidStatus,
|
CreateDate = DateTime.Now,
|
//CreateOrg= Convert.ToDecimal(supplier.FCreateOrgId),
|
UseOrg = se.FUseOrgId,
|
//Remark=supplier.fremarks,
|
PrivateDescSeg = supplier.PrivateDescSeg,
|
Company = "1000",
|
Factory = "1000"
|
};
|
|
list.Add(mesSupplier);
|
}
|
|
return list;
|
}
|
|
/// <summary>
|
/// "插入或更新的方法"调整:修改为先删除再插入。
|
/// </summary>
|
/// <remarks>
|
/// 修改人:池南骏
|
/// 修改日期:2024-12-30
|
/// 修改说明:`
|
/// </remarks>
|
|
//// - 原代码:
|
//private bool InsertOrUpdate(SqlSugarScope db, MesSupplier entity)
|
//{
|
// var exists = db.Queryable<MesSupplier>().Any(e => e.Id == entity.Id);
|
// if (exists)
|
// {
|
// var update = db.Updateable(entity).ExecuteCommand();
|
// if (update > 0)
|
// return true;
|
// }
|
// else
|
// {
|
// var insert = db.Insertable(entity).ExecuteCommand();
|
// if (insert > 0)
|
// return true;
|
// }
|
|
// return false;
|
//}
|
///新代码
|
private bool InsertOrUpdate(SqlSugarScope db, List<MesSupplier> entity)
|
{
|
DeleteSupplier(entity);
|
|
var insert = db.Insertable(entity).PageSize(1).IgnoreColumnsNull().ExecuteCommand();
|
return insert > 0;
|
}
|
}
|