FileCodeController.cs 3.5 KB
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OnlineStore.DeviceLibrary
{
    /// <summary>
    /// 文件条码操作类
    /// </summary>
    public class FileCodeController
    {

        public static bool UseFileCode = false;

        public static string FilePath = "";

        public static List<string> CurrCodeList = new List<string>();
        public static int CurrCodeIndex = -1;

        static FileCodeController()
        {
            int isuse = ConfigAppSettings.GetIntValue(Setting_Init.UseFileCode);
            if (isuse.Equals(1))
            {
                FilePath = ConfigAppSettings.GetValue(Setting_Init.CodeFilePath);
                if (!FilePath.Equals(""))
                {
                    UseFileCode = true;
                }
            }
        }

        /// <summary>
        /// 点击开始批量入库,开始读取文件并把条码放入内存
        /// </summary>
        /// <returns></returns>
        public static  bool ReloadFileCode()
        {
            if (!UseFileCode)
            {
                return false ;
            } 
            try
            {
                CurrCodeList = new List<string>();
                CurrCodeIndex = -1;
                List<string> Data = new List<string>();
                if (File.Exists(FilePath))
                {
                    string[] allLines = File.ReadAllLines(FilePath);
                    foreach (string str in allLines)
                    {
                        string code = str.Trim();
                        if (String.IsNullOrEmpty(code))
                        {
                            continue;
                        }
                        Data.Add(str);
                    }

                    //清理文件内容
                    File.WriteAllLines(FilePath, new string[] { });
                }
                else
                {
                    LogUtil.error("文件【" + FilePath + "】不存在");
                    return false;
                }
                //文件格式,需要拼接成:RI:PN:Q
                //PN1
                //RI1
                //PN2
                //RI2
                //倒叙拼接二维码
                if (Data.Count > 0)
                {
                    string codeMsg = "";
                    for (int i = Data.Count - 1; i >= 1; i -= 2)
                    {
                        string ri = Data[i];
                        string pn = Data[i - 1];
                        string code = ri + ";" + pn + ";1";
                        CurrCodeList.Add(code);
                        codeMsg += code + "\r\n";
                    }
                    LogUtil.info("文件【" + FilePath + "】解析到条码=\r\n" + codeMsg);
                }
                else
                {
                    LogUtil.error("文件【" + FilePath + "】未读取到条码");
                }
            }
            catch (Exception ex)
            {
                LogUtil.error("ReloadFileCode出错:" + ex.ToString());
            }
            return false;
        }


        public static string NextCode()
        {
           
            if (UseFileCode&&CurrCodeList.Count > 0)
            { 
                CurrCodeIndex++;
                if (CurrCodeList.Count > CurrCodeIndex)
                {
                    return CurrCodeList[CurrCodeIndex];
                }
            }
            return "";
        }

    }
}