parent
cb950ec1a9
commit
8bf22a6d9c
@ -0,0 +1,309 @@
|
||||
package com.example.flower.controller;
|
||||
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.example.flower.entity.Flower;
|
||||
import com.example.flower.service.ClassService;
|
||||
import com.example.flower.service.FlowerService;
|
||||
import com.example.flower.unit.JWTUtil;
|
||||
import io.jsonwebtoken.Claims;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import jakarta.annotation.Resource;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/flower")
|
||||
public class FlowerController {
|
||||
@Resource
|
||||
private FlowerService flowerService;
|
||||
@Resource
|
||||
private ClassService classService;
|
||||
|
||||
@PostMapping("/list") //【共用】flowerList获取所有花卉信息列表
|
||||
public JSONObject flowerList(@RequestHeader String Authorization, @RequestBody JSONObject param){
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
|
||||
if(!JWTUtil.checkToken(Authorization)){ //token认证失败
|
||||
JWTUtil.checkTokenFailed(jsonObject);
|
||||
return jsonObject;
|
||||
}
|
||||
|
||||
JSONObject jsonObject1 = new JSONObject();
|
||||
int page=param.getIntValue("page");
|
||||
int page_size=param.getIntValue("page_size");
|
||||
|
||||
List<Flower> flowerList = flowerService.flowerList(page,page_size).getList();
|
||||
|
||||
jsonObject1.put("page_number", flowerService.flowerList(page,page_size).getPages());
|
||||
jsonObject1.put("total", flowerService.flowerList(page,page_size).getTotal());
|
||||
|
||||
int size=flowerList.size();
|
||||
|
||||
JSONObject[] objects = new JSONObject[size];
|
||||
for(int i = 0;i<size;i++){
|
||||
objects[i]=new JSONObject();
|
||||
objects[i].put("flower_id", flowerList.get(i).getFlower_id());
|
||||
objects[i].put("flower_class",classService.classInfo(flowerList.get(i).getFlower_class()).getClass_name());
|
||||
objects[i].put("flower_name", flowerList.get(i).getFlower_name());
|
||||
objects[i].put("flower_price", flowerList.get(i).getFlower_price());
|
||||
objects[i].put("flower_sale", flowerList.get(i).getFlower_sale());
|
||||
objects[i].put("flower_pic", flowerList.get(i).getFlower_pic());
|
||||
}
|
||||
jsonObject1.put("flowers",objects);
|
||||
|
||||
jsonObject.put("code",200 );
|
||||
jsonObject.put("msg","token认证成功!" );
|
||||
jsonObject.put("data",jsonObject1);
|
||||
return jsonObject;
|
||||
}
|
||||
|
||||
@PostMapping("/query") //【共用】flowerListByQuery查询花卉信息列表
|
||||
public JSONObject flowerListByQuery(@RequestHeader String Authorization, @RequestBody JSONObject param){
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
|
||||
if(!JWTUtil.checkToken(Authorization)){ //token认证失败
|
||||
JWTUtil.checkTokenFailed(jsonObject);
|
||||
return jsonObject;
|
||||
}
|
||||
|
||||
JSONObject jsonObject1 = new JSONObject();
|
||||
int page=param.getIntValue("page");
|
||||
int page_size=param.getIntValue("page_size");
|
||||
String flower_name = param.getString("flower_name");
|
||||
|
||||
List<Flower> flowerList = flowerService.flowerListByQuery(page,page_size,flower_name).getList();
|
||||
|
||||
jsonObject1.put("page_number", flowerService.flowerListByQuery(page,page_size,flower_name).getPages());
|
||||
jsonObject1.put("total", flowerService.flowerListByQuery(page,page_size,flower_name).getTotal());
|
||||
|
||||
int size=flowerList.size();
|
||||
|
||||
JSONObject[] objects = new JSONObject[size];
|
||||
for(int i = 0;i<size;i++){
|
||||
objects[i]=new JSONObject();
|
||||
objects[i].put("flower_id", flowerList.get(i).getFlower_id());
|
||||
objects[i].put("flower_class",classService.classInfo(flowerList.get(i).getFlower_class()).getClass_name());
|
||||
objects[i].put("flower_name", flowerList.get(i).getFlower_name());
|
||||
objects[i].put("flower_price", flowerList.get(i).getFlower_price());
|
||||
objects[i].put("flower_sale", flowerList.get(i).getFlower_sale());
|
||||
objects[i].put("flower_pic", flowerList.get(i).getFlower_pic());
|
||||
}
|
||||
jsonObject1.put("flowers",objects);
|
||||
|
||||
jsonObject.put("code",200 );
|
||||
jsonObject.put("msg","token认证成功!" );
|
||||
jsonObject.put("data",jsonObject1);
|
||||
return jsonObject;
|
||||
}
|
||||
|
||||
@PostMapping("/add") //flowerAdd添加花卉
|
||||
public JSONObject flowerAdd(@RequestHeader String Authorization, @RequestBody JSONObject param){
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
|
||||
if(!JWTUtil.checkToken(Authorization)){ //token认证失败
|
||||
JWTUtil.checkTokenFailed(jsonObject);
|
||||
return jsonObject;
|
||||
}
|
||||
|
||||
Flower flower = new Flower();
|
||||
flower.setFlower_name(param.getString("flower_name"));
|
||||
flower.setFlower_class(param.getIntValue("flower_class"));
|
||||
flower.setFlower_price(param.getDoubleValue("flower_price"));
|
||||
flower.setFlower_pic(param.getString("flower_pic"));
|
||||
|
||||
flowerService.flowerAdd(flower);
|
||||
|
||||
jsonObject.put("code",200 );
|
||||
jsonObject.put("msg","成功" );
|
||||
jsonObject.put("flower_id",flower.getFlower_id());
|
||||
|
||||
return jsonObject;
|
||||
}
|
||||
|
||||
@PostMapping("/deletePer") //flowerDeletePer删除单个花卉
|
||||
public JSONObject flowerDeletePer(@RequestHeader String Authorization, @RequestBody JSONObject param){
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
|
||||
if(!JWTUtil.checkToken(Authorization)){ //token认证失败
|
||||
JWTUtil.checkTokenFailed(jsonObject);
|
||||
return jsonObject;
|
||||
}
|
||||
|
||||
int flag = flowerService.flowerDeletePer(param.getIntValue("flower_id"));
|
||||
if(flag==1){
|
||||
jsonObject.put("code",200 );
|
||||
jsonObject.put("msg","成功" );
|
||||
}
|
||||
else {
|
||||
jsonObject.put("code",403 );
|
||||
jsonObject.put("msg","删除失败" );
|
||||
}
|
||||
|
||||
return jsonObject;
|
||||
}
|
||||
|
||||
@PostMapping("/deleteMul") //flowerDeleteMul批量删除花卉
|
||||
public JSONObject flowerDeleteMul(@RequestHeader String Authorization, @RequestBody JSONObject param){
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
|
||||
if(!JWTUtil.checkToken(Authorization)){ //token认证失败
|
||||
JWTUtil.checkTokenFailed(jsonObject);
|
||||
return jsonObject;
|
||||
}
|
||||
|
||||
//获取请求参数
|
||||
JSONArray flower_ids = param.getJSONArray("flower_ids");
|
||||
|
||||
int [] ids = new int[flower_ids.size()];
|
||||
|
||||
//把userid中的数据进行类型转换并存入到int数组中
|
||||
for (int i = 0; i < flower_ids.size(); i++) {
|
||||
ids[i] = Integer.parseInt(flower_ids.getString(i));
|
||||
}
|
||||
|
||||
int result = flowerService.flowerDeleteMul(ids);
|
||||
if(result == ids.length){
|
||||
jsonObject.put("code",200);
|
||||
jsonObject.put("msg","批量删除成功,删除了"+result+"条花卉数据");
|
||||
}else{
|
||||
jsonObject.put("code",403);
|
||||
jsonObject.put("msg","批量删除失败,删除了"+result+"条花卉数据");
|
||||
}
|
||||
return jsonObject;
|
||||
}
|
||||
|
||||
@PostMapping("/info") //【共用】flowerInfo获取花卉的初始信息
|
||||
public JSONObject flowerInfo(@RequestHeader String Authorization, @RequestBody JSONObject param){
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
|
||||
if(!JWTUtil.checkToken(Authorization)){ //token认证失败
|
||||
JWTUtil.checkTokenFailed(jsonObject);
|
||||
return jsonObject;
|
||||
}
|
||||
|
||||
int flower_id = param.getIntValue("flower_id");
|
||||
Flower flower = flowerService.flowerInfo(flower_id);
|
||||
|
||||
if(flower!=null){
|
||||
JSONObject jsonObject1 = new JSONObject();
|
||||
jsonObject1.put("flower_id",flower.getFlower_id());
|
||||
jsonObject1.put("flower_name",flower.getFlower_name());
|
||||
jsonObject1.put("flower_class",classService.classInfo(flower.getFlower_class()).getClass_name());
|
||||
jsonObject1.put("flower_price",flower.getFlower_price());
|
||||
jsonObject1.put("flower_sale",flower.getFlower_sale());
|
||||
jsonObject1.put("flower_pic",flower.getFlower_pic());
|
||||
|
||||
jsonObject.put("code",200);
|
||||
jsonObject.put("msg","成功");
|
||||
jsonObject.put("data",jsonObject1);
|
||||
|
||||
}
|
||||
else{
|
||||
jsonObject.put("code",403);
|
||||
jsonObject.put("msg","获取花卉信息失败");
|
||||
jsonObject.put("data",null);
|
||||
}
|
||||
|
||||
return jsonObject;
|
||||
}
|
||||
|
||||
@PostMapping("/modify") //flowerModify修改花卉信息
|
||||
public JSONObject flowerModify(@RequestHeader String Authorization, @RequestBody JSONObject param){
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
|
||||
if(!JWTUtil.checkToken(Authorization)){ //token认证失败
|
||||
JWTUtil.checkTokenFailed(jsonObject);
|
||||
return jsonObject;
|
||||
}
|
||||
|
||||
Flower flower = new Flower();
|
||||
flower.setFlower_id(param.getIntValue("flower_id"));
|
||||
flower.setFlower_name(param.getString("flower_name"));
|
||||
try{
|
||||
flower.setFlower_class(param.getIntValue("flower_class"));
|
||||
}catch (Exception e){
|
||||
flower.setFlower_class(-1);
|
||||
}
|
||||
flower.setFlower_price(param.getDoubleValue("flower_price"));
|
||||
flower.setFlower_pic(param.getString("flower_pic"));
|
||||
|
||||
int flag = flowerService.flowerModify(flower);
|
||||
if(flag==1){
|
||||
jsonObject.put("code",200 );
|
||||
jsonObject.put("msg","修改成功" );
|
||||
}
|
||||
else{
|
||||
jsonObject.put("code",403 );
|
||||
jsonObject.put("msg","修改失败" );
|
||||
}
|
||||
|
||||
return jsonObject;
|
||||
}
|
||||
|
||||
//前台
|
||||
@PostMapping("/recommend") //flowerRecommend前台花卉专区
|
||||
public JSONObject flowerRecommend(@RequestHeader String Authorization, @RequestBody JSONObject param){
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
|
||||
// if(!JWTUtil.checkToken(Authorization)){ //token认证失败
|
||||
// JWTUtil.checkTokenFailed(jsonObject);
|
||||
// return jsonObject;
|
||||
// }
|
||||
|
||||
JSONObject jsonObject1 = new JSONObject();
|
||||
int class_id = param.getIntValue("class_id");
|
||||
|
||||
List<Flower> flowerList = flowerService.flowerRecommend(class_id);
|
||||
|
||||
int size=flowerList.size();
|
||||
|
||||
JSONObject[] objects = new JSONObject[size];
|
||||
for(int i = 0;i<size;i++){
|
||||
objects[i]=new JSONObject();
|
||||
objects[i].put("flower_id", flowerList.get(i).getFlower_id());
|
||||
objects[i].put("flower_name", flowerList.get(i).getFlower_name());
|
||||
objects[i].put("flower_price", flowerList.get(i).getFlower_price());
|
||||
objects[i].put("flower_sale", flowerList.get(i).getFlower_sale());
|
||||
objects[i].put("flower_pic", flowerList.get(i).getFlower_pic());
|
||||
}
|
||||
jsonObject1.put("flowers",objects);
|
||||
|
||||
jsonObject.put("code",200 );
|
||||
jsonObject.put("msg","token认证成功!" );
|
||||
jsonObject.put("data",jsonObject1);
|
||||
return jsonObject;
|
||||
}
|
||||
|
||||
@PostMapping("/youLike") //flowerYouLike猜你喜欢
|
||||
public JSONObject flowerYouLike(@RequestHeader String Authorization){
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
|
||||
if(!JWTUtil.checkToken(Authorization)){ //token认证失败
|
||||
JWTUtil.checkTokenFailed(jsonObject);
|
||||
return jsonObject;
|
||||
}
|
||||
|
||||
Claims claims = JWTUtil.getBodyByToken(Authorization);
|
||||
int user_id = (int) claims.get("id");
|
||||
|
||||
JSONObject jsonObject1 = new JSONObject();
|
||||
List<Flower> flowerList = flowerService.recommend(user_id);
|
||||
int size=flowerList.size();
|
||||
JSONObject[] objects = new JSONObject[size];
|
||||
for(int i = 0;i<size;i++){
|
||||
objects[i]=new JSONObject();
|
||||
objects[i].put("flower_id", flowerList.get(i).getFlower_id());
|
||||
objects[i].put("flower_name", flowerList.get(i).getFlower_name());
|
||||
objects[i].put("flower_price", flowerList.get(i).getFlower_price());
|
||||
objects[i].put("flower_sale", flowerList.get(i).getFlower_sale());
|
||||
objects[i].put("flower_pic", flowerList.get(i).getFlower_pic());
|
||||
}
|
||||
jsonObject1.put("flowers",objects);
|
||||
|
||||
jsonObject.put("code",200 );
|
||||
jsonObject.put("msg","token认证成功!" );
|
||||
jsonObject.put("data",jsonObject1);
|
||||
return jsonObject;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue