Extension.cs 20.2 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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;
using System.Reflection;
using Asa.FaceControl;
using Model;
using System.Windows.Forms;
using DocumentFormat.OpenXml.Drawing;
using DocumentFormat.OpenXml.Spreadsheet;
using TcpKPIO;
using DocumentFormat.OpenXml.Wordprocessing;

namespace BLL
{
    public class Extension : IDisposable
    {
        public event IExtension.PrintDelegate Printing;
        public event IExtension.PrintDelegate SaveRetrospect;

        private readonly IExtension currentExtension;
        private readonly Dictionary<string, IExtension> dicExtension;
        private readonly Alcoelectro alcoelectro;
        private readonly Inventec inventec;
        private readonly PanaCIM panaCIM;
        private readonly NanRui nanRui;
        private readonly KaiFa kaiFa;
        private readonly General General;
        private readonly Dictionary<string, Func<ControlBase>> dicControls;
        private readonly Dictionary<Type, Action<PropertyInfo, ControlBase, object>> dicControlAttribute;
        private readonly List<ExtensionControl> ctlGroup = new();

        private int ctlX, ctlY;
        private int pnlWidth;
        private FacePanel panel;
        private System.Drawing.Point startPoint;
        private Config config;

        public Extension(Config config)
        {
            this.config = config;
            LogNet.log.Info("加载扩展:" + config.ExtensionName);

            alcoelectro = new(config);
            inventec = new(config);
            panaCIM = new(config);
            nanRui = new(config);
            kaiFa = new(config);
            General = new(config);

            dicExtension = new()
            {
                { "Bwit", alcoelectro },
                { "Inventec", inventec },
                { "PanaCIM", panaCIM },
                { "NanRui", nanRui },
                { "KaiFa", kaiFa },
                { "General", General }
            };
            for (int i = 0; i < dicExtension.Count; i++)
            {
                dicExtension.ElementAt(i).Value.Printing += new IExtension.PrintDelegate(
                    delegate (Dictionary<string, string> content) { Printing?.Invoke(content); });
                dicExtension.ElementAt(i).Value.SaveRetrospect += new IExtension.PrintDelegate(
                    delegate (Dictionary<string, string> content) { SaveRetrospect?.Invoke(content); });
            }

            dicControls = new()
            {
                { "Label", () => { return new FaceLabel(); } },
                { "TextBox", () => { return new FaceTextBox(); } },
                { "Button", () => { return new FaceButton(); } },
                { "ComboBox", () => { return new FaceComboBox(); } },
                { "CheckBox", () => { return new FaceCheckBox(); } },
                { "ListBox", () => { return new FaceListBox(); } },
                { "PictureBox", () => { return new FacePictureBox(); } },
                { "NumericUpDown", () => { return new FaceNumericUpDown(); } },
                { "RadioBox", () => { return new FaceRadioBox(); } }
            };

            dicControlAttribute = new()
            {
                { typeof(string), AttributeString },
                { typeof(int), AttributeInt32 },
                { typeof(float), AttributeSingle },
                { typeof(bool), AttributeBoolean },
                { typeof(System.Drawing.Color), AttributeColor },
                { typeof(System.Drawing.ContentAlignment), AttributeContentAlignment },
                { typeof(System.Drawing.Font), AttributeFont },
                { typeof(System.Windows.Forms.HorizontalAlignment), AttributeHorizontalAlignment }
            };

            currentExtension = dicExtension[config.ExtensionName];
        
        }
        public void Dispose()
        {
            currentExtension.Dispose();
        }
        public string labelText = "等待中";


