package com.ruoyi.other.service.impl;
|
|
import cn.hutool.core.util.ObjectUtil;
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.ruoyi.account.api.feignClient.AppUserClient;
|
import com.ruoyi.account.api.model.AppUser;
|
import com.ruoyi.common.core.constant.TokenConstants;
|
import com.ruoyi.common.core.domain.R;
|
import com.ruoyi.common.core.utils.ServletUtils;
|
import com.ruoyi.common.core.utils.StringUtils;
|
import com.ruoyi.common.core.utils.bean.BeanUtils;
|
import com.ruoyi.common.core.web.page.PageInfo;
|
import com.ruoyi.common.security.service.TokenService;
|
import com.ruoyi.common.security.utils.SecurityUtils;
|
import com.ruoyi.order.feignClient.OrderClient;
|
import com.ruoyi.order.feignClient.RemoteOrderGoodsClient;
|
import com.ruoyi.order.vo.Price;
|
import com.ruoyi.other.api.domain.*;
|
import com.ruoyi.other.api.vo.GetGoodsBargainPrice;
|
import com.ruoyi.other.api.vo.GetSeckillActivityInfo;
|
import com.ruoyi.other.enums.GoodsStatus;
|
import com.ruoyi.other.mapper.GoodsMapper;
|
import com.ruoyi.other.mapper.GoodsShopMapper;
|
import com.ruoyi.other.mapper.ShopMapper;
|
import com.ruoyi.other.service.*;
|
import com.ruoyi.other.util.GeodesyUtil;
|
import com.ruoyi.other.vo.GoodsVO;
|
import com.ruoyi.system.api.model.LoginUser;
|
import org.jetbrains.annotations.NotNull;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.util.CollectionUtils;
|
|
import javax.annotation.Resource;
|
import java.math.BigDecimal;
|
import java.util.*;
|
import java.util.stream.Collectors;
|
import java.util.stream.Stream;
|
|
/**
|
* <p>
|
* 服务实现类
|
* </p>
|
*
|
* @author luodangjia
|
* @since 2024-11-20
|
*/
|
@Service
|
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {
|
@Resource
|
private GoodsMapper goodsMapper;
|
@Resource
|
private TokenService tokenService;
|
@Resource
|
private AppUserClient appUserClient;
|
@Resource
|
private GoodsShopMapper goodsShopMapper;
|
@Resource
|
private GoodsShopService goodsShopService;
|
@Resource
|
private ShopMapper shopMapper;
|
@Resource
|
private RemoteOrderGoodsClient remoteOrderGoodsClient;
|
@Resource
|
private SeckillActivityInfoService seckillActivityInfoService;
|
@Resource
|
private GoodsSeckillService goodsSeckillService;
|
@Resource
|
private OrderClient orderClient;
|
|
|
|
|
@Override
|
public PageInfo<GoodsVO> goodsList(Goods search) {
|
Integer vipId = 0;
|
String provinceCode = "0";
|
String cityCode = "0";
|
String districtCode = "0";
|
String token = SecurityUtils.getToken(ServletUtils.getRequest());
|
if(StringUtils.isNotEmpty(token)){
|
Long userid = tokenService.getLoginUserApplet().getUserid();
|
AppUser appUser = appUserClient.getAppUserById(userid);
|
vipId = appUser.getVipId();
|
provinceCode = appUser.getProvinceCode();
|
cityCode = appUser.getCityCode();
|
districtCode = appUser.getDistrictCode();
|
}
|
List<GoodsVO> list = this.baseMapper.goodsList(search.getGoodsCategoryId(), search.getName(), vipId);
|
for (GoodsVO goods : list) {
|
Price price = getPrice(vipId, goods.getGoodsId(), null, 1, provinceCode, cityCode, districtCode);
|
if(null != price){
|
goods.setCashPayment(price.getCashPayment() ? 1 : 0);
|
goods.setPointPayment(price.getPointPayment() ? 1 : 0);
|
goods.setSellingPrice(price.getCash());
|
goods.setIntegral(price.getPoint());
|
}
|
Integer data = orderClient.getGoodsSaleNum(goods.getGoodsId(), 1).getData();
|
goods.setSaleNum(data);
|
}
|
|
//手动排序
|
if(StringUtils.isNotEmpty(search.getOrderByColumn())){
|
if("tgs.selling_price".equals(search.getOrderByColumn())){
|
list.sort(new Comparator<GoodsVO>() {
|
@Override
|
public int compare(GoodsVO o1, GoodsVO o2) {
|
if(null != o1.getSellingPrice() && null != o2.getSellingPrice()){
|
return o1.getSellingPrice().compareTo(o2.getSellingPrice()) * ("desc".equals(search.getIsAsc()) ? -1 : 1);
|
}
|
if(null == o1.getSellingPrice() && null != o2.getSellingPrice()){
|
return BigDecimal.ZERO.compareTo(o2.getSellingPrice()) * ("desc".equals(search.getIsAsc()) ? -1 : 1);
|
}
|
if(null != o1.getSellingPrice() && null == o2.getSellingPrice()){
|
return o1.getSellingPrice().compareTo(BigDecimal.ZERO) * ("desc".equals(search.getIsAsc()) ? -1 : 1);
|
}
|
return 0;
|
}
|
});
|
}
|
if("integral".equals(search.getOrderByColumn())){
|
list.sort(new Comparator<GoodsVO>() {
|
@Override
|
public int compare(GoodsVO o1, GoodsVO o2) {
|
if(null != o1.getIntegral() && null != o2.getIntegral()){
|
return o1.getIntegral().compareTo(o2.getIntegral()) * ("desc".equals(search.getIsAsc()) ? -1 : 1);
|
}
|
if(null == o1.getIntegral() && null != o2.getIntegral()){
|
return Integer.valueOf(0).compareTo(o2.getIntegral()) * ("desc".equals(search.getIsAsc()) ? -1 : 1);
|
}
|
if(null != o1.getIntegral() && null == o2.getIntegral()){
|
return o1.getIntegral().compareTo(Integer.valueOf(0)) * ("desc".equals(search.getIsAsc()) ? -1 : 1);
|
}
|
return 0;
|
}
|
});
|
}
|
if("sale_num".equals(search.getOrderByColumn())){
|
list.sort(new Comparator<GoodsVO>() {
|
@Override
|
public int compare(GoodsVO o1, GoodsVO o2) {
|
return o1.getSaleNum().compareTo(o2.getSaleNum()) * ("desc".equals(search.getIsAsc()) ? -1 : 1);
|
}
|
});
|
}
|
}
|
// 手动处理分页
|
PageInfo<GoodsVO> pageInfo = new PageInfo<>();
|
pageInfo.setSize(search.getPageSize());
|
pageInfo.setTotal(list.size());
|
|
int pageSize = search.getPageSize();
|
int pageCurr = search.getPageCurr();
|
|
// 计算起始索引
|
int pageNum = (pageCurr - 1) * pageSize;
|
|
// 检查 pageNum 是否合法
|
if (pageNum < 0 || pageNum >= list.size()) {
|
// 如果 pageNum 不合法,返回空列表
|
pageInfo.setRecords(new ArrayList<>());
|
return pageInfo;
|
}
|
|
// 计算结束索引
|
int endIndex = Math.min(pageNum + pageSize, list.size());
|
|
// 获取分页数据
|
List<GoodsVO> pageList = list.subList(pageNum, endIndex);
|
|
// 设置分页结果
|
pageInfo.setRecords(pageList);
|
return pageInfo;
|
}
|
|
@Override
|
public GoodsVO goodsDetail(Long goodsId, Integer shopId, String longitude, String latitude) {
|
if (goodsId == null || goodsId <= 0) {
|
throw new NullPointerException("商品ID不能为空");
|
}
|
|
Integer vipId = 0;
|
String provinceCode = null;
|
String cityCode = null;
|
String districtCode = null;
|
String token = SecurityUtils.getToken(ServletUtils.getRequest());
|
if(StringUtils.isNotEmpty(token)){
|
Long userid = tokenService.getLoginUserApplet().getUserid();
|
AppUser appUser = appUserClient.getAppUserById(userid);
|
vipId = appUser.getVipId();
|
provinceCode = appUser.getProvinceCode();
|
cityCode = appUser.getCityCode();
|
districtCode = appUser.getDistrictCode();
|
}
|
|
Goods goods = this.getById(goodsId);
|
if(null == goods || goods.getDelFlag() == 1){
|
throw new RuntimeException("商品不存在");
|
}
|
if(goods.getStatus() == 1){
|
throw new RuntimeException("商品已被下架");
|
}
|
if(!goods.getCommodityAuthority().contains("-1") && !goods.getCommodityAuthority().contains(vipId.toString())){
|
throw new RuntimeException("权限不足");
|
}
|
GoodsVO goodsVO = new GoodsVO();
|
BeanUtils.copyBeanProp(goodsVO, goods);
|
goodsVO.setGoodsId(goods.getId());
|
goodsVO.setGoodsName(goods.getName());
|
Price price = getPrice(vipId, goods.getId(), shopId, 1, provinceCode, cityCode, districtCode);
|
if(null != price){
|
goodsVO.setPointPayment(price.getPointPayment() ? 1 : 0);
|
goodsVO.setCashPayment(price.getCashPayment() ? 1 : 0);
|
goodsVO.setSellingPrice(price.getCash());
|
goodsVO.setIntegral(price.getPoint());
|
}
|
|
if(goods.getType() == 1){
|
if(goods.getAppointStore() == 2){
|
List<Shop> shopList = shopMapper.selectList(new LambdaQueryWrapper<Shop>()
|
.in(Shop::getDelFlag, 0));
|
goodsVO.setShopList(shopList);
|
}else{
|
List<GoodsShop> goodsShopList = goodsShopMapper.selectList(new LambdaQueryWrapper<GoodsShop>()
|
.eq(GoodsShop::getGoodsId, goodsId));
|
if (!CollectionUtils.isEmpty(goodsShopList)){
|
List<Integer> shopIds = goodsShopList.stream().map(GoodsShop::getShopId).collect(Collectors.toList());
|
List<Shop> shopList = shopMapper.selectList(new LambdaQueryWrapper<Shop>()
|
.in(Shop::getId, shopIds));
|
goodsVO.setShopList(shopList);
|
}
|
}
|
}else{
|
List<Shop> shopList = shopMapper.selectList(new LambdaQueryWrapper<Shop>()
|
.in(Shop::getDelFlag, 0));
|
goodsVO.setShopList(shopList);
|
}
|
if(StringUtils.isNotEmpty(longitude) && StringUtils.isNotEmpty(latitude)){
|
List<Shop> shopList = goodsVO.getShopList();
|
for (Shop shop : shopList) {
|
Double wgs84 = GeodesyUtil.getDistance(longitude + "," + latitude, shop.getLongitude() + "," + shop.getLatitude()).get("WGS84");
|
shop.setDistance(wgs84);
|
}
|
shopList.sort(new Comparator<Shop>() {
|
@Override
|
public int compare(Shop o1, Shop o2) {
|
return o1.getDistance().compareTo(o2.getDistance());
|
}
|
});
|
goodsVO.setShopList(shopList);
|
}
|
Integer integer = orderClient.getGoodsSaleNum(goods.getId(), 1).getData();
|
goodsVO.setSaleNum(integer);
|
return goodsVO;
|
}
|
|
|
@Override
|
public List<Goods> getGoodsListByShopId(PageInfo<Goods> pageInfo, Integer shopId) {
|
Integer vipId = 0;
|
String provinceCode = "0";
|
String cityCode = "0";
|
String districtCode = "0";
|
String token = SecurityUtils.getToken(ServletUtils.getRequest());
|
if(StringUtils.isNotEmpty(token)){
|
Long userid = tokenService.getLoginUserApplet().getUserid();
|
AppUser appUser = appUserClient.getAppUserById(userid);
|
vipId = appUser.getVipId();
|
provinceCode = appUser.getProvinceCode();
|
cityCode = appUser.getCityCode();
|
districtCode = appUser.getDistrictCode();
|
}
|
List<Goods> goods = goodsMapper.selectListByShopId(pageInfo, shopId, vipId);
|
for (Goods good : goods) {
|
Price price = getPrice(vipId, good.getId(), shopId, 1, provinceCode, cityCode, districtCode);
|
if(null != price){
|
good.setCashPayment(price.getCashPayment() ? 1 : 0);
|
good.setPointPayment(price.getPointPayment() ? 1 : 0);
|
good.setSellingPrice(price.getCash());
|
good.setIntegral(price.getPoint());
|
}
|
Integer data = orderClient.getGoodsSaleNum(good.getId(), 1).getData();
|
good.setSaleNum(data);
|
}
|
return goods;
|
}
|
|
@Override
|
public IPage<Goods> getManageGoodsList(Page<Goods> page, Goods goods) {
|
IPage<Goods> goodsIPage = goodsMapper.selectManageGoodsList(page, goods);
|
goodsIPage.getRecords().forEach(goods1 -> {
|
Integer data = orderClient.getGoodsSaleNum(goods1.getId(), 1).getData();
|
goods1.setSaleNum(data);
|
});
|
return goodsIPage;
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public void addGoods(Goods goods) {
|
goods.setSaleNum(0);
|
goods.setStatus(GoodsStatus.DOWN.getCode());
|
goodsMapper.insert(goods);
|
|
// 指定门店
|
Integer appointStore = goods.getAppointStore();
|
if (null != appointStore && appointStore == 1){
|
List<GoodsShop> goodsShopList = goods.getGoodsShopList();
|
if (CollectionUtils.isEmpty(goodsShopList)){
|
throw new NullPointerException("请选择指定门店");
|
}
|
saveGoodsShopList(goodsShopList, goods.getId());
|
}
|
}
|
|
@Override
|
public void updateManageGoods(Goods goods) {
|
this.updateById(goods);
|
//修改个别字段
|
this.update(new LambdaUpdateWrapper<Goods>().eq(Goods::getId, goods.getId()).set(Goods::getSellingPrice, goods.getSellingPrice())
|
.set(Goods::getIntegral, goods.getIntegral()));
|
// 指定门店
|
List<GoodsShop> goodsShopList = goods.getGoodsShopList();
|
saveGoodsShopList(goodsShopList, goods.getId());
|
}
|
|
|
|
|
|
|
|
|
private void saveGoodsShopList(List<GoodsShop> goodsShopList, Integer goodsId) {
|
goodsShopService.remove(new LambdaQueryWrapper<GoodsShop>()
|
.eq(GoodsShop::getGoodsId, goodsId));
|
if (!CollectionUtils.isEmpty(goodsShopList)){
|
for (GoodsShop goodsShop : goodsShopList) {
|
goodsShop.setGoodsId(goodsId);
|
goodsShop.setId(null);
|
}
|
}
|
goodsShopService.saveBatch(goodsShopList);
|
}
|
|
@Override
|
public Goods getManageGoodsDetail(Long goodsId) {
|
Goods goods = getById(goodsId);
|
if (goods == null){
|
return null;
|
}
|
// 指定门店
|
List<GoodsShop> goodsShops = goodsShopService.list(new LambdaQueryWrapper<GoodsShop>()
|
.eq(GoodsShop::getGoodsId, goodsId));
|
|
for (GoodsShop goodsShop : goodsShops) {
|
Shop shop = shopMapper.selectById(goodsShop.getShopId());
|
if(null != shop){
|
goodsShop.setShopName(shop.getName());
|
goodsShop.setOwnerName(shop.getShopManager());
|
goodsShop.setPhone(shop.getPhone());
|
goodsShop.setAddress(shop.getAddress());
|
}
|
}
|
goods.setGoodsShopList(goodsShops);
|
return goods;
|
}
|
|
|
|
/**
|
* 根据商品的价格配置体系获取商品当前的价格
|
* @param vip
|
* @param goodsId
|
* @param type 1普通商品,2秒杀商品
|
* @param shopId
|
* @param provinceCode
|
* @param cityCode
|
* @param districtCode
|
* @return
|
*/
|
public Price getPrice(Integer vip, Integer goodsId, Integer shopId, Integer type, String provinceCode, String cityCode, String districtCode){
|
//获取支付价格
|
//秒杀活动>门店特价>地区价格>会员价格
|
//判断是否有秒杀活动
|
Price price = new Price();
|
SeckillActivityInfo one = seckillActivityInfoService.getOne(new LambdaQueryWrapper<SeckillActivityInfo>().eq(SeckillActivityInfo::getGoodId, goodsId)
|
.eq(SeckillActivityInfo::getIsShelves, 1).eq(SeckillActivityInfo::getDelFlag, 0)
|
.last(" and now() between start_time and end_time and FIND_IN_SET(" + vip + ", vip_ids) order by create_time desc limit 0, 1"));
|
GoodsSeckill goodsSeckill = null;
|
if(null != one){
|
goodsSeckill = goodsSeckillService.getOne(new LambdaQueryWrapper<GoodsSeckill>().eq(GoodsSeckill::getSeckillActivityInfoId, one.getId()).eq(GoodsSeckill::getVip, vip));
|
}
|
//没有秒杀活动或者添加的普通商品则不使用秒杀活动价格
|
if(null != goodsSeckill && type == 1){
|
//构建价格数据
|
if(goodsSeckill.getCashPayment() == 1 && goodsSeckill.getPointPayment() == 1){
|
price.setCash(goodsSeckill.getSellingPrice());
|
price.setPoint(goodsSeckill.getIntegral());
|
}
|
if(goodsSeckill.getCashPayment() == 1 && goodsSeckill.getPointPayment() == 0){
|
price.setCash(goodsSeckill.getSellingPrice());
|
}
|
if(goodsSeckill.getCashPayment() == 0 && goodsSeckill.getPointPayment() == 1){
|
price.setPoint(goodsSeckill.getIntegral());
|
}
|
price.setCashPayment(goodsSeckill.getCashPayment() == 1);
|
price.setPointPayment(goodsSeckill.getPointPayment() == 1);
|
}
|
return price;
|
}
|
|
}
|