package com.foreverwin.mesnac.listener.listener;


import com.foreverwin.mesnac.listener.model.SysUser;
import com.foreverwin.mesnac.listener.service.SysUserService;
import com.sap.security.api.UMException;
import com.sap.security.api.UserListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class NwaUserListener implements UserListener {

    private final Logger logger = LoggerFactory.getLogger(NwaUserListener.class);

    @Autowired
    SysUserService sysUserService;

    @Override
    public void userAdded(String s) throws UMException {

    }

    @Override
    public void userRemoved(String s) throws UMException {
        logger.info("user remove start: " + s);
        //---------------------------------------------------------------------------------------------------------------------------
        String userName = s.substring(s.indexOf(":") + 1);
        String upperUserName = userName.toUpperCase();
        SysUser entity = sysUserService.selectOne(upperUserName);
        if (entity == null) return;
        //---------------------------------------------------------------------------------------------------------------------------
        sysUserService.removeById(entity.getHandle());
        //---------------------------------------------------------------------------------------------------------------------------
        logger.info("user remove complete: " + s);
    }
}