ibms-dome/Repository/BackendRepository/Implement/DeviceImportRepository.cs

70 lines
2.6 KiB
C#
Raw Permalink Normal View History

2022-10-14 16:08:54 +08:00
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)
{
}
/// <summary>
/// 取代單一設備的設備分類(第3層)(包含設備的災害類別)
/// </summary>
/// <param name="device_number"></param>
/// <param name="device_dict"></param>
/// <param name="device_disaster_dicts"></param>
/// <returns></returns>
public async Task ReplaceOneDeviceInfo(string device_guid, Dictionary<string, object> device_dict, List<Dictionary<string, object>> device_disaster_dicts)
{
using (IDbConnection conn = GetDbConnection())
{
conn.Open();
using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
{
try
{
//修改設備基本資訊
List<string> 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<string> 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();
}
}
}
}
}
}