You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

193 lines
6.2 KiB

1 year ago
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using CK.SCP.Models.AppBoxEntity;
using CK.SCP.Models.Enums;
using CK.SCP.Models.ScpEntity;
using CK.SCP.Utils;
namespace CK.SCP.Models
{
public static class ScpCache
{
private static readonly ScpEntities ScpDb = EntitiesFactory.CreateScpInstance();
private static readonly AppBoxContext Db = EntitiesFactory.CreateAppBoxInstance();
private static List<TA_VENDER> _vendList;
private static List<TA_PART> _PartList;
private static List<User> _usersList;
private static List<TB_RECEIVE> _ReceiveList;
private static ScpConfig _config;
public static ScpConfig Config
{
get { return _config ?? (_config = GetConfig(ScpDb)); }
set { _config = value; }
}
private static ScpConfig GetConfig(ScpEntities db)
{
var config = new ScpConfig();
var peroperties = config.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
var configList = GetDbConfigList(db);
foreach (var pi in peroperties)
{
var piName = pi.Name.ToUpper();
foreach (var cfg in configList)
{
if (cfg.ParamName != piName) continue;
var value = ListHelper.ConvertToType(cfg.ParamValue, pi.PropertyType);
if (!pi.CanWrite) continue;
try
{
pi.SetValue(config, value, null);
break;
}
catch (Exception ex)
{
MessageHelper.ShowError(ex);
throw;
}
}
}
return config;
}
private static void SetConfig(ScpEntities db, ScpConfig config)
{
var peroperties = config.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
var configList = GetDbConfigList(db);
foreach (var pi in peroperties)
{
var piName = pi.Name.ToUpper();
if (configList.Any(p => p.ParamName.ToUpper() == piName)) continue;
var cfg = new TA_CONFIG
{
ParamName = piName,
ParamValue = pi.GetValue(Config, null).ToString(),
State = 1,
Remark = piName,
};
db.TA_CONFIG.Add(cfg);
}
EntitiesFactory.SaveDb(db);
}
public static List<TA_CONFIG> GetDbConfigList(ScpEntities db)
{
return db.TA_CONFIG.OrderByDescending(p => p.UID).ToList();
}
public static void Refresh(ScpEntities db)
{
try
{
Config = GetConfig(db);
SetConfig(db, Config);
}
catch (Exception ex)
{
throw new Exception(
$"系统无法连接到{GlobalConfig.ScpDatabase.数据库类型}数据库:{GlobalConfig.ScpDatabase.服务器地址},请检查配置的服务器,数据库,用户名和密码等信息是否正确。");
}
}
public static string GetVendName(string vendId)
{
return VenderList.SingleOrDefault(p => p.VendId == vendId)?.VendName ?? string.Empty;
}
public static string GetVendIDByUser(string name)
{
return UsersList.SingleOrDefault(p => p.Name == name)?.SupplierCode ?? string.Empty;
}
public static string GetPartDesc(string wmsDataPartCode)
{
return "";
}
public static DateTime GetServerTime()
{
return DateTime.Now;
}
public static List<TA_VENDER> VenderList
{
get
{
if (_vendList == null || _vendList.Count == 0)
{
_vendList = ScpDb.TA_VENDER.ToList();
}
return _vendList;
}
set { _vendList = value; }
}
public static List<User> UsersList
{
get
{
if (_usersList == null || _usersList.Count == 0)
{
_usersList = Db.Users.ToList();
}
return _usersList;
}
set { _usersList = value; }
}
public static List<TA_PART> PartList
{
get
{
if (_PartList == null || _PartList.Count == 0)
{
_PartList = ScpDb.TA_PART.ToList();
}
return _PartList;
}
set { _PartList = value; }
}
public static string GetPartDesc1(string partCode)
{
return PartList.SingleOrDefault(p => p.PartCode == partCode)?.PartDesc1 ?? string.Empty;
}
public static List<TB_RECEIVE> ReceiveList
{
get
{
if (_ReceiveList == null || _ReceiveList.Count == 0)
{
_ReceiveList = ScpDb.TB_RECEIVE.ToList();
}
return _ReceiveList;
}
set { _ReceiveList = value; }
}
public static string GetReceivePoBillNum(string billnum)
{
return ReceiveList.SingleOrDefault(p => p.BillNum == billnum)?.PoBillNum ?? string.Empty;
}
public static string GetReceiveAsnBillNum(string billnum)
{
return ReceiveList.SingleOrDefault(p => p.BillNum == billnum)?.AsnBillNum ?? string.Empty;
}
}
public class ScpConfig
{
public string QAD域 { get; set; } = "JZ1";
public string QAD地点 { get; set; } = "0100";
public string WMS接口数据流水号格式 { get; set; } = "yyMMdd_HHmmssffff";
public string { get; set; } = "锦恒项目";
public string WMS用户名 { get; set; } = "W";
public string SCP用户名 { get; set; } = "SCP";
public string SCP接口数据流水号格式 { get; set; } = "yyMMdd_HHmmssffff";
}
}