diff --git a/SolarPower/Controllers/CompanyController.cs b/SolarPower/Controllers/CompanyController.cs index 0a97888..f2181d9 100644 --- a/SolarPower/Controllers/CompanyController.cs +++ b/SolarPower/Controllers/CompanyController.cs @@ -401,6 +401,11 @@ namespace SolarPower.Controllers await companyRepository.UpdateCompany(updateCompany, properties); await companyRepository.CreatCompanyDB(siteDB); + + //取得所有的公司DBName + var siteDBs = await companyRepository.GetAllCompanyDBName(); + await companyRepository.AlterViewCompanyInv(siteDBs); + #endregion apiResult.Code = "0000"; diff --git a/SolarPower/Repository/Implement/CompanyRepository.cs b/SolarPower/Repository/Implement/CompanyRepository.cs index 8f713bf..f407c3e 100644 --- a/SolarPower/Repository/Implement/CompanyRepository.cs +++ b/SolarPower/Repository/Implement/CompanyRepository.cs @@ -686,5 +686,74 @@ namespace SolarPower.Repository.Implement } } } + + public async Task> GetAllCompanyDBName() + { + using (IDbConnection conn = this._databaseHelper.GetConnection()) + { + conn.Open(); + var result = new List(); + + try + { + var sql = $@"SELECT DISTINCT SiteDB FROM company c WHERE c.Deleted = 0 AND c.Status = 1"; + + result = (await conn.QueryAsync(sql)).ToList(); + } + catch (Exception exception) + { + throw exception; + } + finally + { + conn.Close(); + } + + return result; + } + } + + public async Task AlterViewCompanyInv(List db_names) + { + using (IDbConnection conn = this._databaseHelper.GetConnection()) + { + conn.Open(); + using (var trans = conn.BeginTransaction()) + { + try + { + var temp_sub_sql = new List(); + + foreach (var db_name in db_names) + { + var sub_sql = $@"select '{db_name}' AS `dbName`,(`{db_name}`.`inverter`.`InverterId` collate utf8mb4_unicode_ci) AS `InverterId`, + (`{db_name}`.`inverter`.`Brand` collate utf8mb4_unicode_ci) AS `Brand`,`{db_name}`.`inverter`.`Enabled` AS `enabled`, + `{db_name}`.`inverter`.`Status` AS status + from `{db_name}`.`inverter`"; + + temp_sub_sql.Add(sub_sql); + } + + var temp_sql = string.Join(" UNION ALL ", temp_sub_sql); + + var sql = $@"alter view v_company_inv + AS {temp_sql}"; + + await conn.ExecuteAsync(sql, trans); + + trans.Commit(); + } + catch (Exception exception) + { + trans.Rollback(); + throw exception; + } + finally + { + conn.Close(); + } + } + } + } } } diff --git a/SolarPower/Repository/Implement/OperationRepository.cs b/SolarPower/Repository/Implement/OperationRepository.cs index b7cdcb4..73bae5e 100644 --- a/SolarPower/Repository/Implement/OperationRepository.cs +++ b/SolarPower/Repository/Implement/OperationRepository.cs @@ -208,11 +208,9 @@ namespace SolarPower.Repository.Implement var sql = @$"SELECT aa.* FROM ( SELECT opr.*, - ps.Name AS PowerStationName, - u.Name AS WorkPersonName + ps.Name AS PowerStationName FROM operation_record opr LEFT JOIN power_station ps ON opr.PowerStationId = ps.Id - LEFT JOIN user u ON opr.WorkPersonId = u.ID WHERE opr.Deleted = 0 AND opr.Status = {filter.Status} AND ps.Id IN @PowerStationIds"; @@ -236,11 +234,9 @@ namespace SolarPower.Repository.Implement sql += @" UNION"; sql += @$" SELECT opr.*, - ps.Name AS PowerStationName, - u.Name AS WorkPersonName + ps.Name AS PowerStationName FROM operation_record opr LEFT JOIN power_station ps ON opr.PowerStationId = ps.Id - LEFT JOIN user u ON opr.WorkPersonId = u.ID WHERE opr.Deleted = 0 AND opr.Status = {filter.Status} AND ps.Id IN @PowerStationIds AND opr.WorkType = 2"; @@ -267,17 +263,33 @@ namespace SolarPower.Repository.Implement sql += " ) aa ORDER BY aa.CreatedAt DESC"; result = (await conn.QueryAsync(sql, - new { + new + { PowerStationIds = filter.PowerStationIds, WorkType = filter.WorkType, StartDate = filter.StartTime, EndDate = filter.EndTime })).ToList(); - var sql_file = "SELECT * FROM operation_record_file WHERE Deleted = 0 AND RecordId = @RecordId"; - foreach (var x in result) + if (result != null && result.Count > 0) { - x.RecordFiles = (await conn.QueryAsync(sql_file, new { RecordId = x.Id })).ToList(); + + + var sql_file = "SELECT * FROM operation_record_file WHERE Deleted = 0 AND RecordId = @RecordId"; + foreach (var x in result) + { + x.RecordFiles = (await conn.QueryAsync(sql_file, new { RecordId = x.Id })).ToList(); + } + + var sql_WorkPersonId = @"SELECT GROUP_CONCAT(DISTINCT u.`Name` SEPARATOR ', ') + FROM operation_record_personnel op + LEFT JOIN user u ON u.deleted = 0 AND op.UserId = u.Id + WHERE op.Deleted = 0 AND op.OperationRecordId = @RecordId + GROUP BY op.OperationRecordId "; + foreach (var x in result) + { + x.WorkPersonName = await conn.QueryFirstOrDefaultAsync(sql_WorkPersonId, new { RecordId = x.Id }); + } } } catch (Exception exception) @@ -322,7 +334,7 @@ namespace SolarPower.Repository.Implement result.WorkPersonIds = (await conn.QueryAsync(sql_operation_record_personnel, new { OperationRecordId = result.Id })).ToList(); } - + } catch (Exception exception) { @@ -580,7 +592,7 @@ namespace SolarPower.Repository.Implement { try { - List value = new List() ; + List value = new List(); foreach (MyUser a in personal) { value.Add($@"(1,'{a.Name}','{a.Email}','{Title}','{content}',{a.Id},3)"); @@ -589,7 +601,7 @@ namespace SolarPower.Repository.Implement var sql = $"INSERT INTO `notice_schedule` (`Type`, `RecipientName`, `RecipientEmail`, `Subject`, `Content`,`UserId`,`EmailType`) VALUES {values};"; - await conn.ExecuteAsync(sql,trans); + await conn.ExecuteAsync(sql, trans); trans.Commit(); } diff --git a/SolarPower/Repository/Implement/PowerStationRepository.cs b/SolarPower/Repository/Implement/PowerStationRepository.cs index b6a6340..88a8412 100644 --- a/SolarPower/Repository/Implement/PowerStationRepository.cs +++ b/SolarPower/Repository/Implement/PowerStationRepository.cs @@ -4176,7 +4176,8 @@ namespace SolarPower.Repository.Implement c.Name AS CityName, ps.Name AS PowerStationName, inv.InverterName AS InverterName, - inv.InverterId AS InverterId + inv.InverterId AS InverterId, + inv.CreatedAt FROM power_station ps LEFT JOIN `city` c ON ps.CityId = c.Id LEFT JOIN {powerStationDic.Key}.controller con ON ps.Id = con.PowerStationId @@ -4192,7 +4193,7 @@ namespace SolarPower.Repository.Implement } sql = string.Join(" UNION ", sql_perSiteDB); - sql = "(" + sql + ") ORDER BY c.Priority, inv.CreatedAt"; + sql = "(" + sql + ") ORDER BY Priority, CreatedAt"; result = (await conn.QueryAsync(sql, new { Filter = filter })).ToList(); } diff --git a/SolarPower/Repository/Interface/ICompanyRepository.cs b/SolarPower/Repository/Interface/ICompanyRepository.cs index 1774da8..9712f77 100644 --- a/SolarPower/Repository/Interface/ICompanyRepository.cs +++ b/SolarPower/Repository/Interface/ICompanyRepository.cs @@ -101,5 +101,9 @@ namespace SolarPower.Repository.Interface /// /// Task CreatCompanyDB(string dbName); + + Task> GetAllCompanyDBName(); + + Task AlterViewCompanyInv(List db_names); } } diff --git a/SolarPower/Views/Operation/OperationRecord.cshtml b/SolarPower/Views/Operation/OperationRecord.cshtml index 5c684a3..cc4e1b2 100644 --- a/SolarPower/Views/Operation/OperationRecord.cshtml +++ b/SolarPower/Views/Operation/OperationRecord.cshtml @@ -24,8 +24,8 @@
- - + +
@@ -200,7 +200,7 @@
-
+
@@ -274,7 +274,7 @@ var datepicker; var AllpoweridsType = true; var AllidsType = true; - var status = 1; //1:完成 0:未完成 + var status = 0; //1:完成 0:未完成 var Searchtype = false; //#region Array.Remove