Files
WCS/Plugins/Driver/Cowain.Driver/Services/ActionService.cs
2026-03-02 09:08:20 +08:00

165 lines
5.8 KiB
C#

using Cowain.Base.DBContext;
using Cowain.Base.Models;
using Cowain.Base.Services;
using Microsoft.EntityFrameworkCore;
using Plugin.Cowain.Driver.IServices;
using Plugin.Cowain.Driver.Models.Dto;
using Plugin.Cowain.Driver.ViewModels;
using System.Collections.ObjectModel;
namespace Plugin.Cowain.Driver.Services;
public class ActionService : BaseService, IActionService
{
public ActionService(IDbContextFactory<SqlDbContext> dbContextFactory) : base(dbContextFactory)
{
}
public async Task<List<VarActionViewModel>> GetDeviceActionsAsync(int deviceId)
{
var actions = await QueryAsync<VarActionDto>(x => x.DeviceId == deviceId);
var result = actions.Select(a => new VarActionViewModel
{
Id = a.Id,
DeviceId = a.DeviceId,
TagId = a.TagId,
ActionName = a.ActionName,
Param = a.Param,
ActionValue = a.ActionValue,
Desc = a.Desc,
Condition = a.Condition
}).ToList();
return result;
}
public async Task<List<DeviceViewModel>> GetDeviceAsync()
{
using var dbContext = _dbContextFactory.CreateDbContext();
var devices = await dbContext.Set<DeviceDto>().ToListAsync();
var tagAddresses = await dbContext.Set<TagAddressDto>().ToListAsync();
var varActions = await dbContext.Set<VarActionDto>().ToListAsync();
var deviceViewModels = devices.Select(device => new DeviceViewModel
{
Id = device.Id,
DeviceName = device.DeviceName,
DriverName = device.DriverName,
DeviceType = device.DeviceType,
Variables = new ObservableCollection<VariableViewModel>(
tagAddresses.Where(tag => tag.DeviceId == device.Id).Select(tag => new VariableViewModel
{
Id = tag.Id,
DeviceId = tag.DeviceId,
DeviceName = device.DeviceName,
Name = tag.Name,
Address = tag.Address,
Desc = tag.Desc,
}).ToList()
),
VarActions = new ObservableCollection<VarActionViewModel>(
varActions.Where(action => action.DeviceId == device.Id).Select(action => new VarActionViewModel
{
Id = action.Id,
DeviceId = action.DeviceId,
TagId = action.TagId,
ActionName = action.ActionName,
Param = action.Param,
ActionValue = action.ActionValue,
Desc = action.Desc,
Condition = action.Condition
}).ToList()
)
}).ToList();
return deviceViewModels;
}
public async Task<ResultModel> AddActionAsync(VarActionViewModel varAction)
{
//using var dbContext = _dbContextFactory.CreateDbContext();
//var existingAction = await dbContext.Set<VarActionDto>()
// .FirstOrDefaultAsync(a => a.DeviceId == varAction.DeviceId && a.TagId == varAction.TagId);
var existingAction = await FirstOrDefaultAsync<VarActionDto>(x => x.DeviceId == varAction.DeviceId && x.TagId == varAction.TagId);
if (existingAction != null)
{
return ResultModel.Error("Action with the same DeviceId and TagId already exists.");
}
var newAction = new VarActionDto
{
DeviceId = varAction.DeviceId,
TagId = varAction.TagId,
ActionName = varAction.ActionName,
Param = varAction.Param,
ActionValue = varAction.ActionValue,
Desc = varAction.Desc,
Condition = varAction.Condition
};
try
{
var result = await InsertAsync<VarActionDto>(newAction);
if (result > 0)
{
return ResultModel.Success("Action added successfully");
}
return ResultModel.Error("Failed to add Action.");
}
catch (Exception ex)
{
return ResultModel.Error($"An error occurred while adding the Action: {ex.Message}", 500);
}
}
public async Task<ResultModel> DeleteActionAsync(int id)
{
try
{
var result = await DeleteAsync<VarActionDto>(id);
if (result > 0)
{
return ResultModel.Success("Action delete successfully");
}
return ResultModel.Error("Failed to delete Action.");
}
catch (Exception ex)
{
return ResultModel.Error($"An error occurred while deleting the Action: {ex.Message}", 500);
}
}
public async Task<ResultModel> UpdateActionAsync(VarActionViewModel varAction)
{
var existingAction = await FirstOrDefaultAsync<VarActionDto>(a => a.Id == varAction.Id);
if (existingAction == null)
{
return ResultModel.Error("Action not found", 404);
}
existingAction.DeviceId = varAction.DeviceId;
existingAction.TagId = varAction.TagId;
existingAction.ActionName = varAction.ActionName;
existingAction.Param = varAction.Param;
existingAction.ActionValue = varAction.ActionValue;
existingAction.Desc = varAction.Desc;
existingAction.Condition = varAction.Condition;
try
{
var result = await UpdateAsync<VarActionDto>(existingAction);
if (result > 0)
{
return ResultModel.Success("Action Update successfully");
}
return ResultModel.Error("Failed to Update Action.");
}
catch (Exception ex)
{
return ResultModel.Error($"An error occurred while updating the Action: {ex.Message}", 500);
}
}
}