package com.yx.face.service.Aspect;

import com.yx.face.boot.uitls.Base64Util;
import com.yx.face.dao.FaceLogFromDao;
import com.yx.face.model.entity.FaceLog;
import com.yx.face.model.entity.FaceLogFrom;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @description:
 * @ClassName FaceLogDaoAspect
 * @Author WXG
 * @Date 2021/12/2 15:09
 */
@Component
@Aspect
public class FaceLogDaoAspect {

    @Resource
    FaceLogFromDao faceLogFromDao;

    @After("execution( public * com.yx.face.dao.FaceLogDao.delete*(*))")
    public void deleteAfter(JoinPoint joinPoint) {
        String name = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        if (name.equals("deleteByPrimaryKey")) {
            faceLogFromDao.delteByForgeinId((Long) args[0]);
        }
    }

    @After("execution( public * com.yx.face.dao.FaceLogDao.insert*(*))")
    public void insertAfter(JoinPoint joinPoint) {
        String name = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        FaceLogFrom ublf = new FaceLogFrom();
        if (name.equals("insert") || name.equals("insertSelective")) {
            FaceLog ubl = (FaceLog) args[0];
            boolean photo = ubl.getPhoto() != null && !ubl.getPhoto().equals("") && ubl.getPhoto().indexOf("请求失败") == -1 && ubl.getId() != null;
            boolean sfzPhoto = ubl.getSfzPhoto() != null && !ubl.getSfzPhoto().equals("") && ubl.getSfzPhoto().indexOf("请求失败") == -1 && ubl.getId() != null;
            if (photo && sfzPhoto) {
                ublf.setFaceLogId(ubl.getId());
                ublf.setImgBase(Base64Util.getByteArrayFromUrl(ubl.getPhoto()));
                ublf.setSfzImgBase(Base64Util.getByteArrayFromUrl(ubl.getSfzPhoto()));
                faceLogFromDao.insertSelective(ublf);
            }
        }
    }

    @After("execution( public * com.yx.face.dao.FaceLogDao.update*(*))")
    public void updateAfter(JoinPoint joinPoint) {
        String name = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        FaceLogFrom ublf = new FaceLogFrom();
        if (name.equals("updateByPrimaryKey") || name.equals("updateByPrimaryKeySelective")) {
            FaceLog ubl = (FaceLog) args[0];
            boolean photo = ubl.getPhoto() != null && !ubl.getPhoto().equals("") && ubl.getPhoto().indexOf("请求失败") == -1 && ubl.getId() != null;
            boolean sfzPhoto = ubl.getSfzPhoto() != null && !ubl.getSfzPhoto().equals("") && ubl.getSfzPhoto().indexOf("请求失败") == -1 && ubl.getId() != null;
            if (photo && sfzPhoto) {
                ublf.setFaceLogId(ubl.getId());
                ublf.setImgBase(Base64Util.getByteArrayFromUrl(ubl.getPhoto()));
                ublf.setSfzImgBase(Base64Util.getByteArrayFromUrl(ubl.getSfzPhoto()));
                faceLogFromDao.updateByForgeinId(ublf);
            }
        }
    }

}
