OrderCountCheckAspect.java 14.7 KB
package com.lhcredit.framework.aspectj;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.fasterxml.jackson.core.type.TypeReference;
import com.lhcredit.common.utils.JsonUtils;
import com.lhcredit.common.utils.StringUtils;
import com.lhcredit.framework.aspectj.lang.annotation.OrderCountCheck;
import com.lhcredit.framework.web.domain.AjaxResult;
import com.lhcredit.project.business.bCreditReport.domain.BCreditReport;
import com.lhcredit.project.business.bCreditReport.service.BCreditReportServiceImpl;
import com.lhcredit.project.business.frontUser.domain.FrontUser;
import com.lhcredit.project.business.frontUser.domain.FrontUserMon;
import com.lhcredit.project.business.frontUser.domain.TempConf;
import com.lhcredit.project.business.frontUser.service.FrontUserServiceImpl;
import com.lhcredit.project.business.frontUser.service.TokenManager;
import com.lhcredit.project.business.templateConfigurationParameters.domain.TemplateConfigurationParameters;
import com.lhcredit.project.business.templateConfigurationParameters.service.TemplateConfigurationParametersServiceImpl;
import org.apache.commons.collections.CollectionUtils;
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.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Aspect
@Component
public class OrderCountCheckAspect {


    @Autowired
    private TokenManager tokenManager;

    @Autowired
    private BCreditReportServiceImpl bCreditReportService;

    @Autowired
    private FrontUserServiceImpl frontUserService;

    @Autowired
    private TemplateConfigurationParametersServiceImpl templateConfigurationParametersService;

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

