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

add goodsservice, searchgoods interface

parent e6e961ff
No related branches found
No related tags found
No related merge requests found
package com.example.freshonline.controller;
import com.alibaba.fastjson.JSONObject;
import com.example.freshonline.service.CategoryService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class CategoryController {
/**
* @author Josh Sun
* @return JSON
*/
@GetMapping("/goods")
public JSONObject getCategoryTree(){
return new CategoryService().getCategoryTree();
}
}
package com.example.freshonline.controller; package com.example.freshonline.controller;
import com.example.freshonline.utils.ValidationChecker; import com.example.freshonline.utils.ValidationChecker;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
...@@ -26,32 +28,32 @@ public class GoodsController { ...@@ -26,32 +28,32 @@ public class GoodsController {
@GetMapping("/goods") @GetMapping("/goods")
public String getSearchTest(@RequestParam(value = "price_low", required = false) String price_low_req, public String getSearchTest(@RequestParam(value = "price_low", required = false) String price_low_req,
@RequestParam(value = "price_high", required = false) String price_high_req, @RequestParam(value = "price_high", required = false) String price_high_req,
@RequestParam(value = "brands", required = false) String brands, @RequestParam(value = "brands_id", required = false) String brands,
@RequestParam(value = "sort_type", required = false) String sort_type_req, @RequestParam(value = "sort_type", required = false) String sort_type_req,
@RequestParam(value = "keyword", required = false) String keyword, @RequestParam(value = "keyword", required = false) String keyword,
@RequestParam(value = "page", required = false) String page_req, @RequestParam(value = "page", required = false) String page_req,
@RequestParam(value = "category_id", required = false) String category_id_req){ @RequestParam(value = "category_id", required = false) String category_id_req){
StringBuffer test = new StringBuffer(); StringBuilder test = new StringBuilder();
ValidationChecker vc = new ValidationChecker(); ValidationChecker vc = new ValidationChecker();
Integer MIN_PRICE = 0, MAX_PRICE = 10000; Integer MIN_PRICE = 0, MAX_PRICE = 10000;
Integer price_low = vc.str2int(price_low_req, MIN_PRICE); Integer price_low = vc.str2int(price_low_req, MIN_PRICE);
Integer price_high = vc.str2int(price_high_req, MAX_PRICE); Integer price_high = vc.str2int(price_high_req, MAX_PRICE);
test.append("price_low=" + price_low); test.append("price_low=").append(price_low);
test.append(",price_high=" + price_high); test.append(",price_high=").append(price_high);
test.append(",brands=" + brands); test.append(",brands=").append(brands);
Integer sort_type = vc.str2int(sort_type_req, 0); Integer sort_type = vc.str2int(sort_type_req, 0);
test.append(",sort_type=" + sort_type); test.append(",sort_type=").append(sort_type);
test.append(",keyword=" + keyword); test.append(",keyword=").append(keyword);
Integer page = vc.str2int(page_req, 1); Integer page = vc.str2int(page_req, 1);
test.append(",page=" + page); test.append(",page=").append(page);
Integer category_id = vc.str2int(category_id_req, 0); Integer category_id = vc.str2int(category_id_req, 0);
test.append(",category_id=" + category_id); test.append(",category_id=").append(category_id);
return test.toString(); return test.toString();
} }
......
package com.example.freshonline.service;
import com.alibaba.fastjson.JSONObject;
/**
* @author Josh Sun
*/
public class CategoryService {
/**
* @author Josh Sun
* @return JSON
*/
public JSONObject getCategoryTree(){
JSONObject output = new JSONObject();
return output;
}
}
package com.example.freshonline.service; package com.example.freshonline.service;
/** import com.alibaba.fastjson.JSONObject;
* @author Josh Sun import com.example.freshonline.model.StockedGoods;
*/
import java.io.IOException;
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.sql.ResultSet;
import java.util.List;
public class GoodsService { public class GoodsService {
/**
* @author Josh Sun
* @param param
* @return
*/
public List<StockedGoods> getSearch(JSONObject param){
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Properties pros;
Connection conn = null;
try {
pros = new Properties();
pros.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("application.properties"));
Class.forName(pros.getProperty("spring.datasource.driver-class-name"));
conn = DriverManager.getConnection(pros.getProperty("spring.datasource.url"), pros.getProperty("spring.datasource.username"), pros.getProperty("spring.datasource.password"));
}
catch (Exception e){
e.printStackTrace();
}
List<StockedGoods> output = new ArrayList<>();
String sql_base = "select * from stocked_goods";
String sql_condition = "where true";
Integer price_low = param.getInteger("price_low");
if (price_low != null) {
sql_condition += " and price > " + price_low.toString();
}
Integer price_high = param.getInteger("price_high");
if (price_high != null) {
sql_condition += " and price < " + price_high.toString();
}
String brands = param.getString("brands").toString();
if (brands != null){
sql_condition += " and brands like %" + brands + "%";
}
Integer sort_type = param.getInteger("sort_type");
if (sort_type != null){
/**
* @TODO sort_type
*/
}
Integer page = param.getInteger("page");
if (page != null){
/**
* @TODO 分页
*/
}
Integer category_id = param.getInteger("category_id");
if (category_id != null){
sql_condition += " and category_id = " + category_id.toString();
}
String sql = sql_base + sql_condition;
try {
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet result = ps.executeQuery();
while (result.next()) {
StockedGoods tmp = new StockedGoods();
tmp.setId(result.getInt("id"));
tmp.setName(result.getString("name"));
/**
* @TODO Type is a String? or TinyInt?
*/
tmp.setType(result.getString("type"));
tmp.setPrice(result.getBigDecimal("price"));
tmp.setStorage(result.getBigDecimal("storage"));
tmp.setSales(result.getBigDecimal("sales"));
tmp.setDescription(result.getString("description"));
/**
* Byte vs TinyInt
*/
tmp.setOnsale(result.getByte("onsale"));
tmp.setBrand(result.getString("brand"));
tmp.setCategoryId(result.getInt("category_id"));
/**
* Byte vs TinyInt
*/
tmp.setIsNew(result.getByte("is_new"));
tmp.setPic(result.getString("pic"));
/**
* sale_price, rate, rate_count not included
*/
output.add(tmp);
}
result.close();
ps.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
return output;
}
} }
...@@ -4,5 +4,5 @@ ...@@ -4,5 +4,5 @@
spring.datasource.url=jdbc:mysql://localhost:3306/FreshOnline?useUnicode=true&characterEncoding=utf8&allowPublicKeyRetrieval=true&useSSL=false spring.datasource.url=jdbc:mysql://localhost:3306/FreshOnline?useUnicode=true&characterEncoding=utf8&allowPublicKeyRetrieval=true&useSSL=false
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root spring.datasource.username=root
spring.datasource.password= spring.datasource.password=123456
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
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