        public void LoadPanel(FacePanel pnl,List<string> keywords)
        {
            panel = pnl;
            pnlWidth = pnl.Width;
            startPoint = new(6, panel.TextHeight + 6);
            object[] rows;
            if (!System.IO.File.Exists(FilePath.CONFIG_EXTENSION))
            {
                List<object[]> listrow = new List<object[]>();
                foreach (var key in keywords)
                {
                    Dictionary<string, object> L = new()
                    {
                        { "Type", "Label" },
                        {
                            "Attribute",
                            new Dictionary<string, object>()
                            {
                                { "Name", "clbl_" + key},
                                { "Font", "Arial,9,,"},
                                {"BorderWidth", 0},
                                {"Width", 100},
                                {"Height", 40},
                                {"Text", key},
                                {"TextAlign", "MiddleLeft"}
                            }
                        },
                    };
                    Dictionary<string, object> R;
                    string type = "TextBox";
                    if (key == Config.DataSource_DataTitle && Config.DataSource_ShowBox)
                    {
                        type = "ComboBox";
                    }
                    R = new() {
                        { "Type", type },
                        { "Key", key },
                        { "LinkName", key },
                        { "CanClear", "True" },
                        {
                            "Attribute",
                            new Dictionary<string, object>()
                            {
                                { "Name", "ctxt_"+key},
                                { "Font", "Arial,9,,"},
                                { "Width", -1},
                                { "Height",40},
                                { "ShowDel","False"},
                                { "ShowQuery","False"},
                                { "Text",""},
                                {"Enabled",Config.AllowModifyPrintInfo }
                            }
                        }
                    };
                    if (type == "ComboBox") {
                        //(new ComboBox).TextChanged
                        R.Add("Event", new Dictionary<string, object>()
                        {
                            { "TextChanged", "ComboBoxTextChanged"}
                        });
                    }
                    listrow.Add(new object[] { L, R });
                }
                Dictionary<string, object> A = new()
                {
                    { "Type", "Button" },
                    {
                        "Attribute",
                        new Dictionary<string, object>()
                        {
                            { "Name", "BtnGetReelID"},
                            { "Font", "Arial,12,B,"},
                            //{"BorderWidth", 0},
                            {"Width", -1},
                            {"Height", 40},
                            {"Text", "Get ReelID"},

                        }
                    },
                    {
                        "Event",
                        new Dictionary<string, object>()
                        {
                            { "Click", "GetHttpReelID"}
                        }
                    }
                };
                Dictionary<string, object> B = new()
                {
                    { "Type", "Button" },
                    {
                        "Attribute",
                        new Dictionary<string, object>()
                        {
                            { "Name", "BtnPrint"},
                            { "Font", "Arial,12,B,"},
                            //{"BorderWidth", 0},
                            {"Width", -1},
                            {"Height", 40},
                            {"Text", Asa.FaceControl.Language.Dialog("PrintLabel","Print")},//"Print Label"
                            
                        }
                    },
                    {
                        "Event",
                        new Dictionary<string, object>()
                                {
                                    { "Click", "PrintLabel"}
                                }
                    }
                };
               
              
                
                if (!string.IsNullOrEmpty(config.HttpReelID))
                    listrow.Add(new object[] { A });
                #region 显示替换数据按钮
                string pnkey = ConfigHelper.Config.Get("SelectHttpPN_KeyWords", "");
                string pnurl=ConfigHelper.Config.Get("SelectHttpPN_Url","");
                if (BLLCommon.config.SelectHttpPN&&
                    !string.IsNullOrEmpty(pnkey)&&!string.IsNullOrEmpty(pnurl))
                {
                    Dictionary<string, object> C = new(){
                        { "Type", "Button" },
                        {"Attribute",new Dictionary<string, object>(){
                            { "Name", "DynamicButton" },                
                            { "Font", "Arial,12,B,"},                
                            {"Width", -1},                
                            {"Height", 40},                
                            {"Text", Asa.FaceControl.Language.Dialog("ReplaceData","ReplaceData")},}
                         },        
                        {           
                            "Event",           
                            new Dictionary<string, object>()
                            {
                                { "Click", "DynamicButtonClick" }            
                            }      
                        }    
                    };
                    listrow.Add(new object[] { C }); 
                }
                #endregion
                #region 显示重新获取数据源按钮
                bool isdisplaybut = ConfigHelper.Config.Get<bool>("DataSource_isdisplaybut", false);
                if (isdisplaybut)
                {
                    Dictionary<string, object> displaybut = new(){
                        { "Type", "Button" },
                        {"Attribute",new Dictionary<string, object>(){
                            { "Name", "GetDataButton" },
                            { "Font", "Arial,12,B,"},
                            {"Width", -1},
                            {"Height", 40},
                            {"Text", Asa.FaceControl.Language.Dialog("RetrieveDataAgain","重新获取数据")},}
                         },
                        {
                            "Event",
                            new Dictionary<string, object>()
                            {
                                { "Click", "GetDataButtonClick" }
                            }
                        }
                    };
                    listrow.Add(new object[] { displaybut });
                }
                #endregion
                listrow.Add(new object[] { B });
                if (BLLCommon.config.CheckFunction)
                {
                    
                    Dictionary<string, object> newLabel = CreateNewLabel(labelText);
                     Dictionary<string, object> D = new()
                          {
                              { "Type", "Button" },
                              {
                             "Attribute",
                             new Dictionary<string, object>()
                             {
                            { "Name", "BtnSkip"},
                            { "Font", "Arial,12,B,"},
                            //{"BorderWidth", 0},
                            {"Width", -1},
                            {"Height", 40},
                            {"Text", "跳过"},//"Print Label"
                            
                            }
                              },
                              {
                         "Event",
                        new Dictionary<string, object>()
                                {
                                    { "Click", "BtnSkip_Click"},
                                  }
                             }
                       };
                       listrow.Add(new object[] {newLabel, D });
                   
                }
                
              
                rows = listrow.ToArray();
            }
            else
            {
                string json = System.IO.File.ReadAllText(FilePath.CONFIG_EXTENSION);
                JavaScriptSerializer serializer = new();
                rows = (object[])serializer.DeserializeObject(json);

            }
            ctlY = startPoint.Y;
            for (int i = 0; i < rows.Length; i++)
            {
                LoadRow(rows[i]);
            }
               

            currentExtension.Load(ctlGroup);
        }
        // 封装一个方法来创建带有指定文本的 newLabel
        private Dictionary<string, object> CreateNewLabel(string text)
        {
            if (text=="OK")
            {
                return new Dictionary<string, object>
         {
        { "Type", "Label" },
        { "LinkName", "ischeckresult"},
        {
            "Attribute",
            new Dictionary<string, object>()
            {
                { "Name", "labresult" },
                { "Font", "Arial,18,B,"},
                { "BorderWidth", 0 },         // 边框宽度
                { "Width", 100 },              // 宽度
                { "Height", 40 },
                { "Text", text },       // 显示的文本
                { "TextAlign", "MiddleCenter" },  // 文本对齐方式
                { "Foreground", "Green" } // 添加字体颜色为绿色
            }
        }
    };
            }
            else
            {
                return new Dictionary<string, object>
         {
        { "Type", "Label" },
         { "LinkName", "ischeckresult"},
        {
            "Attribute",
            new Dictionary<string, object>()
            {
                { "Name", "labresult" },
                { "Font", "Arial,18,B,"},
                { "BorderWidth", 0 },         // 边框宽度
                { "Width", 100 },
                
                { "Height", 40 },              // 高度
                { "Text", text },       // 显示的文本
                { "TextAlign", "MiddleCenter" },  // 文本对齐方式
                 { "Foreground", "Red" } // 添加字体颜色为绿色
            }
        }
    };
            }
          
        }
        public void Clear()
        {
            currentExtension.Clear();
        }

