namespace BLL
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.IO;
    using System.Data;
    using System.Data.SqlClient;

    public static class SyncFilePathHelper
    {
        // 根据文件路径获取文件的层数
        //public static int GetFileDepth(string filePath)
        //{
        //    int num = Regex.Matches(filePath, "\\").Count;
        //    return num;
        //}

        public static int NumberOfDigits(string theString)
        {
            int count = 0;
            for (int i = 0; i < theString.Length; i++)
            {
                if (Char.IsDigit(theString[i]))
                {
                    count++;
                }
            }
            return count;
        }

        private static List<string> pathList=new List<string>();
        public static void UpdateFilePath3(string filePath)
        {
            string file = "Projects_Transmittals\\C";
           
            string[] strDirs = Directory.GetDirectories(filePath);
            if (strDirs != null && strDirs.Length > 0 )
            {
                for (int i = 0; i < strDirs.Length; i++)
                {
                    
                    if (NumberOfDigits(strDirs[i]) >= 5 && strDirs[i].Contains(file))
                    {
                        pathList.Add(strDirs[i]);
                    }
                    UpdateFilePath3(strDirs[i]);
                }
            }
        }

        public static void GetList(string filePath)
        {
            UpdateFilePath3(filePath);
            List<string> filePathList = pathList;

            string strSql = "SELECT ProjectControl_JobNo FROM dbo.Editor_EProject ORDER BY ProjectControl_JobNo";
            DataTable dt = SQLHelper.GetDataTableRunText(strSql, null);
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    var q = from x in filePathList where x.Contains(dt.Rows[i][0].ToString()) select x;
                    if (q.Count() > 0)
                    {
                        string updateFilePath =q.First().Replace("D:", "\\wcnnji008219389");  // wcnnji008219389
                        string update = @"UPDATE dbo.Editor_EProject SET ProjectControl_FilesLink='" + updateFilePath + "'  WHERE ProjectControl_JobNo='" + dt.Rows[i][0].ToString() + "'";
                        SQLHelper.ExecutSql(update);
                    }
                }
                
            }
        }
        public static void UpdateFilePath(string filePath, String fileName)
        {
            string[] strDirs = Directory.GetDirectories(filePath);
            if (strDirs != null && strDirs.Length > 0)
            {
                for (int i = 0; i < strDirs.Length; i++)
                {
                    if (strDirs[i].ToLower().IndexOf(fileName.ToLower()) >= 0)
                    {
                        string updateFilePath = strDirs[i];
                        string strSql = @"UPDATE dbo.Editor_EProject SET ProjectControl_FilesLink='" + updateFilePath + "'  WHERE ProjectControl_JobNo='" + fileName + "'";
                        SQLHelper.ExecutSql(strSql);
                        break;
                    }
                    UpdateFilePath(strDirs[i], fileName);
                }
            }
        }

        public static void UpdateFilePath2(string filePath, String fileName)
        {
            string[] strDirs = Directory.GetDirectories(filePath);
            if (strDirs != null && strDirs.Length > 0)
            {
                bool aa = false;
                if (strDirs.Contains(fileName))
                {
                    aa = true;
                    var path = from x in strDirs where strDirs.Contains(fileName) select x;
                    string updateFilePath = path.First();
                    string strSql = @"UPDATE dbo.Editor_EProject SET ProjectControl_FilesLink='" + updateFilePath + "'  WHERE ProjectControl_JobNo='" + fileName + "'";
                    SQLHelper.ExecutSql(strSql);
                }
                else
                {
                    for (int i = 0; i < strDirs.Length; i++)
                    {
                        UpdateFilePath(strDirs[i], fileName);
                        if (aa)
                        {
                            break;
                        }
                    }
                }
            }
        }
    }
}