using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

namespace Edu.Common.Data
{
    public class CourseWordsHelper
    {
        /// <summary>
        /// 根据Excel文件获取问题列表
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static List<WordsItem> GetXlsWordsData(string filePath)
        {
            List<WordsItem> xlsItems = new List<WordsItem>();
            var ds = Common.Plugin.NPOIHelper.ExcelToDataSet(filePath);
            if (ds != null && ds.Tables.Count > 0)
            {
                for (var i = 0; i < ds.Tables.Count; i++)
                {
                    var dataRow = ds.Tables[i].Rows;
                    for (var j = 0; j < ds.Tables[i].Rows.Count; j++)
                    {
                        xlsItems.Add(DataRowToModel((i+1),ds.Tables[i].Rows[j]));
                    }
                }
            }
            return xlsItems;
        }

        /// <summary>
        /// DataRow转实体
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="dr"></param>
        /// <returns></returns>
        private static WordsItem DataRowToModel(int ChapterId, DataRow dr)
        {
            WordsItem model = new WordsItem();
            model.ChapterId = ChapterId;
            if (dr != null)
            {
                if (dr.Table.Columns.Contains("品詞名") && !string.IsNullOrEmpty(dr["品詞名"].ToString()))
                {
                    model.WordType = dr["品詞名"].ToString();
                }
                if (dr.Table.Columns.Contains("語彙・表現") && !string.IsNullOrEmpty(dr["語彙・表現"].ToString()))
                {
                    model.WordContent = dr["語彙・表現"].ToString();
                }
                if (dr.Table.Columns.Contains("声调") && !string.IsNullOrEmpty(dr["声调"].ToString()))
                {
                    model.WordTone = dr["声调"].ToString();
                }
                if (dr.Table.Columns.Contains("日文书写") && !string.IsNullOrEmpty(dr["日文书写"].ToString()))
                {
                    model.WordWrite = dr["日文书写"].ToString();
                }
                if (dr.Table.Columns.Contains("中文意思") && !string.IsNullOrEmpty(dr["中文意思"].ToString()))
                {
                    model.ChineseMean = dr["中文意思"].ToString();
                }
            }
            return model;
        }
    }

    /// <summary>
    /// 单词项
    /// </summary>
    public class WordsItem
    {
        /// <summary>
        /// 章节编号
        /// </summary>
        public int ChapterId { get; set; }

        /// <summary>
        /// 单词类型(名词、动词、形容词等)
        /// </summary>
        public string WordType { get; set; }

        /// <summary>
        /// 单词内容
        /// </summary>
        public string WordContent { get; set; }

        /// <summary>
        /// 单词声调
        /// </summary>
        public string WordTone { get; set; }

        /// <summary>
        /// 单词书写
        /// </summary>
        public string WordWrite { get; set; }

        /// <summary>
        /// 中文意思
        /// </summary>
        public string ChineseMean { get; set; }
    }
}
