前提:该机构录入的都是和该机构有关的数据,机构下的funmental(idcard唯一)和creditinfo(funmentalid唯一)不能重复所以推出以下结论:
1.根据userid(机构)=》allfunmental 和 allInfo
2.标示唯一 funmetal以idcard allinfo以funmentalid,所以做出对应的字典allfdic allinfodic
3.if(allfdic.containskey(idcard))取出该数据 funmental,为字典重新赋值(在保存之前就重新赋值因为是引用类型,反之info也一样),然后标记为脏数据,如果不存在则new 出一个funmental,加入字典不管 三七二十一给他们赋值,最后saveorupdate funmental
4.info表的保存
creditinfo=null;
if(allinfodic.containskey(funmetalid))从字典取出该数据,然后重新赋值(引用类型结论和前面的一样),如果字典里没有加入字典,
然后为creditinfo.funmentalid=funmental.id 之后不管三七二十一saveorupdate
private void Import()
{
QueryInfo info=new QueryInfo("SysDict");
info.AddParam("TypeCode","SECTOR_TYPE");
List<SysDict> IndustryDic = Holworth.Utility.Utility.ListToT<SysDict>(Dao.FindList(info)).ToList();
var dics = (from s in IndustryDic select s).ToDictionary(x => x.Name);
StringBuilder sbError = new StringBuilder();
if (string.IsNullOrEmpty(fileup.FileName))
{
ScriptHelper.MessageBox(false, "不能上传空文件。", 0);
return;
}
string excelName = fileup.FileName;
string fileName = Server.MapPath("~/upload/" + excelName);
this.fileup.SaveAs(fileName);
List<CcrPersonalFundamental> fundamentals = new List<CcrPersonalFundamental>();
List<CcrPersonalCreditInfo> creditInfos=new List<CcrPersonalCreditInfo>();
string sql = "begin\n" ;
DataSet ds = new DataSet();
DataTable dt = new DataTable();
ds = ExcelHelper.ReadExcelAllSheets(fileName, 0, false);
dt = ds.Tables[0];
QueryInfo allfunInfo = new QueryInfo("CcrPersonalFundamental");
allfunInfo.AddParam("UserId", int.Parse(CurrentUser.UserId));
var allf =
Holworth.Utility.Utility.ListToT<CcrPersonalFundamental>(
Dao.FindList(allfunInfo)).ToList();
var allfDic = (from s in allf select s).ToDictionary(x => x.IdCard);
// info dic
QueryInfo allCreditInfoDic = new QueryInfo("CcrPersonalCreditInfo");
allCreditInfoDic.AddParam("UserId", int.Parse(CurrentUser.UserId));
var allInfo =
Holworth.Utility.Utility.ListToT<CcrPersonalCreditInfo>(
Dao.FindList(allCreditInfoDic)).ToList();
var allInfoDic = (from s in allInfo select s).ToDictionary(x => x.FundamentalId);
//
for (int i = 0; i < dt.Rows.Count; i++)
{
DataRow row = dt.Rows[i];
var idcard = row[0].ToString().Trim().ToUpper().Trim();
var findf = false;
CcrPersonalFundamental fundamental = null;
if (allfDic.ContainsKey(idcard))
{
findf = true;
fundamental = allfDic[idcard];
fundamental.State.MarkDirty();
}
else
{
fundamental = new CcrPersonalFundamental();
findf = false;
fundamental.State.MarkNew();
}
CcrPersonalCreditInfo creditInfo=null;
fundamental.IdCard = idcard;
fundamental.Marriage = row[7].ToString().Trim();
fundamental.PersonalName = row[1].ToString().Trim();
fundamental.Address = row[8].ToString().Trim();
fundamental.MobilePhone = row[2].ToString().Trim();
fundamental.Industry = dics[row[9].ToString().Trim()].Code;
fundamental.Sex = row[3].ToString().Trim();
fundamental.CompanySize = row[10].ToString().Trim();
fundamental.Birthday = DateTime.Parse(row[4].ToString().Trim());
fundamental.Position = row[11].ToString().Trim();
fundamental.Education = row[5].ToString().Trim();
fundamental.MonthlyIncome =row[12].ToString().Trim();
fundamental.School = row[6].ToString().Trim();
fundamental.UserId = int.Parse(CurrentUser.UserId);
//通过判断身份证IdCard和机构账号UserId判断是否新纪录,新纪录插入,旧记录更新,还要判断信息是否完整
//QueryInfo histroyFundamentalInfo=new QueryInfo();
//histroyFundamentalInfo.QueryObject = "CcrPersonalFundamental";
//histroyFundamentalInfo.AddParam("UserId",int.Parse(CurrentUser.UserId));
//histroyFundamentalInfo.AddParam("IdCard",fundamental.IdCard);
// List<CcrPersonalFundamental> historyFundamentalList = Holworth.Utility.Utility.ListToT<CcrPersonalFundamental>(Dao.FindList(histroyFundamentalInfo)).ToList();
//如果已经存在这个机构下的身份证,则更新改数据
Dao.SaveOrUpdate(fundamental);
//CcrPersonalFundamental importFundamental=Dao.SaveOrUpdate(fundamental) as CcrPersonalFundamental;
if (!findf)
{
allfDic.Add(idcard, fundamental);
}
else
{
allfDic[idcard] = fundamental;
}
//通过上一步的身份证IdCard和机构账号UserId查询出funmentalId,再根据当前的UserId,判断这个funmetal是否存在如果存在判断info表里的userid和funmetalid是否已经存在更新,不存在新增
var findC = false;
if (allInfoDic.ContainsKey(fundamental.Id))
{
findC = true;
creditInfo = allInfoDic[fundamental.Id];
creditInfo.State.MarkDirty();
}
else
{
creditInfo = new CcrPersonalCreditInfo();
findC = false;
creditInfo.State.MarkNew();
}
creditInfo.PresentResidenceSince = row[23].ToString().Trim();
creditInfo.AccountStatus = row[14].ToString().Trim();
creditInfo.Propety = row[24].ToString().Trim();
creditInfo.DurationInMonth =row[15].ToString().Trim();
creditInfo.AgeInYear =row[25].ToString().Trim();
creditInfo.CreditHistory = row[13].ToString().Trim();
creditInfo.OtherInstallmentPlans =row[26].ToString().Trim();
creditInfo.Purpose = row[16].ToString().Trim();
creditInfo.Hounsing = row[27].ToString().Trim();
creditInfo.CreditAmount =row[17].ToString().Trim();
creditInfo.NumberOfCreditcard =row[28].ToString().Trim();
creditInfo.SavingsAccount = row[18].ToString().Trim();
creditInfo.Job =row[29].ToString().Trim();
creditInfo.PresentEmploymentSince = row[19].ToString().Trim();
creditInfo.NumberOfPeopleSupport = row[30].ToString().Trim();
creditInfo.DisposableIncome = row[20].ToString().Trim();
creditInfo.Telphone = row[31].ToString().Trim();
creditInfo.PersonalStatusAndSex =row[21].ToString().Trim();
creditInfo.ForeignWorkder = row[32].ToString().Trim();
creditInfo.OtherGuarantor = row[22].ToString().Trim();
creditInfo.UserId = int.Parse(CurrentUser.UserId);
creditInfo.CreditRate = row[33].ToString().Trim();
creditInfo.Usage = "train";
if ( !findC)
{
creditInfo.FundamentalId = fundamental.Id;
}
var obj = Dao.SaveOrUpdate(creditInfo);
if (!findC)
{
allInfoDic.Add(creditInfo.FundamentalId,creditInfo);
}
}
System.IO.File.Delete(fileName);
//记录日志 成功几条 失败几条
Contract.Domain.EtlLog log = new EtlLog();
log.TableName = "ccr_personal_fundamental,ccr_personal_credit_info";
log.CreateUname = CurrentUser.UserName;
log.Status = "插入成功";
log.FileName = fileName;
log.Remark = string.Format("共导入{0}条", creditInfos.Count);
if (dt.Rows.Count -1- creditInfos.Count > 0)
log.Remark += string.Format("失败{0}条因为{1}", dt.Rows.Count - 1 - creditInfos.Count, sbError.ToString() + "\n");
log.CreateDate = DateTime.Now;
log.State.MarkNew();
Dao.SaveOrUpdate(log);
Page.ClientScript.RegisterClientScriptBlock(JSUtil.CurrentPage.GetType(), Util.NewGuid(), @"<script>$.messager.alert(‘操作提示‘, ‘导入成功‘, ‘info‘, function () {
artDialog.open.origin.JDataGrid.GridSearch();
artDialog.close();
});</script>");
}