| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- 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;
- /// <summary>
- /// S0 客户主数据
- /// </summary>
- [ApiController]
- [Route("api/s0/sales/customers")]
- [AllowAnonymous]
- [NonUnify]
- public class AdoS0CustomersController : ControllerBase
- {
- private readonly SqlSugarRepository<AdoS0Customer> _rep;
- public AdoS0CustomersController(SqlSugarRepository<AdoS0Customer> rep)
- {
- _rep = rep;
- }
- [HttpGet]
- public async Task<IActionResult> GetPagedAsync([FromQuery] AdoS0CustomerQueryDto 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.Keyword), x => x.Code.Contains(q.Keyword!) || x.Name.Contains(q.Keyword!))
- .WhereIF(q.IsEnabled.HasValue, x => x.IsEnabled == q.IsEnabled.Value);
- var total = await query.CountAsync();
- var list = await query
- .OrderByDescending(x => x.CreatedAt)
- .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<IActionResult> GetAsync(long id)
- {
- var item = await _rep.GetByIdAsync(id);
- return item == null ? NotFound() : Ok(item);
- }
- [HttpPost]
- public async Task<IActionResult> CreateAsync([FromBody] AdoS0CustomerUpsertDto dto)
- {
- var entity = new AdoS0Customer
- {
- CompanyRefId = dto.CompanyRefId,
- FactoryRefId = dto.FactoryRefId,
- Code = dto.Code,
- Name = dto.Name,
- NameEn = dto.NameEn,
- CustomerType = dto.CustomerType,
- ContactPerson = dto.ContactPerson,
- ContactPhone = dto.ContactPhone,
- Address = dto.Address,
- Currency = dto.Currency,
- IsTaxIncluded = dto.IsTaxIncluded,
- PrimarySales = dto.PrimarySales,
- BackupSales = dto.BackupSales,
- CustomerLevel = dto.CustomerLevel,
- Remark = dto.Remark,
- IsEnabled = dto.IsEnabled,
- CreatedAt = DateTime.Now
- };
- entity.ForbidStatus = AdoS0SalesRules.ForbidStatusFromIsEnabled(dto.IsEnabled);
- await _rep.AsInsertable(entity).ExecuteReturnEntityAsync();
- return Ok(entity);
- }
- [HttpPut("{id:long}")]
- public async Task<IActionResult> UpdateAsync(long id, [FromBody] AdoS0CustomerUpsertDto dto)
- {
- var entity = await _rep.GetByIdAsync(id);
- if (entity == null) return NotFound();
- entity.CompanyRefId = dto.CompanyRefId;
- entity.FactoryRefId = dto.FactoryRefId;
- entity.Code = dto.Code;
- entity.Name = dto.Name;
- entity.NameEn = dto.NameEn;
- entity.CustomerType = dto.CustomerType;
- entity.ContactPerson = dto.ContactPerson;
- entity.ContactPhone = dto.ContactPhone;
- entity.Address = dto.Address;
- entity.Currency = dto.Currency;
- entity.IsTaxIncluded = dto.IsTaxIncluded;
- entity.PrimarySales = dto.PrimarySales;
- entity.BackupSales = dto.BackupSales;
- entity.CustomerLevel = dto.CustomerLevel;
- entity.Remark = dto.Remark;
- entity.IsEnabled = dto.IsEnabled;
- entity.ForbidStatus = AdoS0SalesRules.ForbidStatusFromIsEnabled(dto.IsEnabled);
- entity.UpdatedAt = DateTime.Now;
- await _rep.AsUpdateable(entity).ExecuteCommandAsync();
- return Ok(entity);
- }
- [HttpPatch("{id:long}/toggle-enabled")]
- public async Task<IActionResult> ToggleEnabledAsync(long id, [FromBody] AdoS0ToggleEnabledDto dto)
- {
- var entity = await _rep.GetByIdAsync(id);
- if (entity == null) return NotFound();
- entity.IsEnabled = dto.IsEnabled;
- entity.ForbidStatus = AdoS0SalesRules.ForbidStatusFromIsEnabled(dto.IsEnabled);
- entity.UpdatedAt = 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 = "删除成功" });
- }
- }
|