    @Around("targetAnnot()")
    public Object checkOrderCount(ProceedingJoinPoint joinPoint) throws Throwable {

        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String token = request.getHeader("authorization");

        //获取 目标方法参数
        Object[] objects = joinPoint.getArgs();
        BCreditReport bCreditReport = (BCreditReport)objects[0];
        //设置报告类型参数 并 查询用户已经下单的 报告数量
        /*ArrayList<String> typeList = new ArrayList<>();
        typeList.add(bCreditReport.getReportType());
        List<BCreditReport> reportCountlist = bCreditReportService.getReportCountByOrg(userMon.getOrgId(), typeList);*/
        //获取用户 报告上线值
        /*List<TempConf> tempConfList = userMon.getTempConfList();
        Map<String, TempConf> numMap = tempConfList.stream().collect(Collectors.toMap(TempConf::getValue, Function.identity()));
        TempConf tempConf = numMap.get(bCreditReport.getReportType());*/

        //定义用户模板
        TempConf tempConf;
        //获取目标方法的 注解值
        MethodSignature signature = (MethodSignature)joinPoint.getSignature();
        OrderCountCheck countCheck = signature.getMethod().getAnnotation(OrderCountCheck.class);

        Integer totalReport = -1;
        //根据注解值 判断 对应的目标方法
        switch (countCheck.reportType()){
            //后台批量下单
            case "backgroundBatchOrder":
                MultipartFile file = bCreditReport.getFile();
                if (file.isEmpty()
                        ||  (!file.getOriginalFilename().endsWith("xls") && !file.getOriginalFilename().endsWith("xlsx"))){
                    return AjaxResult.error("参数有误");
                }

                //根据userId查询用户配置模板信息
                tempConf = getBackgroundUserTempConf(bCreditReport);
                if (tempConf == null || StringUtils.isEmpty(tempConf.getNum()))
                    return AjaxResult.error("用户无此权限");
                /*FrontUser user = frontUserService.getFrontUserById(bCreditReport.getUserId());
                //reports 是为用户配置的报告上限数据,为空则暂时不拦截
                if (StringUtils.isEmpty(user.getReports()))
                    return joinPoint.proceed();

                List<TempConf> tempConfList = JsonUtils.fromJson(user.getReports(), new TypeReference<List<TempConf>>() {});
                TempConf backgroundTemConf = getTempConf(tempConfList, bCreditReport);
                if (backgroundTemConf == null){

//                    return AjaxResult.error("用户无此模板");
                    return joinPoint.proceed();
                }*/
                //限制数量 为 -1则 不做校验
                if (Integer.parseInt(tempConf.getNum()) < 0)
                    return joinPoint.proceed();

                //查询用户的下单数量
                List<BCreditReport> reportCountlist = getbCreditReports(bCreditReport);
                //定义用户下单的个数
                Integer orderedNum;
                if (CollectionUtils.isEmpty(reportCountlist)){
                    orderedNum = 0;
                }else {
                    orderedNum = reportCountlist.get(0).getReportCount();
                }
                //获取 批量下单的企业数量
                ExcelReader reader = ExcelUtil.getReader(bCreditReport.getFile().getInputStream());
                List<Map<String,Object>> fileInfo = reader.readAll();
                //计算 用户报告数量总和 = 当前单下单的报告数量 + 已下单的报告数量
                totalReport = fileInfo.size() + orderedNum;
                //用户的报告数量上限
                if (totalReport > Integer.parseInt("".equals(tempConf.getNum()) ? "0" : tempConf.getNum()))
                    return AjaxResult.error(tempConf.getName() + "下单次数已达上限");

                return joinPoint.proceed();
            //后台 单个下单
            case "backgroundOrder" :
                //获取用户的报告上限数量
                tempConf = getBackgroundUserTempConf(bCreditReport);
                if (tempConf == null || StringUtils.isEmpty(tempConf.getNum()))
                    return AjaxResult.error("用户无此权限");
                /*FrontUser frontUser = frontUserService.getFrontUserById(bCreditReport.getUserId());
                //reports 是为用户配置的报告上限数据,为空则暂时不拦截
                if (StringUtils.isEmpty(frontUser.getReports()))
                    return joinPoint.proceed();

                List<TempConf> backTempConfList = JsonUtils.fromJson(frontUser.getReports(), new TypeReference<List<TempConf>>() {});
                TempConf backgroudTemConfOne = getTempConf(backTempConfList, bCreditReport);

                if (backgroudTemConfOne == null){

//                    return AjaxResult.error("用户无此模板");
                    return joinPoint.proceed();
                }*/

                //限制数量 为 -1则 不做校验
                if (Integer.parseInt(tempConf.getNum()) < 0)
                    return joinPoint.proceed();


                //查询 目前已经下单的数量
                List<BCreditReport> reports1 = getbCreditReports(bCreditReport);

                //定义用户下单的个数
                Integer numOrder;
                if (CollectionUtils.isEmpty(reports1)){
                    numOrder = 0;
                }else {
                    numOrder = reports1.get(0).getReportCount();
                }
                //计算 用户已经下的单量
                Integer totalNum = numOrder + 1;

                //用户的报告数量上限
                if (totalNum > Integer.parseInt("".equals(tempConf.getNum()) ? "0" : tempConf.getNum()))
                    return AjaxResult.error(tempConf.getName() + "下单次数已达上限");
                return joinPoint.proceed();
            //前台 单条/批量下单
            case "frontBatchOrder" :
                //获取用户的报告上限数量 (前台用户登录才可下单,所以user肯定存在)
                FrontUserMon userMon = tokenManager.getUserInfoByToken(token);
                List<TempConf> tempConfFrontList = userMon.getTempConfList();
                // 未配置 报告模板下单上限,直接返回
                if (CollectionUtils.isEmpty(tempConfFrontList))
                    return AjaxResult.error("用户无此权限");

                tempConf = getTempConf(tempConfFrontList, bCreditReport);
                if (tempConf == null || StringUtils.isEmpty(tempConf.getNum())){
                    return AjaxResult.error("用户无此权限");
                }

                //限制数量 为 -1则 不做校验
                if (Integer.parseInt(tempConf.getNum()) < 0)
                    return joinPoint.proceed();

                //查询用户的下单数量
                List<BCreditReport> bCreditReports = getbCreditReports(bCreditReport,userMon);
                //定义用户下单的个数
                Integer frontOrderedNum;
                if (CollectionUtils.isEmpty(bCreditReports)){
                    frontOrderedNum = 0;
                }else {
                    frontOrderedNum = bCreditReports.get(0).getReportCount();
                }
                //获取 批量下单的企业数量  与 已下单的报告数量 之和
                Integer frontOrderCount = bCreditReport.getEnterpriseList().size() + frontOrderedNum;
                //用户的报告数量上限
                if (frontOrderCount > Integer.parseInt("".equals(tempConf.getNum()) ? "0" : tempConf.getNum()))
                    return AjaxResult.error(tempConf.getName() + "下单次数已达上限");

                return joinPoint.proceed();

            //单条 海外下单
            case "overSeaOrder" :
                //获取用户的报告上限数量 (前台用户登录才可下单,所以user肯定存在)
                FrontUserMon userMon2 = tokenManager.getUserInfoByToken(token);
                List<TempConf> overSeaConfFrontList = userMon2.getTempConfList();

                // 未配置 报告模板下单上限,直接返回
                if (CollectionUtils.isEmpty(overSeaConfFrontList))
                    return AjaxResult.error("用户无此权限");

                tempConf = getTempConf(overSeaConfFrontList, bCreditReport);

                if (tempConf == null || StringUtils.isEmpty(tempConf.getNum())){
                    return AjaxResult.error("用户无此权限");
                }

                //限制数量 为 -1则 不做校验
                if (Integer.parseInt(tempConf.getNum()) < 0)
                    return joinPoint.proceed();

                //查询 目前已经下单的数量
                List<BCreditReport> reports2 = getbCreditReports(bCreditReport, userMon2);

                //定义用户下单的个数
                Integer numOrderO;
                if (CollectionUtils.isEmpty(reports2)){
                    numOrderO = 0;
                }else {
                    numOrderO = reports2.get(0).getReportCount();
                }
                //计算 用户已经下的单量
                Integer totalNumO = numOrderO + 1;
                //用户的报告数量上限
                if (totalNumO > Integer.parseInt("".equals(tempConf.getNum()) ? "0" : tempConf.getNum()))
                    return AjaxResult.error(tempConf.getName() + "下单次数已达上限");

                return joinPoint.proceed();

            default:
                return AjaxResult.error("无此用户信息");
        }

    }


