package com.mortals.xhx.daemon.task;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.io.FileUtil;
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.common.code.GenderEnum;
import com.mortals.xhx.common.code.YesNoEnum;
import com.mortals.xhx.module.hik.face.model.FaceGroupEntity;
import com.mortals.xhx.module.hik.face.model.FaceGroupPersonEntity;
import com.mortals.xhx.module.hik.face.model.FaceGroupQuery;
import com.mortals.xhx.module.hik.face.service.FaceGroupPersonService;
import com.mortals.xhx.module.hik.face.service.FaceGroupService;
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.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;

import static com.mortals.xhx.common.key.ParamKey.PARAM_FACE_GROUP_REGISTER;

/**
 * 同步用户人脸数据到海康指定库。
 * 时间间隔以同步注册用户之后
 * 默认同步到一个人脸用户组，如要其它用户组 则页面上进行添加
 * 需要根据计划进行同步人脸链接
 * 默认用户组 主要是陌生人识别 不在当前库中的 都是陌生人
 */
@Slf4j
@Service("SyncUserToHikTask")
@ConditionalOnExpression("'${hik.host:}'!=''")
public class SyncUserToHikTaskImpl implements ITaskExcuteService {

    @Autowired
    private PersonService personService;
    @Autowired
    private IHikFaceService hikFaceService;
    @Autowired
    private UploadService uploadService;
    @Autowired
    private FaceGroupService faceGroupService;
    @Autowired
    private FaceGroupPersonService faceGroupPersonService;

    @Override
    public void excuteTask(ITask task) throws AppException {
        log.info("同步用户人脸到海康任务");
        FaceGroupEntity faceGroup = faceGroupService.selectOne(new FaceGroupQuery().name(PARAM_FACE_GROUP_REGISTER));
        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);
                        faceReq.setFaceGroupIndexCode(faceGroup.getIndexCode());
                        Rest<FaceDataInfo> faceDataInfoRest = hikFaceService.faceSingleAdd(faceReq);

                        if (faceDataInfoRest.getCode() == YesNoEnum.YES.getValue()) {
                            personEntity.setInFaceHk(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);

                            FaceGroupPersonEntity faceGroupPersonEntity = new FaceGroupPersonEntity();
                            faceGroupPersonEntity.initAttrValue();
                            faceGroupPersonEntity.setFaceGroupId(faceGroup.getId());
                            faceGroupPersonEntity.setIndexCode(personEntity.getIndexCode());
                            faceGroupPersonEntity.setCreateTime(new Date());
                            faceGroupPersonEntity.setCreateUserId(1L);
                            faceGroupPersonService.save(faceGroupPersonEntity);
                        }
                    }
                }
            }
        }
    }


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

    }


}
