using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TECSharpFunction;

namespace SlMesDbIterface
{
   public class DeviceTools
    {
        public static void GetToolExcel(string FtpServer, string FtpUser,string FtpPassWord, string FtpFileList)
        {
            FTPHelper ftpClient = new FTPHelper(FtpServer, @"", FtpUser, FtpPassWord);
            //ListType=1代表获取文件列表，ListType=2代表获取文件夹列表，ListType=3代表获取文件和文件夹列表。
            //Detail=true时获文件或文件夹详细信息，Detail=false时只获取文件或文件夹名称。
            //Keyword是只需list名称包含Keyword的文件或文件夹，若要list所有文件或文件夹，则该参数为空。若ListType=3，则该参数无效。

            int ListType = 1;
            bool Detail = false;
            string Keyword = "";

            var fileList = ftpClient.GetFileDirctoryList(ListType, Detail, Keyword);

            foreach (string itemFileName in fileList)
            {
                string tableName = itemFileName.Substring(0, itemFileName.LastIndexOf("."));
                if (FtpFileList.Contains(itemFileName))
                {
                    ftpClient.Download(itemFileName, itemFileName, null);
                    ExcelToDataTable(itemFileName, tableName);
                }
                else
                {
                }
            }
        }

        private static void ExcelToDataTable(string excelPath, string tableName)
        {
            ////判断是否包含当前日期的记录
            //int excelSheetindex = 0;
            //var dt = NPOITest.ExeclHelper.ExcelToDataTable(excelPath, excelSheetindex, true);
            //switch (tableName)
            //{
            //    case "刀具柜-库存明细":
            //        {
            //            string str1 = "delete from[刀具柜-库存明细] where[导入日期] = '" + DateTime.Now.ToShortDateString() + "'";
            //            DataLinkMesWork.SQLCommon.ExecuteSql(str1, Program.ConnectionString_MES, out string errorMessage1);
            //            var dt_DB = GetToolDetail(dt);
            //            SqlBulkCopyByDatatable(Program.ConnectionString_MES, tableName, dt_DB);
            //        }
            //        break;
            //    case "刀具领用记录":
            //        {
            //            string str2 = "delete from[刀具领用记录] where[导入日期] = '" + DateTime.Now.ToShortDateString() + "'";
            //            DataLinkMesWork.SQLCommon.ExecuteSql(str2, Program.ConnectionString_MES, out string errorMessage2);
            //            var dt_DB = GetToolRecoder(dt);
            //            SqlBulkCopyByDatatable(Program.ConnectionString_MES, tableName, dt_DB);
            //        }
            //        break;
            //}
        }

       
        /// <summary>
        /// 刀具领用记录
        /// </summary>
        private static DataTable GetToolRecoder(DataTable dtData)
        {
            //刀具领用记录.xls

            DataTable dt = new DataTable();
            dt.Columns.Add("物料名称", typeof(System.String));
            dt.Columns.Add("物料编号", typeof(System.String));
            dt.Columns.Add("物料型号", typeof(System.String));
            dt.Columns.Add("供应商", typeof(System.String));//品牌（供应商）
            dt.Columns.Add("领取来源", typeof(System.String));
            dt.Columns.Add("领取数量", typeof(System.Int32));
            dt.Columns.Add("实领数量", typeof(System.Int32));
            dt.Columns.Add("领取单位", typeof(System.String));
            dt.Columns.Add("包装数量", typeof(System.Int32));
            dt.Columns.Add("包装单位", typeof(System.String));
            dt.Columns.Add("领用类型", typeof(System.String));
            dt.Columns.Add("单价", typeof(System.Double));//单价（元）
            dt.Columns.Add("金额", typeof(System.Double));
            dt.Columns.Add("部门", typeof(System.String));
            dt.Columns.Add("领用人员", typeof(System.String));
            dt.Columns.Add("已归还数量", typeof(System.Int32));
            dt.Columns.Add("是否归还", typeof(System.String));
            dt.Columns.Add("领用时间", typeof(System.DateTime));
            DataRow dr = dt.NewRow();
            for (int i = 0; i < dtData.Rows.Count; i++)
            {
                dr = dt.NewRow();
                var 物料名称 = dtData.Rows[i][0].ToString();
                if (物料名称 == "合计")
                {
                    continue;
                }
                dr["物料名称"] = 物料名称;
                dr["物料编号"] = dtData.Rows[i][1].ToString();
                dr["物料型号"] = dtData.Rows[i][2].ToString();
                dr["供应商"] = dtData.Rows[i][3].ToString();
                dr["领取来源"] = dtData.Rows[i][4].ToString();
                try
                {
                    dr["领取数量"] = Convert.ToInt32(dtData.Rows[i][5]);
                }
                catch
                {
                    dr["领取数量"] = 0;
                }
                try
                {
                    dr["实领数量"] = Convert.ToInt32(dtData.Rows[i][6]);
                }
                catch
                {
                    dr["实领数量"] = 0;
                }
                dr["领取单位"] = dtData.Rows[i][7].ToString();
                try
                {
                    dr["包装数量"] = Convert.ToInt32(dtData.Rows[i][8]);
                }
                catch
                {
                    dr["包装数量"] = 0;
                }


                dr["包装单位"] = dtData.Rows[i][9].ToString();
                dr["领用类型"] = dtData.Rows[i][10].ToString();
                try
                {
                    dr["单价"] = Convert.ToDouble(dtData.Rows[i][11]);
                }
                catch
                {
                    dr["单价"] = 0;
                }
                try
                {
                    dr["金额"] = Convert.ToDouble(dtData.Rows[i][12]);
                }
                catch
                {
                    dr["金额"] = 0;
                }
                dr["部门"] = dtData.Rows[i][13].ToString();
                dr["领用人员"] = dtData.Rows[i][14].ToString();


                try
                {
                    dr["已归还数量"] = Convert.ToInt32(dtData.Rows[i][15]);
                }
                catch
                {
                    dr["已归还数量"] = 0;
                }
                dr["是否归还"] = dtData.Rows[i][16].ToString();
                try
                {
                    dr["领用时间"] = Convert.ToDateTime(dtData.Rows[i][17]);
                //    dr["领用时间"] = dtData.Rows[i][17].ToString();
                }
                catch
                {
                    dr["领用时间"] = DBNull.Value;
                }

                dt.Rows.Add(dr);
            }


            return dt;

        }
        /// <summary>
        /// 刀具柜-库存明细
        /// </summary>
        private static DataTable GetToolDetail(DataTable dtData)
        {
            //刀具柜 - 库存明细.xls

            DataTable dt = new DataTable();
            dt.Columns.Add("刀具柜名称", typeof(System.String));
            dt.Columns.Add("行号", typeof(System.Int32));
            dt.Columns.Add("列号", typeof(System.Int32));
            dt.Columns.Add("物料名称", typeof(System.String));
            dt.Columns.Add("物料编号", typeof(System.String));
            dt.Columns.Add("物料型号", typeof(System.String));
            dt.Columns.Add("当前数量", typeof(System.Double));
            dt.Columns.Add("包装单位", typeof(System.String));
            dt.Columns.Add("单价", typeof(System.Double));
            dt.Columns.Add("金额", typeof(System.Double));
            dt.Columns.Add("最大存储", typeof(System.Int32));
            dt.Columns.Add("警告阀值", typeof(System.Int32));
            dt.Columns.Add("最后上架时间", typeof(System.DateTime));
            DataRow dr = dt.NewRow();
            for (int i = 0; i < dtData.Rows.Count; i++)
            {
                dr = dt.NewRow();
                dr["刀具柜名称"] = dtData.Rows[i]["刀具柜名称"].ToString();
                try
                {
                    dr["行号"] = Convert.ToInt32(dtData.Rows[i]["行号"]);
                }
                catch
                {
                    dr["行号"] = 0;
                }
                try
                {
                    dr["列号"] = Convert.ToInt32(dtData.Rows[i]["列号"]);
                }
                catch
                {
                    dr["列号"] = 0;
                }
                dr["物料名称"] = dtData.Rows[i]["物料名称"].ToString();
                dr["物料编号"] = dtData.Rows[i]["物料编号"].ToString();
                dr["物料型号"] = dtData.Rows[i]["物料型号"].ToString();
                try
                {
                    dr["当前数量"] = Convert.ToDouble(dtData.Rows[i]["当前数量"]);
                }
                catch
                {
                    dr["当前数量"] = 0;
                }
                dr["包装单位"] = dtData.Rows[i]["包装单位"].ToString();
                try
                {
                    dr["单价"] = Convert.ToDouble(dtData.Rows[i]["单价"]);
                }
                catch
                {
                    dr["单价"] = 0;
                }
                try
                {
                    dr["金额"] = Convert.ToDouble(dtData.Rows[i]["金额"]);
                }
                catch
                {
                    dr["金额"] = 0;
                }
                try
                {
                    dr["最大存储"] = Convert.ToDouble(dtData.Rows[i]["最大存储"]);
                }
                catch
                {
                    dr["最大存储"] = 0;
                }
                try
                {
                    dr["警告阀值"] = Convert.ToDouble(dtData.Rows[i]["警告阀值"]);
                }
                catch
                {
                    dr["警告阀值"] = 0;
                }
                try
                {
                    dr["最后上架时间"] = Convert.ToDateTime(dtData.Rows[i]["最后上架时间"]);
                }
                catch
                {
                    dr["警告阀值"] = DBNull.Value;
                }
                dt.Rows.Add(dr);
            }


            return dt;

        }

        static void SqlBulkCopyByDatatable(string connectionString, string TableName, DataTable dt)
        {
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlBulkCopy sqlbulkcopy =
                new SqlBulkCopy(connectionString, SqlBulkCopyOptions.UseInternalTransaction))
                {
                    try
                    {
                        sqlbulkcopy.DestinationTableName = "[" + TableName + "]";
                        dt.TableName = TableName;
                        for (int i = 0; i < dt.Columns.Count; i++)
                        {
                            sqlbulkcopy.ColumnMappings.Add(dt.Columns[i].ColumnName.Trim(), "[" + dt.Columns[i].ColumnName + "]");
                        }
                        sqlbulkcopy.WriteToServer(dt);
                    }
                    catch (System.Exception ex)
                    {
                        //throw ex;
                    }
                }
            }
        }

       

    }
}
