Exemplo n.º 1
0
        public ApiResponse ImportUser()
        {
            string uploadPath = Path.Combine(
                PublicFunc.GetDeployDirectory(),
                PublicFunc.GetConfigByKey_AppSettings("Upload_Path"));
            HttpPostedFile file = HttpContext.Current.Request.Files[0];
            string strPath = Path.Combine(uploadPath, file.FileName);
            if (!Directory.Exists(uploadPath))
            {
                Directory.CreateDirectory(uploadPath);
            }
            file.SaveAs(strPath);

            var model = new TramUserImportListModel()
            {
                Lists = new List<TeamUserImportModel>(),
                User = new UserInfo()
                {
                    //上传文件时,body为空,在ApiJsonMediaTypeFormatter中ReadFromStreamAsync时要出错
                    //在解决该问题之前,现在这里设置一下登录用户的UserID
                    UserID = "0"//ActionContext.Request.Content.Headers.GetValues("UserID").FirstOrDefault()
                }
            };
            var list = model.Lists;

            var ds = Utility.ExcelToDataSet(strPath, "select * from [Sheet1$]");
            if (ds != null && ds.Tables[0] != null)
            {
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    list.Add(new TeamUserImportModel
                                {
                                    TeamName = row[0].ToString(),
                                    UserID = row[1].ToString(),
                                    UserName = row[2].ToString()
                                });
                }
            }

            if (list.Count > 0)
            {
                userService.ImportTeamUser(model);
            }
            if (File.Exists(strPath))
            {
                File.Delete(strPath);
            }
            return ApiOk(list);
        }
Exemplo n.º 2
0
        public void ImportTeamUser(TramUserImportListModel data)
        {
            if (data == null)
            {
                throw new BusinessException("无数据");
            }

            string sqlStr = @" DELETE FROM dbo.Team;
                                DELETE A FROM dbo.RoleUser A INNER JOIN dbo.[User] B ON A.UserID = B.UserID WHERE B.UserType = 0;
                                    DELETE FROM dbo.[User] WHERE UserType = 0;
                                    DELETE FROM dbo.UserTeam;";
            adonetWrapper.ExecuteSqlCommand(sqlStr);
            string pwd = PublicFunc.GetConfigByKey_AppSettings("DefaultPWD");

            DateTime now = DateTime.Now;
            int studentRoleSysNo = int.Parse(PublicFunc.GetConfigByKey_AppSettings("StudentRoleSysNo"));
            IEnumerable<IGrouping<string, TeamUserImportModel>> groupList = data.Lists.GroupBy(m => m.TeamName);
            foreach (var group in groupList)
            {
                teamRepo.Insert(new Team { TeamName = group.Key, InDate = now, InUser = data.User.UserID });
                foreach (TeamUserImportModel user in group.ToList())
                {
                    userRepo.Insert(new User
                    {
                        UserID = user.UserID,
                        UserName = user.UserName,
                        Password = pwd,
                        Status = 1,
                        InUser = data.User.UserID,
                        InDate = now
                    });
                    userTeamRepo.Insert(new UserTeam
                    {
                        TeamName = user.TeamName,
                        UserID = user.UserID,
                        InDate = now,
                        InUser = data.User.UserID
                    });

                    roleUserRepo.Insert(new RoleUser()
                    {
                        InDate = now,
                        InUser = data.User.UserID,
                        Status = 1,
                        UserID = user.UserID,
                        RoleSysNo = studentRoleSysNo
                    });
                }
            }
        }