KKSU
2023-12-22 0e3064b8722b33ee2cf67f8fac581a8b0e04832b
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package cc.mrbird.febs.mall.service.impl;
 
import cc.mrbird.febs.common.entity.FebsResponse;
import cc.mrbird.febs.common.entity.QueryRequest;
import cc.mrbird.febs.common.utils.AppContants;
import cc.mrbird.febs.mall.entity.MallGoods;
import cc.mrbird.febs.mall.entity.MallGoodsCategory;
import cc.mrbird.febs.mall.mapper.MallGoodsCategoryMapper;
import cc.mrbird.febs.mall.mapper.MallGoodsMapper;
import cc.mrbird.febs.mall.service.IAdminMallGoodsCategoryService;
import cc.mrbird.febs.mall.vo.AdminMallGoodsCategoryTreeVo;
import cc.mrbird.febs.mall.vo.AdminMallGoodsCategoryVo;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
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.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.List;
 
@Slf4j
@Service
@RequiredArgsConstructor
@Transactional
public class AdminMallGoodsCategoryService extends ServiceImpl<MallGoodsCategoryMapper, MallGoodsCategory> implements IAdminMallGoodsCategoryService {
 
    private final MallGoodsCategoryMapper mallGoodsCategoryMapper;
 
    private final MallGoodsMapper mallGoodsMapper;
 
    @Override
    public IPage<MallGoodsCategory> getCategoryList(MallGoodsCategory mallGoodsCategory, QueryRequest request) {
        Page<MallGoodsCategory> page = new Page<>(request.getPageNum(), request.getPageSize());
        IPage<MallGoodsCategory> mallGoodsCategorys = this.baseMapper.selectCategoryListInPage(page, mallGoodsCategory);
        return mallGoodsCategorys;
    }
 
    @Override
    public FebsResponse addCategory(MallGoodsCategory mallGoodsCategory) {
        String name = mallGoodsCategory.getName();
        if(StrUtil.isEmpty(name)){
            return new FebsResponse().fail().message("分类名称不能为空");
        }
        List<MallGoodsCategory> categorys = mallGoodsCategoryMapper.selectCategoryByName(name);
        if(CollUtil.isNotEmpty(categorys)){
            return new FebsResponse().fail().message("分类名称不能重复");
        }
        Long parentIdParam = mallGoodsCategory.getParentId() == null ? 0L:mallGoodsCategory.getParentId();
        Integer isRecommendParam = mallGoodsCategory.getIsRecommend();
        if(parentIdParam > 0 && isRecommendParam == 1){
            return new FebsResponse().fail().message("子分类不能选择【是否推荐】为【是】");
        }
 
        MallGoodsCategory goodsCategory = new MallGoodsCategory();
        goodsCategory.setName(name);
        goodsCategory.setImage(mallGoodsCategory.getImage());
        if(ObjectUtil.isNotEmpty(mallGoodsCategory.getParentId())){
            Long parentId = mallGoodsCategory.getParentId();
            MallGoodsCategory mallGoodsCategoryParent = mallGoodsCategoryMapper.selectById(parentId);
            goodsCategory.setParentId(mallGoodsCategory.getParentId());
            if(StrUtil.isNotEmpty(mallGoodsCategoryParent.getParentIds())){
                goodsCategory.setParentIds(mallGoodsCategoryParent.getParentIds()+","+mallGoodsCategory.getParentId()+",");
            }else{
                goodsCategory.setParentIds(mallGoodsCategory.getParentId()+",");
            }
            goodsCategory.setIsRecommend(0);
        }else{
            goodsCategory.setParentId(0L);
            goodsCategory.setIsRecommend(mallGoodsCategory.getIsRecommend());
        }
        mallGoodsCategoryMapper.insert(goodsCategory);
        return new FebsResponse().success();
    }
 
