| @ -0,0 +1,59 @@ | |||||
| package org.jeecg.modules.applet.service; | |||||
| import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | |||||
| import com.baomidou.mybatisplus.core.metadata.IPage; | |||||
| import com.baomidou.mybatisplus.extension.plugins.pagination.Page; | |||||
| import org.jeecg.modules.demo.appletShippingAddress.entity.AppletShippingAddress; | |||||
| public interface AppletApiAddressService { | |||||
| /** | |||||
| * 分页查询地址 | |||||
| * | |||||
| * @param page | |||||
| * @return | |||||
| */ | |||||
| IPage<AppletShippingAddress> page(Page<AppletShippingAddress> page, AppletShippingAddress appletShippingAddress); | |||||
| /** | |||||
| * 添加地址 | |||||
| * | |||||
| * @param appletShippingAddress | |||||
| */ | |||||
| void save(AppletShippingAddress appletShippingAddress); | |||||
| /** | |||||
| * 通过id查询 | |||||
| * | |||||
| * @param id | |||||
| * @return | |||||
| */ | |||||
| AppletShippingAddress getById(String id); | |||||
| /** | |||||
| * 修改地址 | |||||
| * | |||||
| * @param appletShippingAddress | |||||
| */ | |||||
| void updateById(AppletShippingAddress appletShippingAddress); | |||||
| // /** | |||||
| // * 通过id删除 | |||||
| // * | |||||
| // * @param id | |||||
| // */ | |||||
| // void removeById(String id); | |||||
| // | |||||
| // /** | |||||
| // * 批量删除 | |||||
| // * | |||||
| // * @param list | |||||
| // */ | |||||
| // | |||||
| // void removeByIds(List<String> list); | |||||
| // | |||||
| } | |||||
| @ -0,0 +1,54 @@ | |||||
| package org.jeecg.modules.applet.service; | |||||
| import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | |||||
| import com.baomidou.mybatisplus.core.metadata.IPage; | |||||
| import com.baomidou.mybatisplus.extension.plugins.pagination.Page; | |||||
| import org.jeecg.modules.demo.appletCar.entity.AppletCar; | |||||
| import java.util.List; | |||||
| public interface AppletApiCarService { | |||||
| /** | |||||
| * 查询购物车列表 | |||||
| * | |||||
| * @param page | |||||
| * @param queryWrapper | |||||
| * @return | |||||
| */ | |||||
| IPage<AppletCar> page(Page<AppletCar> page, AppletCar appletCar); | |||||
| /** | |||||
| * 添加购物车 | |||||
| * | |||||
| * @param appletCar | |||||
| */ | |||||
| void save(AppletCar appletCar); | |||||
| /** | |||||
| * 根据id查询购物车 | |||||
| * | |||||
| * @param id | |||||
| * @return | |||||
| */ | |||||
| AppletCar getById(String id); | |||||
| /** | |||||
| * 修改购物车 | |||||
| * | |||||
| * @param appletCar | |||||
| */ | |||||
| void updateById(AppletCar appletCar); | |||||
| /** | |||||
| * 批量删除购物车 | |||||
| * | |||||
| * @param list | |||||
| */ | |||||
| void removeByIds(List<String> list); | |||||
| } | |||||
| @ -0,0 +1,108 @@ | |||||
| package org.jeecg.modules.applet.service.impl; | |||||
| import cn.hutool.core.util.StrUtil; | |||||
| import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | |||||
| import com.baomidou.mybatisplus.core.metadata.IPage; | |||||
| import com.baomidou.mybatisplus.extension.plugins.pagination.Page; | |||||
| import lombok.extern.slf4j.Slf4j; | |||||
| import org.jeecg.common.system.util.AppletUserUtil; | |||||
| import org.jeecg.modules.applet.service.AppletApiAddressService; | |||||
| import org.jeecg.modules.demo.appletShippingAddress.entity.AppletShippingAddress; | |||||
| import org.jeecg.modules.demo.appletShippingAddress.service.IAppletShippingAddressService; | |||||
| import org.springframework.beans.factory.annotation.Autowired; | |||||
| import org.springframework.stereotype.Service; | |||||
| @Service | |||||
| @Slf4j | |||||
| public class AppletApiAddressServiceImpl implements AppletApiAddressService { | |||||
| @Autowired | |||||
| private IAppletShippingAddressService appletShippingAddressService; | |||||
| /** | |||||
| * 查询地址列表 | |||||
| * | |||||
| * @param page | |||||
| * @param address | |||||
| * @return | |||||
| */ | |||||
| @Override | |||||
| public IPage<AppletShippingAddress> page(Page<AppletShippingAddress> page, AppletShippingAddress address) { | |||||
| String userId = AppletUserUtil.getCurrentAppletUserId(); | |||||
| Page<AppletShippingAddress> page1 = appletShippingAddressService | |||||
| .lambdaQuery() | |||||
| .eq(AppletShippingAddress::getUserId, userId)//getId -> getUserId | |||||
| .orderByDesc(AppletShippingAddress::getDefaultFlag) | |||||
| //如果你要写查询条件,这样写 | |||||
| //.eq(AppletShippingAddress::getCity, address.getCity())//这个是 等于 | |||||
| //.eq(StrUtil.isNotBlank((address.getCity()), AppletShippingAddress::getCity, address.getCity())//这样加条件不为空 | |||||
| .page(page); | |||||
| return page1; | |||||
| } | |||||
| /** | |||||
| * 新增地址 | |||||
| * | |||||
| * @param appletShippingAddress | |||||
| */ | |||||
| @Override | |||||
| public void save(AppletShippingAddress appletShippingAddress) { | |||||
| if (appletShippingAddress == null) { | |||||
| throw new IllegalArgumentException("地址信息不能为空"); | |||||
| } | |||||
| if (StrUtil.isBlank(appletShippingAddress.getName())) { | |||||
| throw new IllegalArgumentException("收货人不能为空"); | |||||
| } | |||||
| if (StrUtil.isBlank(appletShippingAddress.getPhone())) { | |||||
| throw new IllegalArgumentException("联系电话不能为空"); | |||||
| } | |||||
| if (StrUtil.isBlank(appletShippingAddress.getDetail())) { | |||||
| throw new IllegalArgumentException("详细地址不能为空"); | |||||
| } | |||||
| String userId = AppletUserUtil.getCurrentAppletUserId(); | |||||
| appletShippingAddress.setUserId(userId); | |||||
| appletShippingAddressService.save(appletShippingAddress); | |||||
| } | |||||
| /** | |||||
| * 根据id查询地址 | |||||
| * | |||||
| * @param id | |||||
| * @return | |||||
| */ | |||||
| @Override | |||||
| public AppletShippingAddress getById(String id) { | |||||
| AppletShippingAddress byId = appletShippingAddressService.getById(id); | |||||
| return byId; | |||||
| } | |||||
| /** | |||||
| * 修改地址 | |||||
| * | |||||
| * @param appletShippingAddress | |||||
| */ | |||||
| @Override | |||||
| public void updateById(AppletShippingAddress appletShippingAddress) { | |||||
| if (appletShippingAddress == null || StrUtil.isBlank(appletShippingAddress.getId())) { | |||||
| throw new IllegalArgumentException("地址信息或ID不能为空"); | |||||
| } | |||||
| if (StrUtil.isBlank(appletShippingAddress.getName())) { | |||||
| throw new IllegalArgumentException("收货人不能为空"); | |||||
| } | |||||
| if (StrUtil.isBlank(appletShippingAddress.getPhone())) { | |||||
| throw new IllegalArgumentException("联系电话不能为空"); | |||||
| } | |||||
| if (StrUtil.isBlank(appletShippingAddress.getDetail())) { | |||||
| throw new IllegalArgumentException("详细地址不能为空"); | |||||
| } | |||||
| appletShippingAddressService.updateById(appletShippingAddress); | |||||
| } | |||||
| } | |||||
| @ -0,0 +1,113 @@ | |||||
| package org.jeecg.modules.applet.service.impl; | |||||
| import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | |||||
| import com.baomidou.mybatisplus.core.metadata.IPage; | |||||
| import com.baomidou.mybatisplus.extension.plugins.pagination.Page; | |||||
| import org.jeecg.common.system.util.AppletUserUtil; | |||||
| import org.jeecg.modules.applet.service.AppletApiCarService; | |||||
| import org.jeecg.modules.demo.appletCar.entity.AppletCar; | |||||
| import org.jeecg.modules.demo.appletCar.service.IAppletCarService; | |||||
| import org.springframework.beans.factory.annotation.Autowired; | |||||
| import org.springframework.stereotype.Service; | |||||
| import java.util.List; | |||||
| import java.util.stream.Collectors; | |||||
| @Service | |||||
| public class AppletApiCarServiceImpl implements AppletApiCarService { | |||||
| @Autowired | |||||
| private IAppletCarService appletCarService; | |||||
| /** | |||||
| * 购物车列表查询 | |||||
| * | |||||
| * @param page | |||||
| * @param appletCar | |||||
| * @return | |||||
| */ | |||||
| @Override | |||||
| public IPage<AppletCar> page(Page<AppletCar> page, AppletCar appletCar) { | |||||
| String userId = AppletUserUtil.getCurrentAppletUserId(); | |||||
| Page<AppletCar> page1 = appletCarService | |||||
| .lambdaQuery() | |||||
| .eq(AppletCar::getUserId, userId) | |||||
| .page(page); | |||||
| return page1; | |||||
| } | |||||
| /** | |||||
| * 添加购物车 | |||||
| * | |||||
| * @param appletCar | |||||
| */ | |||||
| @Override | |||||
| public void save(AppletCar appletCar) { | |||||
| appletCarService.save(appletCar); | |||||
| } | |||||
| /** | |||||
| * 根据id查询购物车 | |||||
| * | |||||
| * @param id 购物车id | |||||
| * @return 购物车对象 | |||||
| */ | |||||
| @Override | |||||
| public AppletCar getById(String id) { | |||||
| // 获取当前登录的小程序用户ID | |||||
| String userId = AppletUserUtil.getCurrentAppletUserId(); | |||||
| // 使用LambdaQuery构造查询条件,同时匹配购物车ID和用户ID,确保数据隔离 | |||||
| AppletCar appletCar = appletCarService | |||||
| .lambdaQuery() | |||||
| .eq(AppletCar::getId, id) // 匹配购物车ID | |||||
| .eq(AppletCar::getUserId, userId) // 匹配用户ID | |||||
| .one(); // 查询单个结果 | |||||
| return appletCar; | |||||
| } | |||||
| /** | |||||
| * 修改购物车 | |||||
| * | |||||
| * @param appletCar | |||||
| */ | |||||
| @Override | |||||
| public void updateById(AppletCar appletCar) { | |||||
| // 获取当前登录的小程序用户ID | |||||
| String userId = AppletUserUtil.getCurrentAppletUserId(); | |||||
| // 验证该购物车是否属于当前用户 | |||||
| AppletCar existingCar = appletCarService | |||||
| .lambdaQuery() | |||||
| .eq(AppletCar::getId, appletCar.getId()) // 匹配购物车ID | |||||
| .eq(AppletCar::getUserId, userId) // 匹配用户ID | |||||
| .one(); | |||||
| // 如果购物车存在且属于当前用户,则执行更新操作 | |||||
| if (existingCar != null) { | |||||
| appletCarService.updateById(appletCar); | |||||
| } | |||||
| } | |||||
| /** | |||||
| * 批量删除购物车 | |||||
| * | |||||
| * @param ids 购物车ID列表 | |||||
| */ | |||||
| @Override | |||||
| public void removeByIds(List<String> ids) { | |||||
| // 获取当前登录的小程序用户ID | |||||
| String userId = AppletUserUtil.getCurrentAppletUserId(); | |||||
| // 查询这些ID中属于当前用户的购物车项 | |||||
| List<AppletCar> appletCars = appletCarService | |||||
| .lambdaQuery() | |||||
| .in(AppletCar::getId, ids) // 匹配购物车ID列表 | |||||
| .eq(AppletCar::getUserId, userId) // 匹配用户ID | |||||
| .list(); | |||||
| // 提取属于当前用户的购物车ID列表 | |||||
| List<String> userCarIds = appletCars.stream() | |||||
| .map(AppletCar::getId) | |||||
| .collect(Collectors.toList()); | |||||
| // 执行删除操作(只会删除属于当前用户的购物车项) | |||||
| if (!userCarIds.isEmpty()) { | |||||
| appletCarService.removeByIds(userCarIds); | |||||
| } | |||||
| } | |||||
| } | |||||