package com.bonus.canteen.core.user.service.impl;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import cn.hutool.core.util.ObjectUtil;
import com.bonus.canteen.core.common.utils.FileUrlUtil;
import com.bonus.canteen.core.common.utils.MqUtil;
import com.bonus.canteen.core.user.domain.DeviceMqPersonalUpdateMessageDTO;
import com.bonus.canteen.core.user.utils.ArcFaceHelper;
import com.bonus.canteen.core.user.utils.FaceResult;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.core.utils.DateUtils;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.houqin.mq.constant.LeMqConstant;
import com.bonus.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bonus.canteen.core.user.mapper.UserFaceMapper;
import com.bonus.canteen.core.user.domain.UserFace;
import com.bonus.canteen.core.user.service.IUserFaceService;

/**
 * 人员生物识别特征Service业务层处理
 *
 * @author xsheng
 * @date 2025-04-03
 */
@Service
public class UserFaceServiceImpl implements IUserFaceService {
    @Autowired
    private UserFaceMapper UserFaceMapper;

    /**
     * 查询人员生物识别特征
     *
     * @param dto 人员生物识别特征主键
     * @return 人员生物识别特征
     */
    @Override
    public AjaxResult uploadUserFace(UserFace dto) {
        if (ObjectUtil.isNull(dto)) {
            throw new ServiceException("参数为空！");
        }
        if (ObjectUtil.isNull(dto.getUserId())) {
            throw new ServiceException("用户ID为空！");
        }
        if (ObjectUtil.isNull(dto.getPhotoUrl())) {
            throw new ServiceException("图片地址为空！");
        }

        System.out.println("开始采集虹软人脸");
        ArcFaceHelper arcFaceHelper = new ArcFaceHelper();
        String url = FileUrlUtil.getFileUrl(dto.getPhotoUrl());
        FaceResult faceResult = arcFaceHelper.getFaceFeatures(url);
        System.err.println("采集结束");
        List<UserFace> list = new ArrayList<>();
        if (faceResult != null) {
            UserFace faceVO = new UserFace();
            faceVO.setErrorMsg("[软件]成功");
            faceVO.setFeatures(Base64.getEncoder().encodeToString(faceResult.getFeatures()));
            faceVO.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
            faceVO.setUserId(dto.getUserId());
            faceVO.setPhotoUrl(dto.getPhotoUrl());
            faceVO.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
            faceVO.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
            faceVO.setCreateTime(DateUtils.getTime());
            faceVO.setUpdateTime(DateUtils.getTime());
            list.add(faceVO);
            System.out.println("faceResult.getFeatures():" + Base64.getEncoder().encodeToString(faceResult.getFeatures()));
            System.out.println("人脸采集成功");
        } else {
            throw new ServiceException("[虹软算法]：特征值提取失败");
        }
        int code = UserFaceMapper.insert(list.get(0));
        if (code == 0){
            throw new ServiceException("[虹软算法]：保存失败");
        }
        DeviceMqPersonalUpdateMessageDTO bean = new DeviceMqPersonalUpdateMessageDTO().setUpdatePersonPhoto(1);
        System.out.println(bean.toString());
        //发送mq
        MqUtil.pushToTenantAllDevice(bean, LeMqConstant.Topic.DEVICE_UPDATE_PERSONAL_CONFIG_V4);
        return AjaxResult.success();
    }
}
