package com.yx.face.service.Aspect;

import com.yx.face.boot.uitls.Base64Util;
import com.yx.face.dao.UserWhiteListFromDao;
import com.yx.face.model.entity.UserWhitelistFrom;
import com.yx.face.model.entity.UserWhitelist;
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 UserWhiteListDaoAspect {

    @Resource
    UserWhiteListFromDao userWhiteListFromDao;

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

    @After("execution( public * com.yx.face.dao.UserWhitelistDao.insert*(*))")
    public void insertAfter(JoinPoint joinPoint) {
        String name = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        UserWhitelistFrom obj = new UserWhitelistFrom();
        if (name.equals("insert") || name.equals("insertSelective")) {
            UserWhitelist userWhiteList = (UserWhitelist) args[0];
            if (userWhiteList.getAvatar() != null && !userWhiteList.getAvatar().equals("") && userWhiteList.getId() != null) {
                obj.setUserWhitelistId(userWhiteList.getId());
                obj.setImgBase(Base64Util.getByteArrayFromUrl(userWhiteList.getAvatar()));
                userWhiteListFromDao.insertSelective(obj);
            }
        }
    }

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

}
