CountReportAspect.java 10.6 KB
package com.lhcredit.framework.aspectj;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.lhcredit.common.utils.ServletUtils;
import com.lhcredit.common.utils.StringUtils;
import com.lhcredit.common.utils.http.HttpUtils;
import com.lhcredit.framework.aspectj.lang.annotation.ReportCount;
import com.lhcredit.framework.web.domain.AjaxResult;
import com.lhcredit.project.business.apiFinance.service.IApiFinanceService;
import com.lhcredit.project.business.bCreditReport.service.BCreditReportServiceImpl;
import com.lhcredit.project.business.financeInfo.service.FinanceInfoServiceImpl;
import com.lhcredit.project.business.frontDept.domain.FrontDept;
import com.lhcredit.project.business.frontDept.service.FrontDeptServiceImpl;
import com.lhcredit.project.business.frontUser.domain.FrontUserMon;
import com.lhcredit.project.business.frontUser.domain.TempConf;
import com.lhcredit.project.business.frontUser.service.TokenManager;
import com.lhcredit.project.business.monitorCompany.service.MonitorCompanyServiceImpl;
import com.lhcredit.project.business.statisticsFinancePageselect.service.IStatisticsFinancePageselectService;
import com.lhcredit.project.business.userDownloadCountLog.domain.UserDownloadCountLog;
import com.lhcredit.project.business.userDownloadCountLog.service.UserDownloadCountLogServiceImpl;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
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.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

@Aspect
@Component
public class CountReportAspect {

    @Autowired
    private TokenManager tokenManager;

    @Autowired
    private FrontDeptServiceImpl frontDeptService;

    @Autowired
    private IApiFinanceService apiFinanceService;

    @Autowired
    private MonitorCompanyServiceImpl monitorCompanyService;

    @Autowired
    private FinanceInfoServiceImpl  financeInfoService;
    @Autowired
    private IStatisticsFinancePageselectService financePageselectService;
    @Autowired
    private UserDownloadCountLogServiceImpl userDownloadCountLogService;



    @Pointcut("@annotation(com.lhcredit.framework.aspectj.lang.annotation.ReportCount)")
    public void countReportMethods() {}


