MatchAnalysis.cs 5.0 KB
using CameraVisionLib.Model;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BLL
{
    public class MatchAnalysis
    {
        
        public static void StartNewAnalysis(List<BarcodeInfo> code)
        {
            MatchTemplateList = new ();
        }
        static string CurrnetTemplateName="";
        static BarcodeInfo CurrnetBarcode;
        static public void SetTemplatename(string name) {
            CurrnetTemplateName = name;

            if (!MatchTemplateList.ContainsKey(CurrnetTemplateName))
            {
                MatchTemplateList.Add(CurrnetTemplateName, new TemplateCollection());
            }
        }
        static public void SetBarcode(BarcodeInfo name)
        {
            CurrnetBarcode = name;
            if (!MatchTemplateList[CurrnetTemplateName].BarcodeMatchs.ContainsKey(name.Text))
            {
                MatchTemplateList[CurrnetTemplateName].BarcodeMatchs.Add(name.Text, new BarcodeMatch(name));
                MatchTemplateList[CurrnetTemplateName].barcodeInfos.Add(name);
            }
            
        }
        public static void AddMatch(string matchKey, bool isMatch, int codeID, string colName,string matchDesc) {
            Match match = new Match(matchKey, isMatch, codeID, colName,matchDesc);

            var ms = MatchTemplateList[CurrnetTemplateName].BarcodeMatchs[CurrnetBarcode.Text].MatchCollections;
            
            if (!ms.ContainsKey(matchKey))
                ms.Add(matchKey,new MatchCollection());
            ms[matchKey].Matchs.Add(match);

            if (!MatchTemplateList[CurrnetTemplateName].MatchCollections.ContainsKey(matchKey))
            {
                MatchTemplateList[CurrnetTemplateName].MatchCollections.Add(matchKey, new MatchCollection());
                MatchTemplateList[CurrnetTemplateName].MatchCollections[matchKey].barcodeInfo = CurrnetBarcode;
            }

            MatchTemplateList[CurrnetTemplateName].MatchCollections[matchKey].Matchs.Add(match);
            
        }
        public static void MatchResult(string matchKey, bool isMatch) {
            if (!MatchTemplateList[CurrnetTemplateName].BarcodeMatchs[CurrnetBarcode.Text].MatchCollections.ContainsKey(matchKey))
                return;
            MatchTemplateList[CurrnetTemplateName].BarcodeMatchs[CurrnetBarcode.Text].MatchCollections[matchKey].isMatch = isMatch;
            MatchTemplateList[CurrnetTemplateName].MatchCollections[matchKey]=MatchTemplateList[CurrnetTemplateName].BarcodeMatchs[CurrnetBarcode.Text].MatchCollections[matchKey];
            MatchTemplateList[CurrnetTemplateName].MatchCollections[matchKey].barcodeInfo = CurrnetBarcode;
        }
        public static void TemplateResult(bool isMatch) {
            MatchTemplateList[CurrnetTemplateName].isMatch = isMatch;
        }
        static Dictionary<string, TemplateCollection> MatchTemplateList;
        public static void ShowResult()
        {
            return;
            foreach (var tp in MatchTemplateList) {
                LogNet.log.Info($"开始解析模版:{tp.Key}");

                foreach (var m in tp.Value.MatchCollections) {
                    LogNet.log.Info($"开始解析关键字:{m.Key}");
                    LogNet.log.Info($"匹配到关键字:{m.Key}");
                    string matchlist = m.Value.barcodeInfo.Text + "\t";
                    foreach (var item in m.Value.Matchs)
                    {      
                        matchlist += $"{item.colName},{item.isMatch}\t";
                    }
                    LogNet.log.Info(matchlist);
                }
            }
        }
        public class TemplateCollection
        {
            public Dictionary<string, BarcodeMatch> BarcodeMatchs=new ();
            public List<BarcodeInfo> barcodeInfos = new();
            public Dictionary<string, MatchCollection> MatchCollections = new();
            public bool isMatch;

        }
        public class MatchCollection
        {
            public string MatchKey;
            public List<Match> Matchs = new ();
            public BarcodeInfo barcodeInfo;
            public bool isMatch;
            public string result;
        }
        public class BarcodeMatch {
            public BarcodeInfo barcode;
            public Dictionary<string, MatchCollection> MatchCollections = new();
            public bool isMatch=false;
            public BarcodeMatch(BarcodeInfo barcode)
            {
                this.barcode = barcode;
               }
        }
        public class Match {
            public string matchKey;
            public bool isMatch;
            public int codeID;
            public string colName;
            public string matchDesc;
            public Match(string matchKey, bool isMatch, int codeID, string colName,string matchDesc) {
                this.matchKey = matchKey;
                this.isMatch = isMatch;
                this.codeID = codeID;
                this.colName = colName;
                this.matchDesc = matchDesc;
            }
        }

    }
}