package com.bonus.bracelet.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.shaded.com.google.common.collect.Maps;
import com.bonus.bracelet.service.FaceInterService;
import com.bonus.common.core.utils.FaceCodeUtil;
import com.bonus.common.core.utils.StringUtils;
import com.bonus.common.core.utils.http.HttpUtils;
import com.bonus.common.core.web.domain.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
@Slf4j
public class FaceInterServiceImpl  implements FaceInterService {

        /**
         * 新增人脸库
         */
      public  final static String addFace="updatedb";

      @Value("${face.path}")
      public  String faceUrl;

        /**
         * 刷新人脸库
         */
      public  final static String refresh="refreshdb";
        /**
         * 识别人脸库
         */
      public  final static String sbface="facerecognition";


    public static void main(String[] args) {

    }
    public AjaxResult addFace2(String image, String type, String id) {
        Map<String,Object> result= Maps.newHashMap();
        try{
            Map<String,Object> map= Maps.newHashMap();
            map.put("img",image);
            map.put("optMode",type);
            map.put("uniqueKey",id);
            String msg= HttpUtils.sendRequest(faceUrl+addFace,JSON.toJSONString(map),true);
            if(StringUtils.isNotEmpty(msg)){
                JSONObject json=JSON.parseObject(msg);
                String code=json.getString("code");
                String eMsg=json.getString("msg");
                if("30002".equals(code)){
                    result.put("code",200);
                }else {
                    result.put("code",201);
                }
                String msgCN= FaceCodeUtil.getCodeString(code);
                result.put("msg",msgCN);
                result.put("eMsg",eMsg);
            }else{
                result.put("code",201);
                result.put("msg","人脸识别接口异常!请检查人脸服务!");
            }
            AjaxResult.success(result);
        }catch (Exception e){
            log.error(e.toString(),e);
        }
        return AjaxResult.error("人脸识别接口异常!请检查人脸服务!");

    }
    /**
     * 人脸信进行修改
     * @param image
     * @param type
     * @param id  ls-
     *            bz-
     * @return
     */
    @Override
    public AjaxResult addFace(String image, String type, String id) {
        Map<String,Object> result= Maps.newHashMap();
        try{
            Map<String,Object> map= Maps.newHashMap();
            map.put("img",image);
            map.put("optMode",type);
            map.put("uniqueKey",id);
            String msg= HttpUtils.sendRequest(faceUrl+addFace,JSON.toJSONString(map),true);
            if(StringUtils.isNotEmpty(msg)){
                JSONObject json=JSON.parseObject(msg);
                String code=json.getString("code");
                String eMsg=json.getString("msg");
                if("30002".equals(code)){
                    result.put("code",200);
                }else {
                    result.put("code",201);
                }
                String msgCN= FaceCodeUtil.getCodeString(code);
                result.put("msg",msgCN);
                result.put("eMsg",eMsg);
            }else{
                result.put("code",201);
                result.put("msg","人脸识别接口异常!请检查人脸服务!");
            }
            return AjaxResult.success(result);
        }catch (Exception e){
            log.error(e.toString(),e);
        }
        return AjaxResult.error("人脸识别接口异常!请检查人脸服务!");

    }

    /**
     * 刷新图像库
     * @return
     */
    @Override
    public AjaxResult refreshFace() {
        Map<String,Object> result= Maps.newHashMap();
        try{
            Map<String,Object> map= Maps.newHashMap();
            String msg= HttpUtils.sendRequest(faceUrl+refresh,JSON.toJSONString(map),true);
            if(StringUtils.isNotEmpty(msg)){
                JSONObject json=JSON.parseObject(msg);
                String code=json.getString("code");
                String eMsg=json.getString("msg");
                if("30002".equals(code)){
                    result.put("code",200);
                    result.put("msg","操作成功");
                }else {
                    String msgCN= FaceCodeUtil.getCodeString(code);
                    result.put("msg",msgCN);
                    result.put("code",201);
                }
                result.put("eMsg",eMsg);
            }else{
                result.put("code",201);
                result.put("msg","人脸识别接口异常!请检查人脸服务!");
            }
            AjaxResult.success(result);
        }catch (Exception e){
            log.error(e.toString(),e);
        }
        return AjaxResult.error("人脸识别接口异常!请检查人脸服务!");
    }

    @Override
    public AjaxResult verifyFace(String image) {
        Map<String,Object> result= Maps.newHashMap();
        try{
            Map<String,Object> map= Maps.newHashMap();
            map.put("img",image);
            String msg= HttpUtils.sendRequest(faceUrl+sbface,JSON.toJSONString(map),true);
            if(StringUtils.isNotEmpty(msg)){
                JSONObject json=JSON.parseObject(msg);
                String code=json.getString("code");
                String eMsg=json.getString("msg");
                //识别成功返回的数据
                if("30000".equals(code)){
                    JSONArray jsonArray=json.getJSONArray("data");
                    Object key= jsonArray.get(0);
                    result.put("code",201);
                    result.put("userId",key);
                    result.put("msg","人脸识别成功");
                }else{
                    result.put("code",201);
                    String msgCN= FaceCodeUtil.getCodeString(code);
                    result.put("msg",msgCN);
                    result.put("eMsg",eMsg);
                    result.put("userId","-9999");
                }
            }else{
                result.put("code",201);
                result.put("msg","人脸识别接口异常!请检查人脸服务!");
            }
            AjaxResult.success(result);
        }catch (Exception e){
            log.error(e.toString(),e);
        }
        return AjaxResult.error("人脸识别接口异常!请检查人脸服务!");

    }
}
