package com.mortals.xhx.daemon.task;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson.JSON;
import com.mortals.framework.common.Rest;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.service.ITask;
import com.mortals.framework.service.ITaskExcuteService;
import com.mortals.xhx.base.system.upload.service.UploadService;
import com.mortals.xhx.base.system.user.model.UserEntity;
import com.mortals.xhx.base.system.user.model.UserQuery;
import com.mortals.xhx.base.system.user.service.UserService;
import com.mortals.xhx.common.code.YesNoEnum;
import com.mortals.xhx.common.pdu.RespData;
import com.mortals.xhx.common.pdu.user.UserPdu;
import com.mortals.xhx.feign.user.IUserFeign;
import com.mortals.xhx.module.hik.face.service.IHikFaceService;
import com.mortals.xhx.module.person.model.PersonEntity;
import com.mortals.xhx.module.person.model.PersonQuery;
import com.mortals.xhx.module.person.service.PersonService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * 同步用户到海康指定库。
 */
@Slf4j
@Service("SyncUserToHikTask")
public class SyncUserToHikTaskImpl implements ITaskExcuteService {

    @Autowired
    private PersonService personService;
    @Autowired
    private IHikFaceService hikFaceService;
    @Autowired
    private UploadService uploadService;

    @Override
    public void excuteTask(ITask task) throws AppException {
        log.info("同步用户人脸到海康任务");
        List<PersonEntity> personUnHikList = personService.find(new PersonQuery().inFaceHk(YesNoEnum.NO.getValue()));
        if (!ObjectUtils.isEmpty(personUnHikList)) {
            for (PersonEntity personEntity : personUnHikList) {
                //查询图片并转成base64
                if (!ObjectUtils.isEmpty(personEntity.getPhoto())) {
                    String filePath = uploadService.getFilePath(personEntity.getPhoto());
                    byte[] bytes = FileUtil.readBytes(filePath);
                    if (bytes.length > 0) {
                        String picBase64Data = Base64.encode(bytes);

                        //log.info("picBase64Data=>{}", picBase64Data);

                    }


                }
                // FileUtil.read

            }
        }


    }


    @Override
    public void stopTask(ITask task) throws AppException {

    }


}
