package org.security.desk.service.impl;


import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.security.common.constant.CommonConstant;
import org.security.desk.dto.FaceMatchIn;
import org.security.desk.dto.FaceMatchOut;
import org.security.desk.dto.FaceRegisterIn;
import org.security.desk.dto.FaceRegisterOut;
import org.security.desk.esb.EsbHttp;
import org.security.desk.esb.HttpEsbResponse;
import org.security.desk.esb.request.EsbRequest;
import org.security.desk.esb.response.EsbResponse;

import org.security.desk.service.FaceService;
import org.security.desk.vo.FaceVO;
import org.springblade.core.tool.api.R;
import org.springframework.stereotype.Service;

/**
 * @author zj 2022/9/1
 */
@AllArgsConstructor
@Service
@Slf4j
public class FaceServiceImpl implements FaceService {
	private final EsbHttp esbHttp;

	@Override
	public R<FaceVO> faceRegister(FaceVO faceVO) {
		FaceRegisterIn faceRegisterIn = new FaceRegisterIn();
		faceRegisterIn.setLibCode(CommonConstant.FACE_LIB_CODE);
		faceRegisterIn.setSceCod(CommonConstant.FACE_SCE_CODE);
		faceRegisterIn.setIdentTp(faceVO.getIdentTp());
		faceRegisterIn.setIdCrdNo(faceVO.getIdCardNo());
		faceRegisterIn.setIdentNo(faceVO.getIdentNo());
		faceRegisterIn.setFulNam(faceVO.getFullName());
		faceRegisterIn.setRgstTp("0");
		faceRegisterIn.setTransTp("2");
		faceRegisterIn.setUserTp("2");
		faceRegisterIn.setPicInfor(faceVO.getFaceImg());
		EsbRequest esbRequest = EsbRequest.creatEsbRequest(" 60120120", "01", "41023", faceRegisterIn);
		esbRequest.getAPP_HEAD().setTlrNo(faceVO.getEmployeeNo());
		esbRequest.getAPP_HEAD().setBranchId(faceVO.getDeptNo());
		log.info(JSONObject.toJSONString(esbRequest));
		HttpEsbResponse httpEsbResponse = esbHttp.creatEsbHttpPost(esbRequest);
		log.info(JSONObject.toJSONString(httpEsbResponse));
		if (!httpEsbResponse.isSuccess()) {
			return R.fail("人脸录入失败, " + httpEsbResponse.getEsbResponse().getBODY().toString());
		}
		EsbResponse esbResponse = httpEsbResponse.getEsbResponse();
		FaceRegisterOut faceRegisterOut = JSONUtil.toBean((cn.hutool.json.JSONObject) esbResponse.getBODY(), FaceRegisterOut.class);
		if (!faceRegisterOut.getRetRslt().equals("0")) {
			return R.fail("人脸录入失败，" + faceRegisterOut.getRestDescn());
		}
		return R.data(faceVO);
	}

	@Override
	public R<FaceVO> faceMatch(String faceImg) {
		FaceMatchIn faceMatchIn = new FaceMatchIn();
		faceMatchIn.setLibCode(CommonConstant.FACE_LIB_CODE);
		faceMatchIn.setSceCod(CommonConstant.FACE_SCE_CODE);
		faceMatchIn.setSpotImg(faceImg);
		EsbRequest esbRequest = EsbRequest.creatEsbRequest(" 60120114", "01", "41023", faceMatchIn);
		HttpEsbResponse httpEsbResponse = esbHttp.creatEsbHttpPost(esbRequest);
		if (!httpEsbResponse.isSuccess()) {
			return R.fail("人脸比对失败，" + httpEsbResponse.getEsbResponse().getBODY().toString());
		}
		EsbResponse esbResponse = httpEsbResponse.getEsbResponse();
		FaceMatchOut faceMatchOut = JSONUtil.toBean((cn.hutool.json.JSONObject) esbResponse.getBODY(), FaceMatchOut.class);
		if (!faceMatchOut.getRetRslt().equals("0")) {
			return R.fail("人脸比对失败，" + faceMatchOut.getRestDescn());
		}
		FaceVO faceVO = new FaceVO();
		faceVO.setOutsiders("0100".equals(faceMatchOut.getIdentTp()));
		faceVO.setIdentNo(faceMatchOut.getIdentNo());
		faceVO.setIdCardNo(faceMatchOut.getIdentNo());
		faceVO.setFullName(faceMatchOut.getFulNam());
		return R.data(faceVO);
	}

}
