using AutoMapper;
using IStation.Untity;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace IStation.Common
{
///
/// 泵曲线
///
public class Product
{
static List allProducts = null;
public static List GetAll()
{
if(allProducts == null)
{
allProducts = ReadFile();
}
return allProducts;
}
///
/// 查询全部泵
///
public static List> GetAllPump()
{
var all = GetAll();
var pumps = all?.Where(x => x.Catalog == "pump").ToList();
if (pumps == null || !pumps.Any())
return default;
var list = pumps.Select(x => new Model.Product(x)).ToList();
return list;
}
public static bool Initial()
{
allProducts = ReadFile();
return true;
}
public static List ReadFile()
{
var fileName = System.IO.Path.Combine(IStation.DataFolderHelper.GetRootPath(), "Product.txt");
if (!System.IO.File.Exists(fileName))
{
return null;
}
if (!File.Exists(fileName))
return default;
var str = File.ReadAllText(fileName);
if (string.IsNullOrEmpty(str))
return default;
return Entity2Models(JsonHelper.Json2Object>(str));
}
private static List Entity2Models(List entities)
{
if (entities == null || entities.Count() < 1)
return default;
var mapper = new MapperConfiguration(cfg => cfg.CreateMap()
.ForMember(d => d.ParentIds, opt => opt.MapFrom(src => TreeParentIdsHelper.ToList(src.ParentIds)))
.ForMember(d => d.Flags, opt => opt.MapFrom(src => FlagsHelper.ToList(src.Flags)))
).CreateMapper();
var models = mapper.Map, List>(entities);
return models;
}
}
}