using Dapper;
using Repository.BackendRepository.Interface;
using Repository.Helper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Transactions;
namespace Repository.BackendRepository.Implement
{
public class DeviceImportRepository : BackendRepository, IDeviceImportRepository
{
public DeviceImportRepository(IDatabaseHelper databaseHelper) : base(databaseHelper)
{
}
///
/// 取代單一設備的設備分類(第3層)(包含設備的災害類別)
///
///
///
///
///
public async Task ReplaceOneDeviceInfo(string device_guid, Dictionary device_dict, List> device_disaster_dicts)
{
using (IDbConnection conn = GetDbConnection())
{
conn.Open();
using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
{
try
{
//修改設備基本資訊
List properties = device_dict.Keys.ToList();
string sql = UpdateGenerateString(properties, "device", "device_guid='" + device_guid + "'");
await conn.ExecuteAsync(sql, device_dict);
if (device_disaster_dicts.Count() > 0)
{
//刪除設備所有災害
var sql_del_disaster = $"DELETE FROM device_disaster WHERE device_guid = @Device_guid";
await conn.ExecuteAsync(sql_del_disaster, new { Device_guid = device_guid });
//新增設備的災害
List add_disaster_properties = device_disaster_dicts[0].Keys.ToList();
string sql_add_disaster = InsertGenerateString(add_disaster_properties, "device_disaster");
await conn.ExecuteAsync(sql_add_disaster, device_disaster_dicts);
}
scope.Complete();
}
catch (Exception exception)
{
throw exception;
}
finally
{
conn.Close();
}
}
}
}
}
}