public ActionResult <ResponseJson> Post([FromBody] Dtos.XsBj model) { Service <jw_xs_bj> xbService = new Service <jw_xs_bj>(); ResponseJson json = new ResponseJson(); try { service.Context.Db.BeginTran(); model.Xs.joinTime = DateTime.Now; long xid = service.Context.Db.Insertable(model.Xs).ExecuteReturnIdentity(); if (xid > 0) { foreach (var bjid in model.Bjs) { xbService.Insert(new jw_xs_bj { xsid = xid, bjid = bjid }); } } service.Context.Db.CommitTran(); } catch { json.Code = "500"; json.Msg = "新建账号失败!"; service.Context.Db.RollbackTran(); } return(json); }
public ActionResult <ResponseJson> Put(int id, [FromBody] Dtos.XsBj model) { Service <jw_xs_bj> xbService = new Service <jw_xs_bj>(); ResponseJson json = new ResponseJson(); List <dynamic> ids = new List <dynamic>(); try { var haved = xbService.GetList(o => o.xsid == id).ToList(); service.Context.Db.BeginTran(); //修改用户信息 model.Xs.id = id; service.Update(model.Xs); //删除用户角色 foreach (var t in haved.Where(o => !model.Bjs.Contains(o.bjid))) { ids.Add(t.id); } xbService.DeleteList(ids); //增加用户角色 var tids = haved.Select(w => w.bjid).Distinct().ToList(); foreach (var t in model.Bjs.Where(o => !tids.Contains(o))) { { xbService.Insert(new jw_xs_bj { xsid = id, bjid = t }); } service.Context.Db.CommitTran(); } } catch { json.Code = "500"; json.Msg = "新建账号失败!"; } return(json); }