This commit is contained in:
2024-09-20 16:10:36 +08:00
parent 7d5ebbfb65
commit d8ed96dff7

View File

@@ -1,25 +1,31 @@
package cn.ysk.cashier.cons.rest;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.ysk.cashier.annotation.Log;
import cn.ysk.cashier.cons.domain.ViewHandover;
import cn.ysk.cashier.cons.service.ViewHandoverService;
import cn.ysk.cashier.cons.service.dto.ViewHandoverQueryCriteria;
import cn.ysk.cashier.exception.BadRequestException;
import cn.ysk.cashier.utils.JSONUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.http.client.methods.CloseableHttpResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import java.util.Objects;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;
/**
* @author admin
@@ -40,18 +46,25 @@ public class ViewHandoverController {
@PostMapping("handoverData")
@Log("提交交班")
@ApiOperation("提交交班")
public ResponseEntity<Object> handoverData(@RequestBody ViewHandover resources){
if(Objects.isNull(resources.getShopId())) throw new BadRequestException("店铺信息不可为空");
HttpResponse response = HttpUtil.createGet(StrUtil.format(wxServiceHostname,"/cashierService/common/handoverData?shopId={}", resources.getShopId())).execute();
if (response.getStatus() >= 200 && response.getStatus() < 300) {
String body = response.body();
return new ResponseEntity<>(HttpStatus.OK);
} else {
// 请求失败,获取状态码和错误信息
int statusCode = response.getStatus();
String errorMessage = response.body(); // 可以获取错误信息
log.error("交班提交失败:{}",errorMessage);
throw new BadRequestException("请求失败,状态码:"+statusCode);
public ResponseEntity<Object> handoverData(@RequestBody Map<String, Object> map) throws Exception {
if (CollectionUtils.isEmpty(map) || !map.containsKey("shopId") || !map.containsKey("isprintProduct")) {
throw new BadRequestException("参数缺失");
}
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost post = new HttpPost(wxServiceHostname + "/cashierService/common/handoverData");
String jsonString = JSONUtil.toJSONString(map);
StringEntity entity = new StringEntity(jsonString);
post.setEntity(entity);
post.setHeader("Content-Type", "application/json");
try (CloseableHttpResponse response = httpClient.execute(post)) {
if (response.getStatusLine().getStatusCode() == 200) {
return new ResponseEntity<>(HttpStatus.OK);
} else {
return new ResponseEntity<>(HttpStatus.valueOf(response.getStatusLine().getStatusCode()));
}
} finally {
httpClient.close();
}
}