PositionDebugManager.cs 5.5 KB
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace OnlineStore.DeviceLibrary
{
    public class PosDebugResultManager
    {
        public static List<DrawerResult> DrawerResults;
        public static bool isInit = false;
        static string filePath = Application.StartupPath + Common.ConfigAppSettings.GetValue(Common.Setting_Init.ConfigPath_PosDebugInfo);
        public static void Init(List<string> posNumList)
        {
            if (!System.IO.File.Exists(filePath))
            {
                DrawerResults = new List<DrawerResult>();
                foreach (var item in posNumList)
                {
                    DrawerResult res = DrawerResults.Find(s => s.DrawerName.Equals(item.Substring(0, 8)));
                    if (res == null)
                    {
                        res = new DrawerResult() { DrawerName = item.Substring(0, 8) };
                        res.PosResultList.Add(new PosDebugResult() { Name = item });
                        DrawerResults.Add(res);
                    }
                    else
                    {
                        res.PosResultList.Add(new PosDebugResult() { Name = item });
                    }
                }
            }
            else
            {
                string[] txt = System.IO.File.ReadAllLines(filePath);
                DrawerResults = new List<DrawerResult>();
                if (txt == null || txt.Length==0)
                {
                    foreach (var item in posNumList)
                    {
                        DrawerResult res = DrawerResults.Find(s => s.DrawerName.Equals(item.Substring(0, 8)));
                        if (res == null)
                        {
                            res = new DrawerResult() { DrawerName = item.Substring(0, 8) };
                            res.PosResultList.Add(new PosDebugResult() { Name = item });
                        }
                        else
                        {
                            res.PosResultList.Add(new PosDebugResult() { Name = item });
                        }
                    }
                }
                else
                {
                    foreach (var item in txt)
                    {
                        DrawerResults.Add(Common.JsonHelper.DeserializeJsonToObject<DrawerResult>(item)) ;
                    }

                }

            }
            isInit = true;
        }
        public static void SaveResult()
        {
            try
            {
                List<string> res = new List<string>();
                foreach (var item in DrawerResults)
                {
                     res.Add(Common.JsonHelper.SerializeObject(item));
                }

                System.IO.File.WriteAllLines(filePath, res);
            }
            catch (Exception e)
            {
                Common.LogUtil.error("SaveResult", e);
            }
        }
        /// <summary>
        /// 获取抽屉调试结果
        /// </summary>
        /// <param name="drawername"></param>
        /// <returns></returns>
        public static int GetDrawerResult(string drawername)
        {
            if (!isInit)
                return -1;
            DrawerResult drawerResult = DrawerResults.Find(s => s.DrawerName.Equals(drawername));
            return drawerResult.ResultCode;
        }

        public static void SetDrawerResult(string drawername, int code)
        {
            if (!isInit)
                return;
            DrawerResult drawerResult = DrawerResults.Find(s => s.DrawerName.Equals(drawername));
            drawerResult.ResultCode = code;
        }
        /// <summary>
        /// 通过料格结果得出抽屉结果
        /// </summary>
        public static int SetDrawerResultWithGrid(string drawername)
        {
            if (!isInit)
                return -1;
            DrawerResult drawerResult = DrawerResults.Find(s => s.DrawerName.Equals(drawername));
            PosDebugResult posDebugResult = drawerResult.PosResultList.Find(s => s.ResCode.Equals(0) || s.ResCode.Equals(-1));
            if (posDebugResult == null)
            {
                drawerResult.ResultCode = 1;
                return 1;
            }
            else
            {
                drawerResult.ResultCode = 0;
                return 0;
            }

        }
        /// <summary>
        /// 设置料格的状态
        /// </summary>
        /// <param name="drawername">抽屉名</param>
        /// <param name="code">结果代码</param>
        public static void SetGridResult(string name, int code)
        {
            DrawerResult drawerResult = DrawerResults.Find(s => s.DrawerName.Equals(name.Substring(0, 8)));
            if (drawerResult != null)
            {
                PosDebugResult res = drawerResult.PosResultList.Find(s => s.Name.Equals(name));
                if (res != null)
                    res.ResCode = code;
            }

        }
        /// <summary>
        /// 获取料格状态
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static int GetGridResult(string name)
        {
            DrawerResult drawerResult = DrawerResults.Find(s => s.DrawerName.Equals(name.Substring(0, 8)));
            if (drawerResult != null)
            {
                PosDebugResult res = drawerResult.PosResultList.Find(s => s.Name.Equals(name));
                if (res != null)
                    return res.ResCode;
            }
            return -1;
        }
    }
}