import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dhcc.tms.webService.stockOutInfo.dto.TokenRes;
import com.dhcc.tms.webService.stockOutInfo.dto.GetStockOutlistReqConfig;
import com.dhcc.tms.webService.stockOutInfo.dto.GetStockOutlistReqParams;
import com.dhcc.tms.webService.stockOutInfo.dto.StockOut;
import com.dhcc.tms.webService.stockOutInfo.dto.TokenReqConfig2;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;
@Slf4j
public class GetStockOutlist {
private TokenReqConfig2 tokenReq;
private GetStockOutlistReqConfig getStockOutlistReq;
public TokenRes getToken() throws Exception {
Map<String, Object> params = new HashMap<String, Object>() {
{
put("grant_type", tokenReq.getGrantType());
put("scope", tokenReq.getScope());
put("username", tokenReq.getUsername());
put("password", tokenReq.getPassword());
}
};
HttpPost httpPost = new HttpPost(tokenReq.getUrl());
httpPost.addHeader("Content-Type", tokenReq.getContentType());
httpPost.addHeader("Authorization", tokenReq.getAuthorization());
httpPost.addHeader("Tenant-Id", tokenReq.getTenantId());
if (null != params && params.size() > 0) {
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
Set<Map.Entry<String, Object>> entrySet = params.entrySet();
Iterator<Map.Entry<String, Object>> iterator = entrySet.iterator();
while (iterator.hasNext()) {
Map.Entry<String, Object> mapEntry = iterator.next();
nvps.add(new BasicNameValuePair(mapEntry.getKey(), mapEntry.getValue().toString()));
}
httpPost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
}
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(35000)
.setConnectionRequestTimeout(35000)
.setSocketTimeout(60000)
.build();
httpPost.setConfig(requestConfig);
String result = "";
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse httpResponse = null;
try {
httpResponse = httpClient.execute(httpPost);
HttpEntity entity = httpResponse.getEntity();
result = EntityUtils.toString(entity);
} finally {
if (null != httpResponse) {
httpResponse.close();
}
if (null != httpClient) {
httpClient.close();
}
}
org.springframework.boot.configurationprocessor.json.JSONObject jsonObj = new org.springframework.boot.configurationprocessor.json.JSONObject(result);
if (StringUtils.isBlank(jsonObj.getString("token_type"))) {
throw new Exception("token获取失败");
}
TokenRes res = new TokenRes();
res.setToken_type(jsonObj.getString("token_type"));
res.setAccess_token(jsonObj.getString("access_token"));
res.setUser_name(jsonObj.getString("user_name"));
res.setReal_name(jsonObj.getString("real_name"));
return res;
}
public List<StockOut> getStockOutlist(GetStockOutlistReqParams reqParams, TokenRes tokenRes) throws Exception {
if (StringUtils.isBlank(reqParams.getFactoryCode())
|| StringUtils.isBlank(reqParams.getInventoryCode())
|| StringUtils.isBlank(reqParams.getCreateTimeStart())
|| StringUtils.isBlank(reqParams.getCreateTimeEnd())) {
throw new RuntimeException("缺失必填参数,请检查:工厂编码、库存地点编码、开始时间、结束时间!");
}
URIBuilder uriBuilder = new URIBuilder(getStockOutlistReq.getUrl());
uriBuilder.addParameter("factoryCode", reqParams.getFactoryCode());
uriBuilder.addParameter("inventoryCode", reqParams.getInventoryCode());
uriBuilder.addParameter("createTimeStart", reqParams.getCreateTimeStart());
uriBuilder.addParameter("createTimeEnd", reqParams.getCreateTimeEnd());
HttpGet httpGet = new HttpGet(uriBuilder.build());
httpGet.addHeader("Authorization", getStockOutlistReq.getAuthorization());
httpGet.addHeader("Blade-Auth", tokenRes.getToken_type() + " " + tokenRes.getAccess_token());
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(35000)
.setConnectionRequestTimeout(35000)
.setSocketTimeout(60000)
.build();
httpGet.setConfig(requestConfig);
String result = "";
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse httpResponse = null;
try {
httpResponse = httpClient.execute(httpGet);
HttpEntity entity = httpResponse.getEntity();
result = EntityUtils.toString(entity, "UTF-8");
} finally {
if (null != httpResponse) {
httpResponse.close();
}
if (null != httpClient) {
httpClient.close();
}
}
JSONObject json = JSON.parseObject(result);
if (json.getIntValue("code") != 200) {
throw new RuntimeException("外向交货单获取失败--->" + json.getString("msg"));
}
JSONArray jsonArray = json.getJSONArray("data");
List<StockOut> resData = JSONObject.parseArray(jsonArray.toJSONString(), StockOut.class);
return resData;
}
public static void main(String[] args) throws Exception {
GetStockOutlist demo = new GetStockOutlist();
demo.tokenReq = new TokenReqConfig2(
"POST",
"http://xxx.com/blade-auth/oauth/token",
"application/x-www-form-urlencoded",
"Basic c2FiZXI6c2FiZXJfc2VjcmV0",
"000000",
"password",
"all",
"20139403",
"e10adc3949ba59abbe56e057f20f883e"
);
TokenRes tokenRes = demo.getToken();
log.info("=====getToken 结束=====");
demo.getStockOutlistReq = new GetStockOutlistReqConfig(
"GET",
"http://xxx.com/blade-warehouse/stockOutInfo/getStockOutlist",
"multipart/form-data",
"Basic c2FiZXI6c2FiZXJfc2VjcmV0"
);
List<StockOut> stockOutlist = demo.getStockOutlist(
new GetStockOutlistReqParams(
"S110", "6000",
"2021-04-01", "2021-06-01"
),
tokenRes
);
log.info("=====getStockOutlist 结束=====");
}
}
@Data
@NoArgsConstructor
@AllArgsConstructor
public class TokenReqConfig2 {
private String method;
private String url;
private String contentType;
private String authorization;
private String tenantId;
private String grantType;
private String scope;
private String username;
private String password;
}
@Data
@NoArgsConstructor
@AllArgsConstructor
public class TokenRes{
private String token_type;
private String access_token;
private String user_name;
private String real_name;
}
@Data
@NoArgsConstructor
@AllArgsConstructor
public class GetStockOutlistReqConfig {
private String method;
private String url;
private String contentType;
private String authorization;
}
@Data
@NoArgsConstructor
@AllArgsConstructor
public class GetStockOutlistReqParams {
private String factoryCode;
private String inventoryCode;
private String createTimeStart;
private String createTimeEnd;
}```
```java
@Data
@NoArgsConstructor
@AllArgsConstructor
public class StockOut {
@ColumnWidth(15)
@ExcelProperty("工厂编码")
private String factoryCode;
@ColumnWidth(15)
@ExcelProperty("工厂名称")
private String factoryName;
@ColumnWidth(20)
@ExcelProperty("库存地点编码")
private String inventoryCode;
@ColumnWidth(20)
@ExcelProperty("库存地点名称")
private String inventoryName;
@ColumnWidth(20)
@ExcelProperty("销售订单号")
private String stockOrderNo;
@ColumnWidth(20)
@ExcelProperty("销售出库单号")
private String stockOutNo;
@ColumnWidth(20)
@ExcelProperty("销售组编码")
private String salesGroupCode;
@ColumnWidth(20)
@ExcelProperty("销售组名称")
private String salesGroupName;
@ColumnWidth(20)
@ExcelProperty("客户编码")
private String customerCode;
@ColumnWidth(40)
@ExcelProperty("客户名称")
private String customerName;
@ExcelIgnore
private List<StockOutDetail> detailVOList;
}
@Data
@NoArgsConstructor
@AllArgsConstructor
public class StockOutDetail {
private String materialCode;
private String materialName;
private BigDecimal quantity;
private BigDecimal stockNum;
}