        public bool SetKey(string[] originalCode, Dictionary<string, string> key,bool hasMatch, out string errmsg)
        {
            return currentExtension.SetKey(originalCode, key, hasMatch, out errmsg);
        }
        public void Print(bool hasMatch, Dictionary<string, string> key)
        {
            currentExtension.Print(hasMatch, key);
        }
        public void DrawTextBackground(Dictionary<string, string> key)
        {
            currentExtension.DrawTextBackground(key);
        }

        public void Update()
        {
            currentExtension.Update();
        }

        public Dictionary<string, string> ReplaceData(Dictionary<string,string> dic) 
        {
           return General.ReplaceData(dic);
        }
        private void LoadRow(object row)
        {
            object[] cols = (object[])row;
            int maxHeight = 0;
            ctlX = startPoint.X;

            for (int i = 0; i < cols.Length; i++)
            {
                Dictionary<string, object> dic = (Dictionary<string, object>)cols[i];
                if (!dic.ContainsKey("Type")) continue;
                ControlBase ctl = dicControls[dic["Type"].ToString()].Invoke();               
                ctl.Location = new System.Drawing.Point(ctlX, ctlY);
                panel.Controls.Add(ctl);

                ExtensionControl ext = new() { Control = ctl };
                PropertyInfo[] info = ext.GetType().GetProperties();
                for (int j = 0; j < info.Length; j++)
                {
                    if (dic.ContainsKey(info[j].Name))
                    {
                        if (info[j].PropertyType == typeof(bool))
                            info[j].SetValue(ext, Convert.ToBoolean(dic[info[j].Name]));
                        else
                            info[j].SetValue(ext, dic[info[j].Name].ToString());
                    }
                }
                ctlGroup.Add(ext);

                if (dic.ContainsKey("Attribute"))
                    LoadAttribute(ctl, (Dictionary<string, object>)dic["Attribute"]);
                ctlX += ctl.Width + 6;
                if (maxHeight < ctl.Height)
                    maxHeight = ctl.Height;

                if (dic.ContainsKey("Event"))
                    LoadEvent(ctl, (Dictionary<string, object>)dic["Event"]);
            }

            ctlY += maxHeight + 6;
        }

