package com.zjrs.ggfw.annotation;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.mohrss.leaf.core.framework.util.CurrentUser;
import org.mohrss.leaf.core.framework.util.SecurityUtils;
import org.mohrss.leaf.core.framework.web.controller.AjaxResponse;
import com.zjrs.ggfw.rlzysc.entity.HmdglMapper;
import com.zjrs.ggfw.rlzysc.po.VRsRlzyHmdglPO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class IsHmdValidimpl {

    @Autowired
    private HmdglMapper hmdglMapper;

    @Pointcut("@annotation(com.zjrs.ggfw.annotation.IsHmdValid)")
    public void annotationPointcut() {

    }

    @Around("annotationPointcut()")
    public AjaxResponse beforePointcut(ProceedingJoinPoint joinPoint) throws Throwable {
        AjaxResponse ajaxResponse = new AjaxResponse();
        CurrentUser currentUser = SecurityUtils.getCurrentUser();
        String userid = currentUser.getUsername() == null ? "" : currentUser.getUsername().trim();
        if (StringUtils.isNotBlank(userid)){
            int cnt = Math.toIntExact(hmdglMapper.selectCount(new QueryWrapper<VRsRlzyHmdglPO>()
                    .eq("rs_rlzy_ts_lsh", userid)
                    .eq("ts_yxbz", "1")));
            if (cnt>0){
                ajaxResponse.setAppcode("1");
                ajaxResponse.setErrorMsg("您当前账号不能正常操作，请到湛江市人力资源和社会保障局_就业服务中心，进行相关咨询");
                return ajaxResponse;
            } else {
                Object[] args = joinPoint.getArgs();
                return (AjaxResponse) joinPoint.proceed(args);
            }
        } else {
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("获取不到该用户信息");
            return ajaxResponse;
        }

    }
}
