feat: Add channel search by model field (close #72)

This commit is contained in:
1808837298@qq.com 2024-03-01 21:57:52 +08:00
parent feb40db2bc
commit 413d4f0a66
3 changed files with 48 additions and 17 deletions

View File

@ -54,8 +54,9 @@ func FixChannelsAbilities(c *gin.Context) {
func SearchChannels(c *gin.Context) { func SearchChannels(c *gin.Context) {
keyword := c.Query("keyword") keyword := c.Query("keyword")
group := c.Query("group") group := c.Query("group")
modelKeyword := c.Query("model")
//idSort, _ := strconv.ParseBool(c.Query("id_sort")) //idSort, _ := strconv.ParseBool(c.Query("id_sort"))
channels, err := model.SearchChannels(keyword, group) channels, err := model.SearchChannels(keyword, group, modelKeyword)
if err != nil { if err != nil {
c.JSON(http.StatusOK, gin.H{ c.JSON(http.StatusOK, gin.H{
"success": false, "success": false,

View File

@ -43,21 +43,39 @@ func GetAllChannels(startIdx int, num int, selectAll bool, idSort bool) ([]*Chan
return channels, err 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`" keyCol := "`key`"
groupCol := "`group`"
modelsCol := "`models`"
// 如果是 PostgreSQL使用双引号
if common.UsingPostgreSQL { if common.UsingPostgreSQL {
keyCol = `"key"` keyCol = `"key"`
}
if group != "" {
groupCol := "`group`"
if common.UsingPostgreSQL {
groupCol = `"group"` groupCol = `"group"`
modelsCol = `"models"`
} }
err = DB.Omit("key").Where("(id = ? or name LIKE ? or "+keyCol+" = ?) and "+groupCol+" LIKE ?", common.String2Int(keyword), keyword+"%", keyword, "%"+group+"%").Find(&channels).Error
// 构造基础查询
baseQuery := DB.Model(&Channel{}).Omit(keyCol)
// 构造WHERE子句
var whereClause string
var args []interface{}
if group != "" {
whereClause = "(id = ? OR name LIKE ? OR " + keyCol + " = ?) AND " + groupCol + " LIKE ? AND " + modelsCol + " LIKE ?"
args = append(args, common.String2Int(keyword), "%"+keyword+"%", keyword, "%"+group+"%", "%"+model+"%")
} else { } 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) { func GetChannelById(id int, selectAll bool) (*Channel, error) {

View File

@ -257,6 +257,7 @@ const ChannelsTable = () => {
const [idSort, setIdSort] = useState(false); const [idSort, setIdSort] = useState(false);
const [searchKeyword, setSearchKeyword] = useState(''); const [searchKeyword, setSearchKeyword] = useState('');
const [searchGroup, setSearchGroup] = useState(''); const [searchGroup, setSearchGroup] = useState('');
const [searchModel, setSearchModel] = useState('');
const [searching, setSearching] = useState(false); const [searching, setSearching] = useState(false);
const [updatingBalance, setUpdatingBalance] = useState(false); const [updatingBalance, setUpdatingBalance] = useState(false);
const [pageSize, setPageSize] = useState(ITEMS_PER_PAGE); const [pageSize, setPageSize] = useState(ITEMS_PER_PAGE);
@ -440,15 +441,15 @@ const ChannelsTable = () => {
} }
}; };
const searchChannels = async (searchKeyword, searchGroup) => { const searchChannels = async (searchKeyword, searchGroup, searchModel) => {
if (searchKeyword === '' && searchGroup === '') { if (searchKeyword === '' && searchGroup === '' && searchModel === '') {
// if keyword is blank, load files instead. // if keyword is blank, load files instead.
await loadChannels(0, pageSize, idSort); await loadChannels(0, pageSize, idSort);
setActivePage(1); setActivePage(1);
return; return;
} }
setSearching(true); 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; const {success, message, data} = res.data;
if (success) { if (success) {
setChannels(data); setChannels(data);
@ -625,13 +626,12 @@ const ChannelsTable = () => {
return ( return (
<> <>
<EditChannel refresh={refresh} visible={showEdit} handleClose={closeEdit} editingChannel={editingChannel}/> <EditChannel refresh={refresh} visible={showEdit} handleClose={closeEdit} editingChannel={editingChannel}/>
<Form onSubmit={() => {searchChannels(searchKeyword, searchGroup)}} labelPosition='left'> <Form onSubmit={() => {searchChannels(searchKeyword, searchGroup, searchModel)}} labelPosition='left'>
<div style={{display: 'flex'}}> <div style={{display: 'flex'}}>
<Space> <Space>
<Form.Input <Form.Input
field='search' field='search_keyword'
label='关键词' label='搜索渠道关键词'
placeholder='ID名称和密钥 ...' placeholder='ID名称和密钥 ...'
value={searchKeyword} value={searchKeyword}
loading={searching} loading={searching}
@ -639,10 +639,22 @@ const ChannelsTable = () => {
setSearchKeyword(v.trim()) setSearchKeyword(v.trim())
}} }}
/> />
<Form.Input
field='search_model'
label='模型'
placeholder='模型关键字'
value={searchModel}
loading={searching}
onChange={(v)=>{
setSearchModel(v.trim())
}}
/>
<Form.Select field="group" label='分组' optionList={groupOptions} onChange={(v) => { <Form.Select field="group" label='分组' optionList={groupOptions} onChange={(v) => {
setSearchGroup(v) setSearchGroup(v)
searchChannels(searchKeyword, v) searchChannels(searchKeyword, v, searchModel)
}}/> }}/>
<Button label='查询' type="primary" htmlType="submit" className="btn-margin-right"
style={{marginRight: 8}}>查询</Button>
</Space> </Space>
</div> </div>
</Form> </Form>