jiangping
2024-03-01 22eb1991a9363473fe9051a7ed3293b7a6ddd671
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
package com.doumee.core.erp;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.doumee.core.constants.ResponseStatus;
import com.doumee.core.erp.model.openapi.request.erp.ApproveAddRequest;
import com.doumee.core.erp.model.openapi.request.erp.OrgListRequest;
import com.doumee.core.erp.model.openapi.request.erp.UserFailRequest;
import com.doumee.core.erp.model.openapi.request.erp.UserListRequest;
import com.doumee.core.erp.model.openapi.response.erp.ApproveInfoResponse;
import com.doumee.core.erp.model.openapi.response.erp.ERPApiResponse;
import com.doumee.core.erp.model.openapi.response.erp.ErpOrgListResponse;
import com.doumee.core.erp.model.openapi.response.erp.ErpUserListResponse;
import com.doumee.core.exception.BusinessException;
import com.doumee.core.model.PageData;
import com.doumee.core.model.PageWrap;
import com.doumee.core.utils.Constants;
import com.doumee.core.utils.HttpsUtil;
import com.doumee.dao.business.model.InterfaceLog;
import com.doumee.dao.business.model.Member;
import com.doumee.service.business.InterfaceLogService;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
@Service
public class ErpTool {
 
    @Autowired
    private InterfaceLogService interfaceLogService;
 
    public void saveInterfaceLog(Object param, String path,String result,Integer type) {
        InterfaceLog interfaceLog=new InterfaceLog();
        interfaceLog.setType(type);
        interfaceLog.setCreateDate(new Date());
        interfaceLog.setIsdeleted(Constants.ZERO);
        if(param!=null){
            interfaceLog.setRequest(JSONObject.toJSONString(param));
        }
        interfaceLog.setPlat(Constants.ONE);
        interfaceLog.setRepose(result);
        interfaceLog.setName(path);
        interfaceLog.setUrl(path);
        interfaceLogService.create(interfaceLog);
    }
 
 
 
    /**
     * 同步组织
     * @param url
     * @param param
     * @return
     */
    public   List<ErpOrgListResponse> getErpOrgList(String url, OrgListRequest param){
        List<ErpOrgListResponse> list = new ArrayList<>();
        try {
            int page = 1;
            int pageSize = 100;
            boolean hasLast = true;
            while (hasLast) {
                hasLast = false;
                PageWrap<OrgListRequest> pageWrap = new PageWrap<>();
                pageWrap.setModel(param);
                pageWrap.setPage(page);
                pageWrap.setCapacity(pageSize);
                String result = HttpsUtil.postJson(url, JSONObject.toJSONString(pageWrap));
                if (StringUtils.isNotBlank(result)) {
                    TypeReference typeReference =
                            new TypeReference<ERPApiResponse<PageData<ErpOrgListResponse>>>() {
                            };
                    ERPApiResponse<PageData<ErpOrgListResponse>> response = JSONObject.parseObject(result, typeReference.getType());
                    if (response != null && response.getData() != null && response.getData().getRecords() != null) {
                        list.addAll(response.getData().getRecords());
                        if (page * pageSize < response.getData().getTotal()) {
                            hasLast = true;//还有下一页
                        }
                        page++;
                    }
                }
            }
        }catch (Exception e){
            throw e;
        }finally {
            saveInterfaceLog(param,url,JSONArray.toJSONString(list),Constants.ZERO);
        }
        return  list;
    }
 
    /**
     * 同步人员
     * @param url
     * @param param
     * @return
     */
    public List<ErpUserListResponse> getErpUserList(String url, UserListRequest param){
 
        List<ErpUserListResponse> list = new ArrayList<>();
        try{
            int page =1;
            int pageSize  = 100;
            boolean hasLast = true;
            while (hasLast){
                hasLast = false;
                PageWrap<UserListRequest> pageWrap  = new PageWrap<>();
                pageWrap.setModel(param);
                pageWrap.setPage(page);
                pageWrap.setCapacity(pageSize);
                String result = HttpsUtil.postJson(url, JSONObject.toJSONString(pageWrap));
                if(StringUtils.isNotBlank(result)){
                    TypeReference typeReference =
                            new TypeReference<ERPApiResponse<PageData<ErpUserListResponse>>>(){};
                   ERPApiResponse<PageData <ErpUserListResponse>> response = JSONObject.parseObject(result, typeReference.getType());
                    if(response!=null && response.getData()!=null && response.getData().getRecords()!=null){
                        list.addAll(response.getData().getRecords());
                        if(pageSize*page < response.getData().getTotal() ){
                            hasLast =true;//还有下一页
                        }
                    }
                    page++;
                }
            }
        }catch (Exception e){
            throw e;
        }finally {
            saveInterfaceLog(param,url, JSONArray.toJSONString(list),Constants.ZERO);
        }
        return  list;
 
    }
 
    /**
     * 审批提交
     * @param url
     * @param param
     * @return
     */
    public  ApproveInfoResponse submitApprove(String url, ApproveAddRequest param){
        System.out.println(JSONObject.toJSONString(param));
        String result = HttpsUtil.postJson(url, JSONObject.toJSONString(param));
        try{
            if(StringUtils.isNotBlank(result)){
                TypeReference typeReference =
                        new TypeReference<ERPApiResponse<String>>(){};
                ERPApiResponse<String> response = JSONObject.parseObject(result, typeReference.getType());
                if(response!=null && response.isSuccess()){
                    ApproveInfoResponse approveInfoResponse = new ApproveInfoResponse();
                    approveInfoResponse.setId(response.getData());
                    return approveInfoResponse;
                }else if(response!=null && !response.isSuccess()){
                    throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(),response.getMessage());
                }
            }
        }catch (Exception e){
            throw e;
        }finally {
           saveInterfaceLog(param,url,JSONObject.toJSONString(result),Constants.ZERO);
        }
        return  null;
    }
 
    /**
     * 通知人员同步失败
     * @param url
     * @param param
     * @return
     */
    public   ERPApiResponse noticeUserStatus(String url, UserFailRequest param){
        String result = HttpsUtil.postJson(url, JSONObject.toJSONString(param));
        try{
            if(StringUtils.isNotBlank(result)){
                TypeReference typeReference =
                        new TypeReference<ERPApiResponse>(){};
                ERPApiResponse response = JSONObject.parseObject(result, typeReference.getType());
                if(response!=null){
                    return response;
                }
            }
        }catch (Exception e){
            throw e;
        }finally {
            saveInterfaceLog(param,url,JSONObject.toJSONString(result),Constants.ZERO);
        }
        return  null;
    }
}