1
phpcjl
2024-12-18 83751617ac342e4168cb714106bc009e3f25f99c
ruoyi-service/ruoyi-other/src/main/java/com/ruoyi/other/service/impl/GoodsServiceImpl.java
@@ -21,14 +21,19 @@
import com.ruoyi.other.service.*;
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.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
 * <p>
@@ -167,48 +172,23 @@
            if (CollectionUtils.isEmpty(goodsShopList)){
                throw new NullPointerException("请选择指定门店");
            }
            for (GoodsShop goodsShop : goodsShopList) {
                goodsShop.setGoodsId(goods.getId());
                goodsShop.setId(null);
            }
            goodsShopService.saveBatch(goodsShopList);
            saveGoodsShopList(goodsShopList, goods.getId());
        }
        // 指定用户
        List<GoodsAppUser> goodsAppUserList = goods.getGoodsAppUserList();
        if (!CollectionUtils.isEmpty(goodsAppUserList)){
            for (GoodsAppUser goodsAppUser : goodsAppUserList) {
                goodsAppUser.setId(null );
                goodsAppUser.setGoodsId(goods.getId());
            }
            goodsAppUserService.saveBatch(goodsAppUserList);
            saveGoodsAppUserList(goodsAppUserList, goods.getId());
        }
        // 会员价格配置
        List<GoodsVip> goodsVipList = goods.getGoodsVipList();
        for (GoodsVip goodsVip : goodsVipList) {
            goodsVip.setId(null);
            goodsVip.setGoodsId(goods.getId());
        }
        goodsVipService.saveBatch(goodsVipList);
        saveGoodsVipList(goodsVipList, goods.getId());
        // 特殊地区售价设置
        List<GoodsArea> goodsAreaList = goods.getGoodsAreaList();
        if (!CollectionUtils.isEmpty(goodsAreaList)){
            for (GoodsArea goodsArea : goodsAreaList) {
                List<GoodsArea> goodsAreaList1 = goodsArea.getGoodsAreaList();
                for (GoodsArea area : goodsAreaList1) {
                    area.setGoodsId(goods.getId());
                    area.setProvince(goodsArea.getProvince());
                    area.setCity(goodsArea.getCity());
                    area.setDistricts(goodsArea.getDistricts());
                    area.setProvinceCode(goodsArea.getProvinceCode());
                    area.setCityCode(goodsArea.getCityCode());
                    area.setDistrictsCode(goodsArea.getDistrictsCode());
                    goodsAreaList1.add(area);
                }
                goodsAreaService.saveBatch(goodsAreaList1);
            }
            saveGoodsAreaList(goodsAreaList, goods.getId());
        }
    }
@@ -218,39 +198,81 @@
        goodsMapper.updateById(goods);
        // 指定门店
        List<GoodsShop> goodsShopList = goods.getGoodsShopList();
        goodsShopService.updateBatchById(goodsShopList);
        saveGoodsShopList(goodsShopList, goods.getId());
        // 指定用户
        List<GoodsAppUser> goodsAppUserList = goods.getGoodsAppUserList();
        goodsAppUserService.updateBatchById(goodsAppUserList);
        saveGoodsAppUserList(goodsAppUserList, goods.getId());
        // 会员价格配置
        List<GoodsVip> goodsVipList = goods.getGoodsVipList();
        goodsVipService.updateBatchById(goodsVipList);
        saveGoodsVipList(goodsVipList, goods.getId());
        // 特殊地区售价设置
        List<GoodsArea> goodsAreaList = goods.getGoodsAreaList();
        saveGoodsAreaList(goodsAreaList, goods.getId());
    }
    private void saveGoodsAreaList(List<GoodsArea> goodsAreaList, Integer id) {
        goodsAreaService.remove(new LambdaQueryWrapper<GoodsArea>()
                .eq(GoodsArea::getGoodsId, id));
        if (!CollectionUtils.isEmpty(goodsAreaList)){
            for (GoodsArea goodsArea : goodsAreaList) {
                List<GoodsArea> goodsAreaList1 = goodsArea.getGoodsAreaList();
                for (GoodsArea area : goodsAreaList1) {
                    area.setGoodsId(goods.getId());
                    area.setGoodsId(id);
                    area.setProvince(goodsArea.getProvince());
                    area.setCity(goodsArea.getCity());
                    area.setDistricts(goodsArea.getDistricts());
                    area.setProvinceCode(goodsArea.getProvinceCode());
                    area.setCityCode(goodsArea.getCityCode());
                    area.setDistrictsCode(goodsArea.getDistrictsCode());
                    goodsAreaList1.add(area);
                }
                goodsAreaService.updateBatchById(goodsAreaList1);
                goodsAreaService.saveBatch(goodsAreaList1);
            }
        }
    }
    private void saveGoodsVipList(List<GoodsVip> goodsVipList, Integer id) {
        goodsVipService.remove(new LambdaQueryWrapper<GoodsVip>()
                .eq(GoodsVip::getGoodsId, id));
        goodsVipList.forEach(item -> {
            item.setId(null);
            item.setGoodsId(id);
        });
        goodsVipService.saveBatch(goodsVipList);
    }
    private void saveGoodsAppUserList(List<GoodsAppUser> goodsAppUserList, Integer id) {
        goodsAppUserService.remove(new LambdaQueryWrapper<GoodsAppUser>()
                .eq(GoodsAppUser::getGoodsId, id));
        if (!CollectionUtils.isEmpty(goodsAppUserList)){
            for (GoodsAppUser goodsAppUser : goodsAppUserList) {
                goodsAppUser.setId(null );
                goodsAppUser.setGoodsId(id);
            }
        }
        goodsAppUserService.saveBatch(goodsAppUserList);
    }
    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));
