java110
2022-07-15 df1a5c1293763f0d08e963b159bc9213125d97c3
优化代码
1个文件已修改
1个文件已添加
99 ■■■■■ 已修改文件
service-store/src/main/java/com/java110/store/cmd/store/UserHasStoreCmd.java 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
service-user/src/main/java/com/java110/user/cmd/user/QueryUserPrivilege.java 97 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
service-store/src/main/java/com/java110/store/cmd/store/UserHasStoreCmd.java
@@ -16,7 +16,7 @@
/**
 * 查询用户是否存在 商户信息
 */
@Java110Cmd(serviceCode = "store.userHasStore")
@Java110Cmd(serviceCode = "query.store.byuser")
public class UserHasStoreCmd extends Cmd {
    @Autowired
    private IQueryServiceSMO queryServiceSMOImpl;
service-user/src/main/java/com/java110/user/cmd/user/QueryUserPrivilege.java
New file
@@ -0,0 +1,97 @@
package com.java110.user.cmd.user;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.java110.core.annotation.Java110Cmd;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.event.cmd.Cmd;
import com.java110.core.event.cmd.CmdEvent;
import com.java110.dto.store.StoreDto;
import com.java110.intf.store.IStoreV1InnerServiceSMO;
import com.java110.service.context.DataQuery;
import com.java110.service.smo.IQueryServiceSMO;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.Assert;
import com.java110.utils.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import java.util.List;
@Java110Cmd(serviceCode = "query.user.privilege")
public class QueryUserPrivilege extends Cmd {
    @Autowired
    private IStoreV1InnerServiceSMO storeV1InnerServiceSMOImpl;
    @Autowired
    private IQueryServiceSMO queryServiceSMOImpl;
    @Override
    public void validate(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {
        String userId = context.getReqHeaders().get("user-id");
        String storeId = context.getReqHeaders().get("store-id");
        Assert.hasLength(userId, "未包含用户");
        Assert.hasLength(storeId, "未包含用户");
    }
    @Override
    public void doCmd(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {
        String userId = context.getReqHeaders().get("user-id");
        String storeId = context.getReqHeaders().get("store-id");
        StoreDto storeDto = new StoreDto();
        storeDto.setStoreId(storeId);
        storeDto.setPage(1);
        storeDto.setRow(1);
        List<StoreDto> storeDtos = storeV1InnerServiceSMOImpl.queryStores(storeDto);
        Assert.listOnlyOne(storeDtos, "商户不存在");
        DataQuery dataQuery = new DataQuery();
        dataQuery.setServiceCode("query.user.privilege");
        JSONObject param = new JSONObject();
        param.put("userId", userId);
        param.put("domain", storeDtos.get(0).getStoreTypeCd());
        dataQuery.setRequestParams(param);
        queryServiceSMOImpl.commonQueryService(dataQuery);
        ResponseEntity<String> privilegeGroup = dataQuery.getResponseEntity();
        if (privilegeGroup.getStatusCode() != HttpStatus.OK) {
            context.setResponseEntity(privilegeGroup);
            return ;
        }
        JSONObject resultObj = JSONObject.parseObject(privilegeGroup.getBody().toString());
        JSONArray privileges = resultObj.getJSONArray("privileges");
        JSONArray tmpPrivilegeArrays = new JSONArray();
        JSONObject privilegeObj = null;
        for (int privilegeIndex = 0; privilegeIndex < privileges.size(); privilegeIndex++) {
            privilegeObj = privileges.getJSONObject(privilegeIndex);
            hasSameData(privilegeObj, tmpPrivilegeArrays);
        }
        JSONObject resObj = new JSONObject();
        resObj.put("datas", privileges);
        context.setResponseEntity(new ResponseEntity<String>(resObj.toJSONString(), HttpStatus.OK));
    }
    private void hasSameData(JSONObject privilegeObj, JSONArray tmpPrivilegeArrays) {
        JSONObject tmpPrivilegeObj = null;
        for (int tmpPrivilegeIndex = 0; tmpPrivilegeIndex < tmpPrivilegeArrays.size(); tmpPrivilegeIndex++) {
            tmpPrivilegeObj = tmpPrivilegeArrays.getJSONObject(tmpPrivilegeIndex);
            if (privilegeObj.getString("pId").equals(tmpPrivilegeObj.getString("pId"))) {
                if (!StringUtil.isEmpty(privilegeObj.getString("pgId"))) {
                    tmpPrivilegeArrays.remove(tmpPrivilegeIndex);
                    tmpPrivilegeArrays.add(privilegeObj);
                }
                return;
            }
        }
        tmpPrivilegeArrays.add(privilegeObj);
    }
}