package com.yx.face.service.Aspect;

import com.yx.face.boot.uitls.Base64Util;
import com.yx.face.dao.UserFaceRecordsDao;
import com.yx.face.dao.UserFaceRecordsFromDao;
import com.yx.face.model.entity.UserBlackList;
import com.yx.face.model.entity.UserFaceRecords;
import com.yx.face.model.entity.UserFaceRecordsFrom;
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 UserFaceRecordsDaoAspect
 * @Author WXG
 * @Date 2021/12/2 15:09
 */
@Component
@Aspect
public class UserFaceRecordsDaoAspect {

    @Resource
    UserFaceRecordsFromDao userFaceRecordsFromDao;

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

    @After("execution( public * com.yx.face.dao.UserFaceRecordsDao.insert*(*))")
    public void insertAfter(JoinPoint joinPoint) {
        String name = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        UserFaceRecordsFrom obj = new UserFaceRecordsFrom();
        if (name.equals("insert") || name.equals("insertSelective")) {
            UserFaceRecords userFaceRecords = (UserFaceRecords) args[0];
            if (userFaceRecords.getPhoto() != null && !userFaceRecords.getPhoto().equals("") && userFaceRecords.getId() != null) {
                obj.setUserFaceRecordsId(userFaceRecords.getId());
                obj.setImgBase(Base64Util.getByteArrayFromUrl(userFaceRecords.getPhoto()));
                userFaceRecordsFromDao.insertSelective(obj);
            }
        }
    }

    @After("execution( public * com.yx.face.dao.UserFaceRecordsDao.update*(*))")
    public void updateAfter(JoinPoint joinPoint) {
        String name = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        UserFaceRecordsFrom obj = new UserFaceRecordsFrom();
        if (name.equals("updateByPrimaryKey") || name.equals("updateByPrimaryKeySelective")) {
            UserFaceRecords ubl = (UserFaceRecords) args[0];
            if (ubl.getPhoto() != null && !ubl.getPhoto().equals("")) {
                obj.setUserFaceRecordsId(ubl.getId());
                obj.setImgBase(Base64Util.getByteArrayFromUrl(ubl.getPhoto()));
                userFaceRecordsFromDao.updateByForgeinId(obj);
            }
        }
    }

}
