wuxw
2019-04-03 ece1ed8af3ee4e5dab9e2b78255a0ac60c2924b2
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package com.java110.api.listener.users;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.java110.api.listener.AbstractServiceApiDataFlowListener;
import com.java110.common.cache.MappingCache;
import com.java110.common.constant.*;
import com.java110.common.util.Assert;
import com.java110.core.annotation.Java110Listener;
import com.java110.core.context.DataFlowContext;
import com.java110.core.factory.AuthenticationFactory;
import com.java110.core.factory.DataFlowFactory;
import com.java110.core.factory.GenerateCodeFactory;
import com.java110.entity.center.AppService;
import com.java110.event.service.api.ServiceDataFlowEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.*;
 
/**
 * 添加员工 2018年12月6日
 * Created by wuxw on 2018/5/18.
 */
@Java110Listener("addStaffServiceListener")
public class AddStaffServiceListener extends AbstractServiceApiDataFlowListener{
 
    private final static Logger logger = LoggerFactory.getLogger(AddStaffServiceListener.class);
 
 
 
    @Override
    public String getServiceCode() {
        return ServiceCodeConstant.SERVICE_CODE_USER_STAFF_ADD;
    }
 
    @Override
    public HttpMethod getHttpMethod() {
        return HttpMethod.POST;
    }
 
 
    @Override
    public int getOrder() {
        return 0;
    }
 
 
    /**
     * 添加员工信息
     *
     *
     *
     * @param event
     */
    @Override
    public void soService(ServiceDataFlowEvent event) {
        //获取数据上下文对象
        DataFlowContext dataFlowContext = event.getDataFlowContext();
        AppService service = event.getAppService();
        String paramIn = dataFlowContext.getReqData();
        Assert.isJsonObject(paramIn,"添加员工时请求参数有误,不是有效的json格式 "+paramIn);
        JSONObject paramInJson = JSONObject.parseObject(paramIn);
        Assert.jsonObjectHaveKey(paramInJson,"storeId","请求参数中未包含storeId 节点,请确认");
        Assert.jsonObjectHaveKey(paramInJson,"storeTypeCd","请求参数中未包含storeTypeCd 节点,请确认");
        JSONArray businesses = new JSONArray();
        //判断请求报文中包含 userId 并且 不为-1时 将已有用户添加为员工,反之,则添加用户再将用户添加为员工
        String userId = "";
        String oldUserId = "";
 
        if(!paramInJson.containsKey("userId") || "-1".equals(paramInJson.getString("userId"))){
            //将userId 强制写成-1
            oldUserId = "-1";
            userId = GenerateCodeFactory.getUserId();
            paramInJson.put("userId",userId);
            //添加用户
            JSONObject business = addUser(paramInJson,dataFlowContext);
            businesses.add(business);
        }
 
        paramInJson.put("userId",userId);
        paramInJson.put("relCd","-1".equals(oldUserId)?StoreUserRelConstant.REL_COMMON:StoreUserRelConstant.REL_ADMIN);
 
        JSONObject staffBusiness = addStaff(paramInJson);
        businesses.add(staffBusiness);
 
        HttpHeaders header = new HttpHeaders();
        dataFlowContext.getRequestCurrentHeaders().put(CommonConstant.HTTP_USER_ID,userId);
        dataFlowContext.getRequestCurrentHeaders().put(CommonConstant.HTTP_ORDER_TYPE_CD,"D");
 
        String paramInObj = super.restToCenterProtocol(businesses,dataFlowContext.getRequestCurrentHeaders()).toJSONString();
 
        //将 rest header 信息传递到下层服务中去
        super.freshHttpHeader(header,dataFlowContext.getRequestCurrentHeaders());
 
        HttpEntity<String> httpEntity = new HttpEntity<String>(paramInObj, header);
        //http://user-service/test/sayHello
        super.doRequest(dataFlowContext, service, httpEntity);
 
        super.doResponse(dataFlowContext);
 
        //如果不成功直接返回
        if(dataFlowContext.getResponseEntity().getStatusCode() != HttpStatus.OK){
            return ;
        }
 
        //赋权
        privilegeUserDefault(dataFlowContext,paramInJson);
    }
 
