package com.heyu.api.controller.imageseg;


import com.aliyun.imageseg20191230.models.SegmentCommonImageResponse;
import com.aliyun.imageseg20191230.models.SegmentCommonImageResponseBody;
import com.heyu.api.alibaba.handle.imageseg.ASegmentCommonImageHandle;
import com.heyu.api.alibaba.request.imageseg.ASegmentCommonImageRequest;
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.SegmentCommonImageResp;
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/SegmentCommonImage?tab=DOC&RegionId=cn-shanghai
 *
 *
 * 通用分割
 *
 * SegmentCommonImage
 *
 *
 * 功能描述
 * 通用分割能力可以识别输入图像中视觉中心的物体轮廓，将物体与背景进行分离，返回分割后的前景物体图（4 通道）。 关于该接口功能的示例图如下：
 */
@Slf4j
@RestController
@RequestMapping("/image/common")
@NotIntercept
public class SegmentCommonImageController extends BaseController {


    @Autowired
    private ASegmentCommonImageHandle segmentCommonImageHandle;


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

        SegmentCommonImageResp resp = new SegmentCommonImageResp();

        ApiR<SegmentCommonImageResponse> aR = segmentCommonImageHandle.handle(request);
        if (aR.isSuccess() && isSuccessStatusCode(aR.getData().getStatusCode())) {
            SegmentCommonImageResponseBody.SegmentCommonImageResponseBodyData responseBodyData = aR.getData().getBody().getData();

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

        }

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



