Skip to content
Snippets Groups Projects
Commit b17326e1 authored by Josh's avatar Josh
Browse files

update goodsService, four params

parent 2b232e14
No related branches found
No related tags found
1 merge request!7update goodsService, four params
......@@ -18,14 +18,6 @@ public class CategoryController {
@Autowired
private CategoryService categoryService;
// /**
// * @author Josh Sun
// * @return JSON
// */
// @GetMapping("/goods/categoryTree")
// public JSONObject getCategoryTree(){
// return new CategoryService().getCategoryTree();
// }
@GetMapping("/goods/categoryTree")
public JSONObject getCategoryTree(){
......
......@@ -4,15 +4,16 @@ package com.example.freshonline.controller;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONArray;
import com.example.freshonline.constants.Constants;
import com.example.freshonline.enums.respVerifyRule.VerifyRule;
import com.example.freshonline.model.StockedGoods;
import com.example.freshonline.service.GoodsService;
import com.example.freshonline.utils.RespBuilder;
import com.example.freshonline.utils.ValidationChecker;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
/**
* @author Josh Sun
......@@ -46,8 +47,7 @@ public class GoodsController {
if (price_low_req != null) param.put("price_low", vc.str2int(price_low_req, 0));
if (price_high_req != null) param.put("price_high", vc.str2int(price_high_req, 10000));
/**
* brands: ('brand1', 'brand2')
* @TODO: 如何前端检验,是否需要后端检验
* brands:brand1, brand2 逗号分隔
*/
if (brands != null) param.put("brands", brands);
if (keyword != null) param.put("keyword", keyword);
......@@ -59,20 +59,16 @@ public class GoodsController {
}
GoodsService gs = new GoodsService();
List<StockedGoods> output_by_service = gs.getSearch(param);
JSONObject output = gs.getSearch(param);
JSONObject output = new JSONObject();
String s = JSONObject.toJSONString(output_by_service);
System.out.println(s);
output.put("Result", s);
return output;
return RespBuilder.create(output, VerifyRule.NOT_NULL, Constants.OPERATE_SUCCESS, Constants.OPERATE_FAIL);
}
/**
* @author Josh Sun
*/
@GetMapping("*")
public void defaultMappingTest(){
System.out.println("default mapping");
}
// /**
// * @author Josh Sun
// */
// @GetMapping("*")
// public void defaultMappingTest(){
// System.out.println("default mapping");
// }
}
......@@ -24,18 +24,6 @@ public class CategoryService {
@Autowired
private CategoryMapper categoryMapper;
// /**
// * @author Josh Sun
// * @return JSON
// */
// public JSONObject getCategoryTree(){
// JSONObject output = new JSONObject();
//
//
// return output;
// }
/**
*
* @return
......
package com.example.freshonline.service;
import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.example.freshonline.model.StockedGoods;
import java.io.IOException;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Properties;
import java.util.*;
import java.sql.ResultSet;
import java.util.List;
public class GoodsService {
final BigDecimal MIN_PRICE = BigDecimal.valueOf(0), MAX_PRICE = BigDecimal.valueOf(10000);
/**
* @author Josh Sun
* @param param
* @return
* goods_list: List<StockedGoods> goods_list
* brand_list: new ArrayList<>(Set<String> brand_set)
* price_range: List<BigDecimal> price_range: Arrays.asList(minInit: MAX_PRICE, maxInit: MIN_PRICE)
* goods_total: Integer goods_total
*/
public List<StockedGoods> getSearch(JSONObject param){
public JSONObject getSearch(JSONObject param){
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
......@@ -46,7 +52,11 @@ public class GoodsService {
Integer price_high = param.getInteger("price_high");
Integer category_id = param.getInteger("category_id");
String keyword = param.getString("keyword");
String brands = param.getString("brands");
String brands;
try{
String[] brands_list = param.getString("brands").split(",");
brands = "('" + String.join("','", brands_list) + "')";
} catch (NullPointerException e) {brands = null;}
/**
* brands: ('brand1', 'brand2')
*/
......@@ -54,9 +64,9 @@ public class GoodsService {
Integer page = param.getInteger("page");
String sql_condition = " where true";
if (price_low != null) sql_condition += " and price > " + price_low.toString();
if (price_high != null) sql_condition += " and price < " + price_high.toString();
if (category_id != null) sql_condition += " and category_id = " + category_id.toString();
if (price_low != null) sql_condition += " and price > " + price_low;
if (price_high != null) sql_condition += " and price < " + price_high;
if (category_id != null) sql_condition += " and category_id = " + category_id;
if (keyword != null) sql_condition += " and name like '%" + keyword + "%'";
if (brands != null) sql_condition += " and brand in " + brands;
if (sort_type != null){
......@@ -83,17 +93,18 @@ public class GoodsService {
else sql_condition += "desc ";
}
}
if (page != null){
Integer numPerRow = 5, rowsPerPage = 5;
sql_condition += "\n limit " + ((page-1)*numPerRow*rowsPerPage) + "," + page*numPerRow*rowsPerPage;
}
String sql = sql_base + sql_condition + ";";
System.out.println("sql = " + sql);
String sql_page;
Integer numPerRow = 5, rowsPerPage = 5;
Integer item_low = (page-1) * numPerRow * rowsPerPage, item_high = page * numPerRow * rowsPerPage;
sql_page = "\n limit " + item_low + "," + item_high;
String sql_with_page = sql_base + sql_condition + sql_page + ";";
System.out.println("sql_with_page = " + sql_with_page);
List<StockedGoods> output = new ArrayList<>();
List<StockedGoods> goods_list = new ArrayList<>();
try {
PreparedStatement ps = conn.prepareStatement(sql);
PreparedStatement ps = conn.prepareStatement(sql_with_page);
ResultSet result = ps.executeQuery();
while (result.next()) {
StockedGoods tmp = new StockedGoods();
......@@ -109,11 +120,26 @@ public class GoodsService {
tmp.setCategoryId(result.getInt("category_id"));
tmp.setIsNew(result.getByte("is_new"));
tmp.setPic(result.getString("pic"));
/**
* sale_price, rate, rate_count not included
*/
output.add(tmp);
System.out.println("id=" + tmp.getId() + " name=" + tmp.getName());
tmp.setSalePrice(result.getBigDecimal("sale_price"));
tmp.setRate(result.getBigDecimal("rate"));
tmp.setRateCount(result.getInt("rate_count"));
goods_list.add(tmp);
}
result.close();
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
String sql_base_raw = "select count(*) from stocked_goods";
String sql_raw = sql_base_raw + sql_condition + ";";
System.out.println("sql_raw = " + sql_raw);
Integer goods_total = 0;
try {
PreparedStatement ps = conn.prepareStatement(sql_raw);
ResultSet result = ps.executeQuery();
while (result.next()) {
goods_total = result.getInt(1);
}
result.close();
ps.close();
......@@ -122,6 +148,32 @@ public class GoodsService {
e.printStackTrace();
}
Set<String> brand_set = new HashSet<>();
List<BigDecimal> price_range = new ArrayList<>(Arrays.asList(MAX_PRICE, MIN_PRICE));
for (int i = 0; i < goods_list.size(); i ++) {
StockedGoods tmp = goods_list.get(i);
brand_set.add(tmp.getBrand());
if (tmp.getOnsale() != 0){
price_range.set(0, price_range.get(0).min(tmp.getSalePrice()));
price_range.set(1, price_range.get(1).max(tmp.getSalePrice()));
}
else{
price_range.set(0, price_range.get(0).min(tmp.getPrice()));
price_range.set(1, price_range.get(1).max(tmp.getPrice()));
}
}
System.out.println(price_range);
System.out.println(brand_set);
System.out.println(goods_total);
JSONObject output = new JSONObject();
output.put("goods_list", JSONObject.toJSONString(goods_list));
output.put("price_range", JSONObject.toJSONString(price_range));
output.put("brand_list", JSONObject.toJSONString(new ArrayList<>(brand_set)));
output.put("goods_total", goods_total);
return output;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment