FIC_Solar/SolarPower/Repository/Implement/OperationRepository.cs
2021-06-23 11:10:27 +08:00

245 lines
8.7 KiB
C#

using Dapper;
using SolarPower.Helper;
using SolarPower.Models;
using SolarPower.Repository.Interface;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
namespace SolarPower.Repository.Implement
{
public class OperationRepository : RepositoryBase<Operation>, IOperationRepository
{
public OperationRepository(IDatabaseHelper databaseHelper) : base(databaseHelper)
{
tableName = "operation_plan_create";
}
public async Task<List<PowerStationIdList>> GetPowerStationIdList(int UserId)
{
List<PowerStationIdList> result;
using (IDbConnection conn = this._databaseHelper.GetConnection())
{
try
{
var sql = @$"SELECT tn.Id AS Value, tn.Name AS Text FROM power_station tn LEFT JOIN power_station_operation_personnel pp
ON tn.Id=pp.PowerStationId
WHERE pp.UserId = @userid";
result = (await conn.QueryAsync<PowerStationIdList>(sql, new { userid = UserId })).ToList();
}
catch (Exception exception)
{
throw exception;
}
return result;
}
}
public async Task AddOperationPlan(OperationCreatePlan OperationPlan, List<string> properties)
{
using (IDbConnection conn = _databaseHelper.GetConnection())
{
conn.Open();
try
{
string sql = GenerateInsertQuery(properties);
await conn.ExecuteAsync(sql, OperationPlan);
}
catch (Exception exception)
{
throw exception;
}
finally
{
conn.Close();
}
}
}
public async Task<List<OperationPlanTable>> OperationPlanTable(List<int> id)
{
List<OperationPlanTable> result;
var count = 0;
string Wheresql = "oc.PowerStationId = ";
foreach (int too in id)
{
if(count == id.Count-1)
{
Wheresql += too.ToString();
}
else
{
Wheresql += too.ToString() + " OR oc.PowerStationId = ";
}
count++;
}
using (IDbConnection conn = this._databaseHelper.GetConnection())
{
try
{
var sql = @$"SELECT oc.Id,oc.PlanId,oc.PowerStationId,oc.Type,
oc.ScheduleNum,oc.ScheduleType,oc.WorkDay,oc.StartTime,
oc.EmailType,oc.Description,oc.CreatedAt,ps.Name AS PowerStationName ,us.Name AS CreatedPerson FROM operation_plan_create oc LEFT JOIN power_station ps
ON oc.PowerStationId = ps.Id LEFT JOIN user us
ON us.Id = oc.CreatedBy WHERE {Wheresql} AND oc.Deleted = 0";
result = (await conn.QueryAsync<OperationPlanTable>(sql)).ToList();
}
catch (Exception exception)
{
throw exception;
}
return result;
}
}
public async Task<OperationCreatePlan> GetOneOperation(int id)
{
OperationCreatePlan result;
using (IDbConnection conn = _databaseHelper.GetConnection())
{
conn.Open();
try
{
var sql = @"SELECT * FROM operation_plan_create WHERE Deleted =0 AND Id = @Id";
result = await conn.QueryFirstOrDefaultAsync<OperationCreatePlan>(sql, new { Id = id });
}
catch (Exception exception)
{
throw exception;
}
finally
{
conn.Close();
}
return result;
}
}
public async Task UpdateOperationPlan(OperationCreatePlan OperationPlan, List<string> properties)
{
using (IDbConnection conn = _databaseHelper.GetConnection())
{
conn.Open();
var trans = conn.BeginTransaction();
try
{
var sql = GenerateUpdateQuery(properties);
await conn.ExecuteAsync(sql, OperationPlan, trans);
trans.Commit();
}
catch (Exception exception)
{
trans.Rollback();
throw exception;
}
finally
{
conn.Close();
}
}
}
public async Task AddToRecord(PlanToRecord record, List<string> properties2)
{
using (IDbConnection conn = _databaseHelper.GetConnection())
{
conn.Open();
var trans = conn.BeginTransaction();
try
{
string sql = GenerateInsertQueryWithCustomTable(properties2, "operation_record");
await conn.ExecuteAsync(sql, record);
trans.Commit();
}
catch (Exception exception)
{
trans.Rollback();
throw exception;
}
finally
{
conn.Close();
}
}
}
/// <summary>
/// 透過搜尋條件,查詢過濾後的運維作業記錄
/// </summary>
/// <param name="filter"></param>
/// <returns></returns>
public async Task<List<OperationRecodeDataTable>> GetAllRecodeByFilterAsync(PostOperationRecodeFilter filter)
{
List<OperationRecodeDataTable> result;
using (IDbConnection conn = this._databaseHelper.GetConnection())
{
try
{
var sql = @$"SELECT
ps.Name AS PowerStationName,
opr.FormId,
opr.WorkType,
opr.FixDO,
opr.Status,
u.Name AS WorkPersonName,
opr.StartTime,
opr.EndTime,
opr.FinishTime
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 ps.CityId IN @CityIds
AND ps.Id IN @PowerStationIds";
if (filter.WorkType > 0)
{
sql += @" AND opr.WorkType = @WorkType";
if (!string.IsNullOrEmpty(filter.Range))
{
}
}
else
{
sql += @" AND opr.WorkType IN (0, 1)";
sql += @" UNION";
sql += @" SELECT
ps.Name AS PowerStationName,
opr.FormId,
opr.WorkType,
opr.FixDO,
opr.Status,
u.Name AS WorkPersonName,
opr.StartTime,
opr.EndTime,
opr.FinishTime
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 ps.CityId IN @CityIds
AND ps.Id IN @PowerStationIds
AND opr.WorkType = 2";
}
result = (await conn.QueryAsync<OperationRecodeDataTable>(sql, filter)).ToList();
}
catch (Exception exception)
{
throw exception;
}
return result;
}
}
}
}