using Dapper;
using Repository.BackendRepository.Interface;
using Repository.BaseRepository.Interface;
using Repository.Helper;
using Repository.Services.Implement;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
namespace Repository.BackendRepository.Implement
{
public class BackendRepository : BaseRepository.Implement.BaseRepository, IBackendRepository
{
private BackgroundService backgroundService;
public BackendRepository(IDatabaseHelper databaseHelper) : base(databaseHelper)
{
UseDB = "MYSQL";
//con = databaseHelper.GetMSSqlConnection();
}
///
/// 新增記錄人員操作記錄
///
///
///
public void InsertOperatorLog(Dictionary dict, string Table_name)
{
List results = new List();
using (IDbConnection conn = GetDbConnection())
{
conn.Open();
using (var trans = conn.BeginTransaction())
{
try
{
List properties = dict.Keys.ToList();
string sql = InsertGenerateString(properties, Table_name);
conn.Execute(sql, dict, trans);
trans.Commit();
}
catch (Exception exception)
{
trans.Rollback();
throw exception;
}
finally
{
conn.Close();
}
}
}
}
///
/// 手動加入資料至背景派送
///
///
///
///
///
///
///
public async Task ManualInsertBackgroundServiceTask(string building_ip, string building_guid, string target_table, string mode, List> parameter = null)
{
List results = new List();
using (IDbConnection conn = GetDbConnection())
{
conn.Open();
using (var trans = conn.BeginTransaction())
{
try
{
backgroundService = new BackgroundService(conn, trans);
await backgroundService.AddTask("", "", target_table, mode, parameter);
trans.Commit();
}
catch (Exception exception)
{
trans.Rollback();
throw exception;
}
finally
{
conn.Close();
}
}
}
}
///
/// 手動加入檔案背至景派送
///
///
///
///
///
///
public async Task ManualInsertFileBackgroundServiceTask(string building_ip, string building_guid, string target_table, List fileInfos)
{
List results = new List();
using (IDbConnection conn = GetDbConnection())
{
conn.Open();
using (var trans = conn.BeginTransaction())
{
try
{
var file_json = JsonSerializer.Serialize(fileInfos);
Dictionary dict = new Dictionary()
{
{ "@target_files", file_json }
};
backgroundService = new BackgroundService(conn, trans);
await backgroundService.AddTask("", "", target_table, fileInfos);
trans.Commit();
}
catch (Exception exception)
{
trans.Rollback();
throw exception;
}
finally
{
conn.Close();
}
}
}
}
}
}