AlgoPNMatch.cs 1.7 KB
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)
        {
            if (string.IsNullOrEmpty(pn))
                return;
            if (string.IsNullOrEmpty(value))
                return;

            //Common.LOG.Info($"set pnalgo pn:{pn}, algo:{value}");
            if (algopnList.ContainsKey(pn))
                algopnList[pn] = value;
            else
                algopnList.Add(pn, value);

            SaveFile();
        }
        public static string MatchPN(string pn)
        {
            if (algopnList.ContainsKey(pn))
                return algopnList[pn];
            else
                return "auto";
        }
    }
}