/// <summary>
 /// 更新用户
 /// </summary>
 /// <param name="users"></param>
 /// <returns></returns>
 public bool UpdateUsers(UsersContract usersContract)
 {
     Users users = usersContract.ToPO<Users>();
       return usersRepository.UpdateUsers(users: users);
 }
 /// <summary>
 /// 添加用户
 /// </summary>
 /// <param name="users"></param>
 /// <returns></returns>
 public DataSet InsertUsers(UsersContract usersContract)
 {
     Users users = usersContract.ToPO<Users>();
       return usersRepository.InsertUsers(users: users);
 }
        /// <summary>
        /// 添加或者更新用户信息
        /// </summary>
        /// <param name="tag"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult AddOrUpdateUser(string id)
        {
            UsersContract usersContract = new UsersContract();
            if (!string.IsNullOrEmpty(id))
            {
                usersContract = usersServices.GetUserEntity(id);
            }
            //查询当前用户所在部门ID
            string branchId = employeeService.GetPositionAndBranchByEmpId(usersContract.EmployeeID).Tables[0].Rows.Count == 0 ? "0" : employeeService.GetPositionAndBranchByEmpId(usersContract.EmployeeID).Tables[0].Rows[0][1].ToString();
            //所有部门

            #region 绑定

            List<BranchInfoContract> list = new List<BranchInfoContract>();
            DataSet dsBranch = branchService.FindIsFBranch(50);
            for (int i = 0; i < dsBranch.Tables[0].Rows.Count; i++)
            {
                BranchInfoContract branch = new BranchInfoContract();
                branch.BranchID = Convert.ToInt32(dsBranch.Tables[0].Rows[i]["BranchID"].ToString());
                branch.BranchName = "├-" + CommonHelper.getListName(dsBranch.Tables[0].Rows[i]["BranchName"].ToString(), dsBranch.Tables[0].Rows[i]["Temp1"].ToString());

                list.Add(branch);
            }

            ViewData["Branch"] = new SelectList(list, "BranchID", "BranchName", branchId);

            #endregion 绑定

            if (!string.IsNullOrEmpty(id))
            {
                //获取当前部门下所有的员工
                ViewData["empInfo"] = new SelectList(employeeService.GetEmployeeByBId(branchId), "EId", "EmpName", usersContract.EmployeeID);
            }
            ViewData["role"] = new SelectList(rolesServices.GetRoles(), "RoleID", "RoleName", usersContract.RoleID);

            return View(usersContract);
        }
 public ActionResult AddOrUpdateUser(UsersContract usersContract)
 {
     if (ModelState.IsValid)
     {
         bool boo = false;
         if (usersContract.UserID == 0)
         {
             //检查用户帐号是否重复
             if (!usersServices.IsExists(usersContract.UserID.ToString(), usersContract.UserName))
             {
                 //执行成功信息
                 ViewData["msg"] = "用户帐户已经重复,请重新输入新的帐户保证正常运行!";
                 //成功视图
                 return View("Error");
             }
             usersContract.CreateTime = DateTime.Now;
             DataSet ds = usersServices.InsertUsers(usersContract: usersContract);
             if (ds.Tables[0].Rows.Count != 0)
             {
                 //执行成功信息
                 ViewData["msg"] = string.Format("添加用户成功! 用户名:{0}  密码:{1}", ds.Tables[0].Rows[0][0].ToString(), ds.Tables[0].Rows[0][1].ToString());
                 //成功后要跳转的路径(如果无须跳转,直接留在当前页面,不写即可)
                 ViewData["url"] = new UrlHelper(Request.RequestContext).Action("UsersList");
                 //成功视图
                 return View("Success");
             }
             else
             {
                 //执行成功信息
                 ViewData["msg"] = "添加用户失败!";
                 //成功视图
                 return View("Error");
             }
         }
         else
         {
             boo = usersServices.UpdateUsers(usersContract: usersContract);
         }
         if (boo)
         {
             //执行成功信息
             ViewData["msg"] = "更新用户成功! ";
             //成功后要跳转的路径(如果无须跳转,直接留在当前页面,不写即可)
             ViewData["url"] = new UrlHelper(Request.RequestContext).Action("UsersList");
             //成功视图
             return View("Success");
         }
     }
     ViewData["role"] = new SelectList(rolesServices.GetRoles(), "RoleID", "RoleName");
     ViewData["Branch"] = new SelectList(branchService.GetAllBranch(), "BranchId", "BranchName");
     ViewData["empInfo"] = new SelectList(employeeService.GetAllEmpInfo(), "EmpId", "EmpName", usersContract.EmployeeID);
     return View(usersContract);
 }
        public ActionResult Login(FormCollection formCollection, UsersContract user)
        {
            if (!ModelState.IsValid)
            {
                ViewData["msg"] = "用户名或者密码错误";
                return View("Error");
            }
            //客户端IP
            string clientIp;
            //获取登录数据
            string userLogin = user.UserName.Trim();
            string userPwd = WeiXin.Core.SecurityEncryption.DESEncrypt(user.Password.Trim());

            //查询数据
            UsersContract usersContract = usersServices.GetUserEntity(userLogin: userLogin, userPwd: userPwd);
            if (usersContract != null)
            {
                //记录Session
                Session["user"] = usersContract;
                //创建票据
                FormsAuthentication.SetAuthCookie(userLogin, true);
                //记录数据用户登录日志

                #region 获得IP地址

                if (Request.ServerVariables["HTTP_VIA"] != null)
                {
                    clientIp = Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
                }
                else
                {
                    clientIp = Request.ServerVariables["REMOTE_ADDR"].ToString();
                }

                #endregion 获得IP地址

                #region 记录登录信息

                loginLogInfoContract loginLogInfoContract = new loginLogInfoContract();
                loginLogInfoContract.UserId = usersContract.UserID;
                loginLogInfoContract.LIP = clientIp;
                loginLogInfoContract.LState = 0;
                loginLogInfoContract.LTime = DateTime.Now.ToString();
                loginLogInfoServices.InsertloginLogInfo(loginLogInfoContract);

                #endregion 记录登录信息

                #region 生成XML文件

                CreateAuthorityXML();

                #region 每日新增订阅人数 每日接收消息数
                // 生成XML
                string set = "";
                string Hd = "";
                DateTime time = DateTime.Now.AddDays(-7);
                for (int i = 0; i < 7; i++)
                {
                    string NowDate = time.ToString("yyyy-MM-dd");
                    //查询企业1天的粉丝增长数据/减少数据/互动数据
                    DataSet ds = fansBll.GetDataInfo(usersContract.EId, NowDate);
                    #region 每日新增订阅人数

                    int jnum = 0;
                    int num = 0;
                    if (ds.Tables[0].Rows.Count == 1)
                    {
                        num = int.Parse(ds.Tables[0].Rows[0]["num"].ToString());
                        if (ds.Tables[1].Rows.Count == 1)
                        {
                            //粉丝有减少
                            jnum = int.Parse(ds.Tables[1].Rows[0]["num"].ToString());
                            num = num - jnum;
                        }

                    }
                    else
                    {

                        if (ds.Tables[1].Rows.Count == 1)
                        {
                            //粉丝有减少
                            jnum = int.Parse(ds.Tables[1].Rows[0]["num"].ToString());
                            num = num - jnum;
                        }

                    }
                    if (num < 0)
                        set += "<set name='" + DateTime.Parse(NowDate).ToString("MM-dd") + "' value='" + num + "' color='FF0000' />";
                    else
                        set += "<set name='" + DateTime.Parse(NowDate).ToString("MM-dd") + "' value='" + num + "' color='AFD8F8' />";

                    #endregion
                    #region 每日接收消息数
                    if (ds.Tables[2].Rows.Count == 1)
                    {
                        int num2 = int.Parse(ds.Tables[2].Rows[0]["num"].ToString());
                        //当天有消息
                        Hd += "<set name='" + DateTime.Parse(NowDate).ToString("MM-dd") + "' value='" + num2 + "' color='AFD8F8' />";
                    }
                    else
                    {
                        Hd += "<set name='" + DateTime.Parse(NowDate).ToString("MM-dd") + "' value='0' color='AFD8F8' />";
                    }
                    #endregion

                    time = time.AddDays(1);
                }
                #endregion
                #region 生成每日新增订阅人数 XML文件

                string xml = "<graph caption='' bgcolor='FAFAFA' canvasbgcolor='FAFAFA' xAxisName='日期' yAxisName='数量' showNames='1' decimalPrecision='1' formatNumberScale='0'>" + set + "</graph>";
                //生成文件
                //文件路径
                string filePath = "/DataXml/" + usersContract.wxOldUser + ".xml";

                System.IO.StreamWriter sw = new System.IO.StreamWriter(Request.PhysicalApplicationPath + filePath, false, System.Text.Encoding.UTF8);
                sw.WriteLine(xml);
                sw.Close();
                sw.Dispose();
                #endregion
                #region 生成每日接受消息数量

                string hdxml = "<graph caption='' bgcolor='FAFAFA' canvasbgcolor='FAFAFA' xAxisName='日期' yAxisName='数量' showNames='1' decimalPrecision='1' formatNumberScale='0'>" + Hd + "</graph>";
                //生成文件
                //文件路径
                filePath = "/DataXml/" + usersContract.wxOldUser + "_2.xml";

                sw = new System.IO.StreamWriter(Request.PhysicalApplicationPath + filePath, false, System.Text.Encoding.UTF8);
                sw.WriteLine(hdxml);
                sw.Close();
                sw.Dispose();
                #endregion
                #endregion 生成XML文件

                if (Request.QueryString["backUrl"] == null)
                {
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    return Redirect(Request.QueryString["backUrl"].ToString());
                }
            }
            else
            {
                //执行失败信息
                ViewData["msg"] = "登录失败!";
                //失败视图
                return View("Error");
            }
        }