using Dapper; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; using SolarPower.Models; using SolarPower.Models.User; using SolarPower.Repository.Interface; using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Threading.Tasks; namespace SolarPower.Controllers { public class UserController : MyBaseController { private readonly IUserRepository userRepository; public UserController(IUserRepository userRepository) : base() { this.userRepository = userRepository; } public IActionResult Index() { return View(); } /// /// 取得個人資訊 /// /// [HttpPost] public async Task> GetPersonalInfo() { ApiResult apiResult = new ApiResult(); try { var user = await userRepository.GetOneAsync(myUser.Id); apiResult.Code = "0000"; apiResult.Data = user; } catch (Exception exception) { apiResult.Code = "9999"; Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } apiResult.Msg = errorCode.GetString(apiResult.Code); return apiResult; } /// /// 修改個人資料 /// /// /// [HttpPost] public async Task> SavePersonalInfoAsync(PostPersonalInfo post) { ApiResult apiResult = new ApiResult(); User user = null; try { user = await userRepository.GetOneAsync(myUser.Id); if (user == null) { apiResult.Code = "9998"; apiResult.Msg = errorCode.GetString(apiResult.Code); return apiResult; } UpdateUser update = new UpdateUser() { Name = post.Name, Email = post.Email, Phone = post.Phone, UpdatedBy = myUser.Id, Id = user.Id }; List properties = new List() { "Name", "Email", "Phone", "UpdatedBy", "UpdatedAt", "Id" }; await userRepository.UpdatePersonInfo(update, properties); apiResult.Code = "0000"; apiResult.Msg = "修改成功"; } catch (Exception exception) { apiResult.Code = "9999"; string json = System.Text.Json.JsonSerializer.Serialize(post); Logger.LogError("【" + controllerName + "/" + actionName + "】" + json); Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } return apiResult; } /// /// 變更密碼 /// /// /// [HttpPost] public async Task> ChangePasswordAsync(PostChangePassword post) { ApiResult apiResult = new ApiResult(); User user = null; try { user = await userRepository.GetOneAsync(myUser.Id); if (user == null) { apiResult.Code = "9998"; apiResult.Msg = errorCode.GetString(apiResult.Code); return apiResult; } EDFunction edFunction = new EDFunction(); if (string.Compare(user.Password, edFunction.GetSHA256Encryption(post.OldPassword)) != 0) { apiResult.Code = "0001"; apiResult.Msg = "密碼錯誤,請重新輸入。"; return apiResult; } if (string.Compare(post.NewPassword, post.AgainPassword) != 0) { apiResult.Code = "0001"; apiResult.Msg = "新密碼輸入不一致,請重新輸入。"; return apiResult; } UpdatePassword update = new UpdatePassword() { Password = edFunction.GetSHA256Encryption(post.NewPassword), UpdatedBy = myUser.Id, Id = user.Id }; List properties = new List() { "Password", "UpdatedBy", "UpdatedAt", "Id" }; await userRepository.UpdatePassword(update, properties); apiResult.Code = "0000"; apiResult.Msg = "修改成功"; } catch (Exception exception) { apiResult.Code = "9999"; string json = System.Text.Json.JsonSerializer.Serialize(post); Logger.LogError("【" + controllerName + "/" + actionName + "】" + json); Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } return apiResult; } /// /// 帳號管理列表 /// /// /// [HttpPost] public async Task UserListAsync(PostUserFilter post) { ApiResult> apiResult = new ApiResult>(); int totalRecords = 0; //總資料筆數 int recFilter = 0; //過濾後資料筆數 List users = null; try { users = await userRepository.GetAllByFilterAsync(post); totalRecords = users.Count(); recFilter = users.Count(); apiResult.Code = "0000"; apiResult.Data = users; } catch (Exception exception) { apiResult.Code = "9999"; string json = System.Text.Json.JsonSerializer.Serialize(post); Logger.LogError("【" + controllerName + "/" + actionName + "】" + json); Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } apiResult.Msg = errorCode.GetString(apiResult.Code); var result = Json(new { recordsTotal = totalRecords, recordsFiltered = recFilter, data = apiResult }); return result; } /// /// 取得單一使用者 /// /// /// [HttpPost] public async Task> GetOneUser(int id) { ApiResult apiResult = new ApiResult(); SimpleUser simpleUser = null; try { simpleUser = await userRepository.GetOneSimpleUser(id); if (simpleUser == null) { apiResult.Code = "9998"; apiResult.Msg = errorCode.GetString(apiResult.Code); return apiResult; } apiResult.Code = "0000"; apiResult.Data = simpleUser; } catch (Exception exception) { apiResult.Code = "9999"; Logger.LogError("【" + controllerName + "/" + actionName + "】" + "Id=" + id); Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } apiResult.Msg = errorCode.GetString(apiResult.Code); return apiResult; } /// /// 新增 / 修改 使用者 /// /// /// [HttpPost] public async Task> SaveUser(PostUser post) { ApiResult apiResult = new ApiResult(); User user = null; try { user = await userRepository.GetOneAsync(post.Id); if (user == null) { if (post.Id != 0) { apiResult.Code = "9998"; apiResult.Msg = errorCode.GetString(apiResult.Code); return apiResult; } #region 新增使用者 EDFunction edFunction = new EDFunction(); user = new User() { CompanyId = post.CompanyId, Name = post.Name, Email = post.Email, Account = post.Account, Password = edFunction.GetSHA256Encryption(post.Account), RoleId = post.RoleId, Phone = post.Phone, CreatedBy = myUser.Id, }; List properties = new List() { "CompanyId", "Name", "Email", "Account", "Password", "RoleId", "Phone", "CreatedBy", }; await userRepository.AddAsync(user, properties); apiResult.Code = "0000"; apiResult.Msg = "儲存成功"; #endregion } else { #region 修改使用者 UpdateUser update = new UpdateUser() { Id = post.Id, Name = post.Name, Email = post.Email, Phone = post.Phone, UpdatedBy = myUser.Id, }; List properties = new List() { "Id", "Name", "Email", "Phone", "UpdatedBy", }; await userRepository.UpdatePersonInfo(update, properties); apiResult.Code = "0000"; apiResult.Msg = "儲存成功"; #endregion } } catch (Exception exception) { apiResult.Code = "9999"; apiResult.Msg = errorCode.GetString(apiResult.Code); string json = System.Text.Json.JsonSerializer.Serialize(post); Logger.LogError("【" + controllerName + "/" + actionName + "】" + json); Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } return apiResult; } /// /// 軟刪除單一使用者 /// /// /// [HttpPost] public async Task> DeleteOneUser(int id) { ApiResult apiResult = new ApiResult(); SimpleUser user = null; try { user = await userRepository.GetOneSimpleUser(id); if (user == null) { apiResult.Code = "9998"; apiResult.Msg = errorCode.GetString(apiResult.Code); return apiResult; } await userRepository.DeleteOne(user.Id); apiResult.Code = "0000"; apiResult.Msg = "刪除成功"; } catch (Exception exception) { apiResult.Code = "9999"; apiResult.Msg = errorCode.GetString(apiResult.Code); Logger.LogError("【" + controllerName + "/" + actionName + "】" + "Id=" + id); Logger.LogError("【" + controllerName + "/" + actionName + "】" + exception.Message); } return apiResult; } } }