AlgoPNMatch.cs
2.4 KB
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
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OnlineStore.Common
{
public class Pn_Algo_Match
{
static Dictionary<string, string> algopnList;
const string algopnFile = "Config\\algopn.txt";
static Pn_Algo_Match()
{
algopnList = new Dictionary<string, string>();
LoadFile();
}
public static void LoadFile()
{
if (!File.Exists(algopnFile))
{
SaveFile();
return;
}
var ls = File.ReadAllText(algopnFile);
algopnList = JsonConvert.DeserializeObject<Dictionary<string, string>>(ls);
}
static void SaveFile()
{
Directory.CreateDirectory(Path.GetDirectoryName(algopnFile));
string s = JsonConvert.SerializeObject(algopnList);
File.WriteAllText(algopnFile, s);
File.Copy(algopnFile, algopnFile + "."+DateTime.Now.ToShortDateString(), true);
}
public static void Set(string pn, string value, double? finetuning = null)
{
if (string.IsNullOrEmpty(pn))
return;
if (string.IsNullOrEmpty(value))
return;
//Common.LOG.Info($"set pnalgo pn:{pn}, algo:{value}, finetuning:{finetuning}");
if (finetuning != null)
value = value + "," + finetuning.ToString();
if (algopnList.ContainsKey(pn))
algopnList[pn] = value;
else
algopnList.Add(pn, value);
SaveFile();
}
public static string MatchPN(string pn, out bool hasMatch, out double? finetuning)
{
hasMatch = false;
finetuning = null;
if (algopnList.ContainsKey(pn))
{
hasMatch = true;
var value = algopnList[pn];
if (value.IndexOf(",") > 0)
{
var vs = value.Split(',');
if (double.TryParse(vs[1], out double result))
{
value = vs[0];
finetuning = result;
}
}
return value;
}
else
return "auto";
}
}
}