Administrator
5 days ago c7822fa55f611d09cec304991ca4a9275791de9d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package cc.mrbird.febs.mall.service.impl;
 
import cc.mrbird.febs.common.enumerates.DataDictionaryEnum;
import cc.mrbird.febs.common.exception.FebsException;
import cc.mrbird.febs.common.utils.AppContants;
import cc.mrbird.febs.common.utils.LoginUserUtil;
import cc.mrbird.febs.mall.conversion.MallGoodsConversion;
import cc.mrbird.febs.mall.conversion.MallMemberConversion;
import cc.mrbird.febs.mall.dto.ApiMallGoodsCommentDto;
import cc.mrbird.febs.mall.dto.MallGoodsQueryDto;
import cc.mrbird.febs.mall.entity.*;
import cc.mrbird.febs.mall.mapper.*;
import cc.mrbird.febs.mall.service.IApiMallGoodsService;
import cc.mrbird.febs.mall.vo.*;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.stereotype.Service;
 
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
 
/**
 * @author wzy
 * @date 2021-09-17
 **/
@Slf4j
@Service
@RequiredArgsConstructor
public class ApiMallGoodsServiceImpl extends ServiceImpl<MallGoodsMapper, MallGoods> implements IApiMallGoodsService {
 
    private final MallGoodsImagesMapper goodsImagesMapper;
    private final MallGoodsCommentMapper mallGoodsCommentMapper;
    private final DataDictionaryCustomMapper dataDictionaryCustomMapper;
    private final MallCarriageRuleMapper mallCarriageRuleMapper;
    private final MallCarriageRuleInfoMapper mallCarriageRuleInfoMapper;
 
 
    @Override
    public IPage<MallGoodsListVo> findMallGoodsListInPage(MallGoodsQueryDto queryDto) {
        MallMember loginUser = LoginUserUtil.getLoginUser();
        if (loginUser != null) {
            queryDto.setMemberId(loginUser.getId());
        }
        Page<MallGoodsListVo> page = new Page<>(queryDto.getPageNow(), queryDto.getPageSize());
        IPage<MallGoodsListVo> result = this.baseMapper.selectMallGoodsListQueryInPage(queryDto, page);
 
        LambdaQueryWrapper<MallGoods> goodsQuery = new LambdaQueryWrapper<>();
        goodsQuery.eq(MallGoods::getIsSale, 1)
                .eq(MallGoods::getIsNormal, 1)
                .orderByDesc(MallGoods::getVolume)
                .last("limit 10");
        List<MallGoods> mallGoods = this.baseMapper.selectList(goodsQuery);
        Map<Long, Integer> goodsMap = new HashMap<>();
        AtomicInteger rank = new AtomicInteger(1);
        mallGoods.forEach(item -> {
            goodsMap.put(item.getId(), rank.getAndIncrement());
        });
 
        result.getRecords().forEach(item -> {
            Integer goodsRank = goodsMap.get(item.getId());
            if (goodsRank != null) {
                item.setRank(goodsRank);
            }
 
        });
        return result;
    }
 
    @Override
    public List<MallGoodsListVo> findMallGoodsListNoPage(MallGoodsQueryDto queryDto) {
        return this.baseMapper.selectMallGoodsListQueryNoPage(queryDto);
    }
 
    @Override
    public ApiMallCarriageRuleVo findMallCarriageRuleById(Long id) {
        ApiMallCarriageRuleVo apiMallCarriageRuleVo = new ApiMallCarriageRuleVo();
 
        MallCarriageRule mallCarriageRule = mallCarriageRuleMapper.selectById(id);
        if(ObjectUtil.isEmpty(mallCarriageRule)){
            throw new FebsException("商品不存在");
        }
        apiMallCarriageRuleVo.setName(mallCarriageRule.getName());
 
        List<MallCarriageRuleInfo> mallCarriageRuleInfos = mallCarriageRuleInfoMapper.selectMallCarriageRuleInfoByRuleIdAndAreaAddress(id, null);
        if(CollUtil.isNotEmpty(mallCarriageRuleInfos)){
            List<ApiMallCarriageRuleInfoVo> apiMallCarriageRuleInfoVos = new ArrayList<>();
            for(MallCarriageRuleInfo mallCarriageRuleInfo : mallCarriageRuleInfos){
                ApiMallCarriageRuleInfoVo apiMallCarriageRuleInfoVo = new ApiMallCarriageRuleInfoVo();
                apiMallCarriageRuleInfoVo.setAreaAddress(mallCarriageRuleInfo.getAreaAddress());
                apiMallCarriageRuleInfoVo.setBasicPrice(mallCarriageRuleInfo.getBasicPrice());
                apiMallCarriageRuleInfoVo.setBasicCnt(mallCarriageRuleInfo.getBasicCnt());
                apiMallCarriageRuleInfoVo.setMoreCnt(mallCarriageRuleInfo.getMoreCnt());
                apiMallCarriageRuleInfoVo.setMorePrice(mallCarriageRuleInfo.getMorePrice());
                apiMallCarriageRuleInfoVos.add(apiMallCarriageRuleInfoVo);
            }
            apiMallCarriageRuleVo.setApiMallCarriageRuleInfoVos(apiMallCarriageRuleInfoVos);
        }
        return apiMallCarriageRuleVo;
    }
 
