123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290 |
- package api
- import (
- "errors"
- "fmt"
- "sku3dweb/db/model"
- "sku3dweb/db/repo"
- "sku3dweb/log"
- "strconv"
- "strings"
- "time"
- "github.com/gin-gonic/gin"
- "go.mongodb.org/mongo-driver/bson"
- "go.mongodb.org/mongo-driver/bson/primitive"
- )
- func FassiApi(r *GinRouter) {
- r.POSTJWT("/image/create", createImg)
- r.GETJWT("/image/fassi/list", SearchByImg)
- r.GETJWT("/image/list", SearchByFields)
- r.POSTJWT("/image/delete/:id", DeleteImage)
- r.POSTJWT("/image/update", UpdateImage)
- }
- func UpdateImage(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- var matImage model.MatImage
- err := c.ShouldBindJSON(&matImage)
- if err != nil {
- log.Error(err)
- return nil, err
- }
- if matImage.Id.IsZero() {
- return nil, errors.New("id错误")
- }
- // searchMat := &model.MatImage{}
- // // 如果更新的是面料图
- // found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
- // CollectName: repo.CollectionMatImages,
- // Query: repo.Map{"_id": matImage.Id},
- // Project: []string{"rawImage"},
- // }, &searchMat)
- // if err != nil {
- // return nil, err
- // }
- // // 没有找到面料数据
- // if !found {
- // return nil, NewError("未找到数据")
- // }
- // // 未更改图片
- // if matImage.RawImage == nil {
- // return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionMatImages, matImage.Id.Hex(), &matImage)
- // }
- // // 更新了面料原图 对应更新fassi 特征数据
- // if searchMat.RawImage.Url != matImage.RawImage.Url {
- // // 先删除
- // _, err := RomoveFassiImage(matImage.Id.Hex())
- // if err != nil {
- // return nil, err
- // }
- // // 再新增
- // err = AddFassiImage(matImage.Id, matImage.RawImage.Url)
- // if err != nil {
- // return nil, err
- // }
- // }
- return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionMatImages, matImage.Id.Hex(), &matImage)
- }
- func DeleteImage(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- id := c.Param("id")
- _, err := RomoveFassiImage(id)
- if err != nil {
- return nil, err
- }
- return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionMatImages, id)
- }
- func SearchByFields(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- page, size, query := UtilQueryPageSize(c)
- // 查询所有分类
- cates := []*model.Category{}
- repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{CollectName: repo.CollectionCategory}, &cates)
- err := parseCategories(query, cates[0])
- if err != nil {
- return nil, err
- }
- pageOption := &repo.PageSearchOptions{
- CollectName: repo.CollectionMatImages,
- Page: page,
- Size: size,
- Query: query,
- // Project: []string{"nameCn", "createTime", "thumbnail", "price", "from"},
- // Sort: bson.M{"_id"},
- }
- return repo.RepoPageSearch(apictx.CreateRepoCtx(), pageOption)
- }
- func createImg(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- data := &model.MatImage{}
- err := c.ShouldBindJSON(data)
- if err != nil {
- return nil, err
- }
- data.CreateTime = time.Now()
- data.UpdateTime = time.Now()
- data.UserId, _ = primitive.ObjectIDFromHex(apictx.User.ID)
- idstr, err := repo.RepoAddDoc(apictx.CreateRepoCtx(), repo.CollectionMatImages, data)
- if err != nil {
- return nil, err
- }
- imgId, _ := primitive.ObjectIDFromHex(idstr)
- return imgId, AddFassiImage(imgId, data.RawImage.Url)
- }
- func SearchByImg(c *gin.Context, apictx *ApiSession) (interface{}, error) {
- url := c.Query("url")
- _limit := c.Query("limit")
- _min_score := c.Query("min_score")
- _max_score := c.Query("max_score")
- limit, _ := strconv.Atoi(_limit)
- min_score, _ := strconv.ParseFloat(_min_score, 64)
- max_score, _ := strconv.ParseFloat(_max_score, 64)
- fmt.Println("search url =>: ", url)
- if !strings.Contains(url, "http") {
- return nil, NewError("参数错误")
- }
- images, err := QueryFassiImage(url, limit, min_score, max_score)
- if err != nil {
- return nil, err
- }
- var list = []map[string]interface{}{}
- if len(images) < 1 {
- return list, nil
- }
- var ids = []primitive.ObjectID{}
- for _, item := range images {
- ids = append(ids, item.Id)
- }
- param := &repo.DocSearchOptions{
- CollectName: repo.CollectionMatImages,
- Query: repo.Map{"_id": bson.M{"$in": ids}},
- }
- err = repo.RepoSeachDocs(apictx.CreateRepoCtx(), param, &list)
- if err != nil {
- return nil, err
- }
- var ImageMap = map[primitive.ObjectID]map[string]interface{}{}
- for _, item := range list {
- ImageMap[item["_id"].(primitive.ObjectID)] = item
- }
- var out = []interface{}{}
- for _, item := range images {
- value := ImageMap[item.Id]
- value["score"] = item.Score
- out = append(out, value)
- }
- return out, nil
- }
- type IteChildren func(n *model.Category) []string
- type FindChild func(n *model.Category, id string) []string
- func FindCategoryIds(c *model.Category, parents []string) ([]string, [][]string) {
- out := []string{}
- andOut := [][]string{}
- var allChildren IteChildren
- allChildren = func(n *model.Category) []string {
- ids := []string{}
- if n == nil {
- return ids
- }
- ids = append(ids, n.IdStr)
- if n.Children != nil {
- for _, c := range n.Children {
- cids := allChildren(c)
- if len(cids) > 0 {
- ids = append(ids, cids...)
- }
- }
- }
- return ids
- }
- var findChildrens FindChild
- findChildrens = func(n *model.Category, id string) []string {
- ids := []string{}
- if n == nil {
- return ids
- }
- if n.IdStr == id {
- ids = allChildren(n)
- return ids
- }
- if n.Children == nil {
- return ids
- }
- //查找孩子节点
- for _, c := range n.Children {
- ids = findChildrens(c, id)
- if len(ids) > 0 {
- break
- }
- }
- return ids
- }
- for _, v := range parents {
- for _, catnode := range c.Children {
- extends := findChildrens(catnode, v)
- if len(extends) > 0 {
- andOut = append(andOut, extends)
- out = append(out, extends...)
- break
- }
- }
- }
- return out, andOut
- }
- func parseCategories(query map[string]interface{}, cateConf *model.Category) error {
- filters := []bson.M{}
- keyfilters := []bson.M{}
- //keyword
- if query["keyword"] != nil {
- keyword := query["keyword"].(string)
- if len(keyword) > 0 {
- keyfilters = append(keyfilters, bson.M{"nameCn": bson.M{"$regex": keyword, "$options": "$i"}})
- keyfilters = append(keyfilters, bson.M{"nameEn": bson.M{"$regex": keyword, "$options": "$i"}})
- keyfilters = append(keyfilters, bson.M{"from": bson.M{"$regex": keyword, "$options": "$i"}})
- filters = append(filters, bson.M{"$or": keyfilters})
- }
- delete(query, "keyword")
- }
- if query["categories"] != nil {
- categories := query["categories"].([]interface{})
- delete(query, "categories")
- //查询所有子内容
- if len(categories) > 0 {
- filterCaties := []string{}
- for _, c := range categories {
- if c != nil {
- if str, ok := c.(string); ok {
- filterCaties = append(filterCaties, str)
- }
- }
- }
- oldextends, andExtends := FindCategoryIds(cateConf, filterCaties)
- fmt.Println("old =>: ", oldextends)
- fmt.Println("and =>: ", andExtends)
- andArrayCons := []bson.M{}
- if len(andExtends) > 0 {
- for _, cateCon := range andExtends {
- if len(cateCon) > 0 {
- andArrayCons = append(andArrayCons, bson.M{"categories": bson.M{"$elemMatch": bson.M{"$in": cateCon}}})
- }
- }
- }
- if len(andArrayCons) > 0 {
- filters = append(filters, bson.M{"$and": andArrayCons})
- }
- }
- }
- if len(filters) > 0 {
- query["$and"] = filters
- }
- fmt.Printf("分类条件 =>: %#v\n", query)
- return nil
- }
|