|
|
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) { }
|
|
|
|
|
|
/// <summary>机床SELECT列映射模板(snake_case列名 → PascalCase属性名)</summary>
|
|
|
private const string SelectColumns = @"id as Id, device_code as DeviceCode, name as Name, workshop_id as WorkshopId, collect_address_id as CollectAddressId, ip_address as IpAddress, brand_id as BrandId, is_enabled as IsEnabled, is_online as IsOnline, last_ping_time as LastPingTime, last_collect_time as LastCollectTime, last_device_status as LastDeviceStatus, last_run_status as LastRunStatus, last_program_name as LastProgramName, last_part_count as LastPartCount, last_operate_mode as LastOperateMode, last_machining_status as LastMachiningStatus, created_at as CreatedAt, updated_at as UpdatedAt";
|
|
|
|
|
|
public Machine GetById(int id)
|
|
|
{
|
|
|
using (var conn = CreateConnection())
|
|
|
{
|
|
|
var sql = $"SELECT {SelectColumns} FROM cnc_machine WHERE id = @Id";
|
|
|
return conn.QuerySingleOrDefault<Machine>(sql, new { Id = id });
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public MachineDetailResponse GetDetailById(int id)
|
|
|
{
|
|
|
using (var conn = CreateConnection())
|
|
|
{
|
|
|
var sql = @"SELECT m.id as Id, m.device_code as DeviceCode, m.name as Name,
|
|
|
m.workshop_id as WorkshopId, ws.name as WorkshopName,
|
|
|
m.collect_address_id as CollectAddressId,
|
|
|
m.brand_id as BrandId, b.brand_name as BrandName,
|
|
|
m.ip_address as IpAddress,
|
|
|
m.is_enabled as IsEnabled, m.is_online as IsOnline,
|
|
|
w.id as WorkerId, w.name as WorkerName,
|
|
|
m.last_program_name as LastProgramName, m.last_collect_time as LastCollectTime
|
|
|
FROM cnc_machine m
|
|
|
LEFT JOIN cnc_workshop ws ON m.workshop_id = ws.id
|
|
|
LEFT JOIN cnc_brand b ON m.brand_id = b.id
|
|
|
LEFT JOIN cnc_worker_machine wm ON m.id = wm.machine_id
|
|
|
LEFT JOIN cnc_worker w ON wm.worker_id = w.id
|
|
|
WHERE m.id = @Id";
|
|
|
return conn.QuerySingleOrDefault<MachineDetailResponse>(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.device_code LIKE @Keyword)";
|
|
|
p.Add("Keyword", $"%{query.Keyword}%");
|
|
|
}
|
|
|
if (query.WorkshopId.HasValue)
|
|
|
{
|
|
|
where += " AND m.workshop_id = @WorkshopId";
|
|
|
p.Add("WorkshopId", query.WorkshopId.Value);
|
|
|
}
|
|
|
if (query.IsOnline.HasValue)
|
|
|
{
|
|
|
where += " AND m.is_online = @IsOnline";
|
|
|
p.Add("IsOnline", query.IsOnline.Value);
|
|
|
}
|
|
|
if (query.BrandId.HasValue)
|
|
|
{
|
|
|
where += " AND m.brand_id = @BrandId";
|
|
|
p.Add("BrandId", query.BrandId.Value);
|
|
|
}
|
|
|
var limit = query.PageSize;
|
|
|
var offset = query.Offset;
|
|
|
var sql = @"SELECT m.id as Id, m.device_code as DeviceCode, m.name as Name, m.workshop_id as WorkshopId, ws.name as WorkshopName, m.collect_address_id as CollectAddressId, m.brand_id as BrandId, b.brand_name as BrandName, m.ip_address as IpAddress, m.is_enabled as IsEnabled, m.is_online as IsOnline, m.last_program_name as LastProgramName, m.last_collect_time as LastCollectTime, w.id as WorkerId, w.name as WorkerName
|
|
|
FROM cnc_machine m
|
|
|
LEFT JOIN cnc_workshop ws ON m.workshop_id = ws.id
|
|
|
LEFT JOIN cnc_brand b ON m.brand_id = b.id
|
|
|
LEFT JOIN cnc_worker_machine wm ON m.id = wm.machine_id
|
|
|
LEFT JOIN cnc_worker w ON wm.worker_id = 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 (device_code, name, workshop_id, collect_address_id, ip_address, brand_id, is_enabled, is_online, created_at, updated_at)
|
|
|
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 device_code = @DeviceCode, name = @Name, workshop_id = @WorkshopId, collect_address_id = @CollectAddressId, ip_address = @IpAddress, brand_id = @BrandId, is_enabled = @IsEnabled, is_online = @IsOnline, updated_at = @UpdatedAt, last_program_name = @LastProgramName, last_collect_time = @LastCollectTime, last_device_status = @LastDeviceStatus, last_run_status = @LastRunStatus, last_machining_status = @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 is_enabled = CASE WHEN is_enabled = 1 THEN 0 ELSE 1 END, updated_at = NOW() WHERE id = @Id";
|
|
|
return conn.Execute(sql, new { Id = id }) > 0;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public Machine GetByDeviceCode(string deviceCode)
|
|
|
{
|
|
|
using (var conn = CreateConnection())
|
|
|
{
|
|
|
var sql = $"SELECT {SelectColumns} FROM cnc_machine WHERE device_code = @DeviceCode";
|
|
|
return conn.QuerySingleOrDefault<Machine>(sql, new { DeviceCode = deviceCode });
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public List<Machine> GetEnabledByAddressId(int collectAddressId)
|
|
|
{
|
|
|
using (var conn = CreateConnection())
|
|
|
{
|
|
|
var sql = $"SELECT {SelectColumns} FROM cnc_machine WHERE collect_address_id = @CollectAddressId AND is_enabled = 1";
|
|
|
return conn.Query<Machine>(sql, new { CollectAddressId = collectAddressId }).ToList();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public List<Machine> GetEnabledOnline()
|
|
|
{
|
|
|
using (var conn = CreateConnection())
|
|
|
{
|
|
|
var sql = $"SELECT {SelectColumns} FROM cnc_machine WHERE is_enabled = 1 AND is_online = 1";
|
|
|
return conn.Query<Machine>(sql).ToList();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public void UpdateOnlineStatus(int id, bool isOnline)
|
|
|
{
|
|
|
using (var conn = CreateConnection())
|
|
|
{
|
|
|
var sql = @"UPDATE cnc_machine SET is_online = @IsOnline, updated_at = 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 last_collect_time = @LastCollectTime, last_program_name = @LastProgramName, last_machining_status = @LastMachiningStatus, updated_at = NOW() WHERE id = @Id";
|
|
|
var param = new
|
|
|
{
|
|
|
Id = id,
|
|
|
LastCollectTime = entity.LastCollectTime,
|
|
|
LastProgramName = entity.LastProgramName,
|
|
|
LastMachiningStatus = entity.LastMachiningStatus
|
|
|
};
|
|
|
conn.Execute(sql, param);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public void SetCollectAddress(int machineId, int? collectAddressId)
|
|
|
{
|
|
|
using (var conn = CreateConnection())
|
|
|
{
|
|
|
conn.Execute("UPDATE cnc_machine SET collect_address_id = @AddressId, updated_at = NOW() WHERE id = @Id",
|
|
|
new { Id = machineId, AddressId = collectAddressId });
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|