zoukankan      html  css  js  c++  java
  • CYQ MAction,子表 新增,删除,修改 集合

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using ZDWorkflow.Model;
    using ZDWorkflow.Common;
    using ZDWorkflow.Common.Extensions;
    using CYQ.Data;
    using CYQ.Data.Orm;
    using CYQ.Data.Table;
    
    namespace ZDWorkflow.BLL
    {
    /// <summary>
    /// GroupRole(角色组与角色关系表)
    /// </summary>
    public class GroupRoleBLL
    {
    /// <summary>
    /// 分组列表操作角色
    /// </summary>
    /// <param name="jsonRoles"></param>
    /// <param name="user"></param>
    /// <returns></returns>
    public ResultModel EditGroupRole(string jsonRoles, long GroupId, LoginUserInfo user)
    {
    ResultModel result = new ResultModel();
    using (MAction mc = new MAction(TableNames.GroupRole))
    {
    mc.BeginTransation();
    try
    {
    //传到过来
    List<GroupRole> grlistnew = new List<GroupRole>();
    grlistnew = jsonRoles.ToObject<List<GroupRole>>();
    // List<long> idsnew = new List<long>();
    var idsnew = grlistnew.Select(s => s.RoleId).ToList();
    
     
    
    //原数据库
    List<GroupRole> grlistold = new List<GroupRole>();
    MDataTable dt = mc.Select("GroupId=" + GroupId);
    grlistold = dt.ToList<GroupRole>();
    //List<long> idsold = new List<long>();
    var idsold = grlistold.Select(s => s.RoleId).ToList();
    
    #region 注释
    //foreach (GroupRole item in grlistnew) {
    
    // GroupRole ditem = grlistold.FirstOrDefault(s => s.RoleId == item.RoleId);
    // if (ditem != null)
    // {
    // //新增
    // }
    // else
    // {
    // //修改
    // item.GroupId = GroupId;
    // }
    //}
    //删除
    //var del= idsold.Where(w => !idsnew.Contains(w)).ToList();
    #endregion
    var a = idsnew.Intersect(idsold); // to modify
    var b = idsnew.Except(idsold); // to add
    var c = idsold.Except(idsnew); // to delete
    List<GroupRole> toAddList = new List<GroupRole>();
    List<GroupRole> toDeleteList = new List<GroupRole>();
    List<GroupRole> toModifyList = new List<GroupRole>();
    // set addList
    foreach (var bitem in b)
    {
    var bModel = grlistnew.Find(p => p.RoleId == bitem);
    bModel.GroupId = GroupId;
    toAddList.Add(bModel);
    }
    //toAddList = grlistnew.Where(w => b.Contains(w.RoleId)).ToList();
    // set delete querymodel
    toDeleteList = grlistold.Where(w => c.Contains(w.RoleId)).ToList();
    // set modify querymodel
    toModifyList = grlistold.Where(w => a.Contains(w.RoleId)).ToList();
    
    //新增
    mc.Data.LoadFrom(toAddList);
    mc.Insert();
    
    //删除
    foreach (GroupRole item in toDeleteList)
    {
    mc.SetPara("GroupId", item.GroupId);
    mc.SetPara("RoleId", item.RoleId);
    mc.Delete("GroupId=@GroupId and RoleId=@RoleId");
    }
    mc.EndTransation();
    result.Status = (int)EStatus.Success;
    result.Msg = "提交成功!";
    }
    catch (Exception ex)
    {
    mc.RollBack();
    result = ex;
    }
    }
    return result;
    }
    }
    }
  • 相关阅读:
    某个牛人做WINDOWS系统文件详解
    常用ASP脚本程序集锦
    LINUX基础:文件安全与权限
    proftpd+mysql+quota
    apache2.0.49tomcat5.0.19jk2建立virtualHost
    URL Redirection(转) Anny
    顶级域名后缀列表(转) Anny
    \u4E00\u9FA5意义 Anny
    How to POST Form Data Using Ruby(转) Anny
    How to get rid of 'Enter password to unlock your login keyring' in Ubuntu(转) Anny
  • 原文地址:https://www.cnblogs.com/chxl800/p/6323418.html
Copyright © 2011-2022 走看看