/// <summary> /// 保存卡数据集合代理商 /// </summary> /// <param name="tB_DATA_CARD_LIST"></param> /// <returns></returns> public int SaveImportCardForAgent(List <TB_DATA_CARD> tB_DATA_CARD_LIST) { Gateway gateway = Gateway.Default; DbTransaction tran = gateway.BeginTransaction(); try { //取所有非删除的终端用户 String endCustomSql = "select END_CUSTOMER_ID,END_CUSTOMER_NAME FROM [dbo].[TB_DATA_END_CUSTOMER] WHERE STATUS>-1"; DataTable endCustomData = gateway.FromCustomSql(endCustomSql).ToDataSet().Tables[0]; for (int i = 0; i < tB_DATA_CARD_LIST.Count; i++) { TB_DATA_CARD tB_DATA_CARD = tB_DATA_CARD_LIST[i]; String cardID = gateway.Find <TB_DATA_CARD>(TB_DATA_CARD._.CARD_ICCID == tB_DATA_CARD.CARD_ICCID || TB_DATA_CARD._.CARD_NO == tB_DATA_CARD.CARD_NO).CARD_ID; if (!String.IsNullOrEmpty(cardID)) { String end_customer_id = String.Empty; if (!String.IsNullOrEmpty(tB_DATA_CARD.END_CUSTOMER_ID.Trim())) { end_customer_id = endCustomData.Select().Where(s => s["END_CUSTOMER_NAME"].ToString() == tB_DATA_CARD.END_CUSTOMER_ID.Trim()).Select(s => s["END_CUSTOMER_ID"].ToString()).FirstOrDefault(); if (String.IsNullOrEmpty(end_customer_id)) { TB_DATA_END_CUSTOMER tB_DATA_END_CUSTOMER = new TB_DATA_END_CUSTOMER(); end_customer_id = Guid.NewGuid().ToString(); tB_DATA_END_CUSTOMER.END_CUSTOMER_ID = end_customer_id; tB_DATA_END_CUSTOMER.END_CUSTOMER_NAME = tB_DATA_CARD.END_CUSTOMER_ID.Trim(); tB_DATA_END_CUSTOMER.STATUS = 1; tB_DATA_END_CUSTOMER.CREATED_BY = tB_DATA_CARD.CREATED_BY; tB_DATA_END_CUSTOMER.CREATED_TIME = DateTime.Now; tB_DATA_END_CUSTOMER.LAST_UPDATED_BY = tB_DATA_CARD.CREATED_BY; tB_DATA_END_CUSTOMER.LAST_UPDATED_TIME = DateTime.Now; gateway.Save <TB_DATA_END_CUSTOMER>(tB_DATA_END_CUSTOMER, tran); } tB_DATA_CARD.END_CUSTOMER_ID = end_customer_id; } tB_DATA_CARD.Attach(); tB_DATA_CARD.SetModifiedProperties(new Dictionary <string, object> { { "END_CUSTOMER_ID", tB_DATA_CARD.END_CUSTOMER_ID }, { "STATUS", tB_DATA_CARD.STATUS }, { "SERVICE_PROVIDER", tB_DATA_CARD.SERVICE_PROVIDER }, { "REAL_NAME_PERSON", tB_DATA_CARD.REAL_NAME_PERSON }, { "REAL_NAME_ID_CARD", tB_DATA_CARD.REAL_NAME_ID_CARD }, { "LAST_UPDATED_BY", tB_DATA_CARD.LAST_UPDATED_BY }, { "LAST_UPDATED_TIME", tB_DATA_CARD.LAST_UPDATED_TIME } }); } gateway.Save <TB_DATA_CARD>(tB_DATA_CARD, tran); } tran.Commit(); return(1); } catch (Exception ex) { tran.Rollback(); Log.WriteLog(ex.Message); return(0); } finally { gateway.CloseTransaction(tran); tran.Dispose(); } }
/// <summary> /// 新增单条卡记录 /// </summary> /// <param name="tB_DATA_CARD"></param> /// <returns></returns> public int SaveCard(TB_DATA_CARD tB_DATA_CARD) { Gateway gateway = Gateway.Default; DbTransaction tran = gateway.BeginTransaction(); try { //取所有非删除的代理商 String agentSql = "SELECT [AGENT_ID],[AGENT_NAME] FROM [dbo].[TB_DATA_AGENT] WHERE STATUS>-1"; DataTable agentData = gateway.FromCustomSql(agentSql).SetTransaction(tran).ToDataSet().Tables[0]; //取所有非删除的终端用户 String endCustomSql = "select END_CUSTOMER_ID,END_CUSTOMER_NAME FROM [dbo].[TB_DATA_END_CUSTOMER] WHERE STATUS>-1"; DataTable endCustomData = gateway.FromCustomSql(endCustomSql).SetTransaction(tran).ToDataSet().Tables[0]; //取所有非删除的品牌信息 String brandSql = "select BRAND_ID,BRAND_NAME FROM [dbo].[TB_DATA_BRAND]"; DataTable brandData = gateway.FromCustomSql(brandSql).SetTransaction(tran).ToDataSet().Tables[0]; String agent_id = String.Empty, end_customer_id = String.Empty, brand_id = String.Empty; //获取导入的代理商 终端客户 品牌信息字段对应的ID,不存在就创建对应的信息 if (!String.IsNullOrEmpty(tB_DATA_CARD.AGENT_ID.Trim())) { agent_id = agentData.Select().Where(s => s["AGENT_NAME"].ToString() == tB_DATA_CARD.AGENT_ID.Trim()).Select(s => s["AGENT_ID"].ToString()).FirstOrDefault(); if (String.IsNullOrEmpty(agent_id)) { TB_DATA_AGENT tB_DATA_AGENT = new TB_DATA_AGENT(); agent_id = Guid.NewGuid().ToString(); tB_DATA_AGENT.AGENT_ID = agent_id; tB_DATA_AGENT.AGENT_NAME = tB_DATA_CARD.AGENT_ID.Trim(); tB_DATA_AGENT.STATUS = 1; tB_DATA_AGENT.CREATED_BY = tB_DATA_CARD.CREATED_BY; tB_DATA_AGENT.CREATED_TIME = DateTime.Now; tB_DATA_AGENT.LAST_UPDATED_BY = tB_DATA_CARD.CREATED_BY; tB_DATA_AGENT.LAST_UPDATED_TIME = DateTime.Now; gateway.Save <TB_DATA_AGENT>(tB_DATA_AGENT, tran); } tB_DATA_CARD.AGENT_ID = agent_id; } if (!String.IsNullOrEmpty(tB_DATA_CARD.END_CUSTOMER_ID.Trim())) { end_customer_id = endCustomData.Select().Where(s => s["END_CUSTOMER_NAME"].ToString() == tB_DATA_CARD.END_CUSTOMER_ID.Trim()).Select(s => s["END_CUSTOMER_ID"].ToString()).FirstOrDefault(); if (String.IsNullOrEmpty(end_customer_id)) { TB_DATA_END_CUSTOMER tB_DATA_END_CUSTOMER = new TB_DATA_END_CUSTOMER(); end_customer_id = Guid.NewGuid().ToString(); tB_DATA_END_CUSTOMER.END_CUSTOMER_ID = end_customer_id; tB_DATA_END_CUSTOMER.END_CUSTOMER_NAME = tB_DATA_CARD.END_CUSTOMER_ID.Trim(); tB_DATA_END_CUSTOMER.STATUS = 1; tB_DATA_END_CUSTOMER.CREATED_BY = tB_DATA_CARD.CREATED_BY; tB_DATA_END_CUSTOMER.CREATED_TIME = DateTime.Now; tB_DATA_END_CUSTOMER.LAST_UPDATED_BY = tB_DATA_CARD.CREATED_BY; tB_DATA_END_CUSTOMER.LAST_UPDATED_TIME = DateTime.Now; gateway.Save <TB_DATA_END_CUSTOMER>(tB_DATA_END_CUSTOMER, tran); } tB_DATA_CARD.END_CUSTOMER_ID = end_customer_id; } if (!String.IsNullOrEmpty(tB_DATA_CARD.BRAND_ID.Trim())) { brand_id = brandData.Select().Where(s => s["BRAND_NAME"].ToString() == tB_DATA_CARD.BRAND_ID.Trim()).Select(s => s["BRAND_ID"].ToString()).FirstOrDefault(); if (String.IsNullOrEmpty(brand_id)) { TB_DATA_BRAND tB_DATA_BRAND = new TB_DATA_BRAND(); brand_id = Guid.NewGuid().ToString(); tB_DATA_BRAND.BRAND_ID = brand_id; tB_DATA_BRAND.BRAND_NAME = tB_DATA_CARD.BRAND_ID.Trim(); tB_DATA_BRAND.CREATED_BY = tB_DATA_CARD.CREATED_BY; tB_DATA_BRAND.CREATED_TIME = DateTime.Now; tB_DATA_BRAND.LAST_UPDATED_BY = tB_DATA_CARD.CREATED_BY; tB_DATA_BRAND.LAST_UPDATED_TIME = DateTime.Now; gateway.Save <TB_DATA_BRAND>(tB_DATA_BRAND, tran); } tB_DATA_CARD.BRAND_ID = brand_id; } String cardID = gateway.Find <TB_DATA_CARD>(TB_DATA_CARD._.CARD_ICCID == tB_DATA_CARD.CARD_ICCID || TB_DATA_CARD._.CARD_NO == tB_DATA_CARD.CARD_NO).CARD_ID; if (!String.IsNullOrEmpty(cardID)) { tB_DATA_CARD.Attach(); tB_DATA_CARD.SetAllPropertiesAsModified(); } gateway.Save <TB_DATA_CARD>(tB_DATA_CARD, tran); String updateSql = @"update TB_DATA_AGENT set AGENT_CARD_NUMBER=T.card_count from ( select count(1) card_count,AGENT_ID from TB_DATA_CARD GROUP BY AGENT_ID ) T where TB_DATA_AGENT.AGENT_ID=T.AGENT_ID"; gateway.FromCustomSql(updateSql).SetTransaction(tran).ExecuteNonQuery(); tran.Commit(); return(1); } catch (Exception ex) { Log.WriteLog(ex.Message); return(0); } }