package models type ReplyItem struct { Id string `json:"item_id"` Content string `json:"item_content"` GroupId string `json:"group_id"` ItemName string `json:"item_name"` UserId string `json:"user_id"` EntId string `json:"ent_id"` } type ReplyGroup struct { Id string `json:"group_id"` GroupName string `json:"group_name"` UserId string `json:"user_id"` EntId string `json:"ent_id"` IsTeam string `json:"is_team"` Items []*ReplyItem `json:"items";"` } func FindReplyItemByUidTitle(uid interface{}, title string) ReplyItem { var reply ReplyItem DB.Where("uid = ? and item_name = ?", uid, title).Find(&reply) return reply } func FindReplyItemByUserIdTitle(userId interface{}, title string) ReplyItem { var reply ReplyItem DB.Where("user_id = ? and item_name like ?", userId, title+"%").Find(&reply) return reply } func FindReplyByUserId(userId interface{}) []*ReplyGroup { var replyGroups []*ReplyGroup //DB.Raw("select a.*,b.* from reply_group a left join reply_item b on a.id=b.group_id where a.user_id=? ", userId).Scan(&replyGroups) var replyItems []*ReplyItem DB.Where("user_id = ? and is_team=1", userId).Find(&replyGroups) DB.Where("user_id = ?", userId).Find(&replyItems) temp := make(map[string]*ReplyGroup) for _, replyGroup := range replyGroups { replyGroup.Items = make([]*ReplyItem, 0) temp[replyGroup.Id] = replyGroup } for _, replyItem := range replyItems { if _, ok := temp[replyItem.GroupId]; !ok { continue } temp[replyItem.GroupId].Items = append(temp[replyItem.GroupId].Items, replyItem) } return replyGroups } //获取企业公用快捷回复列表 func FindReplyByEntId(entId interface{}) []*ReplyGroup { var replyGroups []*ReplyGroup var replyItems []*ReplyItem DB.Where("ent_id = ? and is_team=2", entId).Find(&replyGroups) DB.Where("ent_id = ?", entId).Find(&replyItems) temp := make(map[string]*ReplyGroup) for _, replyGroup := range replyGroups { replyGroup.Items = make([]*ReplyItem, 0) temp[replyGroup.Id] = replyGroup } for _, replyItem := range replyItems { if _, ok := temp[replyItem.GroupId]; !ok { continue } temp[replyItem.GroupId].Items = append(temp[replyItem.GroupId].Items, replyItem) } return replyGroups } func FindReplyTitleByUserId(userId interface{}) []*ReplyGroup { var replyGroups []*ReplyGroup //DB.Raw("select a.*,b.* from reply_group a left join reply_item b on a.id=b.group_id where a.user_id=? ", userId).Scan(&replyGroups) var replyItems []*ReplyItem DB.Where("user_id = ?", userId).Find(&replyGroups) DB.Select("item_name,group_id").Where("user_id = ?", userId).Find(&replyItems) temp := make(map[string]*ReplyGroup) for _, replyGroup := range replyGroups { replyGroup.Items = make([]*ReplyItem, 0) temp[replyGroup.Id] = replyGroup } for _, replyItem := range replyItems { temp[replyItem.GroupId].Items = append(temp[replyItem.GroupId].Items, replyItem) } return replyGroups } func CreateReplyGroup(groupName, userId, entId, isTeam string) { g := &ReplyGroup{ GroupName: groupName, UserId: userId, EntId: entId, IsTeam: isTeam, } DB.Create(g) } func CreateReplyContent(groupId string, userId string, content, itemName, entId string) { g := &ReplyItem{ GroupId: groupId, UserId: userId, Content: content, ItemName: itemName, EntId: entId, } DB.Create(g) } func UpdateReplyContent(id, userId, title, content string) { r := &ReplyItem{ ItemName: title, Content: content, } DB.Model(&ReplyItem{}).Where("user_id = ? and id = ?", userId, id).Update(r) } func DeleteReplyContent(id string, userId string) { DB.Where("user_id = ? and id = ?", userId, id).Delete(ReplyItem{}) } func DeleteReplyGroup(id string, userId string) { DB.Where("user_id = ? and id = ?", userId, id).Delete(ReplyGroup{}) DB.Where("user_id = ? and group_id = ?", userId, id).Delete(ReplyItem{}) } func FindReplyBySearcch(entId, userId interface{}, search string) []*ReplyGroup { var replyGroups []*ReplyGroup var replyItems []*ReplyItem DB.Where("user_id = ? or (ent_id = ? and is_team=2)", userId, entId).Find(&replyGroups) DB.Where("(user_id = ? and (item_name like ? or content like ?)) or (ent_id = ? and (item_name like ? or content like ?))", userId, "%"+search+"%", "%"+search+"%", entId, "%"+search+"%", "%"+search+"%").Find(&replyItems) temp := make(map[string]*ReplyGroup) for _, replyGroup := range replyGroups { replyGroup.Items = make([]*ReplyItem, 0) temp[replyGroup.Id] = replyGroup } for _, replyItem := range replyItems { if _, ok := temp[replyItem.GroupId]; !ok { continue } temp[replyItem.GroupId].Items = append(temp[replyItem.GroupId].Items, replyItem) } var newReplyGroups []*ReplyGroup = make([]*ReplyGroup, 0) for _, replyGroup := range replyGroups { if len(replyGroup.Items) != 0 { newReplyGroups = append(newReplyGroups, replyGroup) } } return newReplyGroups }