Пример #1
0
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                //首先核对用户的短信验证码是否合法
                using (var vCode = new SystemDbContext())
                {
                    var      CurrentUserCode = vCode.VerifyCodes.Find(model.PhoneNumber);
                    DateTime CurTime         = System.DateTime.Now;
                    if (CurTime > CurrentUserCode.OverTime)     //用户短信验证码超时
                    {
                        ModelState.AddModelError("", "抱歉,您的验证码已经过期!");
                        return(View(model));
                    }
                    else if (!CurrentUserCode.Code.Equals(model.Code))
                    {
                        ModelState.AddModelError("", "抱歉,您的验证码输入错误!");
                        return(View(model));
                    }
                }

                //验证企业全称是否已经被注册
                using (var vUser = new SystemDbContext())
                {
                    var tmp = vUser.Users.Where(p => p.CompanyFullName.Equals(model.CompanyFullName)).ToList();
                    if (tmp.Count != 0)
                    {
                        ModelState.AddModelError("", "抱歉,该企业全称已经被注册!");
                        return(View(model));
                    }
                }
                var user = new ApplicationUser
                {
                    CompanyFullName = model.CompanyFullName,
                    PhoneNumber     = model.PhoneNumber,

                    UserName    = model.PhoneNumber,
                    IsProved    = false,        //是否审核的标志
                    IsAvailable = true,         //是否是可用的管理员
                    IsRoot      = true          //注册企业号的人默认就是企业的超级管理员
                };
                //生成企业ID号
                user.CompanyId = Generate.GenerateCompanyId();
                string CompanyDbName = "Bonsaii" + user.CompanyId;
                user.ConnectionString = ConfigurationManager.AppSettings["UserDbConnectionString"] + CompanyDbName + ";";    //"Data Source = localhost,1433;Network Library = DBMSSOCN;Initial Catalog = " + CompanyDbName + ";User ID = test;Password = admin;";

                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    //添加注册的企业信息到Companies数据表当中
                    using (SystemDbContext sys = new SystemDbContext())
                    {
                        Company company = new Company()
                        {
                            CompanyId = user.CompanyId,
                            FullName  = user.CompanyFullName,
                            TelNumber = user.UserName,
                            UserName  = user.UserName
                        };
                        sys.Companies.Add(company);
                        sys.SaveChanges();
                    }

                    /**
                     * 注册成功并不会为企业创建独有的数据库,只有系统平台的超级管理员通过相应用户的审核之后才会为用户创建数据库
                     * */
                    //        await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);
                    // 有关如何启用帐户确认和密码重置的详细信息,请访问 http://go.microsoft.com/fwlink/?LinkID=320771
                    // 发送包含此链接的电子邮件
                    // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                    // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    // await UserManager.SendEmailAsync(user.Id, "确认你的帐户", "请通过单击 <a href=\"" + callbackUrl + "\">這裏</a>来确认你的帐户");
                    return(RedirectToAction("Login", "Account"));
                }
                AddErrors(result);
            }
            // 如果我们进行到这一步时某个地方出错,则重新显示表单
            return(View(model));
        }
