package com.yx.face.service.Aspect;

import com.yx.face.boot.uitls.Base64Util;
import com.yx.face.dao.UserBlackListFromDao;
import com.yx.face.model.entity.UserBlackList;
import com.yx.face.model.entity.UserBlackListFrom;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Arrays;

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

    @Resource
    UserBlackListFromDao userBlackListFromDao;

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

    @After("execution( public * com.yx.face.dao.UserBlackListDao.insert*(*))")
    public void insertAfter(JoinPoint joinPoint) {
        String name = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        UserBlackListFrom ublf = new UserBlackListFrom();
        if (name.equals("insert") || name.equals("insertSelective")) {
            UserBlackList ubl = (UserBlackList) args[0];
            if (ubl.getAvatar() != null && !ubl.getAvatar().equals("") && ubl.getId() != null) {
                ublf.setUserBlackListId(ubl.getId());
                ublf.setImgBase(Base64Util.getByteArrayFromUrl(ubl.getAvatar()));
                userBlackListFromDao.insertSelective(ublf);
            }
        }
    }

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

}