    @Override
    public FebsResponse addAppCategory(MallGoodsCategory mallGoodsCategory) {
 
        String name = mallGoodsCategory.getName();
        if(StrUtil.isEmpty(name)){
            return new FebsResponse().fail().message("分类名称不能为空");
        }
        List<MallGoodsCategory> categorys = mallGoodsCategoryMapper.selectCategoryByName(name);
        if(CollUtil.isNotEmpty(categorys)){
            return new FebsResponse().fail().message("分类名称不能重复");
        }
//        Long parentIdParam = mallGoodsCategory.getParentId() == null ? 0L:mallGoodsCategory.getParentId();
//        Integer isRecommendParam = mallGoodsCategory.getIsRecommend();
//        if(parentIdParam > 0 && isRecommendParam == 1){
//            return new FebsResponse().fail().message("子分类不能选择【是否推荐】为【是】");
//        }
 
        MallGoodsCategory goodsCategory = new MallGoodsCategory();
        goodsCategory.setName(name);
        goodsCategory.setIsApp(AppContants.IS_APP_CATEGORY);
        goodsCategory.setImage(mallGoodsCategory.getImage());
        goodsCategory.setIndexNum(mallGoodsCategory.getIndexNum());
//        if(ObjectUtil.isNotEmpty(mallGoodsCategory.getParentId())){
//            Long parentId = mallGoodsCategory.getParentId();
//            MallGoodsCategory mallGoodsCategoryParent = mallGoodsCategoryMapper.selectById(parentId);
//            goodsCategory.setParentId(mallGoodsCategory.getParentId());
//            if(StrUtil.isNotEmpty(mallGoodsCategoryParent.getParentIds())){
//                goodsCategory.setParentIds(mallGoodsCategoryParent.getParentIds()+","+mallGoodsCategory.getParentId()+",");
//            }else{
//                goodsCategory.setParentIds(mallGoodsCategory.getParentId()+",");
//            }
//            goodsCategory.setIsRecommend(0);
//        }else{
//            goodsCategory.setParentId(0L);
//            goodsCategory.setIsRecommend(mallGoodsCategory.getIsRecommend());
//        }
        mallGoodsCategoryMapper.insert(goodsCategory);
        return new FebsResponse().success();
    }
 
    @Override
    public List<MallGoodsCategory> getCategorys(MallGoodsCategory mallGoodsCategory) {
        List<MallGoodsCategory> mallGoodsCategorys = mallGoodsCategoryMapper.getCategorys();
        return mallGoodsCategorys;
    }
 
    @Override
    public AdminMallGoodsCategoryVo getMallGoodsCategoryInfoById(long id) {
        return mallGoodsCategoryMapper.getMallGoodsCategoryInfoById(id);
    }
 
    @Override
    public List<AdminMallGoodsCategoryTreeVo> getParentCategorys() {
        List<AdminMallGoodsCategoryTreeVo> adminMallGoodsCategoryTreeVos = mallGoodsCategoryMapper.getParentCategorys();
        return adminMallGoodsCategoryTreeVos;
    }
 
    @Override
    public FebsResponse updateCategory(MallGoodsCategory mallGoodsCategoryParam) {
        String name = mallGoodsCategoryParam.getName();
        if(StrUtil.isEmpty(name)){
            return new FebsResponse().fail().message("名称不能为空");
        }
        Long parentIdParam = mallGoodsCategoryParam.getParentId() == null ? 0L:mallGoodsCategoryParam.getParentId();
        Integer isRecommendParam = mallGoodsCategoryParam.getIsRecommend();
        if(parentIdParam > 0 && isRecommendParam == 1){
            return new FebsResponse().fail().message("子分类不能选择【是否推荐】为【是】");
        }
 
        Long id = mallGoodsCategoryParam.getId();
        MallGoodsCategory mallGoodsCategory = mallGoodsCategoryMapper.selectById(id);
        mallGoodsCategory.setName(mallGoodsCategoryParam.getName());
        mallGoodsCategory.setImage(mallGoodsCategoryParam.getImage());
        mallGoodsCategory.setIndexNum(mallGoodsCategoryParam.getIndexNum());
        if(ObjectUtil.isNotEmpty(mallGoodsCategoryParam.getParentId())){
            Long parentId = mallGoodsCategoryParam.getParentId();
            MallGoodsCategory mallGoodsCategoryParent = mallGoodsCategoryMapper.selectById(parentId);
            mallGoodsCategory.setParentId(mallGoodsCategoryParam.getParentId());
            if(StrUtil.isNotEmpty(mallGoodsCategoryParent.getParentIds())){
                mallGoodsCategory.setParentIds(mallGoodsCategoryParent.getParentIds()+","+mallGoodsCategory.getParentId()+",");
            }else{
                mallGoodsCategory.setParentIds(mallGoodsCategory.getParentId()+",");
            }
            mallGoodsCategory.setIsRecommend(0);
        }else{
            mallGoodsCategory.setParentId(0L);
            mallGoodsCategory.setIsRecommend(mallGoodsCategoryParam.getIsRecommend());
        }
        mallGoodsCategoryMapper.updateById(mallGoodsCategory);
 
        return new FebsResponse().success();
    }
 
