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;
}
}
}

 

posted @ 2017-01-20 17:25  cclon  阅读(1293)  评论(0编辑  收藏  举报