package com.yx.face.service.Aspect;

import com.yx.face.boot.uitls.Base64Util;
import com.yx.face.dao.UserInfoDao;
import com.yx.face.dao.UserInfoFromDao;
import com.yx.face.model.entity.UserFaceRecords;
import com.yx.face.model.entity.UserInfo;
import com.yx.face.model.entity.UserInfoFrom;
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 UserInfoDaoAspect {

    @Resource
    UserInfoFromDao userInfoFromDao;

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

    @After("execution( public * com.yx.face.dao.UserInfoDao.insert*(*))")
    public void insertAfter(JoinPoint joinPoint) {
        String name = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        UserInfoFrom obj = new UserInfoFrom();
        if (name.equals("insert") || name.equals("insertSelective")) {
            UserInfo ui = (UserInfo) args[0];
            if (ui.getAvatar() != null && !ui.getAvatar().equals("") && ui.getId() != null) {
                obj.setUserInfoId(ui.getId());
                obj.setImgBase(Base64Util.getByteArrayFromUrl(ui.getAvatar()));
                userInfoFromDao.insertSelective(obj);
            }
        }
    }

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

}
