using Microsoft.AspNetCore.Mvc; using QYZH.InteractiveMagazine.IService; using QYZH.InteractiveMagazine.IService.Dto; using QYZH.InteractiveMagazine.Models.Common; using QYZH.InteractiveMagazine.Models.Dto; namespace QYZH.InteractiveMagazine.WebApi.Controllers; [Route("api/[controller]")] [ApiController] public class WxUserController : BaseController { private readonly IWxUserService _wxUserService; private readonly ILogger _logger; public WxUserController(IWxUserService wxUserService, ILogger logger) { _wxUserService = wxUserService; _logger = logger; } [HttpPost("users")] public async Task> CreateUserAsync([FromBody] WxUserInput input) { try { var result = await _wxUserService.CreateAsync(input); return Success(result, "创建微信用户成功"); } catch (BusinessException ex) { _logger.LogWarning(ex, "创建微信用户业务异常: {Message}", ex.Message); return BaseResponse.Fail(ex.Message, ex.Code); } catch (Exception ex) { _logger.LogError(ex, "创建微信用户系统异常,参数:{Input}", input); return BaseResponse.Fail("创建微信用户失败,请稍后重试", 500); } } [HttpPut("users/{id}")] public async Task> UpdateUserAsync(long id, [FromBody] WxUserInput input) { try { var result = await _wxUserService.UpdateAsync(id, input); return Success(result, "更新微信用户成功"); } catch (BusinessException ex) { _logger.LogWarning(ex, "更新微信用户业务异常: {Message}", ex.Message); return BaseResponse.Fail(ex.Message, ex.Code); } catch (Exception ex) { _logger.LogError(ex, "更新微信用户系统异常,ID:{Id},参数:{Input}", id, input); return BaseResponse.Fail("更新微信用户失败,请稍后重试", 500); } } [HttpDelete("users/{id}")] public async Task> DeleteUserAsync(long id) { try { await _wxUserService.DeleteAsync(id); return Success(new object(), "删除微信用户成功"); } catch (BusinessException ex) { _logger.LogWarning(ex, "删除微信用户业务异常: {Message}", ex.Message); return BaseResponse.Fail(ex.Message, ex.Code); } catch (Exception ex) { _logger.LogError(ex, "删除微信用户系统异常,ID:{Id}", id); return BaseResponse.Fail("删除微信用户失败,请稍后重试", 500); } } [HttpGet("users/{id}")] public async Task> GetUserByIdAsync(long id) { try { var result = await _wxUserService.GetByIdAsync(id); return Success(result); } catch (BusinessException ex) { _logger.LogWarning(ex, "获取微信用户业务异常: {Message}", ex.Message); return BaseResponse.Fail(ex.Message, ex.Code); } catch (Exception ex) { _logger.LogError(ex, "获取微信用户系统异常,ID:{Id}", id); return BaseResponse.Fail("获取微信用户信息失败,请稍后重试", 500); } } [HttpPost("users/list")] public async Task>> GetUsersListAsync([FromBody] WxUserQueryInput input) { try { var result = await _wxUserService.GetListAsync(input); return Success(result); } catch (BusinessException ex) { _logger.LogWarning(ex, "查询微信用户列表业务异常: {Message}", ex.Message); return BaseResponse>.Fail(ex.Message, ex.Code); } catch (Exception ex) { _logger.LogError(ex, "查询微信用户列表系统异常,参数:{Input}", input); return BaseResponse>.Fail("查询微信用户列表失败,请稍后重试", 500); } } }