262 lines
9.9 KiB
C#
262 lines
9.9 KiB
C#
using Cowain.Base.DBContext;
|
|
using Cowain.Base.Models;
|
|
using Cowain.Base.Services;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Plugin.Cowain.Wcs.IServices;
|
|
using Plugin.Cowain.Wcs.Models.Dto;
|
|
using Plugin.Cowain.Wcs.ViewModels;
|
|
|
|
namespace Plugin.Cowain.Wcs.Services;
|
|
|
|
public class ProcessFlowService : BaseService, IProcessFlowService
|
|
{
|
|
public ProcessFlowService(IDbContextFactory<SqlDbContext> dbContextFactory) : base(dbContextFactory)
|
|
{
|
|
}
|
|
public async Task<ResultModel> AddAsync(int processId, List<ProcessFlowViewModel> flows)
|
|
{
|
|
if (flows == null || flows.Count == 0)
|
|
return ResultModel.Error("流程数据不能为空");
|
|
|
|
try
|
|
{
|
|
using var dbContext = _dbContextFactory.CreateDbContext();
|
|
var DbSet = dbContext.GetDbSet<ProcessFlowDto>();
|
|
var entities = new List<ProcessFlowDto>();
|
|
foreach (var f in flows)
|
|
{
|
|
entities.Add(new ProcessFlowDto
|
|
{
|
|
ProcessId = processId,
|
|
Priority = f.Priority,
|
|
FromStationId1 = f.FromStationId1,
|
|
ToStationId1 = f.ToStationId1,
|
|
FromStatus1 = f.FromStatus1,
|
|
ToStatus1 = f.ToStatus1,
|
|
FromStationId2 = f.FromStationId2,
|
|
ToStationId2 = f.ToStationId2,
|
|
FromStatus2 = f.FromStatus2,
|
|
ToStatus2 = f.ToStatus2,
|
|
Action = f.Action
|
|
});
|
|
|
|
}
|
|
await DbSet.AddRangeAsync(entities);
|
|
var count = await dbContext.SaveChangesAsync();
|
|
return count > 0 ? ResultModel.Success("流程数据添加成功") : ResultModel.Error("流程数据添加失败");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ResultModel.Error($"添加流程失败: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
public async Task<ResultModel> UpdateAsync(int processId, List<ProcessFlowViewModel> flows)
|
|
{
|
|
if (flows == null || flows.Count == 0)
|
|
return ResultModel.Error("流程数据不能为空");
|
|
|
|
try
|
|
{
|
|
using var dbContext = _dbContextFactory.CreateDbContext();
|
|
var dbSet = dbContext.GetDbSet<ProcessFlowDto>();
|
|
foreach (var f in flows)
|
|
{
|
|
// 只根据Id和ProcessId查找
|
|
var entity = await dbSet.FirstOrDefaultAsync(x => x.Id == f.Id && x.ProcessId == processId);
|
|
|
|
if (entity != null)
|
|
{
|
|
// 更新已有
|
|
entity.Priority = f.Priority;
|
|
entity.FromStationId1 = f.FromStationId1;
|
|
entity.ToStationId1 = f.ToStationId1;
|
|
entity.FromStatus1 = f.FromStatus1;
|
|
entity.ToStatus1 = f.ToStatus1;
|
|
entity.FromStationId2 = f.FromStationId2;
|
|
entity.ToStationId2 = f.ToStationId2;
|
|
entity.FromStatus2 = f.FromStatus2;
|
|
entity.ToStatus2 = f.ToStatus2;
|
|
entity.Action = f.Action;
|
|
}
|
|
else
|
|
{
|
|
// 新增
|
|
var newEntity = new ProcessFlowDto
|
|
{
|
|
ProcessId = processId,
|
|
Priority = f.Priority,
|
|
FromStationId1 = f.FromStationId1,
|
|
ToStationId1 = f.ToStationId1,
|
|
FromStatus1 = f.FromStatus1,
|
|
ToStatus1 = f.ToStatus1,
|
|
FromStationId2 = f.FromStationId2,
|
|
ToStationId2 = f.ToStationId2,
|
|
FromStatus2 = f.FromStatus2,
|
|
ToStatus2 = f.ToStatus2,
|
|
Action = f.Action
|
|
};
|
|
await dbSet.AddAsync(newEntity);
|
|
}
|
|
}
|
|
|
|
var count = await dbContext.SaveChangesAsync();
|
|
return count >= 0 ? ResultModel.Success("流程更新成功") : ResultModel.Error("流程更新失败");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ResultModel.Error($"更新流程失败: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
public async Task<ResultModel> DeleteAsync(int processId)
|
|
{
|
|
try
|
|
{
|
|
using var dbContext = _dbContextFactory.CreateDbContext();
|
|
var dbSet = dbContext.GetDbSet<ProcessFlowDto>();
|
|
var entitie = await dbSet.Where(x => x.Id == processId).FirstOrDefaultAsync();
|
|
if (entitie == null)
|
|
{
|
|
return ResultModel.Success("未找到要删除的流程数据");
|
|
}
|
|
|
|
dbSet.Remove(entitie);
|
|
var count = await dbContext.SaveChangesAsync();
|
|
return count > 0 ? ResultModel.Success("流程数据删除成功") : ResultModel.Error("流程数据删除失败");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ResultModel.Error($"删除流程失败: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
public async Task<ResultModel> DeleteByIdAsync(int id)
|
|
{
|
|
try
|
|
{
|
|
using var dbContext = _dbContextFactory.CreateDbContext();
|
|
var dbSet = dbContext.GetDbSet<ProcessFlowDto>();
|
|
var existingModel = await dbSet.FirstOrDefaultAsync(x => x.Id == id);
|
|
if (existingModel == null)
|
|
{
|
|
return ResultModel.Error("process no exists");
|
|
}
|
|
|
|
dbSet.Remove(existingModel);
|
|
var count = await dbContext.SaveChangesAsync();
|
|
return count > 0 ? ResultModel.Success("流程数据删除成功") : ResultModel.Error("流程数据删除失败");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return ResultModel.Error($"删除流程失败: {ex.Message}");
|
|
}
|
|
}
|
|
|
|
public async Task<List<ProcessFlowViewModel>> GetAllAsync()
|
|
{
|
|
using var dbContext = _dbContextFactory.CreateDbContext();
|
|
var DbSet = dbContext.GetDbSet<ProcessFlowDto>();
|
|
var data = await DbSet.ToListAsync();
|
|
var result = new List<ProcessFlowViewModel>();
|
|
foreach (var x in data)
|
|
{
|
|
// 1号流程
|
|
if (x.FromStationId1 != 0 || x.ToStationId1 != 0 || !string.IsNullOrEmpty(x.FromStatus1) || !string.IsNullOrEmpty(x.ToStatus1))
|
|
{
|
|
result.Add(new ProcessFlowViewModel
|
|
{
|
|
Id = x.Id,
|
|
ProcessId = x.ProcessId,
|
|
Priority = x.Priority,
|
|
FromStationId1 = x.FromStationId1,
|
|
ToStationId1 = x.ToStationId1,
|
|
FromStatus1 = x.FromStatus1,
|
|
ToStatus1 = x.ToStatus1,
|
|
FromStationId2 = x.FromStationId2,
|
|
ToStationId2 = x.ToStationId2,
|
|
FromStatus2 = x.FromStatus2,
|
|
ToStatus2 = x.ToStatus2,
|
|
Action = x.Action
|
|
});
|
|
}
|
|
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public async Task<(List<ProcessFlowViewModel>, int totals)> GetAllAsync(int pageIndex, int pageSize)
|
|
{
|
|
using var dbContext = _dbContextFactory.CreateDbContext();
|
|
var DbSet = dbContext.GetDbSet<ProcessFlowDto>();
|
|
var data = await DbSet.ToListAsync();
|
|
var result = new List<ProcessFlowViewModel>();
|
|
foreach (var x in data)
|
|
{
|
|
if (x.FromStationId1 != 0 || x.ToStationId1 != 0 || !string.IsNullOrEmpty(x.FromStatus1) || !string.IsNullOrEmpty(x.ToStatus1))
|
|
{
|
|
result.Add(new ProcessFlowViewModel
|
|
{
|
|
Id = x.Id,
|
|
ProcessId = x.ProcessId,
|
|
Priority = x.Priority,
|
|
FromStationId1 = x.FromStationId1,
|
|
ToStationId1 = x.ToStationId1,
|
|
FromStatus1 = x.FromStatus1,
|
|
ToStatus1 = x.ToStatus1,
|
|
FromStationId2 = x.FromStationId2,
|
|
ToStationId2 = x.ToStationId2,
|
|
FromStatus2 = x.FromStatus2,
|
|
ToStatus2 = x.ToStatus2,
|
|
Action = x.Action
|
|
});
|
|
}
|
|
|
|
}
|
|
var paged = result.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
|
|
return (paged, result.Count);
|
|
}
|
|
|
|
public async Task<(List<ProcessFlowViewModel>, int totals)> GetAllAsync(int pageIndex, int pageSize, int? processId)
|
|
{
|
|
using var dbContext = _dbContextFactory.CreateDbContext();
|
|
var DbSet = dbContext.GetDbSet<ProcessFlowDto>();
|
|
IQueryable<ProcessFlowDto> query;
|
|
if (processId.HasValue && await DbSet.AnyAsync(x => x.ProcessId == processId))
|
|
{
|
|
query = DbSet.Where(x => x.ProcessId == processId);
|
|
}
|
|
else
|
|
{
|
|
query = DbSet;
|
|
}
|
|
|
|
var data = await query.OrderBy(x => x.Id).ToListAsync();
|
|
var result = new List<ProcessFlowViewModel>();
|
|
foreach (var x in data)
|
|
{
|
|
if (x.FromStationId1 != 0 || x.ToStationId1 != 0 || !string.IsNullOrEmpty(x.FromStatus1) || !string.IsNullOrEmpty(x.ToStatus1))
|
|
{
|
|
result.Add(new ProcessFlowViewModel
|
|
{
|
|
Id = x.Id,
|
|
ProcessId = x.ProcessId,
|
|
Priority = x.Priority,
|
|
FromStationId1 = x.FromStationId1,
|
|
ToStationId1 = x.ToStationId1,
|
|
FromStatus1 = x.FromStatus1,
|
|
ToStatus1 = x.ToStatus1,
|
|
FromStationId2 = x.FromStationId2,
|
|
ToStationId2 = x.ToStationId2,
|
|
FromStatus2 = x.FromStatus2,
|
|
ToStatus2 = x.ToStatus2,
|
|
Action = x.Action
|
|
});
|
|
}
|
|
}
|
|
|
|
var paged = result.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
|
|
return (paged, result.Count);
|
|
}
|
|
}
|