FIC_Solar/SolarPower/Controllers/UserController.cs
Kai 886e1aa450 1. 新增帳號寄送信件
2. 點圖放大 part1
3. 運維作業記錄 日期範圍
2021-06-28 14:01:34 +08:00

429 lines
14 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 SolarPower.Services.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;
private readonly ISendEmailService sendEmailService;
private string logoPath = "/upload/company_logo/";
public UserController(IUserRepository userRepository,
ISendEmailService sendEmailService) : base()
{
this.userRepository = userRepository;
this.sendEmailService = sendEmailService;
}
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(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;
}
/// <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(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<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(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<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 + "】");
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();
//隨機產生亂數密碼
Random random = new Random((int)DateTime.Now.Ticks);
const string chars = "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz23456789";
string random_password = new string(Enumerable.Repeat(chars, 8).Select(s => s[random.Next(chars.Length)]).ToArray());
var newPassword = edFunction.GetSHA256Encryption(random_password);
user = new User()
{
CompanyId = post.CompanyId,
Name = post.Name,
Email = post.Email,
Account = post.Account,
Password = newPassword,
RoleId = post.RoleId,
Phone = post.Phone,
CreatedBy = myUser.Id,
};
List<string> properties = new List<string>()
{
"CompanyId",
"Name",
"Email",
"Account",
"Password",
"RoleId",
"Phone",
"CreatedBy",
};
await userRepository.AddAsync(user, properties);
var sendSubject = "新增帳號成功";
var sendContent = $"您的新密碼為:{random_password}";
List<string> recipientEmails = new List<string>()
{
user.Email
};
sendEmailService.Send(recipientEmails, sendSubject, sendContent);
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<string> properties = new List<string>()
{
"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;
}
/// <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;
}
}
}