diff --git a/controller/channel.go b/controller/channel.go index dd71259..b98af41 100644 --- a/controller/channel.go +++ b/controller/channel.go @@ -54,8 +54,9 @@ func FixChannelsAbilities(c *gin.Context) { func SearchChannels(c *gin.Context) { keyword := c.Query("keyword") group := c.Query("group") + modelKeyword := c.Query("model") //idSort, _ := strconv.ParseBool(c.Query("id_sort")) - channels, err := model.SearchChannels(keyword, group) + channels, err := model.SearchChannels(keyword, group, modelKeyword) if err != nil { c.JSON(http.StatusOK, gin.H{ "success": false, diff --git a/model/channel.go b/model/channel.go index 96b3635..7c7b0d9 100644 --- a/model/channel.go +++ b/model/channel.go @@ -43,21 +43,39 @@ func GetAllChannels(startIdx int, num int, selectAll bool, idSort bool) ([]*Chan return channels, err } -func SearchChannels(keyword string, group string) (channels []*Channel, err error) { +func SearchChannels(keyword string, group string, model string) ([]*Channel, error) { + var channels []*Channel keyCol := "`key`" + groupCol := "`group`" + modelsCol := "`models`" + + // 如果是 PostgreSQL,使用双引号 if common.UsingPostgreSQL { keyCol = `"key"` + groupCol = `"group"` + modelsCol = `"models"` } + + // 构造基础查询 + baseQuery := DB.Model(&Channel{}).Omit(keyCol) + + // 构造WHERE子句 + var whereClause string + var args []interface{} if group != "" { - groupCol := "`group`" - if common.UsingPostgreSQL { - groupCol = `"group"` - } - err = DB.Omit("key").Where("(id = ? or name LIKE ? or "+keyCol+" = ?) and "+groupCol+" LIKE ?", common.String2Int(keyword), keyword+"%", keyword, "%"+group+"%").Find(&channels).Error + whereClause = "(id = ? OR name LIKE ? OR " + keyCol + " = ?) AND " + groupCol + " LIKE ? AND " + modelsCol + " LIKE ?" + args = append(args, common.String2Int(keyword), "%"+keyword+"%", keyword, "%"+group+"%", "%"+model+"%") } else { - err = DB.Omit("key").Where("id = ? or name LIKE ? or "+keyCol+" = ?", common.String2Int(keyword), keyword+"%", keyword).Find(&channels).Error + whereClause = "(id = ? OR name LIKE ? OR " + keyCol + " = ?) AND " + modelsCol + " LIKE ?" + args = append(args, common.String2Int(keyword), "%"+keyword+"%", keyword, "%"+model+"%") } - return channels, err + + // 执行查询 + err := baseQuery.Where(whereClause, args...).Find(&channels).Error + if err != nil { + return nil, err + } + return channels, nil } func GetChannelById(id int, selectAll bool) (*Channel, error) { diff --git a/web/src/components/ChannelsTable.js b/web/src/components/ChannelsTable.js index 9fdd597..26da9a3 100644 --- a/web/src/components/ChannelsTable.js +++ b/web/src/components/ChannelsTable.js @@ -257,6 +257,7 @@ const ChannelsTable = () => { const [idSort, setIdSort] = useState(false); const [searchKeyword, setSearchKeyword] = useState(''); const [searchGroup, setSearchGroup] = useState(''); + const [searchModel, setSearchModel] = useState(''); const [searching, setSearching] = useState(false); const [updatingBalance, setUpdatingBalance] = useState(false); const [pageSize, setPageSize] = useState(ITEMS_PER_PAGE); @@ -440,15 +441,15 @@ const ChannelsTable = () => { } }; - const searchChannels = async (searchKeyword, searchGroup) => { - if (searchKeyword === '' && searchGroup === '') { + const searchChannels = async (searchKeyword, searchGroup, searchModel) => { + if (searchKeyword === '' && searchGroup === '' && searchModel === '') { // if keyword is blank, load files instead. await loadChannels(0, pageSize, idSort); setActivePage(1); return; } setSearching(true); - const res = await API.get(`/api/channel/search?keyword=${searchKeyword}&group=${searchGroup}`); + const res = await API.get(`/api/channel/search?keyword=${searchKeyword}&group=${searchGroup}&model=${searchModel}`); const {success, message, data} = res.data; if (success) { setChannels(data); @@ -625,13 +626,12 @@ const ChannelsTable = () => { return ( <> -
{searchChannels(searchKeyword, searchGroup)}} labelPosition='left'> - + {searchChannels(searchKeyword, searchGroup, searchModel)}} labelPosition='left'>
{ setSearchKeyword(v.trim()) }} /> + { + setSearchModel(v.trim()) + }} + /> { setSearchGroup(v) - searchChannels(searchKeyword, v) + searchChannels(searchKeyword, v, searchModel) }}/> +