package com.bonus.face.utils;

import com.alibaba.fastjson.JSON;
import com.bonus.common.core.utils.file.FileUtils;
import com.bonus.common.core.utils.http.HttpRequestHelper;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.face.config.FaceConfig;
import com.bonus.face.domain.FaceDataEntity;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author bonus
 */
@Service
public class FaceUtils {

    @Resource
    private FaceConfig faceConfig;
    /**
     * 添加人脸路径
     */
    private static final String ADD_FACE_DATA = "/addFaceData";

    /**
     * 添加人脸
     */
    public AjaxResult addFace(FaceDataEntity faceData) {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("name", faceData.getName());
            params.put("idCard", faceData.getIdCard());
            if (faceData.getOtherInfo() != null && !faceData.getOtherInfo().isEmpty()) {
                params.put("otherInfo", faceData.getOtherInfo());
            }
            params.put("image", FileUtils.convertToBase64WithPrefix(faceData.getFile()));
            params.put("groupCode", faceData.getGroupCode());
            String s = HttpRequestHelper.postJson(faceConfig.getUrl(), ADD_FACE_DATA, JSON.toJSONString(params), new HashMap<String, String>());
            if (ObjectUtils.isNotEmpty(s)) {
                return AjaxResult.success("添加成功");
            } else {
                return AjaxResult.success("添加失败");
            }
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }
}
