| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- using Admin.NET.Plugin.AiDOP.Dto.S0.Supply;
- using Admin.NET.Plugin.AiDOP.Entity.S0.Supply;
- using Admin.NET.Plugin.AiDOP.Entity.S0.Warehouse;
- using Admin.NET.Plugin.AiDOP.Infrastructure;
- namespace Admin.NET.Plugin.AiDOP.Controllers.S0.Supply;
- /// <summary>
- /// S0 供应商主数据(SuppMaster 语义)
- /// </summary>
- [ApiController]
- [Route("api/s0/supply/suppliers")]
- [AllowAnonymous]
- [NonUnify]
- public class AdoS0SuppliersController : ControllerBase
- {
- private readonly SqlSugarRepository<AdoS0SuppMaster> _rep;
- private readonly SqlSugarRepository<AdoS0LocationMaster> _locRep;
- public AdoS0SuppliersController(
- SqlSugarRepository<AdoS0SuppMaster> rep,
- SqlSugarRepository<AdoS0LocationMaster> locRep)
- {
- _rep = rep;
- _locRep = locRep;
- }
- [HttpGet]
- public async Task<IActionResult> GetPagedAsync([FromQuery] AdoS0SuppMasterQueryDto q)
- {
- (q.Page, q.PageSize) = PagingGuard.Normalize(q.Page, q.PageSize);
- var baseQuery = _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.Supp), x => x.Supp.Contains(q.Supp!))
- .WhereIF(!string.IsNullOrWhiteSpace(q.SortName), x => x.SortName != null && x.SortName.Contains(q.SortName!))
- .WhereIF(!string.IsNullOrWhiteSpace(q.Address), x => x.Address != null && x.Address.Contains(q.Address!))
- .WhereIF(!string.IsNullOrWhiteSpace(q.Contact), x => x.Contact != null && x.Contact.Contains(q.Contact!))
- .WhereIF(!string.IsNullOrWhiteSpace(q.Position), x => x.Position != null && x.Position.Contains(q.Position!))
- .WhereIF(!string.IsNullOrWhiteSpace(q.ContactInfo), x => x.ContactInfo != null && x.ContactInfo.Contains(q.ContactInfo!))
- .WhereIF(!string.IsNullOrWhiteSpace(q.CertificationLevel), x => x.CertificationLevel != null && x.CertificationLevel.Contains(q.CertificationLevel!))
- .WhereIF(!string.IsNullOrWhiteSpace(q.CrTerms), x => x.CrTerms != null && x.CrTerms.Contains(q.CrTerms!))
- .WhereIF(!string.IsNullOrWhiteSpace(q.Curr), x => x.Curr != null && x.Curr.Contains(q.Curr!))
- .WhereIF(!string.IsNullOrWhiteSpace(q.Type), x => x.Type != null && x.Type.Contains(q.Type!))
- .WhereIF(q.TaxIn.HasValue, x => x.TaxIn == q.TaxIn.Value)
- .WhereIF(!string.IsNullOrWhiteSpace(q.TaxClass), x => x.TaxClass != null && x.TaxClass.Contains(q.TaxClass!))
- .WhereIF(!string.IsNullOrWhiteSpace(q.PurContact), x => x.PurContact != null && x.PurContact.Contains(q.PurContact!))
- .WhereIF(q.IsActive.HasValue, x => x.IsActive == q.IsActive.Value)
- .WhereIF(
- !string.IsNullOrWhiteSpace(q.Keyword),
- x => x.Supp.Contains(q.Keyword!)
- || (x.SortName != null && x.SortName.Contains(q.Keyword!))
- || (x.Address != null && x.Address.Contains(q.Keyword!))
- || (x.Contact != null && x.Contact.Contains(q.Keyword!))
- || (x.ContactInfo != null && x.ContactInfo.Contains(q.Keyword!)));
- var total = await baseQuery.CountAsync();
- var list = await baseQuery
- .OrderByDescending(x => x.CreateTime)
- .Skip((q.Page - 1) * q.PageSize)
- .Take(q.PageSize)
- .ToListAsync();
- await ApplyLocationNamesAsync(list);
- foreach (var s in list)
- {
- s.APStartDay1Text = FormatApDay(s.APStartDay1);
- s.APEndDay1Text = FormatApDay(s.APEndDay1);
- s.APStartDay2Text = FormatApDay(s.APStartDay2);
- s.APEndDay2Text = FormatApDay(s.APEndDay2);
- }
- return Ok(new { total, page = q.Page, pageSize = q.PageSize, list });
- }
- [HttpGet("{id:long}")]
- public async Task<IActionResult> GetAsync(long id)
- {
- var item = await _rep.GetByIdAsync(id);
- if (item == null) return NotFound();
- await ApplyLocationNamesAsync(new List<AdoS0SuppMaster> { item });
- item.APStartDay1Text = FormatApDay(item.APStartDay1);
- item.APEndDay1Text = FormatApDay(item.APEndDay1);
- item.APStartDay2Text = FormatApDay(item.APStartDay2);
- item.APEndDay2Text = FormatApDay(item.APEndDay2);
- return Ok(item);
- }
- [HttpPost]
- public async Task<IActionResult> CreateAsync([FromBody] AdoS0SuppMasterUpsertDto dto)
- {
- if (await _rep.IsAnyAsync(x => x.FactoryRefId == dto.FactoryRefId && x.Supp == dto.Supp))
- return AdoS0ApiErrors.Conflict(AdoS0ErrorCodes.SupplierCodeExists, "供应商编码已存在");
- var now = DateTime.Now;
- var entity = new AdoS0SuppMaster
- {
- CompanyRefId = dto.CompanyRefId,
- FactoryRefId = dto.FactoryRefId,
- DomainCode = dto.DomainCode,
- Supp = dto.Supp,
- SortName = dto.SortName,
- Address = dto.Address,
- Contact = dto.Contact,
- Position = dto.Position,
- ContactInfo = dto.ContactInfo,
- CertificationLevel = dto.CertificationLevel,
- PurContact = dto.PurContact,
- CrTerms = dto.CrTerms,
- Curr = dto.Curr,
- Type = dto.Type,
- TaxIn = dto.TaxIn,
- TaxClass = dto.TaxClass,
- APStartDay1 = dto.APStartDay1,
- APEndDay1 = dto.APEndDay1,
- APStartDay2 = dto.APStartDay2,
- APEndDay2 = dto.APEndDay2,
- Remark = dto.Remark,
- IsActive = dto.IsActive,
- CreateUser = dto.CreateUser,
- CreateTime = now,
- UpdateUser = dto.UpdateUser,
- UpdateTime = null
- };
- await _rep.AsInsertable(entity).ExecuteReturnEntityAsync();
- return Ok(entity);
- }
- [HttpPut("{id:long}")]
- public async Task<IActionResult> UpdateAsync(long id, [FromBody] AdoS0SuppMasterUpsertDto 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.Supp == dto.Supp))
- return AdoS0ApiErrors.Conflict(AdoS0ErrorCodes.SupplierCodeExists, "供应商编码已存在");
- entity.CompanyRefId = dto.CompanyRefId;
- entity.FactoryRefId = dto.FactoryRefId;
- entity.DomainCode = dto.DomainCode;
- entity.Supp = dto.Supp;
- entity.SortName = dto.SortName;
- entity.Address = dto.Address;
- entity.Contact = dto.Contact;
- entity.Position = dto.Position;
- entity.ContactInfo = dto.ContactInfo;
- entity.CertificationLevel = dto.CertificationLevel;
- entity.PurContact = dto.PurContact;
- entity.CrTerms = dto.CrTerms;
- entity.Curr = dto.Curr;
- entity.Type = dto.Type;
- entity.TaxIn = dto.TaxIn;
- entity.TaxClass = dto.TaxClass;
- entity.APStartDay1 = dto.APStartDay1;
- entity.APEndDay1 = dto.APEndDay1;
- entity.APStartDay2 = dto.APStartDay2;
- entity.APEndDay2 = dto.APEndDay2;
- entity.Remark = dto.Remark;
- entity.IsActive = dto.IsActive;
- entity.UpdateUser = dto.UpdateUser;
- entity.UpdateTime = DateTime.Now;
- await _rep.AsUpdateable(entity).ExecuteCommandAsync();
- return Ok(entity);
- }
- [HttpPatch("{id:long}/toggle-enabled")]
- public async Task<IActionResult> ToggleActiveAsync(long id, [FromBody] AdoS0SuppMasterToggleActiveDto 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<IActionResult> DeleteAsync(long id)
- {
- var item = await _rep.GetByIdAsync(id);
- if (item == null) return NotFound();
- await _rep.DeleteAsync(item);
- return Ok(new { message = "删除成功" });
- }
- /// <summary>
- /// 按 Domain + PurContact 批量匹配库位主数据,填充 LocationName(与源平台 left join LocationMaster 一致)。
- /// </summary>
- private async Task ApplyLocationNamesAsync(List<AdoS0SuppMaster> suppliers)
- {
- if (suppliers.Count == 0) return;
- var domainCodes = suppliers
- .Select(s => s.DomainCode)
- .Where(d => !string.IsNullOrWhiteSpace(d))
- .Distinct()
- .ToList();
- if (domainCodes.Count == 0)
- {
- foreach (var s in suppliers)
- s.LocationName = (s.PurContact ?? "").Trim();
- return;
- }
- var locRows = await _locRep.AsQueryable()
- .Where(l => domainCodes.Contains(l.DomainCode))
- .ToListAsync();
- foreach (var s in suppliers)
- {
- if (string.IsNullOrWhiteSpace(s.DomainCode) || string.IsNullOrWhiteSpace(s.PurContact))
- {
- s.LocationName = (s.PurContact ?? "").Trim();
- continue;
- }
- var loc = locRows.Find(l =>
- string.Equals(l.DomainCode, s.DomainCode, StringComparison.OrdinalIgnoreCase)
- && string.Equals(l.Location, s.PurContact, StringComparison.OrdinalIgnoreCase));
- s.LocationName = FormatLocationDisplay(s.PurContact, loc);
- }
- }
- /// <summary>与 MySQL:TRIM(CONCAT(l.Location,' ',IFNULL(l.Descr,''))) 一致;无匹配时退回库位编码。</summary>
- private static string FormatLocationDisplay(string? purContact, AdoS0LocationMaster? loc)
- {
- if (loc != null && !string.IsNullOrWhiteSpace(loc.Location))
- {
- var tail = loc.Descr ?? "";
- return $"{loc.Location.Trim()} {tail}".Trim();
- }
- return (purContact ?? "").Trim();
- }
- private static string FormatApDay(int? value)
- {
- if (!value.HasValue || value.Value == 0) return "";
- var v = value.Value;
- var prefix = v > 200 ? "下月" : "每月";
- var day = (v % 100).ToString("00");
- if (day == "31") day = "最后";
- return $"{prefix}{day}日";
- }
- }
|