package com.heyu.api.controller.imageseg;


import com.aliyun.imageseg20191230.models.SegmentCommodityResponse;
import com.aliyun.imageseg20191230.models.SegmentCommodityResponseBody;
import com.heyu.api.alibaba.handle.imageseg.ASegmentCommodityHandle;
import com.heyu.api.alibaba.request.imageseg.ASegmentCommodityRequest;
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.resp.imageseg.SegmentCommodityResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/***
 *https://next.api.aliyun.com/api/imageseg/2019-12-30/SegmentCommodity?RegionId=cn-shanghai
 *
 *
 * 商品分割
 *
 * SegmentCommodity
 *
 *功能描述
 * 商品分割能力用于识别输入图像中的商品轮廓，与背景进行分离，返回分割后的前景商品图（4 通道）。功能适用于实景图，不适用于卡通图片。商品分割主要针对拍到全貌的商品进行分割。 关于该接口功能的示例图如下：
 *
 *
 *
 */
@Slf4j
@RestController
@RequestMapping("/goods")
@NotIntercept
public class SegmentCommodityController extends BaseController {


    @Autowired
    private ASegmentCommodityHandle segmentCommodityHandle;

    @RequestMapping("/segment")
    @CacheResult
    public R segment(ASegmentCommodityRequest request) {

        SegmentCommodityResp resp = new SegmentCommodityResp();
        ApiR<SegmentCommodityResponse> aR = segmentCommodityHandle.handle(request);
        if (aR.isSuccess() && isSuccessStatusCode(aR.getData().getStatusCode())) {
            SegmentCommodityResponseBody.SegmentCommodityResponseBodyData responseBodyData = aR.getData().getBody().getData();

            resp.setImageURL(responseBodyData.getImageURL());
            return R.ok().setData(resp);

        }

        return R.error();
    }
}
