86 lines
3.6 KiB
C#
86 lines
3.6 KiB
C#
|
using Dapper;
|
|||
|
using Repository.BackendRepository.Interface;
|
|||
|
using Repository.Helper;
|
|||
|
using Repository.Services.Implement;
|
|||
|
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 DeviceManageRepository : BackendRepository, IDeviceManageRepository
|
|||
|
{
|
|||
|
public DeviceManageRepository(IDatabaseHelper databaseHelper) : base(databaseHelper)
|
|||
|
{
|
|||
|
//con = databaseHelper.GetMSSqlConnection();
|
|||
|
}
|
|||
|
|
|||
|
public async Task UpdateOneDeviceInfo(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))
|
|||
|
using (var trans = conn.BeginTransaction())
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
var backgroundService = new BackgroundService(conn, trans);
|
|||
|
|
|||
|
//修改設備基本資訊
|
|||
|
List<string> properties = device_dict.Keys.ToList();
|
|||
|
string sql = UpdateGenerateString(properties, "device", "device_guid='" + device_guid + "'");
|
|||
|
|
|||
|
await conn.ExecuteAsync(sql, device_dict, trans);
|
|||
|
await backgroundService.AddTask("", "", "device", "update", 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 }, trans);
|
|||
|
|
|||
|
//重整更新的資料
|
|||
|
var pk = "device_guid";
|
|||
|
foreach (var device_disaster_dict in device_disaster_dicts)
|
|||
|
if (device_disaster_dict[pk] != null && pk.Substring(0, 1) != "@")
|
|||
|
{
|
|||
|
device_disaster_dict.Add("@" + pk, device_disaster_dict[pk]);
|
|||
|
device_disaster_dict.Remove(pk);
|
|||
|
}
|
|||
|
|
|||
|
//新增設備的災害
|
|||
|
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, trans);
|
|||
|
await backgroundService.AddTask("", "", "device_disaster", "purge_specify_insert", device_disaster_dicts);
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
var sql_del_disaster = $"DELETE FROM device_disaster WHERE device_guid = @Device_guid";
|
|||
|
|
|||
|
await conn.ExecuteAsync(sql_del_disaster, new { Device_guid = device_guid }, trans);
|
|||
|
}
|
|||
|
|
|||
|
trans.Commit();
|
|||
|
}
|
|||
|
catch (Exception exception)
|
|||
|
{
|
|||
|
trans.Rollback();
|
|||
|
throw exception;
|
|||
|
}
|
|||
|
finally
|
|||
|
{
|
|||
|
conn.Close();
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|