package com.heyu.api.controller.good;


import com.aliyun.goodstech20191230.models.ClassifyCommodityResponse;
import com.aliyun.goodstech20191230.models.ClassifyCommodityResponseBody;
import com.heyu.api.alibaba.handle.goodstech.AClassifyCommodityHandle;
import com.heyu.api.alibaba.request.goodstech.AClassifyCommodityRequest;
import com.heyu.api.controller.BaseController;
import com.heyu.api.data.annotation.CacheResult;
import com.heyu.api.data.annotation.NotIntercept;
import com.heyu.api.data.utils.ApiR;
import com.heyu.api.data.utils.R;
import com.heyu.api.request.good.ClassifyCommodityReq;
import com.heyu.api.resp.good.ClassifyCommodityResp;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/***
 *https://next.api.aliyun.com/api/goodstech/2019-12-30/ClassifyCommodity?lang=JAVA&RegionId=cn-shanghai&tab=DOC
 *
 *'
 *
 *
 商品分类

 ClassifyCommodity

 *
 * 接口说明
 * 功能描述
 * 商品分类能力可以识别图像中的商品分类，返回商品类目、置信度等信息。目前已经支持服饰鞋包、3C 数码、家居用品等超过六千种类目分类。
 *
 * 所属于的类目：
 *
 *
 * https://viapi-demo.oss-cn-shanghai.aliyuncs.com/doc/category.txt?spm=api-workbench.api_explorer.0.0.e67a1634kpvqtJ&file=category.txt
 *
 *
 */
@Slf4j
@RestController
@RequestMapping("/good")
@NotIntercept
public class ClassifyCommodityController extends BaseController {


    @Autowired
    private AClassifyCommodityHandle aClassifyCommodityHandle;


    @RequestMapping("/classify")
    @CacheResult
    public R recognize(ClassifyCommodityReq req) {
        List<ClassifyCommodityResp> resp = new ArrayList<>();

        AClassifyCommodityRequest request = new AClassifyCommodityRequest();
        request.setImageBase64(req.getImageBase64());
        request.setImageUrl(req.getImageUrl());


        ApiR<ClassifyCommodityResponse> aR = aClassifyCommodityHandle.handle(request);
        if (aR.isSuccess() && isSuccessStatusCode(aR.getData().getStatusCode())) {
            ClassifyCommodityResponseBody.ClassifyCommodityResponseBodyData responseBodyData = aR.getData().getBody().getData();


            List<ClassifyCommodityResponseBody.ClassifyCommodityResponseBodyDataCategories> categories = responseBodyData.getCategories();

            if (CollectionUtils.isNotEmpty(categories)) {
                for (ClassifyCommodityResponseBody.ClassifyCommodityResponseBodyDataCategories category : categories) {
                    ClassifyCommodityResp classifyCommodityResp = new ClassifyCommodityResp();
                    classifyCommodityResp.setScore(category.getScore());
                    classifyCommodityResp.setCategoryName(category.getCategoryName());
                    classifyCommodityResp.setCategoryId(category.getCategoryId());

                    resp.add(classifyCommodityResp);
                }

            }

            return R.ok().setData(resp);

        }

        return R.error(aR.getErrorMsg());
    }
}
