406 lines
13 KiB
C#
406 lines
13 KiB
C#
|
|
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<UserController>
|
|
{
|
|
|
|
private readonly IUserRepository userRepository;
|
|
public UserController(IUserRepository userRepository) : base()
|
|
{
|
|
this.userRepository = userRepository;
|
|
}
|
|
|
|
public IActionResult Index()
|
|
{
|
|
return View();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 取得個人資訊
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
public async Task<ApiResult<User>> GetPersonalInfo()
|
|
{
|
|
ApiResult<User> apiResult = new ApiResult<User>();
|
|
|
|
try
|
|
{
|
|
var user = await userRepository.GetOneAsync(mySimpleUser.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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 修改個人資料
|
|
/// </summary>
|
|
/// <param name="post"></param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
public async Task<ApiResult<string>> SavePersonalInfoAsync(PostPersonalInfo post)
|
|
{
|
|
ApiResult<string> apiResult = new ApiResult<string>();
|
|
|
|
User user = null;
|
|
try
|
|
{
|
|
user = await userRepository.GetOneAsync(mySimpleUser.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 = mySimpleUser.Id,
|
|
Id = user.Id
|
|
};
|
|
|
|
List<string> properties = new List<string>()
|
|
{
|
|
"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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 變更密碼
|
|
/// </summary>
|
|
/// <param name="post"></param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
public async Task<ApiResult<string>> ChangePasswordAsync(PostChangePassword post)
|
|
{
|
|
ApiResult<string> apiResult = new ApiResult<string>();
|
|
|
|
User user = null;
|
|
try
|
|
{
|
|
user = await userRepository.GetOneAsync(mySimpleUser.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 = mySimpleUser.Id,
|
|
Id = user.Id
|
|
};
|
|
|
|
List<string> properties = new List<string>()
|
|
{
|
|
"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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 帳號管理列表
|
|
/// </summary>
|
|
/// <param name="post"></param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
public async Task<ActionResult> UserListAsync(PostUserFilter post)
|
|
{
|
|
ApiResult<List<UserDateTable>> apiResult = new ApiResult<List<UserDateTable>>();
|
|
|
|
int totalRecords = 0; //總資料筆數
|
|
int recFilter = 0; //過濾後資料筆數
|
|
|
|
List<UserDateTable> 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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 取得單一使用者
|
|
/// </summary>
|
|
/// <param name="guid"></param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
public async Task<ApiResult<SimpleUser>> GetOneUser(int id)
|
|
{
|
|
ApiResult<SimpleUser> apiResult = new ApiResult<SimpleUser>();
|
|
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 新增 / 修改 使用者
|
|
/// </summary>
|
|
/// <param name="post"></param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
public async Task<ApiResult<string>> SaveUser(PostUser post)
|
|
{
|
|
ApiResult<string> apiResult = new ApiResult<string>();
|
|
|
|
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),
|
|
Phone = post.Phone,
|
|
CreatedBy = mySimpleUser.Id,
|
|
};
|
|
|
|
List<string> properties = new List<string>()
|
|
{
|
|
"CompanyId",
|
|
"Name",
|
|
"Email",
|
|
"Account",
|
|
"Password",
|
|
"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 = mySimpleUser.Id,
|
|
};
|
|
|
|
|
|
List<string> properties = new List<string>()
|
|
{
|
|
"Id",
|
|
"Name",
|
|
"Status",
|
|
"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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 軟刪除單一系統管理員
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
public async Task<ApiResult<string>> DeleteOneUser(int id)
|
|
{
|
|
ApiResult<string> apiResult = new ApiResult<string>();
|
|
|
|
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;
|
|
}
|
|
}
|
|
}
|