wuxw
2024-04-11 c434ece53b249ae10f20f9fd552d03ca671c5b11
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
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.service.context.DataQuery;
import com.java110.service.smo.IQueryServiceSMO;
import com.java110.utils.constant.ServiceCodeConstant;
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.text.ParseException;
 
@Java110Cmd(serviceCode = "query.staff.byName")
public class QueryStaffByNameCmd extends Cmd {
 
    @Autowired
    private IQueryServiceSMO queryServiceSMOImpl;
 
    @Override
    public void validate(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {
        Assert.hasKeyAndValue(reqJson, "storeId", "请求报文中未包含storeId节点");
        Assert.hasKeyAndValue(reqJson, "name", "请求报文中未包含name节点");
    }
 
    @Override
    public void doCmd(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException, ParseException {
        JSONObject resultJson = JSONObject.parseObject("{\"total:\":10,\"datas\":[]}");
        DataQuery dataQuery = new DataQuery();
        dataQuery.setServiceCode(ServiceCodeConstant.SERVICE_CODE_QUERY_USER_BY_NAME);
 
        dataQuery.setRequestParams(reqJson);
        queryServiceSMOImpl.commonQueryService(dataQuery);
        ResponseEntity<String> responseEntity = dataQuery.getResponseEntity();
 
        if (responseEntity.getStatusCode() != HttpStatus.OK) {
            context.setResponseEntity(responseEntity);
            return;
        }
 
        String useIds = getUserIds(responseEntity);
        if (StringUtil.isEmpty(useIds)) {
            context.setResponseEntity(responseEntity);
            return;
        }
        JSONArray userInfos = getUserInfos(responseEntity);
        JSONObject paramIn = new JSONObject();
        paramIn.put("userIds", useIds);
        paramIn.put("storeId", reqJson.getString("storeId"));
 
 
        dataQuery = new DataQuery();
        dataQuery.setServiceCode(ServiceCodeConstant.SERVICE_CODE_QUERY_STOREUSER_BYUSERIDS);
 
        dataQuery.setRequestParams(paramIn);
        queryServiceSMOImpl.commonQueryService(dataQuery);
        responseEntity = dataQuery.getResponseEntity();
 
        if (responseEntity.getStatusCode() != HttpStatus.OK) {
            context.setResponseEntity(responseEntity);
            return;
        }
 
        resultJson.put("datas", getStaffUsers(userInfos, responseEntity));
        responseEntity = new ResponseEntity<String>(resultJson.toJSONString(), HttpStatus.OK);
        context.setResponseEntity(responseEntity);
 
    }
 
    /**
     * 查询商户员工
     *
     * @param userInfos      用户信息
     * @param responseEntity 商户返回的用户ID信息
     * @return
     */
    private JSONArray getStaffUsers(JSONArray userInfos, ResponseEntity<String> responseEntity) {
 
 
        JSONObject storeUserInfo = null;
        JSONArray newStaffUsers = new JSONArray();
        JSONArray storeUsers = JSONObject.parseObject(responseEntity.getBody().toString()).getJSONArray("storeUsers");
        if (storeUsers == null || storeUsers.size() < 1) {
            return newStaffUsers;
        }
 
        for (int storeUserIndex = 0; storeUserIndex < storeUsers.size(); storeUserIndex++) {
            storeUserInfo = storeUsers.getJSONObject(storeUserIndex);
 
            for (int userIndex = 0; userIndex < userInfos.size(); userIndex++) {
                if (userInfos.getJSONObject(userIndex).getString("userId").equals(storeUserInfo.getString("userId"))) {
                    newStaffUsers.add(userInfos.getJSONObject(userIndex));
                }
            }
        }
 
 
        return newStaffUsers;
    }
 
    /**
     * 获取用ID
     * 如:
     * 123,456,567
     *
     * @param responseEntity
     * @return
     */
    private String getUserIds(ResponseEntity<String> responseEntity) {
        JSONObject userInfo = null;
        String userId = "";
        JSONArray resultInfo = JSONObject.parseObject(responseEntity.getBody().toString()).getJSONArray("users");
        if (resultInfo == null || resultInfo.size() < 1) {
            return userId;
        }
 
        for (int userIndex = 0; userIndex < resultInfo.size(); userIndex++) {
            userInfo = resultInfo.getJSONObject(userIndex);
            userId += (userInfo.getString("userId") + ",");
        }
 
        userId = userId.length() > 0 ? userId.substring(0, userId.lastIndexOf(",")) : userId;
 
        return userId;
    }
 
 
    /**
     * 获取用户
     *
     * @param responseEntity
     * @return
     */
    private JSONArray getUserInfos(ResponseEntity<String> responseEntity) {
        JSONArray resultInfo = JSONObject.parseObject(responseEntity.getBody().toString()).getJSONArray("users");
        if (resultInfo == null || resultInfo.size() < 1) {
            return null;
        }
 
        return resultInfo;
    }
 
 
}