251 lines
8.8 KiB
C#
251 lines
8.8 KiB
C#
using Cowain.Base.DBContext;
|
|
using Cowain.Base.Helpers;
|
|
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 StationService : BaseService, IStationService
|
|
{
|
|
public StationService(IDbContextFactory<SqlDbContext> dbContextFactory) : base(dbContextFactory)
|
|
{
|
|
|
|
}
|
|
|
|
public async Task<ResultModel> AddAsync(StationViewModel station)
|
|
{
|
|
if (station == null)
|
|
{
|
|
return ResultModel.Error("station不能为空");
|
|
}
|
|
if (string.IsNullOrWhiteSpace(station.StationName))
|
|
{
|
|
return ResultModel.Error("工站名称不能为空");
|
|
}
|
|
if (string.IsNullOrWhiteSpace(station.StationCode))
|
|
{
|
|
return ResultModel.Error("工站编码不能为空");
|
|
}
|
|
using var dbContext = _dbContextFactory.CreateDbContext();
|
|
var dbSet = dbContext.GetDbSet<StationDto>();
|
|
// 检查StationName是否重复
|
|
var nameExists = await dbSet.AnyAsync(x => x.StationName == station.StationName);
|
|
if (nameExists)
|
|
{
|
|
return ResultModel.Error("工站名称已存在");
|
|
}
|
|
|
|
// 检查StationCode是否重复
|
|
var codeExists = await dbSet.AnyAsync(x => x.StationCode == station.StationCode);
|
|
if (codeExists)
|
|
{
|
|
return ResultModel.Error("工站编码已存在");
|
|
}
|
|
|
|
var entity = new StationDto
|
|
{
|
|
QrCode = station.QrCode,
|
|
ProcessName = station.ProcessName,
|
|
StationName = station.StationName,
|
|
StationCode = station.StationCode,
|
|
PositionX = station.PositionX,
|
|
PositionY = station.PositionY,
|
|
PositionZ = station.PositionZ,
|
|
LayOutX = station.LayOutX,
|
|
LayOutY = station.LayOutY,
|
|
StationPos = station.StationPos,
|
|
Status = station.Status,
|
|
Enable = true,
|
|
};
|
|
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不能小于0");
|
|
}
|
|
using var dbContext = _dbContextFactory.CreateDbContext();
|
|
var DbSet = dbContext.GetDbSet<StationDto>();
|
|
var existingModel = await DbSet.FirstOrDefaultAsync(x => x.Id == id);
|
|
if (existingModel == null)
|
|
{
|
|
return ResultModel.Error("工站id不存在");
|
|
}
|
|
DbSet.Remove(existingModel);
|
|
int count = await dbContext.SaveChangesAsync();
|
|
return count > 0 ? ResultModel.Success("工站删除成功") : ResultModel.Error("工站删除失败");
|
|
}
|
|
|
|
public async Task<ResultModel> DisableAsync(int id)
|
|
{
|
|
if (id <= 0)
|
|
{
|
|
return ResultModel.Error("id不能小于0");
|
|
}
|
|
using var dbContext = _dbContextFactory.CreateDbContext();
|
|
var DbSet = dbContext.GetDbSet<StationDto>();
|
|
var existingModel = await DbSet.FirstOrDefaultAsync(x => x.Id == id);
|
|
if (existingModel == null)
|
|
{
|
|
return ResultModel.Error("工站id不存在");
|
|
}
|
|
existingModel.Enable = false;
|
|
await dbContext.SaveChangesAsync();
|
|
return ResultModel.Success("工站禁用成功");
|
|
}
|
|
|
|
public async Task<ResultModel> EnableAsync(int id)
|
|
{
|
|
if (id <= 0)
|
|
{
|
|
return ResultModel.Error("id不能小于0");
|
|
}
|
|
using var dbContext = _dbContextFactory.CreateDbContext();
|
|
var DbSet = dbContext.GetDbSet<StationDto>();
|
|
var existingModel = await DbSet.FirstOrDefaultAsync(x => x.Id == id);
|
|
if (existingModel == null)
|
|
{
|
|
return ResultModel.Error("工站id不存在");
|
|
}
|
|
existingModel.Enable = true;
|
|
await dbContext.SaveChangesAsync();
|
|
return ResultModel.Success("工站启用成功");
|
|
}
|
|
|
|
public async Task<List<StationViewModel>> GetAllAsync()
|
|
{
|
|
using var dbContext = _dbContextFactory.CreateDbContext();
|
|
var DbSet = dbContext.GetDbSet<StationDto>();
|
|
var data = await DbSet.ToListAsync();
|
|
return new List<StationViewModel>(data.Select(x => new StationViewModel
|
|
{
|
|
Id = x.Id,
|
|
QrCode = x.QrCode,
|
|
ProcessName = x.ProcessName,
|
|
StationName = x.StationName,
|
|
StationCode = x.StationCode,
|
|
PositionX = x.PositionX,
|
|
PositionY = x.PositionY,
|
|
PositionZ = x.PositionZ,
|
|
LayOutX = x.LayOutX,
|
|
LayOutY = x.LayOutY,
|
|
Status = x.Status,
|
|
StationPos = x.StationPos,
|
|
NextStationId = x.NextStationId,
|
|
Enable = x.Enable,
|
|
CreateTime = x.CreateTime,
|
|
UpdateTime = x.UpdateTime
|
|
}));
|
|
}
|
|
|
|
public async Task<(List<StationViewModel>, int totals)> GetAllAsync(int pageIndex, int pageSize)
|
|
{
|
|
using var dbContext = _dbContextFactory.CreateDbContext();
|
|
var dbSet = dbContext.GetDbSet<StationDto>();
|
|
var query = dbSet.AsQueryable();
|
|
|
|
int totals = await query.CountAsync();
|
|
var data = await query
|
|
.OrderBy(x => x.Id)
|
|
.Skip((pageIndex - 1) * pageSize)
|
|
.Take(pageSize)
|
|
.ToListAsync();
|
|
|
|
var list = data.Select(x => new StationViewModel
|
|
{
|
|
Id = x.Id,
|
|
QrCode = x.QrCode,
|
|
ProcessName = x.ProcessName,
|
|
StationName = x.StationName,
|
|
StationCode = x.StationCode,
|
|
PositionX = x.PositionX,
|
|
PositionY = x.PositionY,
|
|
PositionZ = x.PositionZ,
|
|
LayOutX = x.LayOutX,
|
|
LayOutY = x.LayOutY,
|
|
StationPos = x.StationPos,
|
|
Status = x.Status,
|
|
NextStationId = x.NextStationId,
|
|
Enable = x.Enable,
|
|
CreateTime = x.CreateTime,
|
|
UpdateTime = x.UpdateTime
|
|
}).ToList();
|
|
|
|
return (list, totals);
|
|
}
|
|
|
|
public async Task<ResultModel> UpdateAsync(StationViewModel satation)
|
|
{
|
|
if (satation == null)
|
|
{
|
|
return ResultModel.Error("satation不能为空");
|
|
}
|
|
if (satation.Id <= 0)
|
|
{
|
|
return ResultModel.Error("工站ID不能小于0");
|
|
}
|
|
if (string.IsNullOrWhiteSpace(satation.StationName))
|
|
{
|
|
return ResultModel.Error("工站名称不能为空");
|
|
}
|
|
if (string.IsNullOrWhiteSpace(satation.StationCode))
|
|
{
|
|
return ResultModel.Error("工站编码不能为空");
|
|
}
|
|
using var dbContext = _dbContextFactory.CreateDbContext();
|
|
var DbSet = dbContext.GetDbSet<StationDto>();
|
|
var existingModel = await DbSet.FirstOrDefaultAsync(x => x.Id == satation.Id);
|
|
if (existingModel == null)
|
|
{
|
|
return ResultModel.Error("工站id不存在");
|
|
}
|
|
|
|
// 检查是否有同名工站(排除自己)
|
|
var nameExists = await DbSet.AnyAsync(x => x.StationName == satation.StationName && x.Id != satation.Id);
|
|
if (nameExists)
|
|
{
|
|
return ResultModel.Error("工站名称已存在");
|
|
}
|
|
// 检查是否有同编码工站(排除自己)
|
|
var codeExists = await DbSet.AnyAsync(x => x.StationCode == satation.StationCode && x.Id != satation.Id);
|
|
if (codeExists)
|
|
{
|
|
return ResultModel.Error("工站编码已存在");
|
|
}
|
|
|
|
// 检查是否有同位置工站(排除自己)
|
|
var posExists = await DbSet.AnyAsync(x => x.StationPos == satation.StationPos && x.Id != satation.Id);
|
|
if (posExists)
|
|
{
|
|
return ResultModel.Error("工站位置已存在");
|
|
}
|
|
|
|
// 更新字段
|
|
existingModel.ProcessName = satation.ProcessName;
|
|
existingModel.QrCode = satation.QrCode;
|
|
existingModel.StationName = satation.StationName;
|
|
existingModel.StationCode = satation.StationCode;
|
|
existingModel.PositionX = satation.PositionX;
|
|
existingModel.PositionY = satation.PositionY;
|
|
existingModel.PositionZ = satation.PositionZ;
|
|
existingModel.LayOutX = satation.LayOutX;
|
|
existingModel.LayOutY = satation.LayOutY;
|
|
existingModel.Status = satation.Status;
|
|
existingModel.StationPos = satation.StationPos;
|
|
existingModel.NextStationId = satation.NextStationId;
|
|
existingModel.Enable = satation.Enable;
|
|
existingModel.UpdateTime = DateTime.Now;
|
|
|
|
var count = await dbContext.SaveChangesAsync();
|
|
return count > 0 ? ResultModel.Success("工站更新成功") : ResultModel.Error("工站更新失败");
|
|
}
|
|
}
|