Ejemplo n.º 1
0
        public object InsertTemporaryPeople([FromBody] JObject jObject)
        {
            var userService = new UserBLL();

            try
            {
                string res = jObject.Value <string>("json");
                var    dy  = JsonConvert.DeserializeAnonymousType(res, new
                {
                    userid = string.Empty,
                    data   = new List <TemporaryPeopleParameter>()
                });
                UserEntity createUser = userService.GetEntity(dy.userid);;
                if (createUser == null)
                {
                    throw new Exception("你没有权限添加权限");
                }

                List <UserEntity> successList = new List <UserEntity>(); //要新增到数据库里的数据
                List <object>     badList     = new List <object>();     //有问题的数据
                foreach (TemporaryPeopleParameter item in dy.data)
                {
                    try
                    {
                        if (!string.IsNullOrWhiteSpace(item.Mobile) && !userService.ExistMoblie(item.Mobile))
                        {
                            //校验手机号是否重复,重复该条数据不让添加
                            badList.Add(new { data = item, msg = "手机号重复" });
                            continue;
                        }
                        //如果账号存在则再生成一个,直到没有重复的为止
                        string pyStr   = Str.PinYin(item.RealName);
                        string account = pyStr;
                        int    count   = 1;
                        while (!userService.ExistAccount(account))
                        {
                            account = pyStr + GetNumStr(count);
                            count++;
                        }

                        UserEntity user = new UserEntity();
                        user.UserId             = Guid.NewGuid().ToString();
                        user.RealName           = item.RealName;
                        user.IdentifyID         = item.IdentifyID;
                        user.Mobile             = item.Mobile;
                        user.Account            = account;
                        user.IdentifyID         = item.IdentifyID;
                        user.IsEpiboly          = "1";
                        user.Gender             = "男";
                        user.OrganizeId         = createUser.OrganizeId;
                        user.OrganizeCode       = createUser.OrganizeCode;
                        user.CreateUserOrgCode  = createUser.OrganizeCode;
                        user.CreateDate         = DateTime.Now;
                        user.CreateUserDeptCode = createUser.DepartmentCode;
                        user.CreateUserId       = createUser.UserId;
                        user.CreateUserName     = createUser.RealName;
                        user.DepartmentId       = createUser.DepartmentId;
                        user.DepartmentCode     = createUser.DepartmentCode;
                        user.Password           = "******";//默认密码123456
                        user.IsPresence         = "1";

                        //岗位随机分配一个本班组下没有负责人角色的岗位
                        IEnumerable <RoleEntity> rlist = new JobBLL().GetList().Where(p => p.DeptId == createUser.DepartmentId && !p.RoleIds.Contains("27eb996b-1294-41d6-b8e6-837645a66819"));
                        if (rlist != null && rlist.Count() > 0)
                        {
                            var defaultRole = rlist.FirstOrDefault();
                            user.DutyId   = defaultRole.RoleId;
                            user.DutyName = defaultRole.FullName;
                        }
                        //	职务:默认为编码管理中排序为最后一个的职务
                        var defaultJob = new JobBLL().GetList().Where(p => p.OrganizeId == createUser.OrganizeId).OrderByDescending(x => x.SortCode).FirstOrDefault();
                        if (defaultJob != null)
                        {
                            user.PostName = defaultJob.FullName;
                            user.PostId   = defaultJob.RoleId;
                            user.PostCode = defaultJob.EnCode;
                        }
                        //角色,默认班组级用户
                        RoleEntity roleEntity = new RoleCache().GetList().Where(a => a.OrganizeId == createUser.OrganizeId || string.IsNullOrWhiteSpace(a.OrganizeId)).Where(p => p.FullName.Contains("班组级用户")).FirstOrDefault();
                        if (roleEntity != null)
                        {
                            user.RoleId = roleEntity.RoleId;
                        }
                        user.RoleName = roleEntity.FullName;
                        roleEntity    = new RoleCache().GetList().Where(a => a.OrganizeId == createUser.OrganizeId || string.IsNullOrWhiteSpace(a.OrganizeId)).Where(p => p.FullName.Contains("普通用户")).FirstOrDefault();
                        if (roleEntity != null)
                        {
                            if (!string.IsNullOrEmpty(roleEntity.RoleId))
                            {
                                user.RoleId   += "," + roleEntity.RoleId;
                                user.RoleName += "," + roleEntity.FullName;
                            }
                            else
                            {
                                user.RoleId  += roleEntity.RoleId;
                                user.RoleName = roleEntity.FullName;
                            }
                        }
                        string objId = userService.SaveForm(user.UserId, user, 0);
                        if (!string.IsNullOrWhiteSpace(objId))
                        {
                            //不为空则添加成功
                            successList.Add(user);
                            if (!string.IsNullOrWhiteSpace(new DataItemDetailBLL().GetItemValue("bzAppUrl")))
                            {
                                user.Password = "******";
                                var task = Task.Factory.StartNew(() =>
                                {
                                    SaveUser(user);
                                });
                            }
                        }
                        else
                        {
                            badList.Add(new { data = item, msg = "添加失败" });
                        }
                    }
                    catch (Exception itemEx)
                    {
                        badList.Add(new { data = item, msg = itemEx.Message });
                    }
                }

                return(new { Code = 0, Info = "操作成功", data = successList.Select(p => new { p.RealName, p.Account, Password = "******" }) });
            }
            catch (Exception ex)
            {
                return(new { Code = -1, info = "操作失败", data = ex.Message });
            }
        }