AdoS0ContractReviewCycleSyncFlagsController.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using Admin.NET.Plugin.AiDOP.Dto.S0.Sales;
  2. using Admin.NET.Plugin.AiDOP.Entity.S0.Sales;
  3. using Admin.NET.Plugin.AiDOP.Infrastructure;
  4. namespace Admin.NET.Plugin.AiDOP.Controllers.S0.Sales;
  5. /// <summary>
  6. /// 合同评审周期同步开关:工厂级一行。
  7. /// GET 行不存在时返回 IsSyncEnabled=false 兜底;PUT 走 upsert。
  8. /// </summary>
  9. [ApiController]
  10. [Route("api/s0/sales/contract-review-cycle-sync-flag")]
  11. [NonUnify]
  12. public class AdoS0ContractReviewCycleSyncFlagsController : ControllerBase
  13. {
  14. private readonly SqlSugarRepository<AdoS0ContractReviewCycleSyncFlag> _rep;
  15. public AdoS0ContractReviewCycleSyncFlagsController(SqlSugarRepository<AdoS0ContractReviewCycleSyncFlag> rep)
  16. => _rep = rep;
  17. [HttpGet]
  18. public async Task<IActionResult> GetByFactoryAsync([FromQuery] long factoryRefId)
  19. {
  20. if (!AdoS0TenantScope.TryResolveRequired(out var tenantId, out var tenantError)) return tenantError!;
  21. if (factoryRefId <= 0) return BadRequest(new { message = "factoryRefId 不能为空" });
  22. var entity = await _rep.AsQueryable()
  23. .Where(x => x.TenantId == tenantId)
  24. .Where(x => x.FactoryRefId == factoryRefId)
  25. .FirstAsync();
  26. if (entity == null)
  27. {
  28. return Ok(new AdoS0ContractReviewCycleSyncFlagDto
  29. {
  30. FactoryRefId = factoryRefId,
  31. IsSyncEnabled = false,
  32. });
  33. }
  34. return Ok(new AdoS0ContractReviewCycleSyncFlagDto
  35. {
  36. Id = entity.Id,
  37. CompanyRefId = entity.CompanyRefId,
  38. FactoryRefId = entity.FactoryRefId,
  39. DomainCode = entity.DomainCode,
  40. IsSyncEnabled = entity.IsSyncEnabled,
  41. });
  42. }
  43. [HttpPut]
  44. public async Task<IActionResult> UpsertAsync([FromBody] AdoS0ContractReviewCycleSyncFlagUpsertDto dto)
  45. {
  46. if (!AdoS0TenantScope.TryResolveRequired(out var tenantId, out var tenantError)) return tenantError!;
  47. // B-1b:本接口的工厂维度由页面自身驱动(合同评审周期页按所选工厂同步),
  48. // 故不强制 stamp 解析值(会与页面选择错位),改为放行本租户组织、拒绝他租户组织。
  49. var orgError = await AdoS0OrgScope.ValidateOrgBelongsToTenantAsync(_rep.Context, tenantId, dto.CompanyRefId, dto.FactoryRefId);
  50. if (orgError != null) return orgError;
  51. var now = DateTime.Now;
  52. var entity = await _rep.AsQueryable()
  53. .Where(x => x.TenantId == tenantId)
  54. .Where(x => x.FactoryRefId == dto.FactoryRefId)
  55. .FirstAsync();
  56. if (entity == null)
  57. {
  58. entity = new AdoS0ContractReviewCycleSyncFlag
  59. {
  60. TenantId = tenantId,
  61. CompanyRefId = dto.CompanyRefId,
  62. FactoryRefId = dto.FactoryRefId,
  63. DomainCode = dto.DomainCode,
  64. IsSyncEnabled = dto.IsSyncEnabled,
  65. CreateUser = dto.CreateUser,
  66. CreateTime = now,
  67. };
  68. await _rep.AsInsertable(entity).ExecuteReturnEntityAsync();
  69. }
  70. else
  71. {
  72. entity.CompanyRefId = dto.CompanyRefId;
  73. entity.DomainCode = dto.DomainCode;
  74. entity.IsSyncEnabled = dto.IsSyncEnabled;
  75. entity.UpdateUser = dto.UpdateUser;
  76. entity.UpdateTime = now;
  77. await _rep.AsUpdateable(entity).ExecuteCommandAsync();
  78. }
  79. return Ok(new AdoS0ContractReviewCycleSyncFlagDto
  80. {
  81. Id = entity.Id,
  82. CompanyRefId = entity.CompanyRefId,
  83. FactoryRefId = entity.FactoryRefId,
  84. DomainCode = entity.DomainCode,
  85. IsSyncEnabled = entity.IsSyncEnabled,
  86. });
  87. }
  88. }