package cn.hmsoft.hmplatform.identity.control;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Base64;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.hmsoft.ai.identity.IdentifiedResult;
import cn.hmsoft.application.web.AjaxControl;
import cn.hmsoft.frame.exception.BusinessException;
import cn.hmsoft.frame.util.FrameAssertUtil;
import cn.hmsoft.helper.SecureHelper;
import cn.hmsoft.hmplatform.helper.PlatformParamHelper;
import cn.hmsoft.hmplatform.identity.service.FaceMatchService;

@RestController
public class FaceMatchControl extends AjaxControl {

	@Autowired
	private FaceMatchService serviceMatch;
	
	/**
	 * @人脸比对.
	 * @param cert_id 身份证号
	 * @param img1 照片1
	 * @param img2照片2
	 * @param key_pass 秘钥
	 * @return
	 */
	@RequestMapping("baidu/face/match") 
	public IdentifiedResult baduMatch(String img1, String img2, String key_pass) {
		FrameAssertUtil.isNotNull(img1, "参数img1不能为空");
		FrameAssertUtil.isNotNull(img2, "参数img2不能为空");
		FrameAssertUtil.isNotNull(key_pass, "参数key_pass不能为空");
		if (!key_pass.equals(SecureHelper.md5(PlatformParamHelper.FaceMatchPass))) {
			throw new BusinessException("秘钥错误,没有操作权限!");
		}
		return this.serviceMatch.baiduMatch(img1, img2); 
	}
	
	/**
	 * @人脸比对.
	 * @param cert_id 身份证号
	 * @param img1 照片1
	 * @param img2照片2
	 * @param key_pass 秘钥
	 * @return
	 */
	@RequestMapping("hm/face/match") 
	public IdentifiedResult hmMatch(String img1, String img2, String key_pass) {
		FrameAssertUtil.isNotNull(img1, "参数img1不能为空");
		FrameAssertUtil.isNotNull(img2, "参数img2不能为空");
		FrameAssertUtil.isNotNull(key_pass, "参数key_pass不能为空");
		if (!key_pass.equals(SecureHelper.md5(PlatformParamHelper.FaceMatchPass))) {
			throw new BusinessException("秘钥错误,没有操作权限!");
		}
		return this.serviceMatch.hmFaceMatch(img1, img2, key_pass); 
	}
	
	public static void main(String[] args) throws FileNotFoundException, IOException {
		System.out.println(Base64.getEncoder().encodeToString(org.apache.commons.io.IOUtils.toByteArray(new FileInputStream(new File("C:/Users/Lenovo/Desktop/1.png")))));

	}
}
