PosIDManger.cs 9.6 KB
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;

namespace OnlineStore.Common
{
    public static class PosIDManger
    {
        // Dictionary for storing POS information
        public static DRAWERGroup APosList = new DRAWERGroup();
        public static DRAWERGroup BPosList = new DRAWERGroup();

        // File paths for the JSON data
        private static readonly string APosFilePath = "E:\\data1List.json";
        private static readonly string BPosFilePath = "E:\\data2List.json";

        // Static constructor to initialize APosList and BPosList
        static PosIDManger()
        {
            // Load the data from the JSON files if they exist, otherwise initialize from strings
            APosList = LoadFromFile<DRAWERGroup>(APosFilePath) ?? CreatePosListFromString(APosString);
            BPosList = LoadFromFile<DRAWERGroup>(BPosFilePath) ?? CreatePosListFromString(BPosString);

            //if (BPosList == null)
            //{
            //    BPosList = CreatePosListFromString(BPosString);
            //    foreach (var x in BPosList.DRAWER.Values)
            //    {
            //        x.Reverse();
            //    }
            //    LogUtil.error($"反转B面顺序");
            //    SaveToFile();
            //}
            // Merge with strings to ensure new POS are added
            bool updated = false;
            updated |= MergeWithPosString(APosList, APosString);
            updated |= MergeWithPosString(BPosList, BPosString);

            if (updated)
            {
                LogUtil.info($"新 POS 信息已添加,保存数据");
                SaveToFile();
            }
        }
        // Method to merge new POS from a string into an existing DRAWERGroup
        private static bool MergeWithPosString(DRAWERGroup currentGroup, string posString)
        {
            var newGroup = CreatePosListFromString(posString);
            bool updated = false;

            foreach (var kvp in newGroup.DRAWER)
            {
                if (!currentGroup.DRAWER.ContainsKey(kvp.Key))
                {
                    currentGroup.DRAWER[kvp.Key] = kvp.Value;
                    updated = true;
                }
                else
                {
                    var existingList = currentGroup.DRAWER[kvp.Key];
                    foreach (var pos in kvp.Value)
                    {
                        if (!existingList.Any(p => p.PosID == pos.PosID))
                        {
                            existingList.Add(pos);
                            updated = true;
                        }
                    }
                }
            }

            return updated;
        }
        // Method to load data from a file and deserialize it
        private static T LoadFromFile<T>(string filePath)
        {
            try
            {
                if (File.Exists(filePath))
                {
                    string json = File.ReadAllText(filePath);
                    return JsonConvert.DeserializeObject<T>(json);
                }
            }
            catch (Exception ex)
            {
                LogUtil.error($"Error loading from file {filePath}: {ex.Message}");
            }
            return default;
        }

        // Method to save data to a file (serialize to JSON)
        public static void SaveToFile()
        {
            try
            {
                string aPosJson = JsonConvert.SerializeObject(APosList, Formatting.Indented);
                string bPosJson = JsonConvert.SerializeObject(BPosList, Formatting.Indented);

                File.WriteAllText(APosFilePath, aPosJson);
                File.WriteAllText(BPosFilePath, bPosJson);
            }
            catch (Exception ex)
            {
                LogUtil.error($"Error saving to files: {ex.Message}");
            }
        }

        // Method to clear HasReel and Barcode for all entries and save
        public static void EmptyAllPos()
        {
            try
            {
                // Iterate through APosList and BPosList to update the properties
                UpdatePosList(APosList);
                UpdatePosList(BPosList);

                // Save the changes back to the files
                SaveToFile();
            }
            catch (Exception ex)
            {
                LogUtil.error($"Error while emptying all POS: {ex.Message}");
            }
        }

        // Helper method to update the PosInfo objects
        private static void UpdatePosList(DRAWERGroup posList)
        {
            if (posList == null || posList.DRAWER == null) return;

            foreach (var drawer in posList.DRAWER.Values)
            {
                if (drawer == null) continue;

                foreach (var pos in drawer)
                {
                    if (pos == null) continue;

                    pos.HasReel = false;
                    pos.Barcode = string.Empty;
                }
            }
        }

