package com.heyu.api.controller.face;


import com.aliyun.facebody20191230.models.DetectBodyCountResponse;
import com.aliyun.facebody20191230.models.DetectBodyCountResponseBody;
import com.heyu.api.alibaba.handle.facebody.ADetectBodyCountHandle;
import com.heyu.api.alibaba.request.facebody.ADetectBodyCountRequest;
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.face.DetectBodyCountReq;
import com.heyu.api.resp.face.DetectBodyCountResp;
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/facebody/2019-12-30/DetectBodyCount?RegionId=cn-shanghai
 *
 * 人体计数
 *
 * DetectBodyCount
 *
 * 功能描述
 * 人体计数能力可以检测输入图片中人体的个数。
 *
 *
 */
@Slf4j
@RestController
@RequestMapping("/face")
@NotIntercept
public class DetectBodyCountController extends BaseController {

    @Autowired
    private ADetectBodyCountHandle aDetectBodyCountHandle;

    @RequestMapping("/count")
    @CacheResult
    public R count(DetectBodyCountReq req) {
        DetectBodyCountResp detectBodyCountResp = new DetectBodyCountResp();
        ADetectBodyCountRequest aDetectBodyCountRequest = new ADetectBodyCountRequest();
        aDetectBodyCountRequest.setImageBase64(req.getImageBase64());
        aDetectBodyCountRequest.setImageUrl(req.getImageUrl());

        ApiR<DetectBodyCountResponse> aR = aDetectBodyCountHandle.handle(aDetectBodyCountRequest);
        if (aR.isSuccess() && isSuccessStatusCode(aR.getData().getStatusCode())) {
            DetectBodyCountResponseBody responseBody = aR.getData().getBody();
            DetectBodyCountResponseBody.DetectBodyCountResponseBodyData responseBodyData = responseBody.getData();

            detectBodyCountResp.setPersonNumber(responseBodyData.getPersonNumber());

            return R.ok().setData(detectBodyCountResp);
        }
        return R.error(aR.getErrorMsg());
    }
}
