MainService.cs 4.06 KB
using Hh.Mes.Common.Exel;
using Hh.Mes.Common.log;
using Hh.Mes.POJO.Entity;
using Hh.Mes.POJO.Response;
using Hh.Mes.POJO.WebEntity.equipment;
using HH.Data.Excel.SqlHelp;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static Microsoft.AspNetCore.Hosting.Internal.HostingApplication;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using ComboBox = System.Windows.Forms.ComboBox;

namespace HH.Data.Excel.Services
{
    /// <summary>
    /// 
    /// </summary>
    public class MainService
    {
        private readonly DatabaseService _service;

        public MainService()
        {
            // 初始化 SqlDataService 实例
            _service = DatabaseService.Instance;
        }


        /// <summary>
        /// 加载项目列表到指定的 ComboBox 控件中
        /// </summary>
        public List<KeyValuePair<Guid, string>> InitProject(ComboBox comboBox)
        {
            return ExceptionsHelp.Instance.Execute(() =>
             {
                 var projectList = _service.GetProjectList().Select(x => new KeyValuePair<Guid, string>(x.keys, x.projectName))
                                           .ToList();
                 comboBox.DisplayMember = "Value";
                 comboBox.ValueMember = "Key";
                 comboBox.DataSource = projectList;

                 return projectList;

             }, actionCatCh: (ex) =>
             {
                 Program.SysMsgShow("加载设备列表:" + ex.Message);
             });
        }

        /// <summary>
        /// 加载项目列表到指定的 ComboBox 控件中
        /// </summary>
        public List<KeyValuePair<string, string>> InitFactory(ComboBox comboBox, Guid selectValue)
        {
            return ExceptionsHelp.Instance.Execute(() =>
            {
                var projectList = _service.GetFactoryList(selectValue);
                if (projectList.Count == 0)
                {
                    comboBox.Text = "无数据";
                }
                var items = projectList.Select(item => new KeyValuePair<string, string>(item.factoryCode, item.factoryName))
                                        .ToList();
                comboBox.DisplayMember = "Value";
                comboBox.ValueMember = "Key";
                comboBox.DataSource = new BindingSource(items, null);

                return items;

            }, actionCatCh: (ex) =>
            {
                Program.SysMsgShow("加载厂房列表:" + ex.Message);
            });
        }

        /// <summary>
        /// 加载设备类型 CheckedListBox
        /// 默认隐藏
        /// </summary>
        public Dictionary<int, string> InitEqType(CheckedListBox chkList, Panel panel, bool isShow = true)
        {
            return ExceptionsHelp.Instance.Execute(() =>
            {
                // 根据 isShow 参数来设置 CheckedListBox 的可见性
                panel.Visible = !isShow;
                if (chkList.DataSource != null) return Program._mainWindow.EqTypeDataSource;

                // 获取设备类型数据
                var items = _service.GetEqTypeList().ToDictionary(item => item.Id, item => item.Name);

                // 5. 线程安全绑定
                if (chkList.InvokeRequired)
                {
                    chkList.Invoke(new Action(() =>
                    {
                        chkList.DataSource = new BindingSource(items, null);
                        chkList.DisplayMember = "Value";
                        chkList.ValueMember = "Key";
                    }));
                }
                else
                {
                    chkList.DataSource = new BindingSource(items, null);
                    chkList.DisplayMember = "Value";
                    chkList.ValueMember = "Key";
                }
                return items;
            }, actionCatCh: (ex) =>
            {
                Program.SysMsgShow("加载设备类型:" + ex.Message);
            });
        }
    }
}