EquipmentService.cs 1.65 KB
using CNCFanucDataReading;
using FreeSql;
using FreeSql.Internal;
using System;
using System.Threading.Tasks;

namespace CNCFanucDataReading
{
    public class EquipmentService : IDisposable
    {
        private readonly IFreeSql _fsql;
        private bool _disposed = false;

        public EquipmentService(string connectionString)
        {
            if (string.IsNullOrEmpty(connectionString))
                throw new ArgumentException("数据库连接字符串不能为空", nameof(connectionString));

            _fsql = new FreeSql.FreeSqlBuilder()
                .UseConnectionString(FreeSql.DataType.SqlServer, connectionString)
                .UseAutoSyncStructure(false) // 自动同步实体结构到数据库
                .Build();

        }


        /// <summary>
        /// 获取设备列表
        /// </summary>
        public async Task<List<BaseEquipment>> GetList()
        {
            try
            {
                var projectGuid = Guid.Parse(AppConfig.ProjectKeys);
                return await _fsql.Select<BaseEquipment>()
                    .Where(a => a.ProjectKeys == projectGuid //长沙项目
                     && a.FactoryCode == AppConfig.FactoryCode//3厂房
                     && a.EquipmentTypeCode == AppConfig.EquipmentTypeCode)//CNC机床
                    .OrderBy(e => e.EquipmentCode)
                    .ToListAsync();
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        public void Dispose()
        {
            if (!_disposed)
            {
                _fsql?.Dispose();
                _disposed = true;
            }
        }
    }
}