    @Override
    public MallGoodsDetailsVo findMallGoodsDetailsById(Long id) {
        MallGoods mallGoods = this.baseMapper.selectGoodsDetailById(id);
        if (mallGoods == null) {
            throw new FebsException("商品不存在");
        }
        List<String> images = goodsImagesMapper.selectGoodsImagesByGoodsId(mallGoods.getId());
        MallGoodsDetailsVo mallGoodsDetailsVo = MallGoodsConversion.INSTANCE.entityToDetailsVo(mallGoods);
        mallGoodsDetailsVo.setMainImage(mallGoods.getThumb());
 
        List<GoodsDetailsStyleVo> styles = mallGoodsDetailsVo.getStyles();
        if(ObjectUtil.isNotEmpty(styles)){
            for(GoodsDetailsStyleVo goodsDetailsStyleVo : styles){
                List<GoodsDetailsSkuVo> skus = goodsDetailsStyleVo.getSkus();
                if(CollUtil.isNotEmpty(skus) ){
                    for(GoodsDetailsSkuVo goodsDetailsSkuVo : skus){
                        if(StrUtil.isNotEmpty(goodsDetailsSkuVo.getSample())){
                            goodsDetailsSkuVo.setSampleFlag(true);
                        }else{
                            goodsDetailsSkuVo.setSampleFlag(false);
                        }
                    }
                }
            }
        }
 
        if (CollUtil.isNotEmpty(mallGoods.getStyles())) {
            Map<String, BigDecimal> stockAndVolume = this.baseMapper.selectGoodsStockAndVolume(id);
            mallGoodsDetailsVo.setStock(stockAndVolume.get("stock").intValue());
            mallGoodsDetailsVo.setVolume(stockAndVolume.get("volume").intValue());
        }
        mallGoodsDetailsVo.setImages(images);
 
        QueryWrapper<MallGoodsComment> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("goods_id",id);
        Integer commentCount = mallGoodsCommentMapper.selectCount(objectQueryWrapper);
        mallGoodsDetailsVo.setCommentCount(commentCount);
        return mallGoodsDetailsVo;
    }
 
    @Override
    public IPage<MallGoodsCommentVo> findMallGoodsCommentByGoodsId(ApiMallGoodsCommentDto queryDto) {
        Page<MallGoodsCommentVo> page = new Page<>(queryDto.getPageNow(), queryDto.getPageSize());
        MallGoodsComment mallGoodsComment = new MallGoodsComment();
        mallGoodsComment.setGoodsId(queryDto.getGoodsId());
        mallGoodsComment.setCommentType(queryDto.getCommentType());
        return this.baseMapper.selectMallGoodsCommentListQueryInPage(page,mallGoodsComment);
    }
 
    @Override
    public ApiMallGoodsDeliveryVo findDeliverySetting() {
        ApiMallGoodsDeliveryVo apiMallGoodsDeliveryVo = new ApiMallGoodsDeliveryVo();
        DataDictionaryCustom deliverySetting = dataDictionaryCustomMapper.selectDicDataByTypeAndCode(DataDictionaryEnum.HOME_DELIVERY_AMOUNT.getType(), DataDictionaryEnum.HOME_DELIVERY_AMOUNT.getCode());
        if(ObjectUtil.isNotNull(deliverySetting)){
            apiMallGoodsDeliveryVo.setHomeDeliveryServiceAmount(Double.parseDouble(deliverySetting.getValue()));
        }
        List<DataDictionaryCustom> homeDeliverySettings = dataDictionaryCustomMapper.selectDicByType(DataDictionaryEnum.FRIST_COST_LEVEL.getType());
        if(CollUtil.isNotEmpty(homeDeliverySettings)){
            List<ApiMallGoodsDeliverySettingVo> apiMallGoodsDeliverySettingVos = new ArrayList<>();
            for(DataDictionaryCustom dic : homeDeliverySettings){
                String apiMallGoodsDeliverySettingVoJson = dic.getValue();
                ApiMallGoodsDeliverySettingVo apiMallGoodsDeliverySettingVo = JSONUtil.toBean(apiMallGoodsDeliverySettingVoJson, ApiMallGoodsDeliverySettingVo.class);
                apiMallGoodsDeliverySettingVos.add(apiMallGoodsDeliverySettingVo);
            }
            apiMallGoodsDeliveryVo.setApiMallGoodsDeliverySettingVos(apiMallGoodsDeliverySettingVos);
        }
        return apiMallGoodsDeliveryVo;
    }
 
    @Override
    public MallGoodsCommentVo findMallGoodsCommentLevelByGoodsId(Long id) {
        return this.baseMapper.findMallGoodsCommentLevelByGoodsId(id);
    }
}