a-service-img.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. package api
  2. import (
  3. "errors"
  4. "fmt"
  5. "sku3dweb/db/model"
  6. "sku3dweb/db/repo"
  7. "sku3dweb/log"
  8. "strconv"
  9. "strings"
  10. "time"
  11. "github.com/gin-gonic/gin"
  12. "go.mongodb.org/mongo-driver/bson"
  13. "go.mongodb.org/mongo-driver/bson/primitive"
  14. )
  15. func FassiApi(r *GinRouter) {
  16. r.POSTJWT("/image/create", createImg)
  17. r.GETJWT("/image/fassi/list", SearchByImg)
  18. r.GETJWT("/image/list", SearchByFields)
  19. r.POSTJWT("/image/delete/:id", DeleteImage)
  20. r.POSTJWT("/image/update", UpdateImage)
  21. }
  22. func UpdateImage(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  23. var matImage model.MatImage
  24. err := c.ShouldBindJSON(&matImage)
  25. if err != nil {
  26. log.Error(err)
  27. return nil, err
  28. }
  29. if matImage.Id.IsZero() {
  30. return nil, errors.New("id错误")
  31. }
  32. // searchMat := &model.MatImage{}
  33. // // 如果更新的是面料图
  34. // found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
  35. // CollectName: repo.CollectionMatImages,
  36. // Query: repo.Map{"_id": matImage.Id},
  37. // Project: []string{"rawImage"},
  38. // }, &searchMat)
  39. // if err != nil {
  40. // return nil, err
  41. // }
  42. // // 没有找到面料数据
  43. // if !found {
  44. // return nil, NewError("未找到数据")
  45. // }
  46. // // 未更改图片
  47. // if matImage.RawImage == nil {
  48. // return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionMatImages, matImage.Id.Hex(), &matImage)
  49. // }
  50. // // 更新了面料原图 对应更新fassi 特征数据
  51. // if searchMat.RawImage.Url != matImage.RawImage.Url {
  52. // // 先删除
  53. // _, err := RomoveFassiImage(matImage.Id.Hex())
  54. // if err != nil {
  55. // return nil, err
  56. // }
  57. // // 再新增
  58. // err = AddFassiImage(matImage.Id, matImage.RawImage.Url)
  59. // if err != nil {
  60. // return nil, err
  61. // }
  62. // }
  63. return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionMatImages, matImage.Id.Hex(), &matImage)
  64. }
  65. func DeleteImage(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  66. id := c.Param("id")
  67. _, err := RomoveFassiImage(id)
  68. if err != nil {
  69. return nil, err
  70. }
  71. return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionMatImages, id)
  72. }
  73. func SearchByFields(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  74. page, size, query := UtilQueryPageSize(c)
  75. // 查询所有分类
  76. cates := []*model.Category{}
  77. repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{CollectName: repo.CollectionCategory}, &cates)
  78. err := parseCategories(query, cates[0])
  79. if err != nil {
  80. return nil, err
  81. }
  82. pageOption := &repo.PageSearchOptions{
  83. CollectName: repo.CollectionMatImages,
  84. Page: page,
  85. Size: size,
  86. Query: query,
  87. // Project: []string{"nameCn", "createTime", "thumbnail", "price", "from"},
  88. // Sort: bson.M{"_id"},
  89. }
  90. return repo.RepoPageSearch(apictx.CreateRepoCtx(), pageOption)
  91. }
  92. func createImg(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  93. data := &model.MatImage{}
  94. err := c.ShouldBindJSON(data)
  95. if err != nil {
  96. return nil, err
  97. }
  98. data.CreateTime = time.Now()
  99. data.UpdateTime = time.Now()
  100. data.UserId, _ = primitive.ObjectIDFromHex(apictx.User.ID)
  101. idstr, err := repo.RepoAddDoc(apictx.CreateRepoCtx(), repo.CollectionMatImages, data)
  102. if err != nil {
  103. return nil, err
  104. }
  105. imgId, _ := primitive.ObjectIDFromHex(idstr)
  106. return imgId, AddFassiImage(imgId, data.RawImage.Url)
  107. }
  108. func SearchByImg(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  109. url := c.Query("url")
  110. _limit := c.Query("limit")
  111. _min_score := c.Query("min_score")
  112. _max_score := c.Query("max_score")
  113. limit, _ := strconv.Atoi(_limit)
  114. min_score, _ := strconv.ParseFloat(_min_score, 64)
  115. max_score, _ := strconv.ParseFloat(_max_score, 64)
  116. fmt.Println("search url =>: ", url)
  117. if !strings.Contains(url, "http") {
  118. return nil, NewError("参数错误")
  119. }
  120. images, err := QueryFassiImage(url, limit, min_score, max_score)
  121. if err != nil {
  122. return nil, err
  123. }
  124. var list = []map[string]interface{}{}
  125. if len(images) < 1 {
  126. return list, nil
  127. }
  128. var ids = []primitive.ObjectID{}
  129. for _, item := range images {
  130. ids = append(ids, item.Id)
  131. }
  132. param := &repo.DocSearchOptions{
  133. CollectName: repo.CollectionMatImages,
  134. Query: repo.Map{"_id": bson.M{"$in": ids}},
  135. }
  136. err = repo.RepoSeachDocs(apictx.CreateRepoCtx(), param, &list)
  137. if err != nil {
  138. return nil, err
  139. }
  140. var ImageMap = map[primitive.ObjectID]map[string]interface{}{}
  141. for _, item := range list {
  142. ImageMap[item["_id"].(primitive.ObjectID)] = item
  143. }
  144. var out = []interface{}{}
  145. for _, item := range images {
  146. value := ImageMap[item.Id]
  147. value["score"] = item.Score
  148. out = append(out, value)
  149. }
  150. return out, nil
  151. }
  152. type IteChildren func(n *model.Category) []string
  153. type FindChild func(n *model.Category, id string) []string
  154. func FindCategoryIds(c *model.Category, parents []string) ([]string, [][]string) {
  155. out := []string{}
  156. andOut := [][]string{}
  157. var allChildren IteChildren
  158. allChildren = func(n *model.Category) []string {
  159. ids := []string{}
  160. if n == nil {
  161. return ids
  162. }
  163. ids = append(ids, n.IdStr)
  164. if n.Children != nil {
  165. for _, c := range n.Children {
  166. cids := allChildren(c)
  167. if len(cids) > 0 {
  168. ids = append(ids, cids...)
  169. }
  170. }
  171. }
  172. return ids
  173. }
  174. var findChildrens FindChild
  175. findChildrens = func(n *model.Category, id string) []string {
  176. ids := []string{}
  177. if n == nil {
  178. return ids
  179. }
  180. if n.IdStr == id {
  181. ids = allChildren(n)
  182. return ids
  183. }
  184. if n.Children == nil {
  185. return ids
  186. }
  187. //查找孩子节点
  188. for _, c := range n.Children {
  189. ids = findChildrens(c, id)
  190. if len(ids) > 0 {
  191. break
  192. }
  193. }
  194. return ids
  195. }
  196. for _, v := range parents {
  197. for _, catnode := range c.Children {
  198. extends := findChildrens(catnode, v)
  199. if len(extends) > 0 {
  200. andOut = append(andOut, extends)
  201. out = append(out, extends...)
  202. break
  203. }
  204. }
  205. }
  206. return out, andOut
  207. }
  208. func parseCategories(query map[string]interface{}, cateConf *model.Category) error {
  209. filters := []bson.M{}
  210. keyfilters := []bson.M{}
  211. //keyword
  212. if query["keyword"] != nil {
  213. keyword := query["keyword"].(string)
  214. if len(keyword) > 0 {
  215. keyfilters = append(keyfilters, bson.M{"nameCn": bson.M{"$regex": keyword, "$options": "$i"}})
  216. keyfilters = append(keyfilters, bson.M{"nameEn": bson.M{"$regex": keyword, "$options": "$i"}})
  217. keyfilters = append(keyfilters, bson.M{"from": bson.M{"$regex": keyword, "$options": "$i"}})
  218. filters = append(filters, bson.M{"$or": keyfilters})
  219. }
  220. delete(query, "keyword")
  221. }
  222. if query["categories"] != nil {
  223. categories := query["categories"].([]interface{})
  224. delete(query, "categories")
  225. //查询所有子内容
  226. if len(categories) > 0 {
  227. filterCaties := []string{}
  228. for _, c := range categories {
  229. if c != nil {
  230. if str, ok := c.(string); ok {
  231. filterCaties = append(filterCaties, str)
  232. }
  233. }
  234. }
  235. oldextends, andExtends := FindCategoryIds(cateConf, filterCaties)
  236. fmt.Println("old =>: ", oldextends)
  237. fmt.Println("and =>: ", andExtends)
  238. andArrayCons := []bson.M{}
  239. if len(andExtends) > 0 {
  240. for _, cateCon := range andExtends {
  241. if len(cateCon) > 0 {
  242. andArrayCons = append(andArrayCons, bson.M{"categories": bson.M{"$elemMatch": bson.M{"$in": cateCon}}})
  243. }
  244. }
  245. }
  246. if len(andArrayCons) > 0 {
  247. filters = append(filters, bson.M{"$and": andArrayCons})
  248. }
  249. }
  250. }
  251. if len(filters) > 0 {
  252. query["$and"] = filters
  253. }
  254. fmt.Printf("分类条件 =>: %#v\n", query)
  255. return nil
  256. }