PhotoSceneController.java 4.34 KB
package com.yoho.search.restapi.scene;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yoho.search.models.SearchApiResult;
import com.yoho.search.service.scene.others.photo.BigDataSknImageVectorsService;
import com.yoho.search.service.scene.others.photo.SknImageVectorsService;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
public class PhotoSceneController {

    private static final Logger logger = LoggerFactory.getLogger(PhotoSceneController.class);

    private static final String VECTORS_32_KEY = "vectors_32";
    private static final String VECTORS_128_KEY = "vectors_128";
    private static final String PRODUCT_SKN = "product_skn";

    @Autowired
    private SknImageVectorsService sknImageVectorsService;
    @Autowired
    private BigDataSknImageVectorsService bigDataSknImageVectorsService;

    /**
     * 拍照购找相似接口(新)
     */
    @RequestMapping(method = RequestMethod.POST, value = "/findSimilarPhoto")
    @ResponseBody
    public SearchApiResult findSimilarPhoto(@RequestBody JSONObject jsonObject) {
        try {
            String params = MapUtils.getString(jsonObject, "params");
            JSONArray jsonArray = JSONArray.parseArray(params);
            if (jsonArray.size() == 0) {
                return new SearchApiResult().setCode(400).setMessage("入参不能为空");
            }
            return bigDataSknImageVectorsService.search(jsonArray);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return new SearchApiResult().setCode(400).setMessage("服务异常");
        }
    }

    /**
     * 拍照购找相似接口(老)
     */
    @RequestMapping(method = RequestMethod.POST, value = "/searchSknByPhoto")
    @ResponseBody
    public SearchApiResult searchSknByPhoto(@RequestBody JSONObject jsonObject) {
        try {
            String vectors_32 = jsonObject.get(VECTORS_32_KEY) == null ? null : jsonObject.get(VECTORS_32_KEY).toString();
            String vectors_128 = jsonObject.get(VECTORS_128_KEY) == null ? null : jsonObject.get(VECTORS_128_KEY).toString();
            if (StringUtils.isBlank(vectors_32) || StringUtils.isBlank(vectors_128)) {
                return new SearchApiResult().setCode(400).setMessage("vectors_32或vectors_128不能为空");
            }
            logger.warn("vectors_32 is [{}],vectors_128 is[{}]", vectors_32, vectors_128);
            Map<String, String> paramMap = new HashMap<>();
            paramMap.put(VECTORS_32_KEY, vectors_32);
            paramMap.put(VECTORS_128_KEY, vectors_128);
            paramMap.put("viewNum", jsonObject.get("viewNum") == null ? "20" : jsonObject.get("viewNum").toString());
            return sknImageVectorsService.searchSknByPhoto(paramMap);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return new SearchApiResult().setCode(400).setMessage("服务异常");
        }
    }

    @RequestMapping(method = RequestMethod.POST, value = "/searchListByPhotoSkns")
    @ResponseBody
    public SearchApiResult searchListBySkns(@RequestBody JSONObject jsonObject) {
        try {
            String product_skn = jsonObject.get(PRODUCT_SKN) == null ? null : jsonObject.get(PRODUCT_SKN).toString();
            if (StringUtils.isBlank(product_skn)) {
                return new SearchApiResult().setCode(400).setMessage("product_skn不能为空");
            }
            Map<String, String> paramMap = new HashMap<>();
            paramMap.put("product_skn", product_skn);
            paramMap.put("viewNum", jsonObject.get("viewNum") == null ? "20" : jsonObject.get("viewNum").toString());
            return sknImageVectorsService.searchListByPhotoSkns(paramMap);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return new SearchApiResult().setCode(400).setMessage("服务异常");
        }
    }


}