Example #1
0
        public ActionResult ViewNotice(int noticeId, int noticeReceiverId = 0)
        {
            UserItemVM user = new UserItemVM()
            {
                ID   = UserData.UserId.Value,
                Name = UserData.Name,
            };

            NoticeVM notice = new NoticeService().GetNotice(noticeId, user, noticeReceiverId);

            return(View(notice));
        }
Example #2
0
        public NoticeVM GetNotice(int noticeId, UserItemVM user, int noticeReceiverId = 0)
        {
            try
            {
                using (var dbContext = new DbContext().ConnectionStringName(ConnectionUtil.connWXB, DbProviderTypes.MySql))
                {
                    string   sql    = string.Format("select * from notice n where n.ID = {0} ", noticeId);
                    NoticeVM notice = dbContext.Sql(sql).QuerySingle <NoticeVM>(reader =>
                    {
                        NoticeVM noVM = new NoticeVM()
                        {
                            ID        = reader.AsInt("ID"),
                            Title     = reader.AsString("Title"),
                            Content   = reader.AsString("Content"),
                            Inputer   = reader.AsString("Inputer"),
                            InputerID = reader.AsInt("InputerID")
                        };

                        if (!string.IsNullOrEmpty(reader["AttachmentUrl"].ToString()))
                        {
                            noVM.AttachmentUrl = reader.AsString("AttachmentUrl");
                        }

                        if (!string.IsNullOrEmpty(reader["UpdateTime"].ToString()))
                        {
                            noVM.UpdateTimeStr = Convert.ToDateTime(reader["UpdateTime"]).ToString("yyyy-MM-dd");
                        }

                        return(noVM);
                    });

                    if (noticeReceiverId > 0)
                    {
                        dbContext.Insert("receiverstatus").Column("NoticeReceiverID", noticeReceiverId)
                        .Column("ReceiveTime", DateTime.Now)
                        .Column("Receiver", user.Name)
                        .Column("ReceiverID", user.ID)
                        .ExecuteReturnLastId <int>();
                    }

                    return(notice);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Example #3
0
        public ActionResult ViewNotice(int noticeId, int noticeReceiverId = 0)
        {
            try
            {
                UserItemVM user = new UserItemVM()
                {
                    ID   = UserData.UserId.Value,
                    Name = UserData.Name
                };

                NoticeVM notice = new NoticeService().GetNotice(noticeId, user, noticeReceiverId);
                return(View(notice));
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(ex.Message);
                throw ex;
            }
        }
Example #4
0
        /// <summary>
        /// 保存通知
        /// </summary>
        /// <param name="noticeVM"></param>
        /// <returns></returns>
        public NoticeVM Save(NoticeVM noticeVM)
        {
            if (noticeVM == null || string.IsNullOrEmpty(noticeVM.Title) || string.IsNullOrEmpty(noticeVM.Content))
            {
                return(null);
            }

            try
            {
                //去cms获取指定角色或指定ID的用户信息
                List <UserItemVM> userList = new List <UserItemVM>();
                if (noticeVM.ChooseUser == null)
                {
                    noticeVM.ChooseUser = new ChooseUserVM();
                }

                #region 获取用户信息
                if (!string.IsNullOrEmpty(noticeVM.UserOrRoleIds))
                {
                    using (var dbContextOne = new DbContext().ConnectionStringName(ConnectionUtil.connCMS, DbProviderTypes.MySql))
                    {
                        #region 获取用户信息
                        string sqlStr = string.Format(@"select 
		                                                        ud.ID,ud.Name,
		                                                        ur.RoleID as RoleType
                                                        from userdata ud left join userrole ur on ud.ID = ur.UserID
                                                        where 1=1 and ur.RoleID in ({0})", noticeVM.UserOrRoleIds);

                        if (noticeVM.ChooseUserWay == (int)EnumChooseUserWay.角色)
                        {
                            //获取指定角色的用户
                            userList = dbContextOne.Sql(sqlStr).Query <UserItemVM, List <UserItemVM> >(reader =>
                            {
                                UserItemVM vm = new UserItemVM()
                                {
                                    ID   = reader.AsInt("ID"),
                                    Name = reader.AsString("Name")
                                };

                                if (!string.IsNullOrEmpty(reader["RoleType"].ToString()))
                                {
                                    vm.RoleType = reader.AsInt("RoleType");
                                }

                                return(vm);
                            });
                        }
                        else if (noticeVM.ChooseUserWay == (int)EnumChooseUserWay.个人)
                        {
                            sqlStr = string.Format(@"select ID,Name from userdata ud where ud.ID in ({0})", noticeVM.UserOrRoleIds);

                            //获取指定ID的用户信息
                            userList = dbContextOne.Sql(sqlStr).Query <UserItemVM, List <UserItemVM> >(reader =>
                            {
                                return(new UserItemVM()
                                {
                                    ID = reader.AsInt("ID"),
                                    Name = reader.AsString("Name")
                                });
                            });
                        }
                        #endregion
                    }
                }
                #endregion

                using (var dbContext = new DbContext().ConnectionStringName(ConnectionUtil.connWXB, DbProviderTypes.MySql).UseTransaction(true))
                {
                    #region 新增和编辑处理
                    if (!noticeVM.ID.HasValue)
                    {
                        noticeVM.ID = dbContext.Insert("notice").Column("Title", noticeVM.Title)
                                      .Column("Content", noticeVM.Content)
                                      .Column("AttachmentUrl", noticeVM.AttachmentUrl)
                                      .Column("ChooseUserWay", noticeVM.ChooseUserWay)
                                      .Column("UpdateTime", noticeVM.UpdateTime)
                                      .Column("Inputer", noticeVM.Inputer)
                                      .Column("InputerID", noticeVM.InputerID)
                                      .Column("InputTime", noticeVM.InputTime)
                                      .ExecuteReturnLastId <int>();
                    }
                    else
                    {
                        dbContext.Update("notice").Column("Title", noticeVM.Title)
                        .Column("Content", noticeVM.Content)
                        .Column("AttachmentUrl", noticeVM.AttachmentUrl)
                        .Column("ChooseUserWay", noticeVM.ChooseUserWay)
                        .Column("UpdateTime", noticeVM.UpdateTime)
                        .Where("ID", noticeVM.ID)
                        .Execute();
                    }
                    #endregion

                    #region 保存通知发送人

                    //先删除之前的数据
                    dbContext.Delete("noticereceiver").Where("NoticeID", noticeVM.ID).Execute();

                    if (noticeVM.ChooseUserWay == (int)EnumChooseUserWay.个人 && !string.IsNullOrEmpty(noticeVM.UserOrRoleIds))
                    {
                        foreach (var item in userList)
                        {
                            dbContext.Insert("noticereceiver").Column("ReceiverID", item.ID)
                            .Column("Receiver", item.Name)
                            .Column("NoticeID", noticeVM.ID)
                            .Column("PublishTime", noticeVM.UpdateTime)
                            .ExecuteReturnLastId <int>();
                        }
                    }
                    else if (noticeVM.ChooseUserWay == (int)EnumChooseUserWay.角色 && !string.IsNullOrEmpty(noticeVM.UserOrRoleIds))
                    {
                        //批量新增
                        foreach (var item in userList)
                        {
                            dbContext.Insert("noticereceiver").Column("ReceiverID", item.ID)
                            .Column("Receiver", item.Name)
                            .Column("RoleType", item.RoleType)
                            .Column("NoticeID", noticeVM.ID)
                            .Column("PublishTime", noticeVM.UpdateTime)
                            .ExecuteReturnLastId <int>();
                        }
                    }
                    #endregion

                    dbContext.Commit();
                }

                return(noticeVM);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Example #5
0
        /// <summary>
        /// 保存通知
        /// </summary>
        /// <param name="noticeVM"></param>
        /// <returns></returns>
        public NoticeVM Save(NoticeVM noticeVM)
        {
            if (noticeVM == null || string.IsNullOrEmpty(noticeVM.Title) || string.IsNullOrEmpty(noticeVM.Content))
            {
                return(null);
            }

            try
            {
                List <UserItemVM> userList = new List <UserItemVM>();
                #region 获取用户信息
                if (!string.IsNullOrEmpty(noticeVM.ChooseRoles) || !string.IsNullOrEmpty(noticeVM.ChoosedUserIds))
                {
                    using (var dbContextOne = new DbContext().ConnectionStringName(ConnectionUtil.connWXB, DbProviderTypes.MySql))
                    {
                        #region 获取指定角色的用户信息

                        if (!string.IsNullOrEmpty(noticeVM.ChooseRoles))
                        {
                            string sqlRole = string.Format(@"select 
		                                                        ud.ID,ud.Name,
		                                                        ur.RoleID as RoleType
                                                        from userdata ud left join userrole ur on ud.ID = ur.UserID
                                                        where 1=1 and ur.RoleID in ({0})", noticeVM.ChooseRoles);
                            //获取指定角色的用户
                            List <UserItemVM> tempList = dbContextOne.Sql(sqlRole).Query <UserItemVM, List <UserItemVM> >(reader =>
                            {
                                UserItemVM vm = new UserItemVM()
                                {
                                    ID   = reader.AsInt("ID"),
                                    Name = reader.AsString("Name")
                                };

                                if (!string.IsNullOrEmpty(reader["RoleType"].ToString()))
                                {
                                    vm.RoleTypes = reader.AsString("RoleType");
                                }

                                return(vm);
                            });

                            //用户数据去重
                            UserItemVM tempUserItem = new UserItemVM();
                            tempList.ForEach(t =>
                            {
                                tempUserItem = userList.Find(u => u.ID == t.ID);
                                if (tempUserItem == null)
                                {
                                    userList.Add(t);
                                }
                                else if (tempUserItem != null && t.RoleTypes != tempUserItem.RoleTypes)
                                {
                                    userList.Remove(tempUserItem);
                                    tempUserItem.RoleTypes += "," + t.RoleTypes;
                                    userList.Add(tempUserItem);
                                }
                            });
                        }
                        #endregion

                        #region 获取指定用户信息
                        if (!string.IsNullOrEmpty(noticeVM.ChoosedUserIds))
                        {
                            string sqlUser = string.Format(@"select ID,Name from userdata ud where ud.ID in ({0})", noticeVM.ChoosedUserIds);
                            //获取指定ID的用户信息
                            var tempUesrList = dbContextOne.Sql(sqlUser).Query <UserItemVM, List <UserItemVM> >(reader =>
                            {
                                return(new UserItemVM()
                                {
                                    ID = reader.AsInt("ID"),
                                    Name = reader.AsString("Name")
                                });
                            });

                            if (tempUesrList != null && tempUesrList.Count > 0)
                            {
                                if (userList == null)
                                {
                                    userList = new List <UserItemVM>();
                                }

                                //用户去重
                                userList = userList.Where(u => tempUesrList.Find(a => a.ID == u.ID) == null).ToList();
                                userList.AddRange(tempUesrList);
                            }
                        }
                        #endregion
                    }
                }
                #endregion

                using (var dbContext = new DbContext().ConnectionStringName(ConnectionUtil.connWXB, DbProviderTypes.MySql).UseTransaction(true))
                {
                    #region 新增和编辑处理
                    if (!noticeVM.ID.HasValue)
                    {
                        noticeVM.ID = dbContext.Insert("notice").Column("Title", noticeVM.Title)
                                      .Column("Content", noticeVM.Content)
                                      .Column("AttachmentUrl", noticeVM.AttachmentUrl)
                                      .Column("ChooseRoles", noticeVM.ChooseRoles)
                                      .Column("UpdateTime", noticeVM.UpdateTime)
                                      .Column("Inputer", noticeVM.Inputer)
                                      .Column("InputerID", noticeVM.InputerID)
                                      .Column("InputTime", noticeVM.InputTime)
                                      .ExecuteReturnLastId <int>();
                    }
                    else
                    {
                        dbContext.Update("notice").Column("Title", noticeVM.Title)
                        .Column("Content", noticeVM.Content)
                        .Column("AttachmentUrl", noticeVM.AttachmentUrl)
                        .Column("ChooseRoles", noticeVM.ChooseRoles)
                        .Column("UpdateTime", noticeVM.UpdateTime)
                        .Where("ID", noticeVM.ID)
                        .Execute();
                    }
                    #endregion

                    #region 保存通知发送人

                    //先删除之前的数据
                    dbContext.Delete("noticereceiver").Where("NoticeID", noticeVM.ID).Execute();
                    foreach (var item in userList)
                    {
                        dbContext.Insert("noticereceiver").Column("ReceiverID", item.ID)
                        .Column("Receiver", item.Name)
                        .Column("RoleTypes", item.RoleTypes)
                        .Column("NoticeID", noticeVM.ID)
                        .Column("PublishTime", noticeVM.UpdateTime)
                        .ExecuteReturnLastId <int>();
                    }

                    #endregion

                    dbContext.Commit();
                }

                return(noticeVM);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }