using NPOI.SS.UserModel;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using WX.CRM.Common;
using WX.CRM.Model.Entity;
using WX.CRM.Model.Enum;
namespace WX.CRM.WebHelper.ExcelImport.QH
{
public class Qh_CustomerUploadTool : ComUploadTool
{
#region method
public override EnumExcelDataType ExcelDataType { get { return EnumExcelDataType.Qh_Customer; } }
#endregion
#region 继承函数
///
/// 单元格数据格式化
///
/// 表字典
/// 这行excel数据
/// 该单元格字典对象
/// 存储excel 的列 和对应索引
///
protected override object CustomFormatColnum(List dictList, IRow excelRow, BAS_EXCELIMPORTDICT dictModel, Dictionary excelDic)
{
object obj = new object();
if (dictModel.DB_COLNAME == "RESID")//--客户ID
{
string temp = GetTruePhone(dictList, excelRow, excelDic);
obj = temp == "" ? "" : ResUtil.CreateResId(temp);
}
else if (dictModel.DB_COLNAME == "TRADECODE")//--交易商代码
{
obj = GetTradcode(excelRow, dictModel);
}
else if (dictModel.DB_COLNAME == "NUMBER_LAST4")//--手机后四位
{
string temp = GetTruePhone(dictList, excelRow, excelDic);
if (temp.Length > 4)
obj = temp.Substring(temp.Length - 4, 4);
else
obj = "";
}
else if (dictModel.DB_COLNAME == "COMMISSIONRATE")//--佣金比率
{
BAS_EXCELIMPORTDICT entry = dictList.FirstOrDefault(m => m.DB_COLNAME == "COMMISSIONRATE_TXT");
if (entry == null || entry.ColumnIndex == -1)
return 0;
string str = excelRow.Cells[entry.ColumnIndex].ToString();
string number = Common.OperationUtil.GetIntStr(str);
if (string.IsNullOrWhiteSpace(number))
number = "0";
return Convert.ToDecimal(Convert.ToDecimal(number) / 100);
}
else if (dictModel.DB_COLNAME == "SCHEDULESTATUS")//--开户状态
{
BAS_EXCELIMPORTDICT entry1 = dictList.FirstOrDefault(m => m.DB_COLNAME == "CONTRACTSTATUS");//银行签约状态
BAS_EXCELIMPORTDICT entry2 = dictList.FirstOrDefault(m => m.DB_COLNAME == "BOCESTATUS");//渤海状态
string contractStatus = entry1 != null ? excelRow.Cells[entry1.ColumnIndex].ToString() : "";
string boceStatus = entry2 != null ? excelRow.Cells[entry2.ColumnIndex].ToString() : "";
string[] geShi = { "未签约", "已签约", "" };
string[] geShi2 = { "已审核", "已退市" };
if (geShi.Contains(contractStatus) && geShi2.Contains(boceStatus))
{
if (boceStatus == "已审核" && contractStatus == "未签约")
obj = 180;
else if (boceStatus == "已审核" && contractStatus == "已签约")
obj = 200;
else if (boceStatus == "已审核" && contractStatus == "")
{
obj = DBNull.Value;
}
else if (boceStatus == "已退市")
obj = 70;
}
else
obj = 0;
}
return obj;
}
///
/// 获取错误的datable数据(续子类重写)
///
/// 错误数据的Datatable,如果没有(Datatable.Count=0 和message="" 和 errMsg.Count==0)=>数据验证通过
/// 自定义的提示信息
/// 报错信息
///
protected override DataTable CheckErroDataTable(DataTable tab, out string message, ref ValidationErrors errMsg)
{
DataTable errTab = tab.Clone();
message = string.Empty;
if (!DeptTradeCodeCheck(ref message, tab))
{
return null;
}
foreach (DataRow row in tab.Rows)
{
if (!CheckCellData(ref errMsg, row))
{
errTab.ImportRow(row);
}
}
return errTab;
}
#endregion
#region 其他处理函数
///
/// 验证 这条数据是否符合条件
///
/// 错误信息
/// 行数据
///
protected bool CheckCellData(ref ValidationErrors errMsg, DataRow row)
{
bool result = true;
//if (row["RESID"].ToString() == "")
//{
// result = false;
//}
//if (row["TRADECODE"].ToString() == "")
//{
// result = false;
//}
//if (row["NUMBER_LAST4"].ToString() == "")
//{
// result = false;
//}
//if (Convert.ToInt32(row["COMMISSIONRATE"]) == 0)
//{
// result = false;
//}
//if (row["SCHEDULESTATUS"] != DBNull.Value && Convert.ToInt32(row["SCHEDULESTATUS"]) == 0)
//{
// result = false;
//}
return result;
}
///
/// 获取正确的电话号码
///
///
private string GetTruePhone(List dictList, IRow excelRow, Dictionary excelDic)
{
string temp = "";
BAS_EXCELIMPORTDICT entry = dictList.FirstOrDefault(m => m.EXCEL_COLNAME == "手机号码");
temp = entry != null ? excelRow.Cells[entry.ColumnIndex].ToString().Trim() : "";
temp = temp.Replace("T", "").Replace("'", "");//--去除"T"和"'"
bool result = Utility.ChekMobile(temp);
if (!result)// 取电话号码
{
if (excelDic.ContainsKey("联系电话"))
{
temp = excelRow.Cells[excelDic["联系电话"]].ToString().Trim();
}
temp = temp.Replace("T", "").Replace("'", "");//--去除"T"和"'"
result = Utility.ChekMobile(temp);
}
temp = result ? temp : "";
return temp;
}
///
/// 获取交易商代码
///
///
///
///
private string GetTradcode(IRow excelRow, BAS_EXCELIMPORTDICT dictModel)
{
string obj = "";
string temp = excelRow.Cells[dictModel.ColumnIndex].ToString().Trim();
if (temp.Length < 8 || temp.Length > 9)
obj = "";
else if (temp.Length == 9)
{
obj = temp;
}
else if (temp.Length == 8)
{
if (temp.IndexOf("45") == 0)
obj = "0" + temp;
else
obj = "";
}
return obj;
}
#endregion
}
}