package com.java110.api.listener.basePrivilege;

import com.alibaba.fastjson.JSONObject;
import com.java110.api.listener.AbstractServiceApiDataFlowListener;
import com.java110.core.annotation.Java110Listener;
import com.java110.core.context.DataFlowContext;
import com.java110.core.smo.community.IMenuInnerServiceSMO;
import com.java110.dto.basePrivilege.BasePrivilegeDto;
import com.java110.entity.center.AppService;
import com.java110.core.event.service.api.ServiceDataFlowEvent;
import com.java110.utils.constant.ServiceCodeConstant;
import com.java110.utils.util.Assert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

/**
 * 检查用户是否有资源
 */
@Java110Listener("checkUserHasResourceListener")
public class CheckUserHasResourceListener extends AbstractServiceApiDataFlowListener {

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

    @Autowired
    private IMenuInnerServiceSMO menuInnerServiceSMOImpl;

    @Override
    public String getServiceCode() {
        return ServiceCodeConstant.SERVICE_CODE_CHECK_USER_HAS_RESOURCE;
    }

    @Override
    public HttpMethod getHttpMethod() {
        return HttpMethod.POST;
    }

    @Override
    public void soService(ServiceDataFlowEvent event) {
        DataFlowContext dataFlowContext = event.getDataFlowContext();
        AppService service = event.getAppService();
        JSONObject data = dataFlowContext.getReqJson();
        logger.debug("请求信息：{}", JSONObject.toJSONString(dataFlowContext));
        //Assert.hasKeyAndValue(data, "userId", "请求报文中未包含userId节点");
        Assert.hasKeyAndValue(data, "resource", "请求报文中未包含资源路径");
        ResponseEntity<String> responseEntity = null;

        //判断资源路径是否配置权限 ，没有配置权限则都能访问
        BasePrivilegeDto basePrivilegeDto = new BasePrivilegeDto();
        basePrivilegeDto.setResource(data.getString("resource"));
        int count = menuInnerServiceSMOImpl.queryBasePrivilegesCount(basePrivilegeDto);
        //没有配置权限，都能访问
        if (count < 1) {
            responseEntity = new ResponseEntity<String>("该资源路径没有配置权限", HttpStatus.OK);
            dataFlowContext.setResponseEntity(responseEntity);
            return;
        }
        basePrivilegeDto.setUserId(data.getString("userId"));
        boolean checkFlag = menuInnerServiceSMOImpl.checkUserHasResource(basePrivilegeDto);

        if (!checkFlag) {
            responseEntity = new ResponseEntity<String>("没有权限操作", HttpStatus.UNAUTHORIZED);
        } else {

            responseEntity = new ResponseEntity<String>("成功", HttpStatus.OK);

        }

        dataFlowContext.setResponseEntity(responseEntity);
    }

    @Override
    public int getOrder() {
        return 0;
    }

    public IMenuInnerServiceSMO getMenuInnerServiceSMOImpl() {
        return menuInnerServiceSMOImpl;
    }

    public void setMenuInnerServiceSMOImpl(IMenuInnerServiceSMO menuInnerServiceSMOImpl) {
        this.menuInnerServiceSMOImpl = menuInnerServiceSMOImpl;
    }
}

@@ -251,16 +251,80 @@
 ontext;%0A
+import com.java110.core.event.service.api.ServiceDataFlowEvent;%0A
 import c
@@ -479,72 +479,8 @@
 ce;%0A
-import com.java110.core.event.service.api.ServiceDataFlowEvent;%0A
 impo
@@ -1644,85 +1644,8 @@
     
-logger.debug(%22%E8%AF%B7%E6%B1%82%E4%BF%A1%E6%81%AF%EF%BC%9A%7B%7D%22, JSONObject.toJSONString(dataFlowContext));%0A        //
 Asse
@@ -1669,22 +1669,24 @@
 (data, %22
-userId
+resource
 %22, %22%E8%AF%B7%E6%B1%82%E6%8A%A5%E6%96%87
@@ -1693,17 +1693,52 @@
 %E4%B8%AD%E6%9C%AA%E5%8C%85%E5%90%AB
-userId%E8%8A%82%E7%82%B9%22
+%E8%B5%84%E6%BA%90%E8%B7%AF%E5%BE%84%22);%0A        logger.debug(%22%E8%AF%B7%E6%B1%82%E4%BF%A1%E6%81%AF%EF%BC%9A%7B%7D%22, data
 );%0A 
@@ -1736,32 +1736,34 @@
  data);%0A        
+//
 Assert.hasKeyAnd
@@ -1771,32 +1771,30 @@
 alue(data, %22
-resource
+userId
 %22, %22%E8%AF%B7%E6%B1%82%E6%8A%A5%E6%96%87%E4%B8%AD%E6%9C%AA%E5%8C%85%E5%90%AB
@@ -1785,36 +1785,40 @@
 erId%22, %22%E8%AF%B7%E6%B1%82%E6%8A%A5%E6%96%87%E4%B8%AD%E6%9C%AA%E5%8C%85%E5%90%AB
-%E8%B5%84%E6%BA%90%E8%B7%AF%E5%BE%84
+userId%E8%8A%82%E7%82%B9
 %22);%0A        Resp

优化后台 日志打印太多问题