using LiangLiSystem.Services.Helpers; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Linq; using System.Security.Claims; using System.Threading.Tasks; using Traffic.Data.Enums; using Traffic.Data.ViewModels; using Traffic.Service.Filter; using Traffic.Service.Interfaces; namespace Traffic.Api.Controllers { [Route("api/user")] [Authorize] [ApiController] [ServiceFilter(typeof(LogFilter))] public class AccountController : ControllerBase { private readonly IAccountService _service; private readonly ILogger _logger; public AccountController(IAccountService service, ILogger logger) { _service = service; _logger = logger; } /// /// 取得 使用者資訊 /// /// 使用者 Id /// [HttpGet("{id}")] public IActionResult GetAccountUser(int id) { try { var result = _service.GetAccountUser(id); return Ok(result); } catch (Exception) { return BadRequest(); } } /// /// 取得 UserData 列表 /// /// /// /// [HttpGet("GetUserDataList")] public IActionResult GetUserDataList([FromQuery] SearchModelViewModel searchModel, int? Id) { try { var result = new Response(); var model = Util.GetSearchModel(searchModel); result.Result = _service.GetUserDataList(model, Id); return Ok(result); } catch (Exception) { return BadRequest(); } } /// /// 新增使用者 /// /// 新增的使用者資訊 /// /// 管理者Admin RoleCodeId = 0, 一般User RoleCodeId = 1 [HttpPost("PostUserDataList")] public IActionResult InserAccountUser(UserData account) { try { var userIdentity = (ClaimsIdentity)User.Identity; var mID = new ParserForUserFromClaim(userIdentity).GetUserId(); var result = _service.InserAccountUser(mID, account); return result.Success ? Ok(result) : BadRequest(result); } catch (Exception) { return BadRequest(); } } /// /// 管理者修改使用者狀態 /// /// /// 狀態 /// /// 0:Disable, 1:Enable [HttpPut("PutUserStatus/{id}")] public IActionResult PutUserStatus(int id, int status) { try { var userIdentity = (ClaimsIdentity)User.Identity; var byWho = new ParserForUserFromClaim(userIdentity).GetUserId(); var result = _service.PutUserbyAdmin(byWho, id, status); return result.Success ? Ok(result) : BadRequest(result); } catch (Exception) { return BadRequest(); } } /// /// 管理者修改使用者 /// /// /// 管理者欲修改的使用者資訊 /// /// 管理者Admin RoleCodeId = 0, 一般User RoleCodeId = 1 [HttpPut("PutUserData/{id}")] public IActionResult UpdateAccountbyAdmin(int id, UserData account) { try { var userIdentity = (ClaimsIdentity)User.Identity; var byWho = new ParserForUserFromClaim(userIdentity).GetUserId(); account.Id = id; var result = _service.UpdateAccountbyAdmin(byWho, account); return result.Success ? Ok(result) : BadRequest(result); } catch (Exception) { return BadRequest(); } } /// /// 管理者修改使用者密碼 /// /// 使用者Id /// /// 管理者Admin RoleCodeId = 0, 一般User RoleCodeId = 1 [HttpPut("PutUserPassword/{id}")] public IActionResult UpdateAccountPasswordbyAdmin(int id) { try { var userIdentity = (ClaimsIdentity)User.Identity; var byWho = new ParserForUserFromClaim(userIdentity).GetUserId(); var result = _service.UpdateAccountPasswordbyAdmin(id, byWho); return result.Success ? Ok(result) : BadRequest(result); } catch (Exception) { return BadRequest(); } } ///// ///// 使用者修改自己的帳號資訊 ///// ///// 使用者自己欲修改的資訊 ///// //[HttpPut("")] //public IActionResult UpdateAccountbyUser(UserData account) //{ // var userIdentity = (ClaimsIdentity)User.Identity; // var byWho = new ParserForUserFromClaim(userIdentity).GetUserId(); // var result = _service.UpdateAccountbyUser(byWho, account); // return result.Success ? Ok(result) : BadRequest(result); //} /// /// 修改密碼 /// /// 修改前後的密碼 /// [HttpPut("PutUserPassword")] public IActionResult UpdateAccountPassword(UserData account) { try { var userIdentity = (ClaimsIdentity)User.Identity; var byWho = new ParserForUserFromClaim(userIdentity).GetUserId(); var byWhoAccount = new ParserForUserFromClaim(userIdentity).GetAccount(); account.Id = byWho; account.Account = byWhoAccount; var result = _service.UpdateAccountPasswordByUser(byWho, account); return result.Success ? Ok(result) : BadRequest(result); } catch (Exception ex) { return BadRequest(ex.ToString()); } } /// /// 刪除使用者帳號 /// /// /// [HttpDelete("DeleteUserData")] public IActionResult DeleteUserData(int id) { try { return Ok(_service.DeleteUserData(id)); } catch (Exception) { return BadRequest(); } } /// /// 是否存在此User /// /// /// [HttpGet("GetExistUser")] public IActionResult GetExistUser(string account) { try { var result = _service.GetExistUser(account); return Ok(new { Result = result }); } catch (Exception) { return BadRequest(); } } /// /// 取得使用者名稱列表 /// /// [HttpGet("GetUserNameList")] public IActionResult GetUserNameList() { try { var result = _service.GetUserNameList(); return Ok(new { Result = result }); } catch (Exception) { return BadRequest(); } } /// /// 取得使用者站點列表 /// /// [HttpGet("GetUserSite")] public IActionResult GetUserSite() { try { var userIdentity = (ClaimsIdentity)User.Identity; var id = new ParserForUserFromClaim(userIdentity).GetUserId(); var result = _service.GetUserSite(id); return Ok(new { Result = result }); } catch (Exception) { return BadRequest(); } } /// /// 取得使用者站點資訊列表 /// /// [HttpGet("GetUserSiteInfo")] public IActionResult GetUserSiteInfo() { try { var userIdentity = (ClaimsIdentity)User.Identity; var id = new ParserForUserFromClaim(userIdentity).GetUserId(); var result = _service.GetUserSiteInfo(id); return Ok(new { Result = result }); } catch (Exception) { return BadRequest(); } } } }