예제 #1
0
        public ActionResult AddMaster(string DepId, string UsersId)
        {
            short delNormal = (short)Model.Enum.DelFlagEnum.Normal;
            int   idDept    = int.Parse(DepId);

            string[]   idStrs   = UsersId.Split(',');
            Department deptInfo = DepartmentService.LoadEntities(u => u.ID == idDept && u.DelFlag == delNormal).FirstOrDefault();

            if (deptInfo == null)
            {
                return(Content("系统运行出错,错误信息为:部门为空!"));
            }
            foreach (var idStr in idStrs)
            {
                int      userId   = int.Parse(idStr);
                UserInfo userInfo = UserInfoService.LoadEntities(u => u.ID == userId && u.DelFlag == delNormal).FirstOrDefault();
                //先检查系统中是否已经有这条记录
                Master masterExit = MasterService.LoadEntities(u => u.UserInfo == userInfo && u.Department == deptInfo).FirstOrDefault();
                if (masterExit != null)
                {
                    if (masterExit.DelFlag != delNormal)
                    {
                        masterExit.DelFlag = delNormal;
                        MasterService.Update(masterExit);
                        continue;
                    }
                    else
                    {
                        continue;
                    }
                }
                Master master = new Master();
                master.Department = deptInfo;
                master.UserInfo   = userInfo;
                master.DelFlag    = delNormal;
                master.Code       = deptInfo.Code + "-" + userInfo.Code;
                master.Name       = deptInfo.Name + "-" + userInfo.Name;
                MasterService.Add(master);
            }
            if (MasterService.SaveChanges() > 0)
            {
                return(Content("ok"));
            }
            else
            {
                return(Content("系统运行出错!错误信息为:保存出错。"));
            }
        }