Files
WCS/Plugins/Wcs/Plugin.Cowain.Wcs/Services/ProcessService.cs
2026-03-02 10:56:30 +08:00

227 lines
7.8 KiB
C#

using Cowain.Base.DBContext;
using Cowain.Base.Models;
using Cowain.Base.Services;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using Plugin.Cowain.Wcs.IServices;
using Plugin.Cowain.Wcs.Models;
using Plugin.Cowain.Wcs.Models.Dto;
using Plugin.Cowain.Wcs.ViewModels;
namespace Plugin.Cowain.Wcs.Services;
public class ProcessService : BaseService, IProcessService
{
public ProcessService(IDbContextFactory<SqlDbContext> dbContextFactory) : base(dbContextFactory)
{
}
public async Task<ResultModel> AddAsync(ProcessViewModel process)
{
if (process == null)
{
return ResultModel.Error("process不能为空");
}
if (string.IsNullOrWhiteSpace(process.Name))
{
return ResultModel.Error("流程名称不能为空");
}
using var dbContext = _dbContextFactory.CreateDbContext();
var DbSet = dbContext.GetDbSet<ProcessDto>();
// 检查名称是否已存在
var exists = await DbSet.AnyAsync(x => x.Name == process.Name);
if (exists)
{
return ResultModel.Error("已存在同名流程");
}
var entity = new ProcessDto
{
Name = process.Name,
Enable = true,
FlowData = process.FlowData,
CreateTime = DateTime.Now,
UpdateTime = DateTime.Now
};
await DbSet.AddAsync(entity);
var count = await dbContext.SaveChangesAsync();
return count > 0 ? ResultModel.Success("流程添加成功") : ResultModel.Error("流程添加失败");
}
public async Task<ResultModel> DeleteAsync(int id)
{
if (id <= 0)
{
return ResultModel.Error("id cannot be null");
}
using var dbContext = _dbContextFactory.CreateDbContext();
var processDbSet = dbContext.GetDbSet<ProcessDto>();
var flowDbSet = dbContext.GetDbSet<ProcessFlowDto>();
var existingModel = await processDbSet.FirstOrDefaultAsync(x => x.Id == id);
if (existingModel == null)
{
return ResultModel.Error("device no exists");
}
using var transaction = await dbContext.Database.BeginTransactionAsync();
try
{
// 删除流程相关的流程流数据
var relatedFlows = await flowDbSet.Where(f => f.ProcessId == id).ToListAsync();
if (relatedFlows.Count > 0)
{
flowDbSet.RemoveRange(relatedFlows);
}
// 删除流程
processDbSet.Remove(existingModel);
int count = await dbContext.SaveChangesAsync();
await transaction.CommitAsync();
return count > 0 ? ResultModel.Success("process delete successfully") : ResultModel.Error("process delete error");
}
catch (Exception ex)
{
await transaction.RollbackAsync();
return ResultModel.Error($"process delete error: {ex.Message}");
}
}
public async Task<ResultModel> DisableAsync(int id)
{
if (id <= 0)
{
return ResultModel.Error("id cannot be null");
}
using var dbContext = _dbContextFactory.CreateDbContext();
var DbSet = dbContext.GetDbSet<ProcessDto>();
var existingModel = await DbSet.FirstOrDefaultAsync(x => x.Id == id);
if (existingModel == null)
{
return ResultModel.Error("device no exists");
}
existingModel.Enable = false;
await dbContext.SaveChangesAsync();
return ResultModel.Success("device disable successfully");
}
public async Task<ResultModel> EnableAsync(int id)
{
if (id <= 0)
{
return ResultModel.Error("id cannot be null");
}
using var dbContext = _dbContextFactory.CreateDbContext();
var DbSet = dbContext.GetDbSet<ProcessDto>();
var existingModel = await DbSet.FirstOrDefaultAsync(x => x.Id == id);
if (existingModel == null)
{
return ResultModel.Error("device no exists");
}
existingModel.Enable = true;
await dbContext.SaveChangesAsync();
return ResultModel.Success("device enabled successfully");
}
public async Task<ResultModel<RgvJsonModel>> GetJsonData(int id)
{
using var dbContext = _dbContextFactory.CreateDbContext();
var DbSet = dbContext.GetDbSet<ProcessDto>();
var data = await DbSet.FirstOrDefaultAsync(x => x.Id == id);
if (data == null)
{
return ResultModel<RgvJsonModel>.Error("process no exists");
}
if (string.IsNullOrEmpty(data.Json))
{
return ResultModel<RgvJsonModel>.Error("json cannot be null");
}
try
{
RgvJsonModel? d = JsonConvert.DeserializeObject<RgvJsonModel>(data.Json);
if (d == null)
{
return ResultModel<RgvJsonModel>.Error("json DeserializeObject is null");
}
return ResultModel<RgvJsonModel>.Success(d);
}
catch (Exception ex)
{
return ResultModel<RgvJsonModel>.Error($"json DeserializeObject error :{ex.Message}");
}
}
public async Task<List<ProcessViewModel>> GetAllAsync()
{
using var dbContext = _dbContextFactory.CreateDbContext();
var DbSet = dbContext.GetDbSet<ProcessDto>();
var data = await DbSet.ToListAsync();
return new List<ProcessViewModel>(data.Select(x => new ProcessViewModel
{
Id = x.Id,
Name = x.Name,
FlowData = x.FlowData,
CreateTime = x.CreateTime,
UpdateTime = x.UpdateTime,
Json = x.Json,
Enable = x.Enable,
}));
}
public async Task<(List<ProcessViewModel>, int totals)> GetAllAsync(int pageIndex, int pageSize)
{
using var dbContext = _dbContextFactory.CreateDbContext();
var DbSet = dbContext.GetDbSet<ProcessDto>();
var data = await DbSet.ToListAsync();
var list = data.Skip((pageIndex - 1) * pageSize).Take(pageSize);
return (new List<ProcessViewModel>(list.Select(x => new ProcessViewModel
{
Id = x.Id,
Name = x.Name,
FlowData = x.FlowData,
CreateTime = x.CreateTime,
UpdateTime = x.UpdateTime,
Json = x.Json,
Enable = x.Enable,
})), data.Count());
}
public async Task<ResultModel> UpdateAsync(ProcessViewModel? process)
{
if (process == null)
{
return ResultModel.Error("参数不能为空");
}
if (process.Id <= 0)
{
return ResultModel.Error("流程ID不能为空");
}
if (string.IsNullOrWhiteSpace(process.Name))
{
return ResultModel.Error("流程名称不能为空");
}
using var dbContext = _dbContextFactory.CreateDbContext();
var DbSet = dbContext.GetDbSet<ProcessDto>();
var existingModel = await DbSet.FirstOrDefaultAsync(x => x.Id == process.Id);
if (existingModel == null)
{
return ResultModel.Error("流程不存在");
}
// 检查是否有同名流程(排除自己)
var exists = await DbSet.AnyAsync(x => x.Name == process.Name && x.Id != process.Id);
if (exists)
{
return ResultModel.Error("已存在同名流程");
}
existingModel.Name = process.Name;
existingModel.FlowData = process.FlowData;
existingModel.Enable = process.Enable;
existingModel.UpdateTime = DateTime.Now;
existingModel.Json = process.Json;
var count = await dbContext.SaveChangesAsync();
return count > 0 ? ResultModel.Success("流程更新成功") : ResultModel.Error("流程更新失败");
}
}