提交 6caf5af9 authored 作者: 吴德鹏's avatar 吴德鹏

Merge branch 'function-1.01' into dev

......@@ -72,6 +72,8 @@ public class CartController extends Controller {
if (itemCount <= 0) {
record.setCheckFlag(0);
itemCount = 0;
if (itemCount != 0)
record.setItemCount(itemCount);
}
result.setData(itemCount);
result.setMessage("Out of stock");
......
server.servlet.context-path=/zion
spring.jpa.hibernate.ddl-auto=update
server.port=8083
spring.profiles.active=dev
spring.profiles.active=test
#spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/afrishop_test?useUnicode=true&connectionCollation=utf8mb4_general_ci&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
#spring.datasource.username=root
#spring.datasource.password=Diaoyunnuli.8
......
package com.example.afrishop_v3;
import com.example.afrishop_v3.models.*;
import com.example.afrishop_v3.repository.*;
import com.example.afrishop_v3.util.IdUtil;
import com.example.afrishop_v3.util.OssUtil;
import com.example.afrishop_v3.util.PicUtils;
import com.google.gson.JsonObject;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.junit.Test;
import org.junit.runner.Result;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.client.RestTemplate;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Collectors;
@RunWith(SpringRunner.class)
@SpringBootTest
public class AfrishopV3ApplicationTests {
RestTemplate restTemplate = new RestTemplate();
@Test
public void contextLoads() {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String now = format.format(new Date());
System.out.println(now);
}
private String url = "http://localhost:8099/afrishop/discover/bonus/saveNetworkMarketing";
@Autowired
private TbCfOrderRepository orderRepository;
// post35789572fbf643cdab55901d1dc511a6
// userInfo00297bf300ed45e092a5182dececad21
// productSharervv32GmG4q
// amount0.9
// orderId008272b1fb994aef9b199449f46d5aa5
@Test
public void test1() {
Optional<TbCfOrder> byId = orderRepository.findById("12a9d7d1042348278c97980a00fc67c4");
JSONObject json = new JSONObject();
json.put("userInfo", "32dcda5a709c4265a6f01686ae9b1c1c");
json.put("amount", byId.get().getItemsPrice());
json.put("orderId", "12a9d7d1042348278c97980a00fc67c4");
System.out.println(json);
ResponseEntity<Result> resultResponseEntity = restTemplate.postForEntity(url, json, Result.class);
System.err.println(resultResponseEntity);
}
@Autowired
private TokenRepository tokenRepository;
@Autowired
private CountryRepository countryRepository;
@Test
public void test2() {
List list = new ArrayList();
list.add(2);
list.add(1);
list.add(3);
list.forEach(System.out::println);
}
private ReentrantLock reentrantLock = new ReentrantLock();
@Test
public void testImport() {
ExecutorService executorService = Executors.newCachedThreadPool();
for (int i = 0; i < 20; i++) {
executorService.execute(() -> importCountry());
}
}
@Test
public void importCountry() {
String url = "https://api.jisuapi.com/country/query?name=&continent=非洲&language=&iscountry=&appkey=732afb9ddae9eef8";
ResponseEntity<String> forEntity = restTemplate.getForEntity(url, String.class);
String body = forEntity.getBody();
System.err.println(body);
JSONObject jsonObject = JSONObject.fromObject(body);
String status = jsonObject.getString("status");
if ("0".equals(status)) {
JSONArray jsonArray = jsonObject.getJSONArray("result");
for (int i = 0; i < jsonArray.size(); i++) {
Country country = new Country();
JSONObject object = jsonArray.getJSONObject(i);
String ename = object.getString("ename");
String cname = object.getString("cname");
String areaCode = object.getString("areacode");
String nationalFlag = object.getString("nationalflag");
country.setAreaCode(areaCode);
country.setCountryCname(cname);
country.setCountryName(ename);
country.setNationalFlag(nationalFlag);
country.setId(IdUtil.createIdbyUUID());
countryRepository.save(country);
}
}
}
private volatile int count = 1;
@Autowired
private TbCfStationItemRepository tbCfStationItemRepository;
@Test
public void testImg() {
List<TbCfStationItem> list = tbCfStationItemRepository.queryItemsAll();
try {
for (TbCfStationItem item : list) {
System.out.println("压缩数量:" + count++);
System.err.println("商品ID:" + item.getItemId());
String itemImg = item.getItemImg();
String[] urlArr = itemImg.split(";");
List<String> strList = new ArrayList<>();
for (String url : urlArr) {
InputStream stream = getImageStream(url);
if (stream == null) {
continue;
}
byte[] bytes = new byte[0];
try {
bytes = toByteArray(stream);
byte[] by = PicUtils.compressPicForScale(bytes, 70);
long l = System.currentTimeMillis();
String urlName = UUID.randomUUID() + "-" + String.valueOf(l).substring(6) + ".jpg";
String compressedUrl = OssUtil.upload(by, urlName, "afrishop_new");
strList.add(compressedUrl);
} catch (Exception e) {
e.printStackTrace();
}
}
tbCfStationItemRepository.updateItmeImg(strList.stream().collect(Collectors.joining(";")), item.getItemId());
}
} catch (Exception e) {
} finally {
}
}
/**
* 通过URL获取网络图片
* 获取网络图片流
*
* @param url 传入的 URL 必须是以 http:// 开头的,因为我们使用了 HttpURLConnection
* @return 输入流
*/
public static InputStream getImageStream(String url) {
try {
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.setReadTimeout(10000);
connection.setConnectTimeout(10000);
connection.setRequestMethod("GET");
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
return connection.getInputStream();
}
} catch (IOException e) {
System.out.println("获取网络图片出现异常,图片路径为:" + url);
e.printStackTrace();
}
return null;
}
/**
* 将 InputStream 流转换为 byte[]
*
* @param input InputStream
* @return byte[]
* @throws IOException IO流异常
*/
public static byte[] toByteArray(InputStream input) throws IOException {
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[1024 * 4];
int n = 0;
while (-1 != (n = input.read(buffer))) {
output.write(buffer, 0, n);
}
return output.toByteArray();
}
@Test
public void test001(){
System.out.println("test001");
}
}
//package com.example.afrishop_v3;
//
//import com.example.afrishop_v3.models.*;
//import com.example.afrishop_v3.repository.*;
//import com.example.afrishop_v3.util.IdUtil;
//import com.example.afrishop_v3.util.OssUtil;
//import com.example.afrishop_v3.util.PicUtils;
//import com.google.gson.JsonObject;
//import net.sf.json.JSONArray;
//import net.sf.json.JSONObject;
//import org.junit.Test;
//import org.junit.runner.Result;
//import org.junit.runner.RunWith;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.boot.test.context.SpringBootTest;
//import org.springframework.http.ResponseEntity;
//import org.springframework.test.context.junit4.SpringRunner;
//import org.springframework.web.client.RestTemplate;
//
//import java.io.ByteArrayOutputStream;
//import java.io.IOException;
//import java.io.InputStream;
//import java.net.HttpURLConnection;
//import java.net.URL;
//import java.text.SimpleDateFormat;
//import java.time.LocalDateTime;
//import java.util.*;
//import java.util.concurrent.ExecutorService;
//import java.util.concurrent.Executors;
//import java.util.concurrent.Future;
//import java.util.concurrent.locks.ReentrantLock;
//import java.util.stream.Collectors;
//
//@RunWith(SpringRunner.class)
//@SpringBootTest
//public class AfrishopV3ApplicationTests {
//
// RestTemplate restTemplate = new RestTemplate();
//
// @Test
// public void contextLoads() {
// SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
// String now = format.format(new Date());
// System.out.println(now);
// }
//
// private String url = "http://localhost:8099/afrishop/discover/bonus/saveNetworkMarketing";
// @Autowired
// private TbCfOrderRepository orderRepository;
//
//// post35789572fbf643cdab55901d1dc511a6
//// userInfo00297bf300ed45e092a5182dececad21
//// productSharervv32GmG4q
//// amount0.9
//// orderId008272b1fb994aef9b199449f46d5aa5
//
// @Test
// public void test1() {
//
// Optional<TbCfOrder> byId = orderRepository.findById("12a9d7d1042348278c97980a00fc67c4");
// JSONObject json = new JSONObject();
// json.put("userInfo", "32dcda5a709c4265a6f01686ae9b1c1c");
// json.put("amount", byId.get().getItemsPrice());
// json.put("orderId", "12a9d7d1042348278c97980a00fc67c4");
// System.out.println(json);
// ResponseEntity<Result> resultResponseEntity = restTemplate.postForEntity(url, json, Result.class);
// System.err.println(resultResponseEntity);
// }
//
// @Autowired
// private TokenRepository tokenRepository;
//
// @Autowired
// private CountryRepository countryRepository;
//
//
// @Test
// public void test2() {
// List list = new ArrayList();
// list.add(2);
// list.add(1);
// list.add(3);
// list.forEach(System.out::println);
//
// }
//
// private ReentrantLock reentrantLock = new ReentrantLock();
//
// @Test
// public void testImport() {
// ExecutorService executorService = Executors.newCachedThreadPool();
//
// for (int i = 0; i < 20; i++) {
// executorService.execute(() -> importCountry());
// }
//
// }
//
// @Test
// public void importCountry() {
//
// String url = "https://api.jisuapi.com/country/query?name=&continent=非洲&language=&iscountry=&appkey=732afb9ddae9eef8";
// ResponseEntity<String> forEntity = restTemplate.getForEntity(url, String.class);
// String body = forEntity.getBody();
// System.err.println(body);
// JSONObject jsonObject = JSONObject.fromObject(body);
// String status = jsonObject.getString("status");
// if ("0".equals(status)) {
// JSONArray jsonArray = jsonObject.getJSONArray("result");
// for (int i = 0; i < jsonArray.size(); i++) {
// Country country = new Country();
// JSONObject object = jsonArray.getJSONObject(i);
// String ename = object.getString("ename");
// String cname = object.getString("cname");
// String areaCode = object.getString("areacode");
// String nationalFlag = object.getString("nationalflag");
// country.setAreaCode(areaCode);
// country.setCountryCname(cname);
// country.setCountryName(ename);
// country.setNationalFlag(nationalFlag);
// country.setId(IdUtil.createIdbyUUID());
// countryRepository.save(country);
//
// }
// }
//
//
// }
//
// private volatile int count = 1;
// @Autowired
// private TbCfStationItemRepository tbCfStationItemRepository;
//
// @Test
// public void testImg() {
// List<TbCfStationItem> list = tbCfStationItemRepository.queryItemsAll();
// try {
//
// for (TbCfStationItem item : list) {
// System.out.println("压缩数量:" + count++);
// System.err.println("商品ID:" + item.getItemId());
//
// String itemImg = item.getItemImg();
// String[] urlArr = itemImg.split(";");
// List<String> strList = new ArrayList<>();
// for (String url : urlArr) {
//
// InputStream stream = getImageStream(url);
// if (stream == null) {
// continue;
// }
// byte[] bytes = new byte[0];
// try {
// bytes = toByteArray(stream);
// byte[] by = PicUtils.compressPicForScale(bytes, 70);
// long l = System.currentTimeMillis();
// String urlName = UUID.randomUUID() + "-" + String.valueOf(l).substring(6) + ".jpg";
//
// String compressedUrl = OssUtil.upload(by, urlName, "afrishop_new");
// strList.add(compressedUrl);
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
// tbCfStationItemRepository.updateItmeImg(strList.stream().collect(Collectors.joining(";")), item.getItemId());
// }
//
// } catch (Exception e) {
//
// } finally {
//
//
// }
// }
//
// /**
// * 通过URL获取网络图片
// * 获取网络图片流
// *
// * @param url 传入的 URL 必须是以 http:// 开头的,因为我们使用了 HttpURLConnection
// * @return 输入流
// */
// public static InputStream getImageStream(String url) {
// try {
// HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
// connection.setReadTimeout(10000);
// connection.setConnectTimeout(10000);
// connection.setRequestMethod("GET");
// if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
// return connection.getInputStream();
// }
// } catch (IOException e) {
// System.out.println("获取网络图片出现异常,图片路径为:" + url);
// e.printStackTrace();
// }
// return null;
// }
//
// /**
// * 将 InputStream 流转换为 byte[]
// *
// * @param input InputStream
// * @return byte[]
// * @throws IOException IO流异常
// */
// public static byte[] toByteArray(InputStream input) throws IOException {
// ByteArrayOutputStream output = new ByteArrayOutputStream();
// byte[] buffer = new byte[1024 * 4];
// int n = 0;
// while (-1 != (n = input.read(buffer))) {
// output.write(buffer, 0, n);
// }
// return output.toByteArray();
// }
//
// @Test
// public void test001(){
// System.out.println("test001");
// }
//
//
//}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论