wenxiangchao
4 天以前 2e889843b4c171f2d6ad8e0d53084040703d11f6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using HMI.Core.Entity;
using HMI.Core.Setting;
using HMI.Core.Util;
using System.Collections.Generic;
using System.IO;
using System.Linq;
 
namespace HMI.Core.Manager
{
    /// <summary>
    /// TODO 优化数据源,需要支持sqlite和xml
    /// </summary>
    public class ProductManager
    {
        public static string GetFolder()
        {
            if (ParamSetting.ShareConfig != null)
            {
                return Path.Combine(ParamSetting.ShareConfig, "Product");
            }
            return Path.Combine(PathHelper.ConfigFolder, "Product");
        }
 
        public static string GetDataFilePath()
        {
            return Path.Combine(GetFolder(), "Product.data");
        }
 
        public static string GetParamFolderByProductName(string productName)
        {
            return Path.Combine(GetFolder(), "Parameter", $"Parameter_{productName}.data");
        }
 
        /// <summary>
        /// 获取保存的产品
        /// </summary>
        /// <returns></returns>
        public static T GetProducts<T>() where T : IList<ProductData>
        {
            string path = GetDataFilePath();
            if (!File.Exists(path)) return default(T);
            T datas = XmlHelper.DeserializeFromXml<T>(path);
            return datas;
        }
 
        /// <summary>
        /// 获取保存的产品
        /// </summary>
        /// <returns></returns>
        public static T GetParametersByProductName<T>(string productName) where T : IList<ParameterData>
        {
            string path = GetParamFolderByProductName(productName);
            if (!File.Exists(path)) return default(T);
            T datas = XmlHelper.DeserializeFromXml<T>(path);
            return datas;
        }
 
 
        public static List<ProductData> GetProducts()
        {
            List<ProductData> datas = GetProducts<List<ProductData>>();
            return datas ?? new List<ProductData>();
        }
 
        /// <summary>
        /// 根据产品名称获取产品参数
        /// </summary>
        /// <param name="productName"></param>
        /// <returns></returns>
        public static List<ParameterData> GetParametersByProductName(string productName)
        {
            List<ParameterData> datas = GetParametersByProductName<List<ParameterData>>(productName);
            return datas ?? new List<ParameterData>();
        }
 
        public static ParameterData GetParameterByProductAndName(string product, string name)
        {
            List<ParameterData> datas = GetParametersByProductName<List<ParameterData>>(product);
            return datas?.FirstOrDefault(e => e.Name == name);
        }
    }
}