316 lines
11 KiB
C#
316 lines
11 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
||
using Microsoft.Extensions.Logging;
|
||
using SolarPower.Models;
|
||
using SolarPower.Models.Company;
|
||
using SolarPower.Repository.Interface;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace SolarPower.Controllers
|
||
{
|
||
public class CompanyController : MyBaseController<CompanyController>
|
||
{
|
||
private readonly ICompanyRepository companyRepository;
|
||
|
||
public CompanyController(ICompanyRepository companyRepository) : base()
|
||
{
|
||
this.companyRepository = companyRepository;
|
||
}
|
||
|
||
public IActionResult Index()
|
||
{
|
||
return View();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 取得下拉式公司選單,須為Deleted: 0
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
[HttpGet]
|
||
public async Task<ApiResult<List<CompanySelectItemList>>> GetCompanySelectOptionListAsync()
|
||
{
|
||
ApiResult<List<CompanySelectItemList>> apiResult = new ApiResult<List<CompanySelectItemList>>();
|
||
|
||
try
|
||
{
|
||
var companySelectItemLists = await companyRepository.GetCompanySelectOptionListAsync();
|
||
|
||
apiResult.Code = "0000";
|
||
apiResult.Data = companySelectItemLists;
|
||
}
|
||
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<ActionResult> CompanyListAsync(PostCompanyFilter post)
|
||
{
|
||
ApiResult<List<CompanyDataTable>> apiResult = new ApiResult<List<CompanyDataTable>>();
|
||
|
||
int totalRecords = 0; //總資料筆數
|
||
int recFilter = 0; //過濾後資料筆數
|
||
|
||
List<CompanyDataTable> companies = null;
|
||
|
||
try
|
||
{
|
||
|
||
companies = await companyRepository.GetAllByFilterAsync(post);
|
||
|
||
foreach(var company in companies)
|
||
{
|
||
//找出當前狀態正常使用者人數
|
||
var registerNumber = await companyRepository.GetNormalUserNumberByCompanyIdAsync(company.Id);
|
||
|
||
company.RegisterRatio = registerNumber.ToString() + " / " + company.RegisterUpperLimit.ToString();
|
||
|
||
//if(mySimpleCompany.Id == 1)
|
||
//{
|
||
company.Function = @"
|
||
<a href='javascript:;' class='btn btn-success btn-pills waves-effect waves-themed' data-toggle='modal' data-target='#companyrule'>權限池</a>
|
||
<button type='button' class='btn btn-primary btn-pills waves-effect waves-themed edit-btn'>修改</button>
|
||
<button type='button' class='btn btn-danger btn-pills waves-effect waves-themed del-btn'>刪除</button>";
|
||
//}
|
||
//else
|
||
//{
|
||
// company.Functoin = @"
|
||
// <button type='button' class='btn btn-primary btn-pills waves-effect waves-themed edit-btn'>修改</button>";
|
||
//}
|
||
}
|
||
totalRecords = companies.Count();
|
||
recFilter = companies.Count();
|
||
|
||
apiResult.Code = "0000";
|
||
apiResult.Data = companies;
|
||
}
|
||
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<Company>> GetOneCompany(int id)
|
||
{
|
||
ApiResult<Company> apiResult = new ApiResult<Company>();
|
||
|
||
Company company = null;
|
||
|
||
try
|
||
{
|
||
company = await companyRepository.GetOneAsync(id);
|
||
|
||
if (company == null)
|
||
{
|
||
apiResult.Code = "9996";
|
||
apiResult.Msg = errorCode.GetString(apiResult.Code);
|
||
return apiResult;
|
||
}
|
||
|
||
apiResult.Code = "0000";
|
||
apiResult.Data = company;
|
||
|
||
}
|
||
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>> SaveCompany(PostCompany post)
|
||
{
|
||
ApiResult<string> apiResult = new ApiResult<string>();
|
||
|
||
Company company = null;
|
||
|
||
try
|
||
{
|
||
company = await companyRepository.GetOneAsync(post.Id);
|
||
|
||
if (company == null)
|
||
{
|
||
|
||
if (post.Id != 0)
|
||
{
|
||
apiResult.Code = "9996";
|
||
apiResult.Msg = errorCode.GetString(apiResult.Code);
|
||
return apiResult;
|
||
}
|
||
|
||
#region 新增公司
|
||
|
||
//先檢查統編是否已被使用
|
||
var exist = await companyRepository.GetOneNormalSimpleCompanyByTaxIDNumber(post.TaxIDNumber);
|
||
|
||
if(exist != null)
|
||
{
|
||
apiResult.Code = "9995";
|
||
apiResult.Msg = errorCode.GetString(apiResult.Code);
|
||
return apiResult;
|
||
}
|
||
|
||
company = new Company()
|
||
{
|
||
Name = post.Name,
|
||
//Logo = post.Logo,
|
||
TaxIDNumber = post.TaxIDNumber,
|
||
Phone = post.Phone,
|
||
Address = post.Address,
|
||
RegisterUpperLimit= post.RegisterUpperLimit,
|
||
CreatedBy = mySimpleUser.Id
|
||
};
|
||
|
||
List<string> properties = new List<string>()
|
||
{
|
||
"Name",
|
||
"TaxIDNumber",
|
||
"Phone",
|
||
"Address",
|
||
"RegisterUpperLimit",
|
||
"CreatedBy",
|
||
};
|
||
|
||
await companyRepository.AddAsync(company, properties);
|
||
apiResult.Code = "0000";
|
||
apiResult.Msg = "儲存成功";
|
||
#endregion
|
||
}
|
||
else
|
||
{
|
||
#region 修改使用者
|
||
|
||
//先檢查統編是否已被使用
|
||
var exist = await companyRepository.GetOneNormalSimpleCompanyByTaxIDNumber(post.TaxIDNumber);
|
||
if(exist != null && exist.Id != company.Id)
|
||
{
|
||
apiResult.Code = "9995";
|
||
apiResult.Msg = errorCode.GetString(apiResult.Code);
|
||
return apiResult;
|
||
}
|
||
|
||
UpdateCompany update = new UpdateCompany()
|
||
{
|
||
Id = post.Id,
|
||
Name = post.Name,
|
||
TaxIDNumber = post.TaxIDNumber,
|
||
//Status = post.Status,
|
||
Phone = post.Phone,
|
||
Address = post.Address,
|
||
RegisterUpperLimit = post.RegisterUpperLimit,
|
||
UpdatedBy = mySimpleUser.Id,
|
||
};
|
||
|
||
|
||
List<string> properties = new List<string>()
|
||
{
|
||
"Id",
|
||
"Name",
|
||
"TaxIDNumber",
|
||
"Phone",
|
||
"Address",
|
||
"RegisterUpperLimit",
|
||
"UpdatedBy",
|
||
};
|
||
|
||
await companyRepository.UpdateCompany(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>> DeleteOneCompany(int id)
|
||
{
|
||
ApiResult<string> apiResult = new ApiResult<string>();
|
||
|
||
Company company = null;
|
||
|
||
try
|
||
{
|
||
company = await companyRepository.GetOneCompany(id);
|
||
|
||
if (company == null)
|
||
{
|
||
apiResult.Code = "9996";
|
||
apiResult.Msg = errorCode.GetString(apiResult.Code);
|
||
return apiResult;
|
||
}
|
||
|
||
await companyRepository.DeleteOne(company.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;
|
||
}
|
||
}
|
||
}
|