Пример #2
0
        public ActionResult CreatePerson([Bind(Include = "Nian,Range,Week1,Week2,Week3,week4,Week5,Week6,Week7")] WeekTag weektag)
        {
            string date = weektag.Nian;
            var    year = (from wt in db.WeekTags
                           where wt.Nian == date && wt.Range == weektag.Range
                           select wt).ToList();

            if (year.Count() != 0)
            {
                //List<SelectListItem> departmentList = db.Departments.ToList().Select(d => new SelectListItem
                //{
                //    Value = d.DepartmentId,
                //    Text = d.Name,
                //}).ToList();
                //ViewBag.departmentList = departmentList;
                ModelState.AddModelError("", "本员工的该年日历已经存在!");

                return(View(weektag));
            }
            else
            {
                DateTime begindate = Convert.ToDateTime(date + "-01-01");
                //DateTime begindate = Convert.ToDateTime(date+"01-01");
                //DateTime enddate = Convert.ToDateTime(date+"12-31");
                //System.TimeSpan tsdiffer = enddate.Date - begindate.Date;
                // int intdiffer = tsdiffer.Days + 1;
                int intdiffer;
                if (DateTime.IsLeapYear(int.Parse(weektag.Nian)))
                {
                    intdiffer = 366;
                }
                else
                {
                    intdiffer = 365;
                }
                //生成对应人员或者部门或者公司的假日表
                string        tablename = Generate.GenerateTableName();
                var           flag      = CreateTableForHoliday(tablename, this.ConnectionString);
                HolidayRecord hr        = new HolidayRecord();
                hr.HolidayName = tablename;
                hr.Number      = weektag.Range;
                db.HolidayRecords.Add(hr);
                db.SaveChanges();
                //数据库连接
                SqlConnection conn = new SqlConnection(this.ConnectionString);
                conn.Open();
                List <DateTime> list = new List <DateTime>();
                for (int i = 0; i < intdiffer; i++)
                {
                    DateTime dttemp = begindate.Date.AddDays(i);

                    if ((dttemp.DayOfWeek == System.DayOfWeek.Monday && "1" == weektag.Week1) ||
                        (dttemp.DayOfWeek == System.DayOfWeek.Tuesday && "1" == weektag.Week2) ||
                        (dttemp.DayOfWeek == System.DayOfWeek.Wednesday && "1" == weektag.Week3) ||
                        (dttemp.DayOfWeek == System.DayOfWeek.Thursday && "1" == weektag.Week4) ||
                        (dttemp.DayOfWeek == System.DayOfWeek.Friday && "1" == weektag.Week5) ||
                        (dttemp.DayOfWeek == System.DayOfWeek.Saturday && "1" == weektag.Week6) ||
                        (dttemp.DayOfWeek == System.DayOfWeek.Sunday && "1" == weektag.Week7))
                    {
                        //插入数据
                        DataTable dtInsert = new DataTable();
                        dtInsert.Columns.Add("Recordtime", typeof(DateTime));
                        dtInsert.Columns.Add("Tag", typeof(string));
                        dtInsert.Columns.Add("Year", typeof(string));
                        dtInsert.Columns.Add("Month", typeof(string));
                        dtInsert.Columns.Add("Day", typeof(string));
                        dtInsert.Rows.Add(new object[] { dttemp, "1", dttemp.Year.ToString(), dttemp.Month.ToString(), dttemp.Day.ToString() });
                        string         sql = "insert into " + tablename + "(Recordtime,Tag,Year,Month,Day) values(@Recordtime,@Tag,@Year,@Month,@Day)";
                        SqlDataAdapter dr  = new SqlDataAdapter();                                 //上面两句可以合并成这一
                        dr.InsertCommand = new SqlCommand(sql, conn);
                        dr.SelectCommand = new SqlCommand("select * from Staffs where 1=0", conn); //仅为了获得框架
                        dr.InsertCommand.Parameters.Add("Recordtime", SqlDbType.DateTime);
                        dr.InsertCommand.Parameters.Add("Tag", SqlDbType.VarChar);
                        dr.InsertCommand.Parameters.Add("Year", SqlDbType.VarChar);
                        dr.InsertCommand.Parameters.Add("Month", SqlDbType.VarChar);
                        dr.InsertCommand.Parameters.Add("Day", SqlDbType.VarChar);

                        dr.InsertCommand.Parameters["Recordtime"].SourceColumn = "Recordtime";
                        dr.InsertCommand.Parameters["Tag"].SourceColumn        = "Tag";
                        dr.InsertCommand.Parameters["Year"].SourceColumn       = "Year";
                        dr.InsertCommand.Parameters["Month"].SourceColumn      = "Month";
                        dr.InsertCommand.Parameters["Day"].SourceColumn        = "Day";
                        dr.InsertCommand.UpdatedRowSource = UpdateRowSource.None;

                        if (dtInsert.Rows.Count > 0)
                        {
                            dr.Update(dtInsert);
                        }
                        //RecordDatetime temp = new RecordDatetime();
                        //temp.Recordtime = dttemp;
                        //temp.Tag = "1";
                        //temp.Year = dttemp.Year.ToString();
                        //temp.Month = dttemp.Month.ToString();
                        //temp.Day = dttemp.Day.ToString();
                        //db.RecordDatetimes.Add(temp);
                        //db.SaveChanges();
                    }
                    else
                    {
                        //插入数据
                        DataTable dtInsert = new DataTable();
                        dtInsert.Columns.Add("Recordtime", typeof(DateTime));
                        dtInsert.Columns.Add("Tag", typeof(string));
                        dtInsert.Columns.Add("Year", typeof(string));
                        dtInsert.Columns.Add("Month", typeof(string));
                        dtInsert.Columns.Add("Day", typeof(string));
                        dtInsert.Rows.Add(new object[] { dttemp, "0", dttemp.Year.ToString(), dttemp.Month.ToString(), dttemp.Day.ToString() });
                        string         sql = "insert into " + tablename + "(Recordtime,Tag,Year,Month,Day) values(@Recordtime,@Tag,@Year,@Month,@Day)";
                        SqlDataAdapter dr  = new SqlDataAdapter();                                 //上面两句可以合并成这一
                        dr.InsertCommand = new SqlCommand(sql, conn);
                        dr.SelectCommand = new SqlCommand("select * from Staffs where 1=0", conn); //仅为了获得框架
                        dr.InsertCommand.Parameters.Add("Recordtime", SqlDbType.DateTime);
                        dr.InsertCommand.Parameters.Add("Tag", SqlDbType.VarChar);
                        dr.InsertCommand.Parameters.Add("Year", SqlDbType.VarChar);
                        dr.InsertCommand.Parameters.Add("Month", SqlDbType.VarChar);
                        dr.InsertCommand.Parameters.Add("Day", SqlDbType.VarChar);

                        dr.InsertCommand.Parameters["Recordtime"].SourceColumn = "Recordtime";
                        dr.InsertCommand.Parameters["Tag"].SourceColumn        = "Tag";
                        dr.InsertCommand.Parameters["Year"].SourceColumn       = "Year";
                        dr.InsertCommand.Parameters["Month"].SourceColumn      = "Month";
                        dr.InsertCommand.Parameters["Day"].SourceColumn        = "Day";
                        dr.InsertCommand.UpdatedRowSource = UpdateRowSource.None;

                        if (dtInsert.Rows.Count > 0)
                        {
                            dr.Update(dtInsert);
                        }
                    }
                }
                db.WeekTags.Add(weektag);
                db.SaveChanges();

                return(RedirectToAction("Index"));
            }
        }