using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Text.RegularExpressions;
using System.Linq;
using Edu.Common.Plugin;

namespace Edu.Common.Data
{
    /// <summary>
    /// 客户导入帮助类
    /// </summary>
    public class QYCustomerClueHelper
    {
        /// <summary>
        /// 根据Excel文件获取列表
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static List<CustomerClueListModel> GetCustomerClueData(string filePath, List<string> fieldList)
        {
            List<CustomerClueListModel> xlsItems = new List<CustomerClueListModel>();
            var dt = Common.Plugin.NPOIHelper.ImportExcelToDatatable(filePath, 0, 2, true);
            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow item in dt.Rows)
                {
                    xlsItems.Add(DataRowToModel(item, fieldList));
                }
            }
            return xlsItems;
        }

        /// <summary>
        /// DataRow转实体
        /// </summary>
        /// <param name="dr"></param>
        /// <returns></returns>
        private static CustomerClueListModel DataRowToModel(DataRow dr, List<string> fieldList)
        {
            List<CustomerClueModel> list = new List<CustomerClueModel>();
            if (dr != null)
            {
                foreach (var item in fieldList) {
                    if (dr.Table.Columns.Contains(item) && !string.IsNullOrEmpty(dr[item].ToString()))
                    {
                        list.Add(new CustomerClueModel()
                        {
                            Name = item,
                            Value = dr[item].ToString()
                        });
                    }
                    //有必填带*情况
                    else if (dr.Table.Columns.Contains(item + "*") && !string.IsNullOrEmpty(dr[item + "*"].ToString()))
                    {
                        list.Add(new CustomerClueModel()
                        {
                            Name = item,
                            Value = dr[item + "*"].ToString()
                        });
                    }
                }
            }
            return new CustomerClueListModel() {
                 list = list
            };
        }
    }

    /// <summary>
    /// 导入数据格式
    /// </summary>
    public class CustomerClueModel
    {
        /// <summary>
        /// 字段名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 字段值
        /// </summary>
        public string Value { get; set; }
    }

    /// <summary>
    /// 导入数据列表
    /// </summary>
    public class CustomerClueListModel
    {
        public List<CustomerClueModel> list;
    }
}
