diff --git a/SolarPower/Controllers/CompanyController.cs b/SolarPower/Controllers/CompanyController.cs index 9d75961..e6f2b41 100644 --- a/SolarPower/Controllers/CompanyController.cs +++ b/SolarPower/Controllers/CompanyController.cs @@ -114,10 +114,19 @@ namespace SolarPower.Controllers if (myUser.IsGod == 1 || IsPlatformLayer(myUser.Role.Layer)) { //只有超級使用者 及 平台 可以使用 - company.Function = @" - - - "; + if(company.Id == 1) + { //平台公司不能被刪 + company.Function = @" + + "; + } + else + { + company.Function = @" + + + "; + } } else { @@ -271,6 +280,8 @@ namespace SolarPower.Controllers var id = await companyRepository.AddOneAsync(company, properties); + + UpdateCompany updateCompany; //處裡公司Logo圖片 if (post.LogoFile != null) { @@ -284,7 +295,7 @@ namespace SolarPower.Controllers post.LogoFile.CopyTo(stream); } - UpdateCompany updateCompany = new UpdateCompany() + updateCompany = new UpdateCompany() { Id = id, Logo = fileName @@ -319,7 +330,26 @@ namespace SolarPower.Controllers await roleRepository.AddAsync(role, roleProperties); #endregion - //TODO 自動新增公司DB及Table,公司DB編號規則 solar_com_(公司編號共四碼),ex:solar_com_0001。 + #region 新增公司DB及Table,公司DB編號規則 solar_com_(公司編號共四碼),ex:solar_com_0001 + + var relationalDB = "solar_com_" + id.ToString().Trim().PadLeft(4, '0'); + //修改 + updateCompany = new UpdateCompany() + { + Id = id, + RelationalDB = relationalDB + }; + + properties = new List() + { + "Id", + "RelationalDB" + }; + + await companyRepository.UpdateCompany(updateCompany, properties); + + await companyRepository.CreatCompanyDB(relationalDB); + #endregion apiResult.Code = "0000"; apiResult.Msg = "儲存成功"; diff --git a/SolarPower/Models/Company.cs b/SolarPower/Models/Company.cs index a54fd59..e1b2ecf 100644 --- a/SolarPower/Models/Company.cs +++ b/SolarPower/Models/Company.cs @@ -51,6 +51,7 @@ namespace SolarPower.Models.Company public string TaxIDNumber { get; set; } //統一編號 public string Phone { get; set; } //電話 public string Address { get; set; } + public string RelationalDB { get; set; } //關聯的公司自己資料庫 public int RegisterUpperLimit { get; set; } //註冊上限 } diff --git a/SolarPower/Repository/Implement/CompanyRepository.cs b/SolarPower/Repository/Implement/CompanyRepository.cs index a1db8f8..4de37af 100644 --- a/SolarPower/Repository/Implement/CompanyRepository.cs +++ b/SolarPower/Repository/Implement/CompanyRepository.cs @@ -409,5 +409,181 @@ namespace SolarPower.Repository.Implement return count; } } + + /// + /// 創建公司自己的DB + /// + /// + /// + public async Task CreatCompanyDB(string dbName) + { + using (IDbConnection conn = this._databaseHelper.GetConnection()) + { + conn.Open(); + using (var trans = conn.BeginTransaction()) + { + try + { + var sql = @$" + -- 傾印 資料庫結構 + CREATE DATABASE IF NOT EXISTS `{dbName}`; + USE `{dbName}`; + + -- 傾印 資料表 device 結構 + CREATE TABLE IF NOT EXISTS `device` ( + `Id` int(10) unsigned NOT NULL AUTO_INCREMENT, + `Deleted` tinyint(4) NOT NULL DEFAULT 0 COMMENT '是否刪除, 0:否 1:是', + `UID` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '設備編號,縣市 +區域+電廠流水號(0001~9999)+設備類別(字母:3碼) + SN(設備流水號;3碼)', + `PowerStationId` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '所屬電站編號', + `Name` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '名稱', + `Type` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '類型', + `Brand` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '廠牌', + `ProductModel` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '型號', + `DBName` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL, + `TableName` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL, + `ColName` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL, + `Remark` text COLLATE utf8mb4_unicode_ci DEFAULT NULL, + `CreatedBy` int(10) unsigned NOT NULL COMMENT '建立者', + `CreatedAt` timestamp NOT NULL DEFAULT current_timestamp() COMMENT '建立時間', + `UpdatedBy` int(10) unsigned DEFAULT NULL COMMENT '修改者', + `UpdatedAt` timestamp NULL DEFAULT NULL ON UPDATE current_timestamp() COMMENT '修改時間', + PRIMARY KEY (`Id`) USING BTREE, + KEY `IDX_01` (`Deleted`) USING BTREE, + KEY `IDX_02` (`UID`,`PowerStationId`) + ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='裝置列表'; + + -- 傾印 資料表 land_building 結構 + CREATE TABLE IF NOT EXISTS `land_building` ( + `Id` int(10) unsigned NOT NULL AUTO_INCREMENT, + `Deleted` tinyint(4) NOT NULL DEFAULT 0 COMMENT '是否刪除, 0:否 1:是', + `PowerStationId` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '所屬電站編號', + `Address` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '地址', + `LeaseNotarizationAt` timestamp NULL DEFAULT NULL COMMENT '租約公證日期', + `Landowner` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '地主姓名', + `Purpose` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '房屋用途', + `LeaseRate` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '租金比例(%)', + `Coordinate` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '座標', + `phone` varchar(20) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '電話', + `CreatedBy` int(10) unsigned NOT NULL COMMENT '建立者', + `CreatedAt` timestamp NOT NULL DEFAULT current_timestamp() COMMENT '建立時間', + `UpdatedBy` int(10) unsigned DEFAULT NULL COMMENT '修改者', + `UpdatedAt` timestamp NULL DEFAULT NULL ON UPDATE current_timestamp() COMMENT '修改時間', + PRIMARY KEY (`Id`), + KEY `IDX_01` (`Deleted`,`PowerStationId`) + ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='土地與房屋'; + + -- 傾印 資料表 operation_firm 結構 + CREATE TABLE IF NOT EXISTS `operation_firm` ( + `Id` int(10) unsigned NOT NULL AUTO_INCREMENT, + `Deleted` tinyint(4) NOT NULL DEFAULT 0 COMMENT '是否刪除, 0:否 1:是', + `PowerStationId` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '所屬電站編號', + `Name` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '名稱', + `Type` tinyint(4) NOT NULL DEFAULT 0 COMMENT '廠商類別,0:施工 1:清洗 2:運維', + `ContactPerson` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '聯絡人', + `Phone` varchar(20) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '電話', + `Email` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT 'Email', + `CreatedBy` int(10) unsigned NOT NULL COMMENT '建立者', + `CreatedAt` timestamp NOT NULL DEFAULT current_timestamp() COMMENT '建立時間', + `UpdatedBy` int(10) unsigned DEFAULT NULL COMMENT '修改者', + `UpdatedAt` timestamp NULL DEFAULT NULL ON UPDATE current_timestamp() COMMENT '修改時間', + PRIMARY KEY (`Id`), + KEY `IDX_01` (`Deleted`,`PowerStationId`) + ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='運維廠商'; + + -- 傾印 資料表 power_station 結構 + CREATE TABLE IF NOT EXISTS `power_station` ( + `Id` int(10) unsigned NOT NULL AUTO_INCREMENT, + `Deleted` tinyint(4) NOT NULL DEFAULT 0 COMMENT '是否刪除, 0:否 1:是', + `CompanyId` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '公司編號', + `Name` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '名稱', + `Code` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '電站代碼,縣市+區域+流水號 ', + `IsEscrow` tinyint(4) NOT NULL DEFAULT 0 COMMENT '是否為代管,0:否 1:是', + `EscrowName` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '代管名稱', + `ElectricityMeterAt` timestamp NULL DEFAULT NULL COMMENT '台電掛錶日', + `EstimatedRecoveryTime` varchar(10) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '預估回收時間', + `GeneratingCapacity` decimal(10,1) NOT NULL DEFAULT 0.0 COMMENT '電廠發電容量,單位(千瓦)', + `PowerRate` varchar(10) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '受電費率', + `Coordinate` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '座標', + `InverterBrand` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '逆變器廠牌', + `InverterProductModel` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '逆變器型號', + `InverterAmount` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '逆變器數量', + `PhotovoltaicPanelBrand` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '光電板廠牌', + `PhotovoltaicPanelProductModel` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '光電板型號', + `PhotovoltaicPanelSpecification` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '光電板規格', + `PhotovoltaicPanelAmount` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '光電板數量', + `BoEFile` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '能源局檔案', + `BoEDiscountRate` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '能源局折扣率', + `BoEDeviceRegisterNumber` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '能源局設備登記編號', + `BoERentRatio` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '能源局租金比率,單位(%)', + `TPCContractNumber` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '台電契約編號', + `TPCContractAt` timestamp NULL DEFAULT NULL COMMENT '台電簽約日期', + `TPCSellDeadline` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '台電售電期限,單位(年)', + `TPCMeterReading` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '台電每期抄錶日', + `TPCPurchaseElectricityAt` timestamp NULL DEFAULT NULL COMMENT '台電正式購電日', + `TPCSellElectricityAt` timestamp NULL DEFAULT NULL COMMENT '台電正式售電日', + `CreatedBy` int(10) unsigned NOT NULL COMMENT '建立者', + `CreatedAt` timestamp NOT NULL DEFAULT current_timestamp() COMMENT '建立時間', + `UpdatedBy` int(10) unsigned DEFAULT NULL COMMENT '修改者', + `UpdatedAt` timestamp NULL DEFAULT NULL ON UPDATE current_timestamp() COMMENT '修改時間', + PRIMARY KEY (`Id`), + KEY `IDX_01` (`Deleted`), + KEY `IDX_02` (`CompanyId`) + ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='電站資料'; + + -- 傾印 資料表 power_station_image 結構 + CREATE TABLE IF NOT EXISTS `power_station_image` ( + `Id` int(10) unsigned NOT NULL AUTO_INCREMENT, + `Deleted` tinyint(4) NOT NULL DEFAULT 0 COMMENT '是否刪除, 0:否 1:是', + `PowerStationId` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '電站編號', + `IsMainDisplay` tinyint(4) NOT NULL DEFAULT 0 COMMENT '是否主要顯示圖片, 0:否 1:是', + ` image` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '圖片檔名', + `CreatedBy` int(10) unsigned NOT NULL COMMENT '建立者', + `CreatedAt` timestamp NOT NULL DEFAULT current_timestamp() COMMENT '建立時間', + PRIMARY KEY (`Id`), + KEY `IDX_01` (`Deleted`,`IsMainDisplay`,`PowerStationId`) USING BTREE + ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='電站資料'; + + -- 傾印 資料表 power_station_operation_personnel 結構 + CREATE TABLE IF NOT EXISTS `power_station_operation_personnel` ( + `Id` int(10) unsigned NOT NULL AUTO_INCREMENT, + `Deleted` tinyint(4) NOT NULL DEFAULT 0, + `PowerStationId` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '電站編號', + `UserId` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '人員標號', + `CreatedBy` int(10) unsigned NOT NULL COMMENT '建立者', + `CreatedAt` timestamp NOT NULL DEFAULT current_timestamp() COMMENT '建立時間', + PRIMARY KEY (`Id`), + KEY `IDX_01` (`Deleted`), + KEY `IDX_02` (`PowerStationId`,`UserId`) + ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='電站運維人員'; + + -- 傾印 資料表 power_station_single_line_diagram 結構 + CREATE TABLE IF NOT EXISTS `power_station_single_line_diagram` ( + `Id` int(10) unsigned NOT NULL AUTO_INCREMENT, + `Deleted` tinyint(4) NOT NULL DEFAULT 0 COMMENT '是否刪除, 0:否 1:是', + `PowerStationId` int(10) unsigned NOT NULL DEFAULT 0 COMMENT '電站編號', + ` image` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '圖片檔名', + `CreatedBy` int(10) unsigned NOT NULL COMMENT '建立者', + `CreatedAt` timestamp NOT NULL DEFAULT current_timestamp() COMMENT '建立時間', + PRIMARY KEY (`Id`), + KEY `IDX_01` (`Deleted`,`PowerStationId`) USING BTREE + ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='電站單線圖'; + "; + + await conn.ExecuteAsync(sql, trans); + + trans.Commit(); + } + catch (Exception exception) + { + trans.Rollback(); + throw exception; + } + finally + { + conn.Close(); + } + } + } + } } } diff --git a/SolarPower/Repository/Interface/ICompanyRepository.cs b/SolarPower/Repository/Interface/ICompanyRepository.cs index 2491197..1774da8 100644 --- a/SolarPower/Repository/Interface/ICompanyRepository.cs +++ b/SolarPower/Repository/Interface/ICompanyRepository.cs @@ -94,5 +94,12 @@ namespace SolarPower.Repository.Interface /// /// Task AddCompanyAuthAsync(List entity, List properties); + + /// + /// 創建公司自己的DB + /// + /// + /// + Task CreatCompanyDB(string dbName); } } diff --git a/SolarPower/Views/Company/Index.cshtml b/SolarPower/Views/Company/Index.cshtml index 41de1b3..1a7fdb0 100644 --- a/SolarPower/Views/Company/Index.cshtml +++ b/SolarPower/Views/Company/Index.cshtml @@ -101,7 +101,7 @@