/// <summary> ///根据Id更新指定字段 /// </summary> /// <param name="user">主键Id,更新字段值</param> /// <param name="attr">更新字段 数组</param> /// <returns></returns> public int Update(T_Agent_Relation user, string[] attr) { if (attr == null || attr.Length <= 0) { return(0); } var sb = new StringBuilder(); sb.Append(" update T_Agent_Relation "); for (int i = 0; i < attr.Length; i++) { if (i == 0) { sb.Append(" set " + attr[i] + " = @" + attr[i]); } else { sb.Append(" ," + attr[i] + "=@" + attr[i]); } } sb.Append(" where userId = @userId"); using (IDbConnection conn = DapperAdapter.MySQLOpenConnection(ConfigurationHelper.MySQLConnectionStr)) { return(conn.Execute(sb.ToString(), user)); } }
/// <summary> /// 获取用户的 所有上级代理 /// </summary> /// <param name="userid"></param> /// <returns></returns> public ActionResult GetHightAgentList(int userid) { T_Agent_Relation model = this._agent.GetAgent(userid); if (model != null) { if (!string.IsNullOrWhiteSpace(model.agentUsers)) { Dictionary <int, int> dic = new Dictionary <int, int>(); List <int> ids = new List <int>(); foreach (var item in model.agentUsers.Split(',')) { string[] id_Index_s = item.Split('_'); int ID = Convert.ToInt32(id_Index_s[0]); int index = Convert.ToInt32(id_Index_s[1]); dic[ID] = index; ids.Add(ID); } List <T_User> userList = this._user.GetAgentList(ids.ToArray()); for (int i = 0; i < userList.Count; i++) { userList[i].applyClubCount = dic[userList[i].userId]; } userList = userList.OrderBy(w => w.applyClubCount).ToList(); return(Success(userList)); } } return(Success("")); }
/// <summary> /// 设置用户为代理 /// </summary> /// <param name="userid"></param> /// <returns></returns> public bool SetUserforAgent(int userid) { //获取用户信息,是否有推荐人 T_User userModel = userDal.GetUserInfo(userid.ToString()); if (userModel.isProxy == 1) //已经是代理 { return(false); } T_Agent_Relation model = new T_Agent_Relation(); model.createTime = model.updateTime = DateTime.Now; model.userId = userid; model.inviteUserId = userModel.inviteCode; if (userModel.inviteCode != 0)//被邀请 { T_User hModel = GetUpAgents(userModel.inviteCode); if (hModel != null && hModel.isProxy == 1) { model.higherAgent = hModel.userId; string array = ""; if (hModel.isProxy == 1) //上级是代理 { T_Agent_Relation agentRelation = GetAgent(hModel.userId); if (!string.IsNullOrWhiteSpace(agentRelation.agentUsers)) // 上级代理 是否有 上上级 { string[] ID_Index_Array = agentRelation.agentUsers.Split(','); if (ID_Index_Array.Length > 0) { int index = Convert.ToInt32(ID_Index_Array[0].Split('_')[1]); array = hModel.userId.ToString() + '_' + (index + 1) + ',' + agentRelation.agentUsers; } model.agentUsers = array.TrimEnd(','); } else //无代理关系 ,将上级写入代理序列中 { model.agentUsers = hModel.userId + "_1"; } } } } if (Insert(model) > 0) { userDal.UpdateIsProxy(model.userId, true); return(true); } else { return(false); } }
/// <summary> /// 新增代理 /// </summary> /// <param name="model"></param> /// <returns></returns> private int Insert(T_Agent_Relation model) { using (IDbConnection conn = DapperAdapter.MySQLOpenConnection(ConfigurationHelper.MySQLConnectionStr)) { return(conn.Execute(@"insert into T_Agent_Relation(userId,higherAgent,inviteUserId,agentUsers,createTime,updateTime) VALUES (@userId,@higherAgent,@inviteUserId,@agentUsers,@createTime,@updateTime);" , model)); } }
/// <summary> /// 重置下级代理 /// </summary> /// <param name="model"></param> /// <returns></returns> private void ResetAgent(T_Agent_Relation model, int removerAgentUserID) { List <T_Agent_Relation> lowerAgentList = GetAgentForhigherAgent(model.userId); for (int i = 0; i < lowerAgentList.Count; i++) { lowerAgentList[i].agentUsers = ResetDataModel(lowerAgentList[i].agentUsers, removerAgentUserID); int result = Update(lowerAgentList[i], new string[] { "agentUsers" }); if (result > 0) { ResetAgent(lowerAgentList[i], removerAgentUserID); } } }
/// <summary> /// 修改代理状态 /// </summary> /// <param name="keyValue"></param> /// <param name="status"></param> /// <returns></returns> public ActionResult UpdateState(int keyValue, int status) { if (keyValue > 0) { T_Agent_Relation model = new T_Agent_Relation(); model.userId = keyValue; model.status = status; int result = this._agent.Update(model, new string[] { "status" }); return(Response(result)); } else { return(Fail("错误:未知记录ID")); } }
/// <summary> /// 取消代理 /// </summary> /// <param name="userid"></param> /// <returns></returns> public bool SetAgentForUser(int userid) { /* * 1.删除玩家代理的记录 * 2.修改下级绑定到上级代理 A->B->C ==> A->C * 2.修改下级玩家 代理列表缓存模型 ->ID_index,ID_index * 2.1 查找 下级玩家组 */ T_Agent_Relation beseAgent = GetAgent(userid); //当前代理数据 if (DelAgentForUserID(userid)) { userDal.UpdateIsProxy(userid, false); int resultUpdate = 0; List <T_Agent_Relation> lowerAgentList = GetAgentForhigherAgent(userid); // 2.1 #region 直属下级代理模型数据更新 foreach (var lowerAgent in lowerAgentList) //遍历 { if (lowerAgent != null) { lowerAgent.higherAgent = beseAgent.higherAgent; //C代理的上级代理 绑定到 A代理 if (!string.IsNullOrWhiteSpace(lowerAgent.agentUsers)) { lowerAgent.agentUsers = ResetDataModel(lowerAgent.agentUsers, userid); resultUpdate += Update(lowerAgent, new string[] { "higherAgent", "agentUsers" }); //更新直属下级玩家 } } } #endregion //遍历 更新 下级代理的子代理数据模型 // C_3,B_2,A_1 ==> C_2,A_1 if (resultUpdate == lowerAgentList.Count) { foreach (var lowerAgent in lowerAgentList) { ResetAgent(lowerAgent, userid); } } return(true); } return(false); }