AdoS0ContractReviewCycleSyncFlagsController.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. var now = DateTime.Now;
  48. var entity = await _rep.AsQueryable()
  49. .Where(x => x.TenantId == tenantId)
  50. .Where(x => x.FactoryRefId == dto.FactoryRefId)
  51. .FirstAsync();
  52. if (entity == null)
  53. {
  54. entity = new AdoS0ContractReviewCycleSyncFlag
  55. {
  56. TenantId = tenantId,
  57. CompanyRefId = dto.CompanyRefId,
  58. FactoryRefId = dto.FactoryRefId,
  59. DomainCode = dto.DomainCode,
  60. IsSyncEnabled = dto.IsSyncEnabled,
  61. CreateUser = dto.CreateUser,
  62. CreateTime = now,
  63. };
  64. await _rep.AsInsertable(entity).ExecuteReturnEntityAsync();
  65. }
  66. else
  67. {
  68. entity.CompanyRefId = dto.CompanyRefId;
  69. entity.DomainCode = dto.DomainCode;
  70. entity.IsSyncEnabled = dto.IsSyncEnabled;
  71. entity.UpdateUser = dto.UpdateUser;
  72. entity.UpdateTime = now;
  73. await _rep.AsUpdateable(entity).ExecuteCommandAsync();
  74. }
  75. return Ok(new AdoS0ContractReviewCycleSyncFlagDto
  76. {
  77. Id = entity.Id,
  78. CompanyRefId = entity.CompanyRefId,
  79. FactoryRefId = entity.FactoryRefId,
  80. DomainCode = entity.DomainCode,
  81. IsSyncEnabled = entity.IsSyncEnabled,
  82. });
  83. }
  84. }