        private void LoadAttribute(ControlBase ctl, Dictionary<string, object> att)
        {
            Type type = ctl.GetType();
            foreach (string key in att.Keys)
            {
                PropertyInfo info = type.GetProperty(key);
                if (info == null) continue;
                dicControlAttribute[info.PropertyType].Invoke(info, ctl, att[key]);
            }        
        }

        private void LoadEvent(ControlBase ctl, Dictionary<string, object> att)
        {
            try
            {
                Type type = ctl.GetType();
                foreach (string key in att.Keys)
                {
                    EventInfo info = type.GetEvent(key);
                    if (info == null) continue;
                    Delegate handler = Delegate.CreateDelegate(info.EventHandlerType, currentExtension, att[key].ToString());
                    info.AddEventHandler(ctl, handler);
                }
            }
            catch (Exception ex)
            {
                LogNet.log.Error("LoadEvent", ex);
            }
        }

        private void AttributeInt32(PropertyInfo info, ControlBase ctl, object obj)
        {
            if (int.TryParse(obj.ToString(), out int result))
            {
                if (info.Name == "Width" && result < 1)
                    info.SetValue(ctl, pnlWidth - ctl.Left - 6);
                else
                    info.SetValue(ctl, result);
            }
        }

        private void AttributeString(PropertyInfo info, ControlBase ctl, object obj)
        {
            info.SetValue(ctl, obj.ToString());
        }

        private void AttributeBoolean(PropertyInfo info, ControlBase ctl, object obj)
        {
            if (bool.TryParse(obj.ToString(), out bool result))
                info.SetValue(ctl, result);
        }

        private void AttributeSingle(PropertyInfo info, ControlBase ctl, object obj)
        {
            if (float.TryParse(obj.ToString(), out float result))
                info.SetValue(ctl, result);
        }

        private void AttributeContentAlignment(PropertyInfo info, ControlBase ctl, object obj)
        {
            System.Drawing.ContentAlignment alignment = (System.Drawing.ContentAlignment)Enum.Parse(typeof(System.Drawing.ContentAlignment), obj.ToString());
            info.SetValue(ctl, alignment);
        }

        private void AttributeColor(PropertyInfo info, ControlBase ctl, object obj)
        {
            string[] s = obj.ToString().Split(',');
            if (s.Length != 3) return;

            if (!int.TryParse(s[0], out int r)) return;
            if (!int.TryParse(s[1], out int g)) return;
            if (!int.TryParse(s[2], out int b)) return;

            System.Drawing.Color color = System.Drawing.Color.FromArgb(r, g, b);
            info.SetValue(ctl, color);
        }

        private void AttributeFont(PropertyInfo info, ControlBase ctl, object obj)
        {
            System.Drawing.Font font = ObjConversion.StrToFont(obj.ToString());
            info.SetValue(ctl, font);
        }

        private void AttributeHorizontalAlignment(PropertyInfo info, ControlBase ctl, object obj)
        {
            System.Windows.Forms.HorizontalAlignment alignment = (System.Windows.Forms.HorizontalAlignment)Enum.Parse(typeof(System.Windows.Forms.HorizontalAlignment), obj.ToString());
            info.SetValue(ctl, alignment);
        }
       
    }
}