package semantic_analysis import ( "eta/eta_api/global" "eta/eta_api/utils" "fmt" "strings" "time" ) // SaCompareSearchKeyword 文档比对搜索关键词表 type SaCompareSearchKeyword struct { SaCompareSearchKeywordId int `orm:"column(sa_compare_search_keyword_id);pk" gorm:"primaryKey"` CompareId int `description:"文档比对ID"` Keyword string `description:"关键词"` CreateTime time.Time `description:"创建时间"` } var SaCompareSearchKeywordColumns = struct { SaCompareSearchKeywordId string CompareId string Keyword string CreateTime string }{ SaCompareSearchKeywordId: "sa_compare_search_keyword_id", CompareId: "compare_id", Keyword: "keyword", CreateTime: "create_time", } func (m *SaCompareSearchKeyword) TableName() string { return "sa_compare_search_keyword" } func (m *SaCompareSearchKeyword) Create() (err error) { o := global.DbMap[utils.DbNameMaster] err = o.Create(m).Error return } func (m *SaCompareSearchKeyword) Update(cols []string) (err error) { o := global.DbMap[utils.DbNameMaster] err = o.Select(cols).Updates(m).Error return } func (m *SaCompareSearchKeyword) Del() (err error) { o := global.DbMap[utils.DbNameMaster] sql := `DELETE FROM sa_compare_search_keyword WHERE sa_compare_search_keyword_id = ? LIMIT 1` err = o.Exec(sql, m.SaCompareSearchKeywordId).Error return } func (m *SaCompareSearchKeyword) GetItemById(id int) (err error) { o := global.DbMap[utils.DbNameMaster] sql := `SELECT * FROM sa_compare_search_keyword WHERE sa_compare_search_keyword_id = ? LIMIT 1` err = o.Raw(sql, id).First(&m).Error return } func (m *SaCompareSearchKeyword) GetItemByCondition(condition string, pars []interface{}) (err error) { o := global.DbMap[utils.DbNameMaster] sql := `SELECT * FROM sa_compare_search_keyword WHERE 1=1 ` sql += condition sql += ` LIMIT 1` err = o.Raw(sql, pars...).First(&m).Error return } func (m *SaCompareSearchKeyword) GetCountByCondition(condition string, pars []interface{}) (count int, err error) { o := global.DbMap[utils.DbNameMaster] sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition) err = o.Raw(sql, pars...).Scan(&count).Error return } func (m *SaCompareSearchKeyword) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*SaCompareSearchKeyword, err error) { o := global.DbMap[utils.DbNameMaster] fields := strings.Join(fieldArr, ",") if len(fieldArr) == 0 { fields = `*` } order := `ORDER BY create_time DESC` if orderRule != "" { order = ` ORDER BY ` + orderRule } sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order) err = o.Raw(sql, pars...).Find(&items).Error return } func (m *SaCompareSearchKeyword) GetPageItemsByCondition(startSize, pageSize int, condition string, pars []interface{}, fieldArr []string, orderRule string) (total int, items []*SaCompareSearchKeyword, err error) { o := global.DbMap[utils.DbNameMaster] fields := strings.Join(fieldArr, ",") if len(fieldArr) == 0 { fields = `*` } order := `ORDER BY create_time DESC` if orderRule != "" { order = ` ORDER BY ` + orderRule } sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order) totalSql := `SELECT COUNT(1) total FROM (` + sql + `) z` if err = o.Raw(totalSql, pars...).Scan(&total).Error; err != nil { return } sql += ` LIMIT ?,?` pars = append(pars, startSize, pageSize) err = o.Raw(sql, pars...).Find(&items).Error return } type SaCompareSearchKeywordSaveReq struct { SaCompareId int `description:"文档比对ID"` Keywords []string `description:"关键词"` } func (m *SaCompareSearchKeyword) DelAndCreateSearchKeywords(compareId int, keywords []string) (err error) { o := global.DbMap[utils.DbNameMaster] tx := o.Begin() defer func() { if err != nil { _ = tx.Rollback() return } _ = tx.Commit() }() sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ?`, m.TableName(), SaCompareSearchKeywordColumns.CompareId) if e := o.Exec(sql, compareId).Error; e != nil { return e } if len(keywords) > 0 { items := make([]*SaCompareSearchKeyword, 0) for _, v := range keywords { items = append(items, &SaCompareSearchKeyword{ CompareId: compareId, Keyword: v, CreateTime: time.Now().Local(), }) } if e := o.CreateInBatches(items, utils.MultiAddNum).Error; e != nil { return e } } return }