fs-danaus
2024-02-21 0a4f165eec7585da85a0bd58444efb5eac06d35f
提交 | 用户 | age
bfda87 1 package com.yc.open.yejibao.controller;
F 2
3 import com.alibaba.fastjson.JSON;
e44ea2 4 import com.alibaba.fastjson.serializer.SerializerFeature;
bfda87 5 import com.yc.entity.DataSourceEntity;
F 6 import com.yc.entity.DemoConstant;
7 import com.yc.exception.ApplicationException;
8 import com.yc.exception.CallBackMessage;
e44ea2 9 import com.yc.factory.FactoryBean;
bfda87 10 import com.yc.multiData.MultiDataSource;
F 11 import com.yc.multiData.SpObserver;
12 import com.yc.open.controller.BaseController;
e44ea2 13 import com.yc.open.deli.entity.T110565Entity;
bfda87 14 import com.yc.open.deli.service.PostDataBaseService;
e44ea2 15 import com.yc.open.init.Task;
F 16 import com.yc.open.mutual.schedule.GateEntity;
bfda87 17 import com.yc.open.utils.ApiUtils;
F 18 import com.yc.open.utils.CallBackMessageToObject;
19 import com.yc.open.yejibao.entry.*;
20 import com.yc.open.yejibao.service.YeJiBaoIfc;
21 import com.yc.sdk.weixinmp.util.MD5Util;
22 import com.yc.service.build.type.T_22_Ifc;
23 import com.yc.service.upload.AttachmentIfc;
24 import com.yc.utils.SessionKey;
25 import lombok.extern.slf4j.Slf4j;
26 import org.apache.commons.lang3.RandomStringUtils;
27 import org.apache.http.HttpResponse;
28 import org.apache.http.HttpStatus;
29 import org.apache.http.client.HttpClient;
30 import org.apache.http.client.config.RequestConfig;
31 import org.apache.http.client.methods.HttpPost;
32 import org.apache.http.entity.StringEntity;
33 import org.apache.http.impl.client.HttpClients;
34 import org.apache.http.util.EntityUtils;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
37 import org.springframework.web.bind.annotation.RequestMapping;
38 import org.springframework.web.bind.annotation.ResponseBody;
39 import org.springframework.web.bind.annotation.RestController;
40
41 import javax.servlet.http.HttpServletRequest;
42 import java.util.ArrayList;
43 import java.util.List;
e44ea2 44 import java.util.Map;
bfda87 45 import java.util.concurrent.ConcurrentHashMap;
F 46
47 /**
48  * 业绩宝相关
49  */
50 @RestController
51 @RequestMapping("/open/yejibao")
52 @Slf4j
53 public class YeJiBaoController extends BaseController {
54     @Autowired
55     T_22_Ifc t22ifc;
56     @Autowired
57     AttachmentIfc attachmentIfc;
58
59     @Autowired
60     YeJiBaoIfc yeJiBaoIfc;
61     @Autowired
62     ThreadPoolTaskExecutor threadPoolExecutor;
63     @Autowired
64     PostDataBaseService mPostDataBaseService;
65
66     /**
67      * 查询与业绩宝关联的所有系统
68      *
69      * @return
70      */
71     @RequestMapping(value = "/allCompany.do")
72     public @ResponseBody Object allCompany(HttpServletRequest request) {
73         CallBackMessageToObject callBackMessageToObject = new CallBackMessageToObject();
74         final StringBuilder errorMsg = new StringBuilder();
75         try {
76             SpObserver.setDBtoDemo();
77             List<DataSoureEntry> dataSoure = yeJiBaoIfc.getDataSoure();
78             if (dataSoure != null && dataSoure.size() > 0) {
79
80                 dataSoure.stream().forEach(x -> {
81                     List<SubCompanyEntry> list = new ArrayList<>();
82                     try {
83                         if (MultiDataSource.getDataSourceMap(x.getDbId() + "") == null) {
84                             //有可能存在当前系统没数据源,需要重新生成
85                             DataSourceEntity dataSourceEntity = null;
86                             DemoConstant demoConstant = new DemoConstant();
87                             demoConstant.setAccessKey(x.getAccessKey());
88                             MultiDataSource.getDataSourceFromDemoDataBase(demoConstant, dataSourceEntity);
89                         }
90                         SpObserver.setDBtoInstance("_" + x.getDbId());
91                         List<CompanyEntry> companyInfo = yeJiBaoIfc.getCompanyInfo();
9e6422 92                         for (CompanyEntry companyEntry : companyInfo) {
F 93                             SubCompanyEntry subCompanyEntry = new SubCompanyEntry();
bfda87 94                             subCompanyEntry.setCompanyID(companyEntry.getCompanyID());
F 95                             subCompanyEntry.setCompanyName(companyEntry.getCompanyName());
96                             list.add(subCompanyEntry);
97                         }
9e6422 98                         String[] temp = x.getSystemUrl().split(";");
F 99                         for (String s : temp) {
100                             if (s.indexOf(":") < 0) {// 只取443端口 https方式
101                                 x.setSystemUrl("https://" + s.toLowerCase());
102                                 break;
103                             }
104                         }
105                        /* boolean isHttps=false;//是否已开启https
bfda87 106                         boolean isfound=false;
F 107                         if(!"https".equalsIgnoreCase(x.getProtocol())){
108                             isHttps=false;
109                         }else{
110                             isHttps=true;
111                         }
112                         if(isHttps){
113                                 for (String s : temp) {
114                                     if (s.indexOf(":") < 0) {// 取443端口
115                                         x.setSystemUrl("https://"+s);
116                                         break;
117                                     }
118                                 }
119
120                         }else{
121                             if ( x.getSystemUrl().indexOf(":9001") > 0) {
122                                 for (String s : temp) {
123                                     if (s.indexOf(":9001") > 0) {// 优先9001,&&s.startsWith("192.168.100.202")
124                                         x.setSystemUrl("http://"+s);
125                                         isfound = true;
126                                         break;
127                                     }
128                                 }
129                             }
130                             if (!isfound && x.getSystemUrl().indexOf(":80") > 0) {
131                                 for (String s : temp) {
132                                     if (s.indexOf(":80") > 0) {
133                                         x.setSystemUrl("http://"+s);
134                                         break;
135                                     }
136                                 }
137                             }
9e6422 138                         }*/
bfda87 139                         x.setAccessKey(MD5Util.string2MD5(x.getAccessKey()));
F 140                         x.setCompanys(list);
141                     } catch (Exception ex) {
142                         errorMsg.append(this.getErrorMsg(ex));
143                     } finally {
144                         SpObserver.setDBtoInstance();
145                     }
146                 });
147                 if (errorMsg.length() == 0) {
148                     return callBackMessageToObject.setData(dataSoure).sendSuccessMessage("成功");
149                 } else {
150                     return callBackMessageToObject.sendErrorMessage(errorMsg.toString());
151                 }
152             }
153             return callBackMessageToObject.sendSuccessMessage("成功");
154         } catch (Exception ex) {
155             return callBackMessageToObject.sendErrorMessage(this.getErrorMsg(ex));
156         } finally {
157             SpObserver.setDBtoInstance();
158         }
159     }
160
161     /**
162      * 查询公司
163      *
164      * @return
165      */
166     @RequestMapping(value = "/getCompanyInfo.do")
167     public @ResponseBody Object getCompanyInfo(HttpServletRequest request) {
168         CallBackMessage callBackMessage = new CallBackMessage();
169         try {
170             Object dbid = request.getAttribute(SessionKey.SHOPPING_DBID);
171             SpObserver.setDBtoInstance("_" + dbid);
172             List<com.yc.open.yejibao.entry.CompanyEntry> result = yeJiBaoIfc.getCompanyInfo();
173             if (result != null && result.size() > 0) {
174                 callBackMessage.setInfo(result);
175                 callBackMessage.sendSuccessMessage("成功");
176                 return callBackMessage.toJSONObject();
177             } else {
178                 throw new ApplicationException("查找不到相关数据");
179             }
180         } catch (Exception ex) {
181             callBackMessage.sendErrorMessage(this.getErrorMsg(ex));
182             return callBackMessage.toJSONObject();
183         } finally {
184             SpObserver.setDBtoInstance();
185         }
186     }
187
188     /**
189      * 查询部门
190      *
191      * @return
192      */
193     @RequestMapping(value = "/getDepartmentInfo.do")
194     public @ResponseBody Object getDepartmentInfo(HttpServletRequest request) {
195         CallBackMessage callBackMessage = new CallBackMessage();
196         try {
197             String data = request.getAttribute(ApiUtils.MAP_OBJECT) + "";
198             DepartmentEntry departmentEntry = JSON.parseObject(data, DepartmentEntry.class);
199             Object dbid = request.getAttribute(SessionKey.SHOPPING_DBID);
200             SpObserver.setDBtoInstance("_" + dbid);
201             List<DepartmentEntry> result = yeJiBaoIfc.getDepartmentInfo(departmentEntry);
202             if (result != null && result.size() > 0) {
203                 callBackMessage.setInfo(result);
204                 callBackMessage.sendSuccessMessage("成功");
205                 return callBackMessage.toJSONObject();
206             } else {
207                 throw new ApplicationException("查找不到相关数据");
208             }
209         } catch (Exception ex) {
210             callBackMessage.sendErrorMessage(this.getErrorMsg(ex));
211             return callBackMessage.toJSONObject();
212         } finally {
213             SpObserver.setDBtoInstance();
214         }
215     }
216
217     /**
218      * 查询人员
219      *
220      * @return
221      */
222     @RequestMapping(value = "/getEmployeeInfo.do")
223     public @ResponseBody Object getEmployeeInfo(HttpServletRequest request) {
224         CallBackMessage callBackMessage = new CallBackMessage();
225         try {
226             String data = request.getAttribute(ApiUtils.MAP_OBJECT) + "";
227             EmployeeEntry employeeEntry = JSON.parseObject(data, EmployeeEntry.class);
228             Object dbid = request.getAttribute(SessionKey.SHOPPING_DBID);
229             SpObserver.setDBtoInstance("_" + dbid);
230             List<EmployeeEntry> result = yeJiBaoIfc.getEmployeeInfo(employeeEntry);
231             if (result != null && result.size() > 0) {
232                 callBackMessage.setInfo(result);
233                 callBackMessage.sendSuccessMessage("成功");
234                 return callBackMessage.toJSONObject();
235             } else {
236                 throw new ApplicationException("查找不到相关数据");
237             }
238         } catch (Exception ex) {
239             callBackMessage.sendErrorMessage(this.getErrorMsg(ex));
240             return callBackMessage.toJSONObject();
241         } finally {
242             SpObserver.setDBtoInstance();
243         }
244     }
245
246     /**
247      * 查询客户
248      *
249      * @return
250      */
9e6422 251     @RequestMapping(value = "/getCustomerInfo.do")
F 252     public @ResponseBody Object getCustomerInfo(HttpServletRequest request) {
bfda87 253         CallBackMessage callBackMessage = new CallBackMessage();
F 254         try {
255             String data = request.getAttribute(ApiUtils.MAP_OBJECT) + "";
9e6422 256             CustomerEntry clientEntry = JSON.parseObject(data, CustomerEntry.class);
bfda87 257             Object dbid = request.getAttribute(SessionKey.SHOPPING_DBID);
F 258             SpObserver.setDBtoInstance("_" + dbid);
9e6422 259             List<CustomerEntry> result = yeJiBaoIfc.getCustomerInfo(clientEntry);
bfda87 260             if (result != null && result.size() > 0) {
F 261                 callBackMessage.setInfo(result);
262                 callBackMessage.sendSuccessMessage("成功");
263                 return callBackMessage.toJSONObject();
264             } else {
265                 throw new ApplicationException("查找不到相关数据");
266             }
267         } catch (Exception ex) {
268             callBackMessage.sendErrorMessage(this.getErrorMsg(ex));
269             return callBackMessage.toJSONObject();
270         } finally {
271             SpObserver.setDBtoInstance();
272         }
273     }
274
275     /**
276      * 接收客户
277      *
278      * @return
279      */
9e6422 280     @RequestMapping(value = "/customer.do")
F 281     public @ResponseBody Object customer(HttpServletRequest request) {
bfda87 282         CallBackMessage callBackMessage = new CallBackMessage();
F 283         try {
284             String data = request.getAttribute(ApiUtils.MAP_OBJECT) + "";
9e6422 285             List<CustomerEntry> clientEntry = JSON.parseArray(data, CustomerEntry.class);
bfda87 286             if (clientEntry == null || clientEntry.size() == 0) {
F 287                 throw new ApplicationException("客户数据不能为空");
288             }
289             Object dbid = request.getAttribute(SessionKey.SHOPPING_DBID);
290             SpObserver.setDBtoInstance("_" + dbid);
291             env.put("dbid", dbid + "");
9e6422 292             List<CustomerCallBackEntry> result = yeJiBaoIfc.customer(clientEntry, env);
bfda87 293             if (result != null && result.size() > 0) {
F 294                 callBackMessage.setInfo(result);
295                 callBackMessage.sendSuccessMessage("成功");
296                 return callBackMessage.toJSONObject();
297             } else {
298                 throw new ApplicationException("全部客户数据更新都不成功");
299             }
300         } catch (Exception ex) {
301             callBackMessage.sendErrorMessage(this.getErrorMsg(ex));
302             return callBackMessage.toJSONObject();
303         } finally {
304             SpObserver.setDBtoInstance();
305         }
306     }
307
308     /**
309      * 接收部门
310      *
311      * @return
312      */
313     @RequestMapping(value = "/department.do")
314     public @ResponseBody Object department(HttpServletRequest request) {
315         CallBackMessage callBackMessage = new CallBackMessage();
316         try {
317             String data = request.getAttribute(ApiUtils.MAP_OBJECT) + "";
318             List<DepartmentEntry> departmentEntries = JSON.parseArray(data, DepartmentEntry.class);
319             if (departmentEntries == null || departmentEntries.size() == 0) {
320                 throw new ApplicationException("部门数据不能为空");
321             }
322             Object dbid = request.getAttribute(SessionKey.SHOPPING_DBID);
323             SpObserver.setDBtoInstance("_" + dbid);
324             env.put("dbid", dbid + "");
325             List<DepartmentCallBackEntry> result = yeJiBaoIfc.department(departmentEntries, env);
326             if (result != null && result.size() > 0) {
327                 callBackMessage.setInfo(result);
328                 callBackMessage.sendSuccessMessage("成功");
329                 return callBackMessage.toJSONObject();
330             } else {
331                 throw new ApplicationException("全部部门数据更新都不成功");
332             }
333         } catch (Exception ex) {
334             callBackMessage.sendErrorMessage(this.getErrorMsg(ex));
335             return callBackMessage.toJSONObject();
336         } finally {
337             SpObserver.setDBtoInstance();
338         }
339     }
340
341     /**
342      * 接收人员
343      *
344      * @return
345      */
346     @RequestMapping(value = "/employee.do")
347     public @ResponseBody Object employee(HttpServletRequest request) {
348         CallBackMessage callBackMessage = new CallBackMessage();
349         try {
350             String data = request.getAttribute(ApiUtils.MAP_OBJECT) + "";
351             List<EmployeeEntry> employeeEntries = JSON.parseArray(data, EmployeeEntry.class);
352             if (employeeEntries == null || employeeEntries.size() == 0) {
353                 throw new ApplicationException("人员数据不能为空");
354             }
355             Object dbid = request.getAttribute(SessionKey.SHOPPING_DBID);
356             SpObserver.setDBtoInstance("_" + dbid);
357             env.put("dbid", dbid + "");
358             List<EmployeeCallBackEntry> result = yeJiBaoIfc.employee(employeeEntries, env);
359             if (result != null && result.size() > 0) {
360                 callBackMessage.setInfo(result);
361                 callBackMessage.sendSuccessMessage("成功");
362                 return callBackMessage.toJSONObject();
363             } else {
364                 throw new ApplicationException("全部人员数据更新都不成功");
365             }
366         } catch (Exception ex) {
367             callBackMessage.sendErrorMessage(this.getErrorMsg(ex));
368             return callBackMessage.toJSONObject();
369         } finally {
370             SpObserver.setDBtoInstance();
371         }
372     }
373
374     public CallBackMessage doPost(String url, String companyId, String data) throws Exception {//不指定参数名发送
375
376         CallBackMessage callBackMessage = new CallBackMessage();
377         try {
378             ConcurrentHashMap<String, String> concurrentHashMap = new ConcurrentHashMap();
379             String timestamp = System.currentTimeMillis() + "";
380             String nonce = RandomStringUtils.randomAlphanumeric(8);
381             concurrentHashMap.putIfAbsent("timestamp", timestamp);
382             concurrentHashMap.putIfAbsent("nonce", nonce);
383             concurrentHashMap.putIfAbsent("companyID", companyId);
384             String sign = ApiUtils.SignRequestByWms(concurrentHashMap, "", data);
385             HttpClient httpClient = HttpClients.createDefault();
386             HttpPost post = new HttpPost(url);
387             post.setHeader("cache-control", "no-cache");
388             post.setHeader("contentType", "text/html;charset=uft-8");
389             post.setHeader("timestamp", timestamp);
390             post.setHeader("nonce", nonce);
391             post.setHeader("companyID", companyId);
392             post.setHeader("sign", sign);
393             RequestConfig requestConfig = RequestConfig.custom()
394                     .setConnectTimeout(3000).setConnectionRequestTimeout(3000)
395                     .setSocketTimeout(3000).build();
396             post.setConfig(requestConfig);//连接及获得数据的超时设置,3秒
397             if (data != null && !"".equals(data)) {
398                 StringEntity myEntity = new StringEntity(data, "UTF-8");
399                 post.setHeader("Content-type", "application/json;charset=UTF-8");
400                 myEntity.setContentType("application/json;charset=UTF-8");
401                 post.setEntity(myEntity);
402
403             }
404             HttpResponse response = httpClient.execute(post);
405             int statusCode = response.getStatusLine().getStatusCode();
406             if (statusCode != HttpStatus.SC_OK) {
407                 callBackMessage.sendErrorMessage("获取响应失败,请重新提交-" + response.getStatusLine().getStatusCode() + "[" + response.getStatusLine().getReasonPhrase() + "]");
408                 return callBackMessage;
409             } else {
410                 callBackMessage.setData(EntityUtils.toString(response.getEntity(), "UTF-8"));
411                 callBackMessage.sendSuccessMessage("推送成功");
412                 return callBackMessage;
413             }
414         } catch (Exception e) {
415             callBackMessage.sendErrorMessage(e.getMessage());
416             return callBackMessage;
417         }
418
419     }
e44ea2 420
a6c1ad 421     public void canelT120201(GateEntity entity, Task task) throws Exception {
F 422         CancelEntry result = this.yeJiBaoIfc.cancelInfo(entity.getDocCode(), task);
423         if (result != null) {
424             String data = JSON.toJSONString(result, SerializerFeature.SortField);
425             com.yc.exception.CallBackMessage callBackMessage = this.doPost(task.getApiUrl(), result.getCompanyID(), data);
426             if (callBackMessage.getState() == 0) {
427                 Map<String, String> map = JSON.parseObject(callBackMessage.getData(), Map.class);
428                 if (String.valueOf(map.get("code")).equals("1")) {
429                     //成功
430                     callBackMessage.sendSuccessMessage("业绩宝接收取消确认成功");
431                     updateStateCanel(result, callBackMessage, entity);
432                 } else {
433                     throw new ApplicationException(map.get("msg"));
434                 }
435             } else {
436                 throw new ApplicationException(callBackMessage.getError());
437             }
438         }
439     }
440
441     private void updateStateCanel(CancelEntry cancelEntry, CallBackMessage callBackMessage, GateEntity entity) {
442         PostDataBaseService mPostDataBaseService = (PostDataBaseService) FactoryBean.getBean("postDataBaseService");
443         try {
444             SpObserver.setDBtoInstance("_" + entity.getDbid());
0a4f16 445             mPostDataBaseService.getSimpleJdbcTemplate().queryForObject("update a set a.YeJiBaoPushStatus=" + callBackMessage.getState() + ",a.YeJiBaoPushDate=getdate(),a.YeJiBaoPushTimes=isnull(a.YeJiBaoPushTimes,0)+1,a.YeJiBaoPushMsg='" + callBackMessage.getSuccess() + "'  from t120201H  a where a.doccode='" + cancelEntry.getDocCode() + "' \n select @@rowcount", Integer.class);
a6c1ad 446         } catch (Exception ex) {
F 447             mPostDataBaseService.doLogBy110565(new T110565Entity(
448                     120201,
449                     cancelEntry.getDocCode(),
450                     null,
451                     "销售订单120201",
452                     "取消确认出错:" + ex.getMessage().replaceAll("'", "''")
453             ));
454         } finally {
455             SpObserver.setDBtoInstance();
456         }
457     }
458
e44ea2 459     public void pushT120201(GateEntity entity, Task task) throws Exception {
F 460         T120201Entity result = this.yeJiBaoIfc.getT120201List(entity.getDocCode(), task);
461         if (result != null) {
462             String data = JSON.toJSONString(result, SerializerFeature.SortField);
463             CallBackMessage callBackMessage = this.doPost(task.getApiUrl(), result.getCompanyID(), data);
464             if (callBackMessage.getState() == 0) {
465                 Map<String, String> map = JSON.parseObject(callBackMessage.getData(), Map.class);
466                 if (String.valueOf(map.get("code")).equals("1")) {
467                     //成功
468                     callBackMessage.sendSuccessMessage("推送成功");
469                     updateState(result, callBackMessage, entity);
470                 } else {
471                     throw new ApplicationException(map.get("msg"));
472                 }
473             } else {
474                 throw new ApplicationException(callBackMessage.getError());
475             }
476         }
477     }
478
479     private void updateState(T120201Entity t120201Entity, CallBackMessage callBackMessage, GateEntity entity) {
480         PostDataBaseService mPostDataBaseService = (PostDataBaseService) FactoryBean.getBean("postDataBaseService");
481         try {
482             SpObserver.setDBtoInstance("_" + entity.getDbid());
0a4f16 483             mPostDataBaseService.getSimpleJdbcTemplate().queryForObject("update a set a.YeJiBaoPushStatus=" + callBackMessage.getState() + ",a.YeJiBaoPushDate=getdate(),a.YeJiBaoPushTimes=isnull(a.YeJiBaoPushTimes,0)+1,a.YeJiBaoPushMsg='" + callBackMessage.getSuccess() + "'  from t120201H  a where a.doccode='" + t120201Entity.getDocCode() + "' \n select @@rowcount", Integer.class);
e44ea2 484         } catch (Exception ex) {
F 485             mPostDataBaseService.doLogBy110565(new T110565Entity(
486                     120201,
487                     t120201Entity.getDocCode(),
488                     null,
489                     "销售订单120201",
490                     ex.getMessage().replaceAll("'", "''")
491             ));
492         } finally {
493             SpObserver.setDBtoInstance();
494         }
495     }
bfda87 496 }