    /**
     * 添加员工
     * @param paramInJson
     * @return
     */
    private JSONObject addStaff(JSONObject paramInJson){
 
        JSONObject business = JSONObject.parseObject("{\"datas\":{}}");
        business.put(CommonConstant.HTTP_BUSINESS_TYPE_CD, BusinessTypeConstant.BUSINESS_TYPE_SAVE_STORE_USER);
        business.put(CommonConstant.HTTP_SEQ,1);
        business.put(CommonConstant.HTTP_INVOKE_MODEL,CommonConstant.HTTP_INVOKE_MODEL_S);
        JSONArray businessStoreUsers = new JSONArray();
        JSONObject businessStoreUser = new JSONObject();
        businessStoreUser.put("storeId",paramInJson.getString("storeId"));
        businessStoreUser.put("storeUserId","-1");
        businessStoreUser.put("userId",paramInJson.getString("userId"));
        businessStoreUser.put("relCd",paramInJson.getString("relCd"));
        businessStoreUsers.add(businessStoreUser);
        business.getJSONObject(CommonConstant.HTTP_BUSINESS_DATAS).put("businessStoreUser",businessStoreUsers);
 
        return business;
    }
 
    /**
     * 添加用户
     * @param paramObj
     */
    private JSONObject addUser(JSONObject paramObj,DataFlowContext dataFlowContext){
 
        //校验json 格式中是否包含 name,email,levelCd,tel
        Assert.jsonObjectHaveKey(paramObj,"name","请求参数中未包含name 节点,请确认");
        Assert.jsonObjectHaveKey(paramObj,"email","请求参数中未包含email 节点,请确认");
        Assert.jsonObjectHaveKey(paramObj,"tel","请求参数中未包含tel 节点,请确认");
 
 
        JSONObject business = JSONObject.parseObject("{\"datas\":{}}");
        business.put(CommonConstant.HTTP_BUSINESS_TYPE_CD, BusinessTypeConstant.BUSINESS_TYPE_SAVE_USER_INFO);
        business.put(CommonConstant.HTTP_SEQ,1);
        business.put(CommonConstant.HTTP_INVOKE_MODEL,CommonConstant.HTTP_INVOKE_MODEL_S);
 
        business.getJSONObject(CommonConstant.HTTP_BUSINESS_DATAS).put("businessUser",refreshParamIn(paramObj));
 
        return business;
    }
 
    /**
     * 对请求报文处理
     * @param paramObj
     * @return
     */
    private JSONObject refreshParamIn(JSONObject paramObj){
        //paramObj.put("userId","-1");
        paramObj.put("levelCd", UserLevelConstant.USER_LEVEL_STAFF);
        //设置默认密码
        String staffDefaultPassword = MappingCache.getValue(MappingConstant.KEY_STAFF_DEFAULT_PASSWORD);
        Assert.hasLength(staffDefaultPassword,"映射表中未设置员工默认密码,请检查"+MappingConstant.KEY_STAFF_DEFAULT_PASSWORD);
        staffDefaultPassword = AuthenticationFactory.passwdMd5(staffDefaultPassword);
        paramObj.put("password",staffDefaultPassword);
        return paramObj;
    }
 
 
    /**
     * 用户赋权
     * @return
     */
    private void privilegeUserDefault(DataFlowContext dataFlowContext,JSONObject paramObj){
        ResponseEntity responseEntity= null;
        AppService appService = DataFlowFactory.getService(dataFlowContext.getAppId(), ServiceCodeConstant.SERVICE_CODE_SAVE_USER_DEFAULT_PRIVILEGE);
        if(appService == null){
            responseEntity = new ResponseEntity<String>("当前没有权限访问"+ServiceCodeConstant.SERVICE_CODE_SAVE_USER_DEFAULT_PRIVILEGE,HttpStatus.UNAUTHORIZED);
            dataFlowContext.setResponseEntity(responseEntity);
            return ;
        }
        String requestUrl = appService.getUrl();
        HttpHeaders header = new HttpHeaders();
        header.add(CommonConstant.HTTP_SERVICE.toLowerCase(),ServiceCodeConstant.SERVICE_CODE_SAVE_USER_DEFAULT_PRIVILEGE);
        super.freshHttpHeader(header,dataFlowContext.getRequestCurrentHeaders());
        JSONObject paramInObj = new JSONObject();
        paramInObj.put("userId",paramObj.getString("userId"));
        paramInObj.put("storeTypeCd",paramObj.getString("storeTypeCd"));
        paramInObj.put("userFlag","staff");
        HttpEntity<String> httpEntity = new HttpEntity<String>(paramInObj.toJSONString(), header);
        doRequest(dataFlowContext,appService,httpEntity);
        responseEntity = dataFlowContext.getResponseEntity();
 
        if(responseEntity.getStatusCode() != HttpStatus.OK){
            dataFlowContext.setResponseEntity(responseEntity);
        }
    }
 
 
}