    @Around("countReportMethods()")
    public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {

        //获取注解
        MethodSignature signature =  (MethodSignature)joinPoint.getSignature();
        Method method = signature.getMethod();
        ReportCount reportCount = method.getAnnotation(ReportCount.class);
        //注解的 reportType 无值 则直接返回
        if ("".equals(reportCount.reportType())) {
            return AjaxResult.error("reportType属性不能为空");
        }

        //获取用户得总公司部门
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String token = request.getHeader("authorization");

        FrontUserMon user = tokenManager.getUserInfoByToken(token);
        if (user == null || CollectionUtils.isEmpty(user.getTempConfList())){
            setErrorRespons("用户暂无此项权限");
            return null;
//            return AjaxResult.error("用户暂无此项权限");
        }


        Map<String, TempConf> tempConfMap = user.getTempConfList().stream().filter(te -> te.getTempType().equals(3)).collect(Collectors.toMap(TempConf::getValue, Function.identity()));

        if (MapUtils.isEmpty(tempConfMap)){
            setErrorRespons("用户暂无此项权限");
            return null;
//            return AjaxResult.error("用户暂无此权限");
        }

        // 用户配置的目标对象
        TempConf tempConf ;
                //根据 headId 查询 各种报告的限查询数量
//        FrontDept dept = frontDeptService.getFrontDeptById(Long.parseLong(user.getHeadId()));
//        FrontDept dept = frontDeptService.getFrontDeptById(23l);
        switch (reportCount.reportType()) {
            //信用报告
//            case "creditReport" :
//                //查询信用报告的次数:
//                Long credtiReportCount = bCreditReportService.getCreditSearchCountByOrgId(user.getHeadId());
////            Long credtiReportCount = bCreditReportService.getCreditSearchCountByOrgId("23");
//                int reportNum = dept.getReportNum();
//                if (credtiReportCount > reportNum) {
//                    return AjaxResult.error("报告查询次数已达上限");
//                }else {
//                    return joinPoint.proceed();
//                }
            //监控企业
            case "monitorCompany" :
//            Long monitorCompanyCount = monitorCompanyService.getMonitorCompanySearchCount("23");
               tempConf =  tempConfMap.get("0");
                if (Objects.isNull(tempConf) || StringUtils.isEmpty(tempConf.getNum())){
                    return AjaxResult.error("用户暂无此项权限");
                }
                int monitorNum = Integer.parseInt(tempConf.getNum());
                // -1 不校验
                if(monitorNum == -1){
                    return joinPoint.proceed();
                }
                Long monitorCompanyCount = monitorCompanyService.getMonitorCompanySearchCount(user.getId().toString());
                if (monitorCompanyCount >= monitorNum) {
                    return AjaxResult.error("公司监控查询次数已达上限");
                }
                return  joinPoint.proceed();
            //财务次数监控
            case "financeInfo" :
//                  Long financeInfoCount = financeInfoService.getFinanceInfoSearchCount("23");
                tempConf = tempConfMap.get("1");
                if (Objects.isNull(tempConf) || StringUtils.isEmpty(tempConf.getNum())){
                    return AjaxResult.error("用户暂无此项权限");
                }
                // -1 不校验
                int financeNum =  Integer.parseInt(tempConf.getNum());
                if(financeNum == -1){
                    return joinPoint.proceed();
                }
                Long financeInfoCount = financeInfoService.getFinanceInfoSearchCount(user.getId().toString());
                Integer countval23=financePageselectService.selectCountByOrgId(user.getId().toString());
                if (null!=countval23)financeInfoCount=financeInfoCount+countval23;
                if (financeInfoCount >= financeNum) {
                    return AjaxResult.error("财务信息查询次数已达上限");
                }
                return joinPoint.proceed();
            //司法导出数据
            case "sFAssist":
                tempConf = tempConfMap.get("2");
                if (Objects.isNull(tempConf) || StringUtils.isEmpty(tempConf.getNum())){
//                    return AjaxResult.error("用户暂无此项权限");
                    setErrorRespons("用户暂无此权限");
                    return null;
                }
                int sFAssistNum =  Integer.parseInt(tempConf.getNum());
                // -1 不做校验
                if (sFAssistNum == -1){
                    return joinPoint.proceed();
                }

                //查询用户下载司法数据次数
                UserDownloadCountLog userSFAssitDataNum = userDownloadCountLogService.getUserSFAssitDataNum(user.getId());
                //没查到: 一次没下载过
                if (userSFAssitDataNum == null)
                    return joinPoint.proceed();
                //超过限制返回 错误信息
                if (userSFAssitDataNum.getSfAssistNum() > sFAssistNum){
                    //获取 response
                    /*HttpServletResponse response = null;
                    Object[] objects = joinPoint.getArgs();
                    for (Object arg : objects){
                        if (arg instanceof HttpServletResponse){
                            response  = (HttpServletResponse)arg;
                            break;
                        }
                    }
                    //目标方法为void 手动修改返回值
                    response.setContentType("application/json");
                    response.setCharacterEncoding("UTF-8");
                    //序列化 AjaxResult对象为json,并写入response流
                    ObjectMapper objectMapper = new ObjectMapper();
                    objectMapper.writeValue(response.getWriter(),AjaxResult.error("司法数据下载次数已达到上限"));*/
                    setErrorRespons("司法数据下载次数已达到上限");
                    return null;
//                        return AjaxResult.error("司法数据下载次数已达到上限");
                }
                return joinPoint.proceed();

            //税务信息
            case "taxInfo":
                tempConf = tempConfMap.get("3");
                if (Objects.isNull(tempConf) || StringUtils.isEmpty(tempConf.getNum())){
                    return AjaxResult.error("用户暂无此项权限");
                }
                int taxCount = Integer.parseInt(tempConf.getNum());
                // -1 不校验
                if(taxCount == -1){
                    return joinPoint.proceed();
                }

                //查询用户实际开票条数
                Long taxNum = apiFinanceService.getFinanceCountByUser(user.getId().toString());
                if (taxNum > taxCount)
                    return AjaxResult.error("用户开票次数以达到上限");

                return joinPoint.proceed();

            default:
                return null;
        }
    }

    /**
     * response 设置返回错误信息
     * @param msg
     * @throws Exception
     */
    private void setErrorRespons(String msg) throws Exception{
        HttpServletResponse response = ServletUtils.getResponse();
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");

        ObjectMapper mapper = new ObjectMapper();
        mapper.writeValue(response.getWriter(),AjaxResult.error(msg));
    }


}