package com.yx.face.service.Aspect;

import com.yx.face.boot.uitls.Base64Util;
import com.yx.face.dao.UserVisitorListFromDao;
import com.yx.face.model.entity.UserFaceRecords;
import com.yx.face.model.entity.UserVisitorList;
import com.yx.face.model.entity.UserVisitorListFrom;
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 UserInfoDaoAspect
 * @Author WXG
 * @Date 2021/12/2 15:09
 */
@Component
@Aspect
public class UserVisitorListDaoAspect {

    @Resource
    UserVisitorListFromDao userVisitorListFromDao;

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

    @After("execution( public * com.yx.face.dao.UserVisitorListDao.insert*(*))")
    public void insertAfter(JoinPoint joinPoint) {
        String name = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        UserVisitorListFrom obj = new UserVisitorListFrom();
        if (name.equals("insert") || name.equals("insertSelective")) {
            UserVisitorList userVisitorList = (UserVisitorList) args[0];
            if (userVisitorList.getAvatar() != null && !userVisitorList.getAvatar().equals("") && userVisitorList.getId() != null) {
                obj.setUserVisitorListId(userVisitorList.getId());
                obj.setImgBase(Base64Util.getByteArrayFromUrl(userVisitorList.getAvatar()));
                userVisitorListFromDao.insertSelective(obj);
            }
        }
    }

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

}
