using System.Collections.Concurrent; using System.Text.Json; using YLErp.Helpers; namespace YLErp.Web.WebAPI.Controllers { /// /// 仅供本地开发验证收益互换移动审批调用链的 OA 模拟接口。 /// [ApiController] [Route("api/mock/tradeApprovalOa")] public class TradeApprovalOaMockController : ControllerBase { private static readonly ConcurrentDictionary Flows = new(); private static readonly JsonSerializerOptions JsonOptions = new() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }; [HttpPost("createOaFlow")] public IActionResult CreateOaFlow([FromBody] JsonElement payload) { if (!IsEnabled()) return NotFound(); var requestId = "mock-" + Guid.NewGuid().ToString("N"); Flows[requestId] = new MockOaFlow { RequestId = requestId }; return Json(new { code = 0, data = new { msg = string.Empty, requestid = requestId }, status = "SUCCESS" }); } [HttpGet("getRequestData")] public IActionResult GetRequestData([FromQuery] string requestId) { if (!IsEnabled()) return NotFound(); if (!Flows.TryGetValue(requestId ?? string.Empty, out var flow)) return Json(new { code = 0, data = Array.Empty(), status = "SUCCESS" }); return Json(new { code = 0, data = new[] { new { requestId = flow.RequestId, flowStatusType = flow.FlowStatusType, status = flow.Status } }, status = "SUCCESS" }); } [HttpPost("forceEndOaFlow")] public IActionResult ForceEndOaFlow([FromQuery] string requestId) { if (!IsEnabled()) return NotFound(); if (requestId != null && Flows.TryGetValue(requestId, out var flow)) { flow.FlowStatusType = "3"; flow.Status = "归档"; flow.IsEnded = true; } return Json(new { code = 0, data = new { resultcode = "0" }, status = "SUCCESS" }); } [HttpPost("setStatus")] public IActionResult SetStatus([FromQuery] string requestId, [FromQuery] string flowStatusType, [FromQuery] string status = null) { if (!IsEnabled()) return NotFound(); if (flowStatusType != "0" && flowStatusType != "1" && flowStatusType != "2" && flowStatusType != "3") return BadRequest(new { code = 1, msg = "flowStatusType 只能为 0、1 或 2" }); if (!Flows.TryGetValue(requestId ?? string.Empty, out var flow)) return NotFound(new { code = 1, msg = "未找到 requestId" }); if (flow.IsEnded) return BadRequest(new { code = 1, msg = "OA 流程已归档" }); flow.FlowStatusType = flowStatusType; if (!string.IsNullOrWhiteSpace(status)) flow.Status = status; return Json(new { code = 0, data = new { requestId, flowStatusType, status = flow.Status }, status = "SUCCESS" }); } [HttpGet("flows")] public IActionResult GetFlows() { if (!IsEnabled()) return NotFound(); return Json(new { code = 0, data = Flows.Values.Select(x => new { requestId = x.RequestId, flowStatusType = x.FlowStatusType, status = x.Status, isEnded = x.IsEnded }), status = "SUCCESS" }); } private static bool IsEnabled() { return bool.TryParse(AppManager.GetConfiguration()["TradeApprovalOaMock:Enabled"], out var enabled) && enabled; } private JsonResult Json(object value) { return new JsonResult(value, JsonOptions); } private sealed class MockOaFlow { public string RequestId { get; set; } public string FlowStatusType { get; set; } = "0"; public string Status { get; set; } = "创建"; public bool IsEnded { get; set; } } } }