package thyyxxk.webserver.aop.aspect;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import thyyxxk.webserver.config.exception.BizException;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.entity.login.UserInfo;
import thyyxxk.webserver.service.hutoolcache.UserCache;
import thyyxxk.webserver.utils.StringUtil;

/**
 * @author 肖蟾
 */
@Aspect
@Component
@Slf4j
@RequiredArgsConstructor
public class NeedYbCodeAop {
    private final UserCache userCache;

    @Pointcut("@annotation(thyyxxk.webserver.aop.interfaces.NeedYbCode)")
    public void needYbCode() {
    }

    @Before("needYbCode()")
    public void doBefore() {
        UserInfo userInfo = userCache.getUserInfoByToken();
        if (userInfo == null) {
            throw new BizException(ExceptionEnum.LOGICAL_ERROR, "没有查询到该员工信息");
        }
        if (getYbCode(userInfo)) {
            throw new BizException(ExceptionEnum.LOGICAL_HTML_ERROR, String.format("您没有医保编码无法进行操作，" +
                    "请联系<span style='color:red'>人力资源部</span>，为您添加医保编码，您的工号为：<span style='color:red'>【%s】</span>", userInfo.getCodeRs()));
        }
    }

    private Boolean getYbCode(UserInfo userInfo) {
        return StringUtil.isBlank(userInfo.getYbCode());
    }

}
