package com.heyu.api.controller.imagerecog;


import com.aliyun.imagerecog20190930.models.TaggingAdImageResponse;
import com.aliyun.imagerecog20190930.models.TaggingAdImageResponseBody;
import com.heyu.api.alibaba.handle.imagerecog.ATaggingAdImageHandle;
import com.heyu.api.alibaba.request.imagerecog.ATaggingAdImageRequest;
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.imagerecog.TaggingAdImageReq;
import com.heyu.api.resp.imagerecog.TaggingAdImageResp;
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/imagerecog/2019-09-30/TaggingAdImage?tab=DOC&lang=JAVA&RegionId=cn-shanghai
 * 广告素材分析 TaggingAdImage
 *
 */
@Slf4j
@RestController
@RequestMapping("/image")
@NotIntercept
public class TaggingAdImageController extends BaseController {


    @Autowired
    private ATaggingAdImageHandle ataggingAdImageHandle;


    @RequestMapping("/taggingAd")
    @CacheResult
    public R recognize(TaggingAdImageReq req) {
        TaggingAdImageResp resp = new TaggingAdImageResp();
        ATaggingAdImageRequest request = new ATaggingAdImageRequest();
        request.setImageBase64(req.getImageBase64());
        request.setImageUrl(req.getImageUrl());

        ApiR<TaggingAdImageResponse> aR = ataggingAdImageHandle.handle(request);
        if (aR.isSuccess() && isSuccessStatusCode(aR.getData().getStatusCode())) {
            TaggingAdImageResponseBody.TaggingAdImageResponseBodyData responseBodyData = aR.getData().getBody().getData();
            resp.setTagMap(responseBodyData.getTagInfo());
            return R.ok().setData(resp);

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


}