@@ -267,11 +289,30 @@
        // 指定用户
        List<GoodsAppUser> goodsAppUserList = goodsAppUserService.list(new LambdaQueryWrapper<GoodsAppUser>()
                .eq(GoodsAppUser::getGoodsId, goodsId));
        List<Long> userIds = goodsAppUserList.stream().map(GoodsAppUser::getAppUserId).collect(Collectors.toList());
        List<AppUser> appUsers = appUserClient.listByIds(userIds);
        for (GoodsAppUser goodsAppUser : goodsAppUserList) {
            appUsers.stream().filter(u -> u.getId().equals(goodsAppUser.getAppUserId())).findFirst().ifPresent( u -> {
                goodsAppUser.setUserName(u.getName());
                goodsAppUser.setPhone(u.getPhone());
                VipSetting vipSetting = vipSettingService.getById(u.getVipId());
                if (vipSetting != null){
                    goodsAppUser.setVipName(vipSetting.getVipName());
                    goodsAppUser.setVipId(u.getVipId());
                }
            });
        }
        goods.setGoodsAppUserList(goodsAppUserList);
        // 会员价格
        List<GoodsVip> goodsVipList = goodsVipService.list(new LambdaQueryWrapper<GoodsVip>()
                .eq(GoodsVip::getGoodsId, goodsId));
        List<VipSetting> vipSettings = getVipSettings(goodsVipList.stream().map(GoodsVip::getVip));
        goodsVipList.forEach(goodsArea -> vipSettings.stream()
                .filter(vipSetting -> vipSetting.getId().equals(goodsArea.getVip()))
                .findFirst().ifPresent( vipSetting -> goodsArea.setVipName(vipSetting.getVipName())));
        goods.setGoodsVipList(goodsVipList);
        // 特殊地区售价设置
@@ -284,19 +325,39 @@
        List<GoodsArea> goodsAreaList1 = new ArrayList<>();
        listMap.forEach((s, goodsAreas) -> {
            GoodsArea goodsArea = goodsAreas.get(0);
            GoodsArea goodsArea1 = new GoodsArea();
            goodsArea1.setProvinceCode(goodsArea.getProvinceCode());
            goodsArea1.setCityCode(goodsArea.getCityCode());
            goodsArea1.setDistrictsCode(goodsArea.getDistrictsCode());
            goodsArea1.setProvince(goodsArea.getProvince());
            goodsArea1.setCity(goodsArea.getCity());
            goodsArea1.setDistricts(goodsArea.getDistricts());
            goodsArea1.setGoodsAreaList(goodsAreas);
            List<VipSetting> vipSettings1 = getVipSettings(goodsAreas.stream().map(GoodsArea::getVip));
            goodsAreas.forEach(goodsArea -> vipSettings1.stream()
                    .filter(vipSetting -> vipSetting.getId().equals(goodsArea.getVip()))
                    .findFirst().ifPresent( vipSetting -> goodsArea.setVipName(vipSetting.getVipName())));
            GoodsArea goodsArea1 = getGoodsArea(goodsAreas);
            goodsAreaList1.add(goodsArea1);
        });
        goods.setGoodsAreaList(goodsAreaList1);
        return goods;
    }
    private List<VipSetting> getVipSettings(Stream<Integer> goodsVipList) {
        List<Integer> vipIds = goodsVipList.collect(Collectors.toList());
        if (CollectionUtils.isEmpty(vipIds)){
            return new ArrayList<>();
        }
        return vipSettingService.listByIds(vipIds);
    }
    @NotNull
    private static GoodsArea getGoodsArea(List<GoodsArea> goodsAreas) {
        GoodsArea goodsArea = goodsAreas.get(0);
        GoodsArea goodsArea1 = new GoodsArea();
        goodsArea1.setProvinceCode(goodsArea.getProvinceCode());
        goodsArea1.setCityCode(goodsArea.getCityCode());
        goodsArea1.setDistrictsCode(goodsArea.getDistrictsCode());
        goodsArea1.setProvince(goodsArea.getProvince());
        goodsArea1.setCity(goodsArea.getCity());
        goodsArea1.setDistricts(goodsArea.getDistricts());
        goodsArea1.setGoodsAreaList(goodsAreas);
        return goodsArea1;
    }
}