parent
cfb3591640
commit
2c844711fd
@ -0,0 +1,435 @@
|
||||
package com.example.flower.controller;
|
||||
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.example.flower.entity.Cart;
|
||||
import com.example.flower.entity.Flower;
|
||||
import com.example.flower.entity.Pack;
|
||||
import com.example.flower.service.CartService;
|
||||
import com.example.flower.service.FlowerService;
|
||||
import com.example.flower.service.PackService;
|
||||
import com.example.flower.unit.JWTUtil;
|
||||
import io.jsonwebtoken.Claims;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/cart")
|
||||
public class CartController {
|
||||
@Resource
|
||||
private CartService cartService;
|
||||
@Resource
|
||||
private FlowerService flowerService;
|
||||
@Resource
|
||||
private PackService packService;
|
||||
|
||||
@PostMapping("/list") //cartList获取所有购物车信息列表
|
||||
public JSONObject cartList(@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");
|
||||
Claims claims = JWTUtil.getBodyByToken(Authorization);
|
||||
int user_id = (int) claims.get("id");
|
||||
|
||||
List<Cart> cartList = cartService.cartList(page,page_size,user_id).getList();
|
||||
|
||||
jsonObject1.put("page_number",cartService.cartList(page,page_size,user_id).getPages());
|
||||
jsonObject1.put("total",cartService.cartList(page,page_size,user_id).getTotal());
|
||||
|
||||
int size=cartList.size();
|
||||
|
||||
JSONObject[] objects = new JSONObject[size];
|
||||
for(int i = 0;i<size;i++) {
|
||||
objects[i] = new JSONObject();
|
||||
objects[i].put("cart_id", cartList.get(i).getCart_id());
|
||||
Flower flower = flowerService.flowerInfo(cartList.get(i).getFlower_id());
|
||||
objects[i].put("flower_id", flower.getFlower_id());
|
||||
objects[i].put("flower_name", flower.getFlower_name());
|
||||
objects[i].put("flower_price", flower.getFlower_price());
|
||||
objects[i].put("flower_pic", flower.getFlower_pic());
|
||||
objects[i].put("number", cartList.get(i).getNumber());
|
||||
|
||||
double sumP = 0.00; //花材金额小计
|
||||
//花材信息
|
||||
if (cartList.get(i).getPack_ids() == null || Objects.equals(cartList.get(i).getPack_ids(), "")) {
|
||||
objects[i].put("packs", "");
|
||||
objects[i].put("single_price",(flower.getFlower_price())); //花卉+花材 单价金额小计
|
||||
} else {
|
||||
//数组拆分
|
||||
String[] pack_ids_string = cartList.get(i).getPack_ids().split(",");
|
||||
int[] pack_ids_int = new int[pack_ids_string.length];
|
||||
for (int j = 0; j < pack_ids_string.length; j++) {
|
||||
if (pack_ids_string[j] != null)
|
||||
pack_ids_int[j] = Integer.parseInt(pack_ids_string[j].trim());
|
||||
}
|
||||
|
||||
JSONObject[] object_pack = new JSONObject[pack_ids_int.length];
|
||||
for (int k = 0; k < pack_ids_int.length; k++) {
|
||||
Pack pack = packService.packInfo(pack_ids_int[k]);
|
||||
object_pack[k] = new JSONObject();
|
||||
object_pack[k].put("pack_id", pack.getPack_id());
|
||||
object_pack[k].put("pack_name", pack.getPack_name());
|
||||
object_pack[k].put("pack_price", pack.getPack_price());
|
||||
sumP += pack.getPack_price();
|
||||
}
|
||||
objects[i].put("single_price",(flower.getFlower_price()+sumP)); //花卉+花材 单价金额小计
|
||||
objects[i].put("pack",object_pack);
|
||||
}
|
||||
jsonObject1.put("carts", objects);
|
||||
|
||||
jsonObject.put("code", 200);
|
||||
jsonObject.put("msg", "token认证成功!");
|
||||
jsonObject.put("data", jsonObject1);
|
||||
}
|
||||
|
||||
return jsonObject;
|
||||
}
|
||||
|
||||
@PostMapping("/query") //cartQuery查询购物车信息列表
|
||||
public JSONObject cartListByQuery(@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");
|
||||
Claims claims = JWTUtil.getBodyByToken(Authorization);
|
||||
int user_id = (int) claims.get("id");
|
||||
|
||||
List<Cart> cartList = cartService.cartListByQuery(page,page_size,user_id,flower_name).getList();
|
||||
|
||||
jsonObject1.put("page_number",cartService.cartListByQuery(page,page_size,user_id,flower_name).getPages());
|
||||
jsonObject1.put("total",cartService.cartListByQuery(page,page_size,user_id,flower_name).getTotal());
|
||||
|
||||
int size=cartList.size();
|
||||
|
||||
JSONObject[] objects = new JSONObject[size];
|
||||
for(int i = 0;i<size;i++) {
|
||||
objects[i] = new JSONObject();
|
||||
objects[i].put("cart_id", cartList.get(i).getCart_id());
|
||||
Flower flower = flowerService.flowerInfo(cartList.get(i).getFlower_id());
|
||||
objects[i].put("flower_id", flower.getFlower_id());
|
||||
objects[i].put("flower_name", flower.getFlower_name());
|
||||
objects[i].put("flower_price", flower.getFlower_price());
|
||||
objects[i].put("flower_pic", flower.getFlower_pic());
|
||||
objects[i].put("number", cartList.get(i).getNumber());
|
||||
|
||||
double sumP = 0.00; //花材金额小计
|
||||
//花材信息
|
||||
if (cartList.get(i).getPack_ids() == null || Objects.equals(cartList.get(i).getPack_ids(), "")) {
|
||||
objects[i].put("packs","");
|
||||
objects[i].put("single_price",(flower.getFlower_price())); //花卉+花材 单价金额小计
|
||||
} else {
|
||||
//数组拆分
|
||||
String[] pack_ids_string = cartList.get(i).getPack_ids().split(",");
|
||||
int[] pack_ids_int = new int[pack_ids_string.length];
|
||||
for (int j = 0; j < pack_ids_string.length; j++) {
|
||||
if (pack_ids_string[j] != null)
|
||||
pack_ids_int[j] = Integer.parseInt(pack_ids_string[j].trim());
|
||||
}
|
||||
|
||||
JSONObject[] object_pack = new JSONObject[pack_ids_int.length];
|
||||
for (int k = 0; k < pack_ids_int.length; k++) {
|
||||
Pack pack = packService.packInfo(pack_ids_int[k]);
|
||||
object_pack[k] = new JSONObject();
|
||||
object_pack[k].put("pack_id", pack.getPack_id());
|
||||
object_pack[k].put("pack_name", pack.getPack_name());
|
||||
object_pack[k].put("pack_price", pack.getPack_price());
|
||||
sumP += pack.getPack_price();
|
||||
}
|
||||
objects[i].put("single_price",(flower.getFlower_price()+sumP)); //花卉+花材 单价金额小计
|
||||
objects[i].put("pack",object_pack);
|
||||
}
|
||||
jsonObject1.put("carts", objects);
|
||||
|
||||
jsonObject.put("code", 200);
|
||||
jsonObject.put("msg", "token认证成功!");
|
||||
jsonObject.put("data", jsonObject1);
|
||||
}
|
||||
|
||||
return jsonObject;
|
||||
}
|
||||
|
||||
@PostMapping("/add") //cartAdd添加新购物车
|
||||
public JSONObject cartAdd(@RequestHeader String Authorization, @RequestBody JSONObject param){
|
||||
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");
|
||||
int flower_id = param.getIntValue("flower_id");
|
||||
String pack_ids = param.getString("pack_ids");
|
||||
int number = param.getIntValue("number");
|
||||
|
||||
Cart cart = new Cart();
|
||||
cart.setUser_id(user_id);
|
||||
cart.setFlower_id(flower_id);
|
||||
|
||||
//重复添加购物车只增加数量
|
||||
List<Cart> cart_db = cartService.cartRepeat(user_id,flower_id);
|
||||
|
||||
//无重复 add
|
||||
if(cart_db.size()==0){
|
||||
cart.setPack_ids(pack_ids);
|
||||
cart.setNumber(param.getIntValue("number"));
|
||||
cartService.cartAdd(cart);
|
||||
}
|
||||
|
||||
//有重复,比较花材信息
|
||||
else {
|
||||
boolean flag = false; //记录是否有重复
|
||||
int i = 0;
|
||||
while(i<cart_db.size() && !flag){
|
||||
//要添加的 与 数据库中的购物车均无花材信息 => 即购物车信息一样 num
|
||||
if( (cart_db.get(i).getPack_ids()== null || Objects.equals(cart_db.get(i).getPack_ids(), ""))
|
||||
&& (pack_ids==null || Objects.equals(pack_ids, "")) ){
|
||||
flag = true;
|
||||
}
|
||||
else {
|
||||
//至少一个为空 => 花材不一样 add
|
||||
if((cart_db.get(i).getPack_ids()== null || Objects.equals(cart_db.get(i).getPack_ids(), ""))
|
||||
|| (pack_ids==null || Objects.equals(pack_ids, "")) ){
|
||||
i++;
|
||||
}
|
||||
//都不空,判断是否相同
|
||||
else{
|
||||
String[] pack_ids_string1 = pack_ids.split(",");
|
||||
int[] pack_ids_int1 = new int[pack_ids_string1.length];
|
||||
for (int j = 0; j < pack_ids_string1.length; j++) {
|
||||
if (pack_ids_string1[j] != null)
|
||||
pack_ids_int1[j] = Integer.parseInt(pack_ids_string1[j].trim());
|
||||
}
|
||||
|
||||
String[] pack_ids_string2 = cart_db.get(i).getPack_ids().split(",");
|
||||
int[] pack_ids_int2 = new int[pack_ids_string2.length];
|
||||
for (int j = 0; j < pack_ids_string2.length; j++) {
|
||||
if (pack_ids_string2[j] != null)
|
||||
pack_ids_int2[j] = Integer.parseInt(pack_ids_string2[j].trim());
|
||||
}
|
||||
|
||||
if(Arrays.equals(pack_ids_int1,pack_ids_int2)){ //花材重复 num
|
||||
flag = true;
|
||||
}
|
||||
else{ //不重复 add
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if(flag){ //重复,只改数量
|
||||
cart.setCart_id(cart_db.get(i).getCart_id());
|
||||
cart.setNumber(cart_db.get(i).getNumber()+number);
|
||||
cartService.cartModify_num(cart);
|
||||
}
|
||||
else{ //不重复,添加购物车
|
||||
cart.setPack_ids(pack_ids);
|
||||
cart.setNumber(param.getIntValue("number"));
|
||||
cartService.cartAdd(cart);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
jsonObject.put("code",200 );
|
||||
jsonObject.put("msg","成功" );
|
||||
jsonObject.put("cart_id",cart.getCart_id());
|
||||
|
||||
return jsonObject;
|
||||
}
|
||||
|
||||
@PostMapping("/deletePer") //cartDeletePer删除单个购物车
|
||||
public JSONObject cartDeletePer(@RequestHeader String Authorization, @RequestBody JSONObject param){
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
|
||||
if(!JWTUtil.checkToken(Authorization)){ //token认证失败
|
||||
JWTUtil.checkTokenFailed(jsonObject);
|
||||
return jsonObject;
|
||||
}
|
||||
|
||||
int flag = cartService.cartDeletePer(param.getIntValue("cart_id"));
|
||||
if(flag==1){
|
||||
jsonObject.put("code",200 );
|
||||
jsonObject.put("msg","成功" );
|
||||
}
|
||||
else {
|
||||
jsonObject.put("code",403 );
|
||||
jsonObject.put("msg","删除失败" );
|
||||
}
|
||||
return jsonObject;
|
||||
}
|
||||
|
||||
@PostMapping("/deleteMul") //cartDeleteMul批量删除购物车
|
||||
public JSONObject cartDeleteMul(@RequestHeader String Authorization, @RequestBody JSONObject param){
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
|
||||
if(!JWTUtil.checkToken(Authorization)){ //token认证失败
|
||||
JWTUtil.checkTokenFailed(jsonObject);
|
||||
return jsonObject;
|
||||
}
|
||||
|
||||
//获取请求参数
|
||||
JSONArray cart_ids = param.getJSONArray("cart_ids");
|
||||
if(cart_ids.size()==0){
|
||||
jsonObject.put("code",403);
|
||||
jsonObject.put("msg","批量删除失败,请选择商品");
|
||||
return jsonObject;
|
||||
}
|
||||
|
||||
int [] ids = new int[cart_ids.size()];
|
||||
|
||||
//把cart_id中的数据进行类型转换并存入到int数组中
|
||||
for (int i = 0; i < cart_ids.size(); i++) {
|
||||
ids[i] = Integer.parseInt(cart_ids.getString(i));
|
||||
}
|
||||
|
||||
int result = cartService.cartDeleteMul(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") //cartInfo获取购物车信息
|
||||
public JSONObject cartInfo(@RequestHeader String Authorization, @RequestBody JSONObject param){
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
|
||||
if(!JWTUtil.checkToken(Authorization)){ //token认证失败
|
||||
JWTUtil.checkTokenFailed(jsonObject);
|
||||
return jsonObject;
|
||||
}
|
||||
|
||||
int cart_id = param.getIntValue("cart_id");
|
||||
Cart cart = cartService.cartInfo(cart_id);
|
||||
|
||||
if(cart!=null){
|
||||
JSONObject jsonObject1 = new JSONObject();
|
||||
jsonObject1.put("cart_id",cart.getCart_id());
|
||||
jsonObject1.put("number",cart.getNumber());
|
||||
|
||||
if (cart.getPack_ids() == null || Objects.equals(cart.getPack_ids(), "")) {
|
||||
jsonObject1.put("packs", "");
|
||||
} else {
|
||||
//数组拆分
|
||||
String[] pack_ids_string = cart.getPack_ids().split(",");
|
||||
int[] pack_ids_int = new int[pack_ids_string.length];
|
||||
for (int j = 0; j < pack_ids_string.length; j++) {
|
||||
if (pack_ids_string[j] != null)
|
||||
pack_ids_int[j] = Integer.parseInt(pack_ids_string[j].trim());
|
||||
}
|
||||
jsonObject1.put("pack_id_array",pack_ids_int);
|
||||
|
||||
JSONObject[] object_pack = new JSONObject[pack_ids_int.length];
|
||||
for (int k = 0; k < pack_ids_int.length; k++) {
|
||||
Pack pack = packService.packInfo(pack_ids_int[k]);
|
||||
object_pack[k] = new JSONObject();
|
||||
object_pack[k].put("pack_id", pack.getPack_id());
|
||||
object_pack[k].put("pack_name", pack.getPack_name());
|
||||
object_pack[k].put("pack_price", pack.getPack_price());
|
||||
}
|
||||
jsonObject1.put("pack",object_pack);
|
||||
}
|
||||
|
||||
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") //cartModify修改购物车信息_花材
|
||||
public JSONObject cartModify(@RequestHeader String Authorization, @RequestBody JSONObject param){
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
|
||||
if(!JWTUtil.checkToken(Authorization)){ //token认证失败
|
||||
JWTUtil.checkTokenFailed(jsonObject);
|
||||
return jsonObject;
|
||||
}
|
||||
|
||||
Cart cart = new Cart();
|
||||
cart.setCart_id(param.getIntValue("cart_id"));
|
||||
cart.setPack_ids(param.getString("pack_ids"));
|
||||
|
||||
int flag = cartService.cartModify(cart);
|
||||
if(flag==1){
|
||||
jsonObject.put("code",200 );
|
||||
jsonObject.put("msg","修改成功" );
|
||||
}
|
||||
else{
|
||||
jsonObject.put("code",403 );
|
||||
jsonObject.put("msg","修改失败" );
|
||||
}
|
||||
|
||||
return jsonObject;
|
||||
}
|
||||
|
||||
@PostMapping("/modify_num") //cartModify修改购物车信息_数量
|
||||
public JSONObject cartModify_num(@RequestHeader String Authorization, @RequestBody JSONObject param){
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
|
||||
if(!JWTUtil.checkToken(Authorization)){ //token认证失败
|
||||
JWTUtil.checkTokenFailed(jsonObject);
|
||||
return jsonObject;
|
||||
}
|
||||
|
||||
Cart cart = new Cart();
|
||||
cart.setCart_id(param.getIntValue("cart_id"));
|
||||
cart.setNumber(param.getIntValue("number"));
|
||||
|
||||
int flag = cartService.cartModify_num(cart);
|
||||
if(flag==1){
|
||||
jsonObject.put("code",200 );
|
||||
jsonObject.put("msg","修改成功" );
|
||||
}
|
||||
else{
|
||||
jsonObject.put("code",403 );
|
||||
jsonObject.put("msg","修改失败" );
|
||||
}
|
||||
|
||||
return jsonObject;
|
||||
}
|
||||
|
||||
@PostMapping("/count")
|
||||
public JSONObject cartCountNum(@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.put("cartNum",cartService.cartNum(user_id));
|
||||
jsonObject.put("code",200);
|
||||
jsonObject.put("msg","成功");
|
||||
return jsonObject;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue