using Admin.NET.Plugin.AiDOP.Dto.S0.Sales;
using Admin.NET.Plugin.AiDOP.Entity.S0.Sales;
using Admin.NET.Plugin.AiDOP.Infrastructure;
namespace Admin.NET.Plugin.AiDOP.Controllers.S0.Sales;
///
/// S0 客户主数据(CustMaster 语义)
///
[ApiController]
[Route("api/s0/sales/customers")]
[AllowAnonymous]
[NonUnify]
public class AdoS0CustomersController : ControllerBase
{
private readonly SqlSugarRepository _rep;
public AdoS0CustomersController(SqlSugarRepository rep)
{
_rep = rep;
}
[HttpGet]
public async Task GetPagedAsync([FromQuery] AdoS0CustMasterQueryDto q)
{
(q.Page, q.PageSize) = PagingGuard.Normalize(q.Page, q.PageSize);
var query = _rep.AsQueryable()
.WhereIF(q.CompanyRefId.HasValue, x => x.CompanyRefId == q.CompanyRefId.Value)
.WhereIF(q.FactoryRefId.HasValue, x => x.FactoryRefId == q.FactoryRefId.Value)
.WhereIF(!string.IsNullOrWhiteSpace(q.DomainCode), x => x.DomainCode == q.DomainCode)
.WhereIF(!string.IsNullOrWhiteSpace(q.Cust), x => x.Cust.Contains(q.Cust!))
.WhereIF(!string.IsNullOrWhiteSpace(q.SortName), x => x.SortName != null && x.SortName.Contains(q.SortName!))
.WhereIF(!string.IsNullOrWhiteSpace(q.Curr), x => x.Curr != null && x.Curr.Contains(q.Curr!))
.WhereIF(!string.IsNullOrWhiteSpace(q.Slspsn1), x => x.Slspsn1 != null && x.Slspsn1.Contains(q.Slspsn1!))
.WhereIF(!string.IsNullOrWhiteSpace(q.ShipTo), x => x.ShipTo != null && x.ShipTo.Contains(q.ShipTo!))
.WhereIF(q.IsActive.HasValue, x => x.IsActive == q.IsActive.Value)
.WhereIF(q.IsConfirm.HasValue, x => x.IsConfirm == q.IsConfirm.Value)
.WhereIF(
!string.IsNullOrWhiteSpace(q.Keyword),
x => x.Cust.Contains(q.Keyword!)
|| (x.SortName != null && x.SortName.Contains(q.Keyword!))
|| (x.CustFullName != null && x.CustFullName.Contains(q.Keyword!)));
var total = await query.CountAsync();
var list = await query
.OrderByDescending(x => x.CreateTime)
.Skip((q.Page - 1) * q.PageSize)
.Take(q.PageSize)
.ToListAsync();
return Ok(new { total, page = q.Page, pageSize = q.PageSize, list });
}
[HttpGet("{id:long}")]
public async Task GetAsync(long id)
{
var item = await _rep.GetByIdAsync(id);
return item == null ? NotFound() : Ok(item);
}
[HttpPost]
public async Task CreateAsync([FromBody] AdoS0CustMasterUpsertDto dto)
{
if (await _rep.IsAnyAsync(x => x.FactoryRefId == dto.FactoryRefId && x.Cust == dto.Cust))
return AdoS0ApiErrors.Conflict(AdoS0ErrorCodes.CustomerCodeExists, "客户编码已存在");
var now = DateTime.Now;
var entity = new AdoS0CustMaster
{
CompanyRefId = dto.CompanyRefId,
FactoryRefId = dto.FactoryRefId,
DomainCode = dto.DomainCode,
Cust = dto.Cust,
SortName = dto.SortName,
CustFullName = dto.CustFullName,
Curr = dto.Curr,
Slspsn1 = dto.Slspsn1,
Slspsn2 = dto.Slspsn2,
ShipTo = dto.ShipTo,
TaxClass = dto.TaxClass,
TaxIn = dto.TaxIn,
CustClass = dto.CustClass,
Address = dto.Address,
Contact = dto.Contact,
Position = dto.Position,
ContactInfo = dto.ContactInfo,
CreditRating = dto.CreditRating,
Remarks = dto.Remarks,
IsActive = dto.IsActive,
IsConfirm = dto.IsConfirm,
CreateUser = dto.CreateUser,
CreateTime = now,
UpdateUser = dto.UpdateUser,
UpdateTime = null
};
await _rep.AsInsertable(entity).ExecuteReturnEntityAsync();
return Ok(entity);
}
[HttpPut("{id:long}")]
public async Task UpdateAsync(long id, [FromBody] AdoS0CustMasterUpsertDto dto)
{
var entity = await _rep.GetByIdAsync(id);
if (entity == null) return NotFound();
if (await _rep.IsAnyAsync(x => x.Id != id && x.FactoryRefId == dto.FactoryRefId && x.Cust == dto.Cust))
return AdoS0ApiErrors.Conflict(AdoS0ErrorCodes.CustomerCodeExists, "客户编码已存在");
entity.CompanyRefId = dto.CompanyRefId;
entity.FactoryRefId = dto.FactoryRefId;
entity.DomainCode = dto.DomainCode;
entity.Cust = dto.Cust;
entity.SortName = dto.SortName;
entity.CustFullName = dto.CustFullName;
entity.Curr = dto.Curr;
entity.Slspsn1 = dto.Slspsn1;
entity.Slspsn2 = dto.Slspsn2;
entity.ShipTo = dto.ShipTo;
entity.TaxClass = dto.TaxClass;
entity.TaxIn = dto.TaxIn;
entity.CustClass = dto.CustClass;
entity.Address = dto.Address;
entity.Contact = dto.Contact;
entity.Position = dto.Position;
entity.ContactInfo = dto.ContactInfo;
entity.CreditRating = dto.CreditRating;
entity.Remarks = dto.Remarks;
entity.IsActive = dto.IsActive;
entity.IsConfirm = dto.IsConfirm;
entity.UpdateUser = dto.UpdateUser;
entity.UpdateTime = DateTime.Now;
await _rep.AsUpdateable(entity).ExecuteCommandAsync();
return Ok(entity);
}
[HttpPatch("{id:long}/toggle-enabled")]
public async Task ToggleActiveAsync(long id, [FromBody] AdoS0CustMasterToggleActiveDto dto)
{
var entity = await _rep.GetByIdAsync(id);
if (entity == null) return NotFound();
entity.IsActive = dto.IsActive;
entity.UpdateTime = DateTime.Now;
await _rep.AsUpdateable(entity).ExecuteCommandAsync();
return Ok(entity);
}
[HttpDelete("{id:long}")]
public async Task DeleteAsync(long id)
{
var item = await _rep.GetByIdAsync(id);
if (item == null) return NotFound();
await _rep.DeleteAsync(item);
return Ok(new { message = "删除成功" });
}
}