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.GenderEnum;
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.model.req.face.FaceInfo;
import com.mortals.xhx.module.hik.face.model.req.face.FacePic;
import com.mortals.xhx.module.hik.face.model.req.face.FaceReq;
import com.mortals.xhx.module.hik.face.model.rsp.face.FaceDataInfo;
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.Date;
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);
                        FaceReq faceReq = new FaceReq();
                        FaceInfo faceInfo = new FaceInfo();
                        faceInfo.setName(personEntity.getName());
                        faceInfo.setSex(personEntity.getGender() == GenderEnum.男.getValue() ? "1" : "2");
                        faceInfo.setCertificateType("111");
                        faceInfo.setCertificateNum(personEntity.getIdCard());
                        faceReq.setFaceInfo(faceInfo);
                        FacePic facePic = new FacePic();
                        facePic.setFaceBinaryData(picBase64Data);
                        faceReq.setFacePic(facePic);


                        Rest<FaceDataInfo> faceDataInfoRest = hikFaceService.faceSingleAdd(faceReq);

                        if(faceDataInfoRest.getCode()==YesNoEnum.YES.getValue()){
                            personEntity.setIndexCode(faceDataInfoRest.getData().getIndexCode());
                            personEntity.setIfaceGroupIndexCode(faceDataInfoRest.getData().getFaceGroupIndexCode());
                            personEntity.setUpdateTime(new Date());
                            personEntity.setUpdateUser("admin");
                            personEntity.setUpdateUserId(1L);
                            personService.update(personEntity);

                        }



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

                    }


                }
                // FileUtil.read

            }
        }


    }


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

    }


}
