zxl
5 天以前 8063ee7eee51bfe25a09428e6efc60f828b270c6
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
package cn.lili.modules.goods.serviceimpl;
 
import cn.hutool.json.JSONUtil;
import cn.lili.common.enums.ResultCode;
import cn.lili.common.exception.ServiceException;
import cn.lili.modules.goods.entity.dos.Brand;
import cn.lili.modules.goods.entity.dos.CategoryBrand;
import cn.lili.modules.goods.entity.dos.Goods;
import cn.lili.modules.goods.entity.dto.BrandPageDTO;
import cn.lili.modules.goods.entity.vos.BrandVO;
import cn.lili.modules.goods.mapper.BrandMapper;
import cn.lili.modules.goods.service.BrandService;
import cn.lili.modules.goods.service.CategoryBrandService;
import cn.lili.modules.goods.service.CategoryService;
import cn.lili.modules.goods.service.GoodsService;
import cn.lili.mybatis.util.PageUtil;
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.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
 
/**
 * 商品品牌业务层实现
 *
 * @author pikachu
 * @since 2020-02-18 16:18:56
 */
@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements BrandService {
 
    /**
     * 分类品牌绑定
     */
    @Autowired
    private CategoryBrandService categoryBrandService;
 
    @Autowired
    private CategoryService categoryService;
 
    @Autowired
    private GoodsService goodsService;
 
    @Override
    public IPage<Brand> getBrandsByPage(BrandPageDTO page) {
        LambdaQueryWrapper<Brand> queryWrapper = new LambdaQueryWrapper<>();
        if (page.getName() != null) {
            queryWrapper.like(Brand::getName, page.getName());
        }
        return this.page(PageUtil.initPage(page), queryWrapper);
    }
 
    @Override
    public List<Brand> getBrandsByCategory(String categoryId) {
        QueryWrapper<CategoryBrand> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_id", categoryId);
        List<CategoryBrand> list = categoryBrandService.list(queryWrapper);
        if (list != null && !list.isEmpty()) {
            List<String> collect = list.stream().map(CategoryBrand::getBrandId).collect(Collectors.toList());
            return this.list(new LambdaQueryWrapper<Brand>().in(Brand::getId, collect));
        }
        return new ArrayList<>();
    }
 
    @Override
    public List<Map<String, Object>> getBrandsMapsByCategory(List<String> categoryIds, String columns) {
        QueryWrapper<Brand> queryWrapper = new QueryWrapper<>();
        queryWrapper.select(columns);
        queryWrapper.in("id", categoryIds);
        return this.listMaps(queryWrapper);
    }
 
    @Override
    public boolean addBrand(BrandVO brandVO) {
 
        if (getOne(new LambdaQueryWrapper<Brand>().eq(Brand::getName, brandVO.getName())) != null) {
            throw new ServiceException(ResultCode.BRAND_NAME_EXIST_ERROR);
        }
        return this.save(brandVO);
    }
 
    @Override
    public boolean updateBrand(BrandVO brandVO) {
        this.checkExist(brandVO.getId());
        if (getOne(new LambdaQueryWrapper<Brand>().eq(Brand::getName, brandVO.getName()).ne(Brand::getId, brandVO.getId())) != null) {
            throw new ServiceException(ResultCode.BRAND_NAME_EXIST_ERROR);
        }
        return this.updateById(brandVO);
    }
 
    @Override
    public boolean brandDisable(String brandId, boolean disable) {
        Brand brand = this.checkExist(brandId);
        //如果是要禁用,则需要先判定绑定关系
        if (Boolean.TRUE.equals(disable)) {
            List<String> ids = new ArrayList<>();
            ids.add(brandId);
            checkBind(ids);
        }
        brand.setDeleteFlag(disable);
        return updateById(brand);
    }
 
    @Override
    public void deleteBrands(List<String> ids) {
        checkBind(ids);
        this.removeByIds(ids);
    }
 
 
    /**
     * 校验绑定关系
     *
     * @param brandIds 品牌Ids
     */
    private void checkBind(List<String> brandIds) {
        //分了绑定关系查询
        List<CategoryBrand> categoryBrands = categoryBrandService.getCategoryBrandListByBrandId(brandIds);
        if (!categoryBrands.isEmpty()) {
            List<String> categoryIds = categoryBrands.stream().map(CategoryBrand::getCategoryId).collect(Collectors.toList());
            throw new ServiceException(ResultCode.BRAND_USE_DISABLE_ERROR,
                    JSONUtil.toJsonStr(categoryService.getCategoryNameByIds(categoryIds)));
        }
 
        //分了商品绑定关系查询
        List<Goods> goods = goodsService.getByBrandIds(brandIds);
        if (!goods.isEmpty()) {
            List<String> goodsNames = goods.stream().map(Goods::getGoodsName).collect(Collectors.toList());
            throw new ServiceException(ResultCode.BRAND_BIND_GOODS_ERROR,
                    JSONUtil.toJsonStr(goodsNames));
        }
    }
 
    /**
     * 校验是否存在
     *
     * @param brandId 品牌ID
     * @return 品牌
     */
    private Brand checkExist(String brandId) {
        Brand brand = getById(brandId);
        if (brand == null) {
            log.error("品牌ID为" + brandId + "的品牌不存在");
            throw new ServiceException(ResultCode.BRAND_NOT_EXIST);
        }
        return brand;
    }
 
}