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 { private readonly ICompanyRepository companyRepository; public CompanyController(ICompanyRepository companyRepository) : base() { this.companyRepository = companyRepository; } public IActionResult Index() { return View(); } /// /// 取得下拉式公司選單,須為Deleted: 0 /// /// [HttpGet] public async Task>> GetCompanySelectOptionListAsync() { ApiResult> apiResult = new ApiResult>(); 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; } /// /// 公司管理列表 /// /// /// [HttpPost] public async Task CompanyListAsync(PostCompanyFilter post) { ApiResult> apiResult = new ApiResult>(); int totalRecords = 0; //總資料筆數 int recFilter = 0; //過濾後資料筆數 List 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 = @" 權限池 "; //} //else //{ // company.Functoin = @" // "; //} } 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; } /// /// 取得單一系統管理員 /// /// /// [HttpPost] public async Task> GetOneCompany(int id) { ApiResult apiResult = new ApiResult(); 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; } /// /// 新增 / 修改 公司資料 /// /// /// [HttpPost] public async Task> SaveCompany(PostCompany post) { ApiResult apiResult = new ApiResult(); 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 properties = new List() { "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 properties = new List() { "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; } /// /// 軟刪除單一公司 /// /// /// [HttpPost] public async Task> DeleteOneCompany(int id) { ApiResult apiResult = new ApiResult(); 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; } } }