package org.security.desk.controller;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import org.security.common.utils.CommonUtil;
import org.security.config.FaceProperties;
import org.security.desk.service.FaceService;
import org.security.desk.vo.FaceVO;
import org.security.imagePlatformDock.service.IUploadFileRecordService;
import org.security.system.user.entity.User;
import org.security.system.user.feign.IUserClient;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Calendar;

/**
 * @author zj 2022/9/1
 */
@Slf4j
@AllArgsConstructor
@RequestMapping("/face")
@RestController
public class FaceMatchController {
	private final FaceService faceService;
	private final IUploadFileRecordService uploadFileRecordService;
	private final IUserClient userClient;
	private final FaceProperties faceProperties;

	/**
	 * 人脸识别
	 * 调用esb两证一脸6012012501
	 *
	 * @return
	 */
	@PostMapping("/match")
	public R<FaceVO> faceRecognition(@RequestPart("file") MultipartFile file) {
		String faceImg = CommonUtil.multipartFileToBase64(file);
		String fileName = file.getOriginalFilename();
		String formatName = fileName.substring(fileName.lastIndexOf(".") + 1);
		if (faceImg.length() == 0) {
			return R.fail("传入文件有误");
		}
		if (faceImg.length() > 40000) {
			try {
				faceImg = CommonUtil.compressBase64Image(formatName, faceImg, 40000, null, null);
			} catch (IOException e) {
				e.printStackTrace();
				return R.fail(e.getMessage());
			}
		}
		return faceService.faceMatch(faceImg);
	}

	@PostMapping("/register")
	public R<FaceVO> faceRegister(@RequestPart("faceVo") FaceVO faceVO, @RequestPart("file") MultipartFile file) {
		Calendar now = Calendar.getInstance();
		Calendar start = Calendar.getInstance();
		start.set(now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DAY_OF_MONTH), 7, 30, 0);
		Calendar end = Calendar.getInstance();
		end.set(now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DAY_OF_MONTH), 19, 30, 0);
		if (now.before(start) || now.after(end)) {
			return R.fail("此接口由于需要联网核查，只在7:30至19:30开放");
		}
		String base64File = CommonUtil.multipartFileToBase64(file);
		String fileName = file.getOriginalFilename();
		String formatName = fileName.substring(fileName.lastIndexOf(".") + 1);
		if (base64File.length() == 0) {
			return R.fail("传入文件有误");
		}
		if (faceVO.isOutsiders()) {
			if (Func.isEmpty(faceVO.getFullName())) {
				return R.fail("录入行外用户时姓名不能为空");
			}
			faceVO.setEmployeeNo(faceProperties.getVirtualTelNo());
			faceVO.setDeptNo(faceProperties.getLeadDeptNo());
			faceVO.setIdentTp("0100");
			faceVO.setIdentNo(faceVO.getIdCardNo());
		} else {
			R<User> userByAccount = userClient.userByAccount("000000", faceVO.getEmployeeNo());
			if (!userByAccount.isSuccess() || Func.isEmpty(userByAccount.getData())) {
				return R.fail("为查询到工号为" + userByAccount.getData().getAccount() + "的用户");
			} else if (!userByAccount.getData().getName().equals(faceVO.getFullName())) {
				return R.fail("员工工号" + faceVO.getEmployeeNo() + "与姓名" + faceVO.getFullName() + "不匹配");
			}
			String deptCode = uploadFileRecordService.getDeptCode(AuthUtil.getDeptId());
			if (Func.isEmpty(deptCode)) {
				return R.fail("查询用户" + AuthUtil.getUserName() + "的部门" + AuthUtil.getDeptId() + "编号失败");
			}
			faceVO.setDeptNo(deptCode);
			faceVO.setIdentTp(faceProperties.getTelIdentTp());
			faceVO.setIdentNo(faceVO.getEmployeeNo());
		}
		if (base64File.length() > 40000) {
			try {
				String faceImg = CommonUtil.compressBase64Image(formatName, base64File, 40000, null, null);
				faceVO.setFaceImg(faceImg);
			} catch (IOException e) {
				log.error(e.getMessage(), e);
				return R.fail(e.getMessage());
			}
		}
		return faceService.faceRegister(faceVO);
	}

}
