You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
haoliang-net/src/CncRepository/Impl/MachineRepository.cs

168 lines
9.2 KiB
C#

using System.Collections.Generic;
using System.Linq;
using Dapper;
using CncModels.Entity;
using CncModels.Dto;
using CncModels.Dto.Machine;
using CncRepository.Base;
using CncRepository.Interface;
namespace CncRepository.Impl
{
/// <summary>
/// 机床实现
/// </summary>
public class MachineRepository : BusinessRepository, IMachineRepository
{
public MachineRepository(string connectionString) : base(connectionString) { }
public Machine GetById(int id)
{
using (var conn = CreateConnection())
{
var sql = @"SELECT Id as Id, DeviceCode as DeviceCode, Name as Name, WorkshopId as WorkshopId, CollectAddressId as CollectAddressId, IpAddress as IpAddress, BrandId as BrandId, IsEnabled as IsEnabled, IsOnline as IsOnline, LastPingTime as LastPingTime, LastCollectTime as LastCollectTime, LastDeviceStatus as LastDeviceStatus, LastRunStatus as LastRunStatus, LastProgramName as LastProgramName, LastPartCount as LastPartCount, LastOperateMode as LastOperateMode, LastMachiningStatus as LastMachiningStatus, CreatedAt as CreatedAt, UpdatedAt as UpdatedAt FROM cnc_machine WHERE Id = @Id";
return conn.QuerySingleOrDefault<Machine>(sql, new { Id = id });
}
}
public PagedResult<MachineListItem> GetList(MachineQuery query)
{
using (var conn = CreateConnection())
{
var where = " WHERE 1=1";
var p = new DynamicParameters();
if (!string.IsNullOrWhiteSpace(query.Keyword))
{
where += " AND (m.Name LIKE @Keyword OR m.DeviceCode LIKE @Keyword)";
p.Add("Keyword", $"%{query.Keyword}%");
}
if (query.WorkshopId.HasValue)
{
where += " AND m.WorkshopId = @WorkshopId";
p.Add("WorkshopId", query.WorkshopId.Value);
}
if (query.IsOnline.HasValue)
{
where += " AND m.IsOnline = @IsOnline";
p.Add("IsOnline", query.IsOnline.Value);
}
if (query.BrandId.HasValue)
{
where += " AND m.BrandId = @BrandId";
p.Add("BrandId", query.BrandId.Value);
}
var limit = query.PageSize;
var offset = query.Offset;
var sql = @"SELECT m.Id as Id, m.DeviceCode as DeviceCode, m.Name as Name, m.WorkshopId as WorkshopId, ws.Name as WorkshopName, m.CollectAddressId as CollectAddressId, m.BrandId as BrandId, b.BrandName as BrandName, m.IpAddress as IpAddress, m.IsEnabled as IsEnabled, m.IsOnline as IsOnline, m.LastProgramName as LastProgramName, m.LastCollectTime as LastCollectTime, w.Id as WorkerId, w.Name as WorkerName
FROM cnc_machine m
LEFT JOIN cnc_workshop ws ON m.WorkshopId = ws.Id
LEFT JOIN cnc_brand b ON m.BrandId = b.Id
LEFT JOIN cnc_worker_machine wm ON m.Id = wm.MachineId
LEFT JOIN cnc_worker w ON wm.WorkerId = w.Id" + where + @" ORDER BY m.Id DESC LIMIT @Limit OFFSET @Offset";
p.Add("Limit", limit);
p.Add("Offset", offset);
var totalSql = @"SELECT COUNT(*) FROM cnc_machine m" + where + @"";
var total = conn.ExecuteScalar<int>(totalSql, p);
var items = conn.Query<MachineListItem>(sql, p).ToList();
return new PagedResult<MachineListItem>
{
Items = items,
Total = total,
Page = query.Page,
PageSize = limit
};
}
}
public int Create(Machine entity)
{
using (var conn = CreateConnection())
{
var sql = @"INSERT INTO cnc_machine (DeviceCode, Name, WorkshopId, CollectAddressId, IpAddress, BrandId, IsEnabled, IsOnline, CreatedAt, UpdatedAt)
VALUES (@DeviceCode, @Name, @WorkshopId, @CollectAddressId, @IpAddress, @BrandId, @IsEnabled, @IsOnline, @CreatedAt, @UpdatedAt);
SELECT LAST_INSERT_ID();";
return conn.QuerySingle<int>(sql, entity);
}
}
public bool Update(Machine entity)
{
using (var conn = CreateConnection())
{
var sql = @"UPDATE cnc_machine SET DeviceCode = @DeviceCode, Name = @Name, WorkshopId = @WorkshopId, CollectAddressId = @CollectAddressId, IpAddress = @IpAddress, BrandId = @BrandId, IsEnabled = @IsEnabled, IsOnline = @IsOnline, UpdatedAt = @UpdatedAt, LastProgramName = @LastProgramName, LastCollectTime = @LastCollectTime, LastDeviceStatus = @LastDeviceStatus, LastRunStatus = @LastRunStatus, LastMachiningStatus = @LastMachiningStatus WHERE Id = @Id";
return conn.Execute(sql, entity) > 0;
}
}
public bool Delete(int id)
{
using (var conn = CreateConnection())
{
var sql = @"DELETE FROM cnc_machine WHERE Id = @Id";
return conn.Execute(sql, new { Id = id }) > 0;
}
}
public bool ToggleEnabled(int id)
{
using (var conn = CreateConnection())
{
var sql = @"UPDATE cnc_machine SET IsEnabled = CASE WHEN IsEnabled = 1 THEN 0 ELSE 1 END, UpdatedAt = NOW() WHERE Id = @Id";
return conn.Execute(sql, new { Id = id }) > 0;
}
}
public Machine GetByDeviceCode(string deviceCode)
{
using (var conn = CreateConnection())
{
var sql = @"SELECT Id as Id, DeviceCode as DeviceCode, Name as Name, WorkshopId as WorkshopId, CollectAddressId as CollectAddressId, IpAddress as IpAddress, BrandId as BrandId, IsEnabled as IsEnabled, IsOnline as IsOnline, LastPingTime as LastPingTime, LastCollectTime as LastCollectTime, LastDeviceStatus as LastDeviceStatus, LastRunStatus as LastRunStatus, LastProgramName as LastProgramName, LastPartCount as LastPartCount, LastOperateMode as LastOperateMode, LastMachiningStatus as LastMachiningStatus, CreatedAt as CreatedAt, UpdatedAt as UpdatedAt FROM cnc_machine WHERE DeviceCode = @DeviceCode";
return conn.QuerySingleOrDefault<Machine>(sql, new { DeviceCode = deviceCode });
}
}
public List<Machine> GetEnabledByAddressId(int collectAddressId)
{
using (var conn = CreateConnection())
{
var sql = @"SELECT Id as Id, DeviceCode as DeviceCode, Name as Name, WorkshopId as WorkshopId, CollectAddressId as CollectAddressId, IpAddress as IpAddress, BrandId as BrandId, IsEnabled as IsEnabled, IsOnline as IsOnline, LastPingTime as LastPingTime, LastCollectTime as LastCollectTime, LastDeviceStatus as LastDeviceStatus, LastRunStatus as LastRunStatus, LastProgramName as LastProgramName, LastPartCount as LastPartCount, LastOperateMode as LastOperateMode, LastMachiningStatus as LastMachiningStatus, CreatedAt as CreatedAt, UpdatedAt as UpdatedAt FROM cnc_machine WHERE CollectAddressId = @CollectAddressId AND IsEnabled = 1";
return conn.Query<Machine>(sql, new { CollectAddressId = collectAddressId }).ToList();
}
}
public List<Machine> GetEnabledOnline()
{
using (var conn = CreateConnection())
{
var sql = @"SELECT Id as Id, DeviceCode as DeviceCode, Name as Name, WorkshopId as WorkshopId, CollectAddressId as CollectAddressId, IpAddress as IpAddress, BrandId as BrandId, IsEnabled as IsEnabled, IsOnline as IsOnline, LastPingTime as LastPingTime, LastCollectTime as LastCollectTime, LastDeviceStatus as LastDeviceStatus, LastRunStatus as LastRunStatus, LastProgramName as LastProgramName, LastPartCount as LastPartCount, LastOperateMode as LastOperateMode, LastMachiningStatus as LastMachiningStatus, CreatedAt as CreatedAt, UpdatedAt as UpdatedAt FROM cnc_machine WHERE IsEnabled = 1 AND IsOnline = 1";
return conn.Query<Machine>(sql).ToList();
}
}
public void UpdateOnlineStatus(int id, bool isOnline)
{
using (var conn = CreateConnection())
{
var sql = @"UPDATE cnc_machine SET IsOnline = @IsOnline, UpdatedAt = NOW() WHERE Id = @Id";
conn.Execute(sql, new { Id = id, IsOnline = isOnline ? 1 : 0 });
}
}
public void UpdateLastCollect(int id, Machine entity)
{
using (var conn = CreateConnection())
{
var sql = @"UPDATE cnc_machine SET LastCollectTime = @LastCollectTime, LastProgramName = @LastProgramName, LastMachiningStatus = @LastMachiningStatus, UpdatedAt = NOW() WHERE Id = @Id";
var param = new
{
Id = id,
LastCollectTime = entity.LastCollectTime,
LastProgramName = entity.LastProgramName,
LastMachiningStatus = entity.LastMachiningStatus
};
conn.Execute(sql, param);
}
}
}
}