    @Override
    public FebsResponse updateAppCategory(MallGoodsCategory mallGoodsCategoryParam) {
        String name = mallGoodsCategoryParam.getName();
        if(StrUtil.isEmpty(name)){
            return new FebsResponse().fail().message("名称不能为空");
        }
//        Long parentIdParam = mallGoodsCategoryParam.getParentId() == null ? 0L:mallGoodsCategoryParam.getParentId();
//        Integer isRecommendParam = mallGoodsCategoryParam.getIsRecommend();
//        if(parentIdParam > 0 && isRecommendParam == 1){
//            return new FebsResponse().fail().message("子分类不能选择【是否推荐】为【是】");
//        }
 
        Long id = mallGoodsCategoryParam.getId();
        MallGoodsCategory mallGoodsCategory = mallGoodsCategoryMapper.selectById(id);
        mallGoodsCategory.setName(mallGoodsCategoryParam.getName());
        mallGoodsCategory.setImage(mallGoodsCategoryParam.getImage());
        mallGoodsCategory.setIndexNum(mallGoodsCategoryParam.getIndexNum());
//        if(ObjectUtil.isNotEmpty(mallGoodsCategoryParam.getParentId())){
//            Long parentId = mallGoodsCategoryParam.getParentId();
//            MallGoodsCategory mallGoodsCategoryParent = mallGoodsCategoryMapper.selectById(parentId);
//            mallGoodsCategory.setParentId(mallGoodsCategoryParam.getParentId());
//            if(StrUtil.isNotEmpty(mallGoodsCategoryParent.getParentIds())){
//                mallGoodsCategory.setParentIds(mallGoodsCategoryParent.getParentIds()+","+mallGoodsCategory.getParentId()+",");
//            }else{
//                mallGoodsCategory.setParentIds(mallGoodsCategory.getParentId()+",");
//            }
//            mallGoodsCategory.setIsRecommend(0);
//        }else{
//            mallGoodsCategory.setParentId(0L);
//            mallGoodsCategory.setIsRecommend(mallGoodsCategoryParam.getIsRecommend());
//        }
        mallGoodsCategoryMapper.updateById(mallGoodsCategory);
 
        return new FebsResponse().success();
    }
 
    @Override
    public FebsResponse delCategary(Long id) {
        MallGoodsCategory mallGoodsCategory = mallGoodsCategoryMapper.selectById(id);
        if(ObjectUtil.isEmpty(mallGoodsCategory)){
            return new FebsResponse().fail().message("系统繁忙,请刷新页面重试");
        }
        List<MallGoodsCategory> childCategarys = mallGoodsCategoryMapper.selectChildCategaryById(id);
        if(CollUtil.isNotEmpty(childCategarys)){
            for(MallGoodsCategory childCategary : childCategarys){
                Long childCategaryId = childCategary.getId();
                List<MallGoods> mallChildGoods = mallGoodsMapper.selectMallGoodsByCategaryId(childCategaryId);
                if(CollUtil.isNotEmpty(mallChildGoods)){
                    return new FebsResponse().fail().message("该分类下的子类【"+childCategary.getName()+"】还有商品,请先删除商品或者修改商品分类");
                }
            }
        }
 
        if(CollUtil.isNotEmpty(childCategarys)){
            return new FebsResponse().fail().message("该分类下还有子类,请先删除子类");
        }
 
        List<MallGoods> mallGoods = mallGoodsMapper.selectMallGoodsByCategaryId(id);
        if(CollUtil.isNotEmpty(mallGoods)){
            return new FebsResponse().fail().message("该分类下还有商品,请先删除商品或者修改商品分类");
        }
        mallGoodsCategoryMapper.deleteById(mallGoodsCategory);
        return new FebsResponse().success();
    }
 
    @Override
    public FebsResponse delAppCategary(Long id) {
        MallGoodsCategory mallGoodsCategory = mallGoodsCategoryMapper.selectById(id);
        if(ObjectUtil.isEmpty(mallGoodsCategory)){
            return new FebsResponse().fail().message("系统繁忙,请刷新页面重试");
        }
        List<MallGoodsCategory> childCategarys = mallGoodsCategoryMapper.selectChildCategaryById(id);
        if(CollUtil.isNotEmpty(childCategarys)){
            for(MallGoodsCategory childCategary : childCategarys){
                Long childCategaryId = childCategary.getId();
                List<MallGoods> mallChildGoods = mallGoodsMapper.selectMallGoodsByCategaryId(childCategaryId);
                if(CollUtil.isNotEmpty(mallChildGoods)){
                    return new FebsResponse().fail().message("该分类下的子类【"+childCategary.getName()+"】还有商品,请先删除商品或者修改商品分类");
                }
            }
        }
 
        if(CollUtil.isNotEmpty(childCategarys)){
            return new FebsResponse().fail().message("该分类下还有子类,请先删除子类");
        }
 
        List<MallGoods> mallGoods = mallGoodsMapper.selectMallGoodsByCategaryId(id);
        if(CollUtil.isNotEmpty(mallGoods)){
            return new FebsResponse().fail().message("该分类下还有商品,请先删除商品或者修改商品分类");
        }
        mallGoodsCategoryMapper.deleteById(mallGoodsCategory);
        return new FebsResponse().success();
    }
 
    @Override
    public List<AdminMallGoodsCategoryTreeVo> getAllCategorys() {
        List<AdminMallGoodsCategoryTreeVo> adminMallGoodsCategoryTreeVos = mallGoodsCategoryMapper.getAllCategorys();
        return adminMallGoodsCategoryTreeVos;
    }
 
}