using Admin.NET.Plugin.AiDOP.Dto.S0.Warehouse;
using Admin.NET.Plugin.AiDOP.Entity.S0.Warehouse;
using Admin.NET.Plugin.AiDOP.Infrastructure;
namespace Admin.NET.Plugin.AiDOP.Controllers.S0.Warehouse;
///
/// S0 物料职责维护(EmpWorkDutyMaster 语义)
///
[ApiController]
[Route("api/s0/warehouse/emp-work-duties")]
[AllowAnonymous]
[NonUnify]
public class AdoS0EmpWorkDutiesController : ControllerBase
{
private readonly SqlSugarRepository _rep;
private readonly SqlSugarRepository _empRep;
private readonly SqlSugarRepository _locRep;
public AdoS0EmpWorkDutiesController(
SqlSugarRepository rep,
SqlSugarRepository empRep,
SqlSugarRepository locRep)
{
_rep = rep;
_empRep = empRep;
_locRep = locRep;
}
[HttpGet]
public async Task GetPagedAsync([FromQuery] AdoS0EmpWorkDutyQueryDto 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.Employee), x => x.Employee.Contains(q.Employee!))
.WhereIF(!string.IsNullOrWhiteSpace(q.Location), x => x.Location == q.Location)
.WhereIF(!string.IsNullOrWhiteSpace(q.Keyword),
x => x.Employee.Contains(q.Keyword!) || (x.ItemNum1 != null && x.ItemNum1.Contains(q.Keyword!)));
var total = await query.CountAsync();
var list = await query
.OrderBy(x => x.Employee)
.Skip((q.Page - 1) * q.PageSize)
.Take(q.PageSize)
.ToListAsync();
await ApplyDisplayFieldsAsync(list);
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);
if (item == null) return NotFound();
await ApplyDisplayFieldsAsync(new List { item });
return Ok(item);
}
[HttpPost]
public async Task CreateAsync([FromBody] AdoS0EmpWorkDutyUpsertDto dto)
{
var entity = new AdoS0EmpWorkDutyMaster
{
CompanyRefId = dto.CompanyRefId,
FactoryRefId = dto.FactoryRefId,
DomainCode = dto.DomainCode ?? string.Empty,
Employee = dto.Employee,
ItemNum1 = dto.ItemNum1,
ItemNum2 = dto.ItemNum2,
Location = dto.Location,
ProdLine = dto.ProdLine,
Duty = dto.Duty,
EmpType = dto.EmpType,
Ufld2 = dto.Ufld2,
CreateUser = dto.CreateUser,
CreateTime = DateTime.Now
};
await _rep.AsInsertable(entity).ExecuteReturnEntityAsync();
return Ok(entity);
}
[HttpPut("{id:long}")]
public async Task UpdateAsync(long id, [FromBody] AdoS0EmpWorkDutyUpsertDto dto)
{
var entity = await _rep.GetByIdAsync(id);
if (entity == null) return NotFound();
entity.CompanyRefId = dto.CompanyRefId;
entity.FactoryRefId = dto.FactoryRefId;
entity.DomainCode = dto.DomainCode ?? string.Empty;
entity.Employee = dto.Employee;
entity.ItemNum1 = dto.ItemNum1;
entity.ItemNum2 = dto.ItemNum2;
entity.Location = dto.Location;
entity.ProdLine = dto.ProdLine;
entity.Duty = dto.Duty;
entity.EmpType = dto.EmpType;
entity.Ufld2 = dto.Ufld2;
entity.UpdateUser = dto.UpdateUser;
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 = "删除成功" });
}
private static readonly Dictionary DutyMap = new(StringComparer.OrdinalIgnoreCase)
{
["iss-po"] = "采购收货",
["iss-so"] = "销售发货",
["iss-wo"] = "发料",
["packing"] = "退料",
["rct-unp"] = "计划外入库",
["rct-wo"] = "入库",
["Up-Shelf"] = "上架"
};
private async Task ApplyDisplayFieldsAsync(List duties)
{
if (duties.Count == 0) return;
var domainCodes = duties.Select(d => d.DomainCode).Distinct().ToList();
var employees = await _empRep.AsQueryable()
.Where(e => domainCodes.Contains(e.DomainCode))
.ToListAsync();
var locations = await _locRep.AsQueryable()
.Where(l => domainCodes.Contains(l.DomainCode))
.ToListAsync();
foreach (var duty in duties)
{
var emp = employees.Find(e =>
string.Equals(e.DomainCode, duty.DomainCode, StringComparison.OrdinalIgnoreCase)
&& string.Equals(e.Employee, duty.Employee, StringComparison.OrdinalIgnoreCase));
duty.EmployeeName = emp?.Name;
if (!string.IsNullOrWhiteSpace(duty.Location))
{
var loc = locations.Find(l =>
string.Equals(l.DomainCode, duty.DomainCode, StringComparison.OrdinalIgnoreCase)
&& string.Equals(l.Location, duty.Location, StringComparison.OrdinalIgnoreCase));
duty.LocationName = $"{duty.Location} {loc?.Descr ?? ""}".Trim();
}
// Duty 翻译(原始多值逗号/空格拼接场景兼容)
if (!string.IsNullOrWhiteSpace(duty.Duty))
{
var parts = duty.Duty.Split(new[] { ',', ';', ' ' }, StringSplitOptions.RemoveEmptyEntries);
duty.DutyDisplay = string.Join("、", parts.Select(p => DutyMap.TryGetValue(p.Trim(), out var cn) ? cn : p.Trim()));
}
}
}
}