電站卡片及縣市分佈後端

This commit is contained in:
桂任 林 2021-06-23 09:31:33 +08:00
parent 7dcca6b66d
commit b102d2e46e
5 changed files with 113 additions and 9 deletions

View File

@ -255,7 +255,7 @@ namespace SolarPower.Controllers
return apiResult;
}
await operationRepository.DeleteOneOtherTable(Id, "operation_plan_create");
//await operationRepository.DeleteOneOtherTable(Id, "operation_plan_create");
apiResult.Code = "0000";
apiResult.Msg = "刪除成功";

View File

@ -1864,5 +1864,28 @@ namespace SolarPower.Controllers
return apiResult;
}
/// <summary>
/// 取得該使用者可看的所有電站分佈縣市以及各縣市電站數量
/// </summary>
/// <returns></returns>
public async Task<ApiResult<List<PowerStation>>> GetSolarByCity(List<int> cityid)
{
ApiResult<List<PowerStation>> apiResult = new ApiResult<List<PowerStation>>();
List<PowerStation> solaramount = new List<PowerStation>();
try
{
apiResult.Code = "0000";
solaramount = await powerStationRepository.GetSolarByCity(myUser, cityid);
apiResult.Data = solaramount;
}
catch (Exception exception)
{
apiResult.Code = "9999";
apiResult.Msg = exception.ToString();
}
return apiResult;
}
}
}

View File

@ -621,7 +621,7 @@ namespace SolarPower.Repository.Implement
try
{
string sql = @$"SELECT * FROM variable WHERE name = @name";
var json = await conn.QueryFirstOrDefaultAsync<Variable>(sql, new { name = "Type" });
var json = await conn.QueryFirstOrDefaultAsync<SolarPower.Models.PowerStation.Variable>(sql, new { name = "Type" });
Root jsonfor = JsonSerializer.Deserialize<Root>(json.value);
foreach (Models.PowerStation.Type a in jsonfor.Type)
{
@ -1286,19 +1286,19 @@ namespace SolarPower.Repository.Implement
{
if (User.Role.Layer == 0 || User.Role.Layer == 1)
{
var sql = "SELECT city.Id AS CityId city.Name AS City,COUNT(*) AS Amount FROM power_station LEFT JOIN city ON power_station.CityId = city.Id GROUP BY power_station.CityId";
var sql = "SELECT city.Id AS CityId, city.Name AS City,COUNT(*) AS Amount FROM power_station LEFT JOIN city ON power_station.CityId = city.Id GROUP BY power_station.CityId";
solaramount = (await conn.QueryAsync<SolarCityAmount>(sql)).ToList();
trans.Commit();
}
else if (User.Role.Layer == 2)
{
var sql = "SELECT city.Id AS CityId city.Name AS City,COUNT(*) AS Amount FROM power_station LEFT JOIN city ON power_station.CityId = city.Id WHERE CompanyId = @CompanyId GROUP BY power_station.CityId ORDER BY power_station.CityId ";
var sql = "SELECT city.Id AS CityId, city.Name AS City,COUNT(*) AS Amount FROM power_station LEFT JOIN city ON power_station.CityId = city.Id WHERE CompanyId = @CompanyId GROUP BY power_station.CityId ORDER BY power_station.CityId ";
solaramount = (await conn.QueryAsync<SolarCityAmount>(sql, new { CompanyId = User.CompanyId })).ToList();
trans.Commit();
}
else
{
var sql = "SELECT city.Id AS CityId city.Name AS City,COUNT(*) AS Amount FROM power_station LEFT JOIN city ON power_station.CityId = city.Id LEFT JOIN power_station_operation_personnel ON power_station.Id = power_station_operation_personnel.PowerStationId WHERE UserId = @UserId GROUP BY power_station.CityId ORDER BY power_station.CityId";
var sql = "SELECT city.Id AS CityId, city.Name AS City,COUNT(*) AS Amount FROM power_station LEFT JOIN city ON power_station.CityId = city.Id LEFT JOIN power_station_operation_personnel ON power_station.Id = power_station_operation_personnel.PowerStationId WHERE UserId = @UserId GROUP BY power_station.CityId ORDER BY power_station.CityId";
solaramount = (await conn.QueryAsync<SolarCityAmount>(sql, new { UserId = User.Id })).ToList();
trans.Commit();
}
@ -1314,5 +1314,52 @@ namespace SolarPower.Repository.Implement
}
return solaramount;
}
public async Task<List<PowerStation>> GetSolarByCity(MyUser User, List<int> CityId)
{
using IDbConnection conn = _databaseHelper.GetConnection();
List<PowerStation> powerstation = new List<PowerStation>();
conn.Open();
var trans = conn.BeginTransaction();
try
{
var ids = "";
foreach(var id in CityId)
{
ids = ids + id + ",";
}
if (ids.Length != 0)
{
ids = ids.Substring(0, ids.Length - 1);
}
if (User.Role.Layer == 0 || User.Role.Layer == 1)
{
var sql = "SELECT * FROM power_station WHERE CityId IN(@Ids)";
powerstation = (await conn.QueryAsync<PowerStation>(sql, new { Ids = ids })).ToList();
trans.Commit();
}
else if (User.Role.Layer == 2)
{
var sql = "SELECT * FROM power_station WHERE CityId IN(@Ids) AND CompanyId=@CompanyId";
powerstation = (await conn.QueryAsync<PowerStation>(sql, new { Ids = ids ,CompanyId = User.CompanyId })).ToList();
trans.Commit();
}
else
{
var sql = "SELECT power_station.* FROM power_station LEFT JOIN power_station_operation_personnel ON power_station.Id = power_station_operation_personnel.PowerStationId WHERE CityId IN(@Ids) AND Userid = @UserId";
powerstation = (await conn.QueryAsync<PowerStation>(sql, new { UserId = User.Id })).ToList();
trans.Commit();
}
}
catch (Exception exception)
{
trans.Rollback();
throw exception;
}
finally
{
conn.Close();
}
return powerstation;
}
}
}

View File

@ -326,5 +326,20 @@ namespace SolarPower.Repository.Interface
/// <param name="db_name"></param>
/// <returns></returns>
Task DeleteOnePowerStationSingleLine(int id, string db_name);
/// <summary>
/// 取得電站分佈縣市以及數量
/// </summary>
/// <param name="User"></param>
/// <returns></returns>
Task<List<SolarCityAmount>> GetSolarCitySummary(MyUser User);
/// <summary>
/// 透過縣市取得電站卡片資訊
/// </summary>
/// <param name="User"></param>
/// <param name="CityId"></param>
/// <returns></returns>
Task<List<PowerStation>> GetSolarByCity(MyUser User, List<int> CityId);
}
}

View File

@ -414,15 +414,35 @@
//#region 電站區域Tab
var url = "/PowerStation/GetSolarCitySummary";
$.post(url, function (rel) {
//var url = "/PowerStation/GetSolarCitySummary";
//$.post(url, function (rel) {
// if (rel.code != "0000") {
// toast_error(rel.msg);
// return;
// }
// else {
// $.each(rel.data, function (index, val) {
// alert(val.city + val.amount);
// });
// return;
// }
//}, 'json');
var url = "/PowerStation/GetSolarByCity";
var ids = [1, 3];
var send_data = {
cityid: ids
}
$.post(url, send_data, function (rel) {
if (rel.code != "0000") {
toast_error(rel.msg);
return;
}
else {
$.each(rel.data, function (index, val) {
//alert(val.city + val.amount);
alert(val.name);
});
return;
}
@ -430,7 +450,6 @@
}, 'json');
//#endregion
//電站區域