        // Method to create PosInfo list from a string
        private static DRAWERGroup CreatePosListFromString(string posString)
        {
            var drawerGroup = new DRAWERGroup();
            int currentKey = 0;
            var lines = posString.Split(new[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);

            foreach (var line in lines)
            {
                if (int.TryParse(line.Trim(), out int key))
                {
                    // Update the current key
                    currentKey = key;
                    if (!drawerGroup.DRAWER.ContainsKey(currentKey))
                    {
                        drawerGroup.DRAWER[currentKey] = new List<PosInfo>();
                    }
                }
                else
                {
                    // Add PosInfo to the current key
                    drawerGroup.DRAWER[currentKey].Add(new PosInfo
                    {
                        PosID = line.Trim(),
                        HasReel = false,
                        Barcode = string.Empty
                    });
                }
            }

            return drawerGroup;
        }

        // New method to retrieve POS lists based on drawer index
        public static void GetDarwerPoslist(int darwerindex, out string apos, out string bpos, out string aout, out string bout)
        {
            apos = string.Empty;
            bpos = string.Empty;
            aout = string.Empty;
            bout = string.Empty;

            if (APosList.DRAWER.TryGetValue(darwerindex, out var aDrawer))
            {
                var pos = aDrawer.FirstOrDefault(p => !p.HasReel);
                var outPos = aDrawer.FirstOrDefault(p => p.HasReel);

                apos = pos?.PosID ?? string.Empty;
                aout = outPos?.PosID ?? string.Empty;
            }

            if (BPosList.DRAWER.TryGetValue(darwerindex, out var bDrawer))
            {
                var pos = bDrawer.FirstOrDefault(p => !p.HasReel);
                var outPos = bDrawer.FirstOrDefault(p => p.HasReel);

                bpos = pos?.PosID ?? string.Empty;
                bout = outPos?.PosID ?? string.Empty;
            }
        }

        // Method to update PosInfo based on PosID and HasReel
        public static void UpdatePosinfo(string posid, bool hasreel)
        {
            LogUtil.info($"UpdatePosinfo {posid}={hasreel}");
            foreach (var drawer in APosList.DRAWER.Values.Concat(BPosList.DRAWER.Values))
            {
                var pos = drawer.FirstOrDefault(p => p.PosID == posid);
                if (pos != null)
                {
                    pos.HasReel = hasreel;
                    SaveToFile(); // Save the updated data
                    return;
                }
            }
        }

        public static bool GetPosHasReel(string posid)
        {
            
            foreach (var drawer in APosList.DRAWER.Values.Concat(BPosList.DRAWER.Values))
            {
                var pos = drawer.FirstOrDefault(p => p.PosID == posid);
                if (pos != null)
                {
                    return pos.HasReel;
                }
            }
            return true;
        }

        // APos and BPos string values
        static string APosString = @"
1
01AA15060101
01AA15060103
01AA15060105
2
01BB01060220
01BB01060218
01BB01060216
3
01AA15060420
01AA15060418
01AA15060416
4
01BB01020220
01BB01020218
01BB01020216
5
01AA15040101
01AA15040103
01AA15040105
6
01BB15020220
01BB15020218
01BB15020216
7
01AA15020101
01AA15020103
01AA15020105
8
01BB14050401
01BB14050403
01BB14050405
9
01AA01040101
01AA01040103
01AA01040105
10
01BB01040220
01BB01040218
01BB01040216
11
01AA05060101
01AA05060103
01AA05060105
12
01BB15060220
01BB15060218
01BB15060216
13
01AA01060101
01AA01060103
01AA01060105
";

        static string BPosString = @"
1
01BB05040220
01BB05040218
01BB05040216
2
01AA01020101
01AA01020103
01AA01020105
3
01BB05020220
01BB05020218
01BB05020216
4
01AA04040101
01AA04040103
01AA04040105
5
01BB05060220
01BB05060218
01BB05060216
6
01AA05040101
01AA05040103
01AA05040105
7
01BB04020220
01BB04020218
01BB04020216
8
01AA05020101
01AA05020103
01AA05020105
9
01BB04060220
01BB04060218
01BB04060216
10
01AA04060101
01AA04060103
01AA04060105
11
01BB15040220
01BB15040218
01BB15040216
12
01AA04020101
01AA04020103
01AA04020105
13
01BB04040220
01BB04040218
01BB04040216
";
    }
    [Serializable]
    public class PosInfo
    {
        public string PosID { get; set; }
        public bool HasReel { get; set; }
        public string Barcode { get; set; }
    }
    [Serializable]
    public class DRAWERGroup
    {
        public Dictionary<int, List<PosInfo>> DRAWER = new Dictionary<int, List<PosInfo>>();
    }
}