    /**
     * 后台用户 -- 获取配置模板
     * @param bCreditReport
     * @return
     */
    private TempConf getBackgroundUserTempConf(BCreditReport bCreditReport){
        //获取用户的报告上限数量
        FrontUser frontUser = frontUserService.getFrontUserById(bCreditReport.getUserId());
        //reports 是为用户配置的报告上限数据,为空则暂时不拦截
        if (frontUser == null || StringUtils.isEmpty(frontUser.getReports()))
            return null;

        List<TempConf> backTempConfList = JsonUtils.fromJson(frontUser.getReports(), new TypeReference<List<TempConf>>() {});
        return getTempConf(backTempConfList, bCreditReport);
    }


    /**
     * 根据报告类型 和 orgId 查询用户的下单数量
     * @param bCreditReport
     * @return
     */
    private List<BCreditReport> getbCreditReports(BCreditReport bCreditReport) {
        ArrayList<String> typeList = new ArrayList<>();
        typeList.add(bCreditReport.getReportType());
        List<BCreditReport> reportCountlist = bCreditReportService.getReportCountByUserId(bCreditReport.getUserId(), typeList);
        return reportCountlist;
    }

    private List<BCreditReport> getbCreditReports(BCreditReport bCreditReport,FrontUserMon userMon) {
        ArrayList<String> typeList = new ArrayList<>();
        typeList.add(bCreditReport.getReportType());
        List<BCreditReport> reportCountlist = bCreditReportService.getReportCountByUserId(userMon.getId(), typeList);
        return reportCountlist;
    }

    /**
     * 获取用户报告类型的上限
     * @param tempConfList
     * @param bCreditReport
     * @return
     */
    private TempConf getTempConf(List<TempConf> tempConfList, BCreditReport bCreditReport) {
        //去掉 tempType =3 的数据
        Map<String, TempConf> tempConfMap = tempConfList.stream().filter(te -> !te.getTempType().equals(3)).collect(Collectors.toMap(TempConf::getValue, Function.identity()));
        TempConf backgroundTemConf = tempConfMap.get(bCreditReport.getReportType());
        //报告类型为 -11时,为定制报告,需要单独查询clientID 区分报告模板
        if (bCreditReport.getReportType().equals("-11")){
            TemplateConfigurationParameters templateParam = templateConfigurationParametersService.selectTemplateConfigurationParametersById(bCreditReport.getTemplateParamId());
            for (TempConf tef : tempConfList) {
                //定制模板
                if (tef.getTempType() == 2 && tef.getValue().equals(templateParam.getClientId().toString())){
                    return tef;
                }
            }

        }
        return backgroundTemConf;
    }


}