|
@@ -1,17 +1,27 @@
|
|
|
package com.huimv.env.produce.controller;
|
|
|
|
|
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
import com.huimv.common.utils.Result;
|
|
|
+import com.huimv.common.utils.ResultCode;
|
|
|
+import com.huimv.env.produce.service.DataSourceService;
|
|
|
import com.huimv.env.produce.service.StockDetailService;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.http.HttpEntity;
|
|
|
+import org.springframework.http.HttpHeaders;
|
|
|
+import org.springframework.http.MediaType;
|
|
|
+import org.springframework.http.ResponseEntity;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
|
|
|
import java.text.ParseException;
|
|
|
+import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
|
|
|
/**
|
|
|
* <p>
|
|
|
- * 前端控制器
|
|
|
+ * 前端控制器
|
|
|
* </p>
|
|
|
*
|
|
|
* @author zn
|
|
@@ -23,6 +33,52 @@ import java.util.Map;
|
|
|
public class StockDetailController {
|
|
|
@Autowired
|
|
|
private StockDetailService stockDetailService;
|
|
|
+ @Autowired
|
|
|
+ private DataSourceService dataSourceService;
|
|
|
+ // 从Spring的容器中获取restTemplate
|
|
|
+ @Autowired
|
|
|
+ private RestTemplate restTemplate;
|
|
|
+
|
|
|
+ //查询
|
|
|
+ @PostMapping("/getCurrentStock")
|
|
|
+ public Result getCurrentStock(@RequestBody Map<String, String> paramsMap) throws ParseException {
|
|
|
+ Integer dsStatu = dataSourceService.getDataSourceStatus(paramsMap);
|
|
|
+ System.out.println("1 dsStatu="+dsStatu);
|
|
|
+ if (dsStatu == null) {
|
|
|
+ dsStatu = 0;
|
|
|
+ }
|
|
|
+ System.out.println("2 dsStatu="+dsStatu);
|
|
|
+ if (dsStatu == 1) {
|
|
|
+ //读取远程数据
|
|
|
+ return getRemoteData();
|
|
|
+ } else {
|
|
|
+ //读取填报数据
|
|
|
+ return stockDetailService.getCurrentStock(paramsMap);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //读取远程数据
|
|
|
+ public Result getRemoteData(){
|
|
|
+ HttpHeaders httpHeaders = new HttpHeaders();
|
|
|
+ MediaType type = MediaType.parseMediaType("application/json;charset=UTF-8");
|
|
|
+ httpHeaders.setContentType(type);
|
|
|
+// MultiValueMap<String, Object> map=new LinkedMultiValueMap<>();
|
|
|
+ HashMap<String, Object> map = new HashMap<>();
|
|
|
+ map.put("farmCode", 330110);
|
|
|
+ HttpEntity<Map<String, Object>> objectHttpEntity = new HttpEntity<>(map, httpHeaders);
|
|
|
+ //读取真实数据
|
|
|
+ String remoteUrl = "http://123.60.134.84:10001/breed/api/v1.0.0/stock/getCurrentStock";
|
|
|
+ ResponseEntity<String> entity = restTemplate.postForEntity(remoteUrl, objectHttpEntity, String.class);
|
|
|
+// System.out.println("状态码:"+entity.getStatusCode());
|
|
|
+// System.out.println("响应体:"+entity.getBody());
|
|
|
+ if(entity.getStatusCode().toString().contains("200")){
|
|
|
+ String body = entity.getBody();
|
|
|
+ System.out.println("body>>>>>>>>>>>>>"+body);
|
|
|
+ return new Result(ResultCode.SUCCESS,JSONArray.parseArray(JSON.parseObject(body).getString("data")));
|
|
|
+ }else{
|
|
|
+ return new Result(ResultCode.FAIL,"调取远程数据出错.");
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
//查询
|
|
|
@PostMapping("/getStock")
|