jiaosong
2023-09-19 9c20f268e0628017d09427989724cdf2a8e9e8e2
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
package com.doumee.service.business.impl;
 
import com.doumee.biz.system.SystemDictDataBiz;
import com.doumee.core.constants.ResponseStatus;
import com.doumee.core.exception.BusinessException;
import com.doumee.core.model.LoginUserInfo;
import com.doumee.core.model.PageData;
import com.doumee.core.model.PageWrap;
import com.doumee.core.utils.Constants;
import com.doumee.core.utils.Utils;
import com.doumee.dao.business.BrandMapper;
import com.doumee.dao.business.model.Brand;
import com.doumee.dao.business.model.Category;
import com.doumee.dao.business.model.Goods;
import com.doumee.service.business.BrandService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
 
import java.util.Date;
import java.util.List;
import java.util.Objects;
 
/**
 * 品牌信息表Service实现
 * @author 江蹄蹄
 * @date 2023/05/12 13:58
 */
@Service
public class BrandServiceImpl implements BrandService {
 
    @Autowired
    private BrandMapper brandMapper;
 
    @Autowired
    private SystemDictDataBiz systemDictDataBiz;
    @Override
    public Integer create(Brand brand) {
        if(StringUtils.isBlank(brand.getName())){
            throw new BusinessException(ResponseStatus.BAD_REQUEST);
        }
        LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        if(brandMapper.selectCount(new QueryWrapper<Brand>()
                .eq("ISDELETED",Constants.ZERO)
                .eq(user.getType().equals(Constants.UserType.SYSTEM.getKey()),"name",brand.getName())
                .and(user.getType().equals(Constants.UserType.COMPANY.getKey()),
                        i->i.apply(" name = '"+brand.getName()+"' and type = 1 ")
                                .or().apply( "  name = '"+brand.getName()+"' and COMPANY_ID = '"+user.getCompanyId()+"' and type = 0 ")
                )
        )>0){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"【"+brand.getName()+"】已存在");
        }
        if(Constants.equalsInteger(user.getType(), Constants.UserType.SYSTEM.getKey())){
            //平台品牌
            brand.setType(Constants.ONE);
        }else{
            brand.setType(Constants.ZERO);
            brand.setCompanyId(user.getCompanyId());
        }
        brand.setStatus(Constants.ZERO);
        brand.setCreateDate(new Date());
        brand.setCreator(user.getId());
        brand.setIsdeleted(Constants.ZERO);
        brandMapper.insert(brand);
        return brand.getId();
    }
 
    @Override
    public void deleteById(Integer id) {
        Brand brand = brandMapper.selectById(id);
        if(Objects.isNull(brand)){
            throw new BusinessException(ResponseStatus.DATA_EMPTY.getCode(),"未查询到对象信息");
        }
        brand.setIsdeleted(Constants.ONE);
        brandMapper.updateById(brand);
    }
 
    @Override
    public void delete(Brand brand) {
        UpdateWrapper<Brand> deleteWrapper = new UpdateWrapper<>(brand);
        brandMapper.delete(deleteWrapper);
    }
 
    @Override
    public void deleteByIdInBatch(List<Integer> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return;
        }
        brandMapper.deleteBatchIds(ids);
    }
 
    @Override
    public void updateById(Brand brand) {
        LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
 
        Brand brand1 = brandMapper.selectOne(new QueryWrapper<Brand>()
                .eq("ISDELETED", Constants.ZERO)
                .eq(user.getType().equals(Constants.UserType.SYSTEM.getKey()), "name", brand.getName())
                .and(user.getType().equals(Constants.UserType.COMPANY.getKey()),
                        i -> i.apply(" name = '" + brand.getName() + "' and type = 1 ")
                                .or().apply("  name = '" + brand.getName() + "' and COMPANY_ID = '" + user.getCompanyId() + "' and type = 0 ")
                ).last("limit 1")
        );
 
        if(Objects.nonNull(brand1) && (!brand.getId().equals(brand.getId()))){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"【"+brand.getName()+"】已存在");
        }
        brand.setType(null);
        brand.setCompanyId(null);
        brand.setStatus(Constants.ZERO);
        brand.setEditDate(new Date());
        brand.setEditor(user.getId());
        brand.setIsdeleted(Constants.ZERO);
        brandMapper.updateById(brand);
    }
 
    @Override
    public void updateByIdInBatch(List<Brand> brands) {
        if (CollectionUtils.isEmpty(brands)) {
            return;
        }
        for (Brand brand: brands) {
            this.updateById(brand);
        }
    }
 
    @Override
    public Brand findById(Integer id) {
        Brand brand = brandMapper.selectById(id);
        if(StringUtils.isNotBlank(brand.getImgurl())){
            String prefixUrl = systemDictDataBiz.queryByCode(Constants.OSS, Constants.RESOURCE_PATH).getCode()
                    + systemDictDataBiz.queryByCode(Constants.OSS, Constants.BRAND_IMG).getCode();
            brand.setImgfullurl(prefixUrl+brand.getImgurl());
        }
        return brand;
    }
 
    @Override
    public Brand findOne(Brand brand) {
        QueryWrapper<Brand> wrapper = new QueryWrapper<>(brand);
        return brandMapper.selectOne(wrapper);
    }
 
    @Override
    public List<Brand> findList(Brand brand) {
        LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        QueryWrapper<Brand> wrapper = new QueryWrapper<>(brand);
        wrapper.lambda().eq(Brand::getIsdeleted,Constants.ZERO);
//        wrapper.lambda().eq(Brand::getStatus,Constants.ZERO);
        wrapper.lambda().orderByAsc(Brand::getSortnum);
        // 类型 0企业 1平台
        if (Constants.equalsInteger(user.getType(), Constants.UserType.SYSTEM.getKey())) {
            wrapper.lambda().eq(Brand::getType,Constants.ONE);
        }else {
            wrapper.lambda().and(i->i.eq(Brand::getCompanyId,user.getCompanyId())
                            .or().eq(Brand::getType,Constants.ONE)
            );
        }
        List<Brand> list = brandMapper.selectList(wrapper);
        String prefixUrl = systemDictDataBiz.queryByCode(Constants.OSS, Constants.RESOURCE_PATH).getCode()
                + systemDictDataBiz.queryByCode(Constants.OSS, Constants.BRAND_IMG).getCode();
        for (Brand b:list) {
            if(StringUtils.isNotBlank(b.getImgurl())){
                b.setImgfullurl(prefixUrl+b.getImgurl());
            }
        }
        return list;
    }
 
    @Override
    public PageData<Brand> findPage(PageWrap<Brand> pageWrap) {
        LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        IPage<Brand> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
        QueryWrapper<Brand> queryWrapper = new QueryWrapper<>();
        Utils.MP.blankToNull(pageWrap.getModel());
        queryWrapper.lambda().eq(Brand::getIsdeleted, Constants.ZERO);
        if (pageWrap.getModel().getName() != null) {
            queryWrapper.lambda().like(Brand::getName, pageWrap.getModel().getName());
        }
        if (pageWrap.getModel().getStatus() != null) {
            queryWrapper.lambda().eq(Brand::getStatus, pageWrap.getModel().getStatus());
        }
        if (pageWrap.getModel().getType() != null) {
            queryWrapper.lambda().eq(Brand::getType, pageWrap.getModel().getType());
        }
        // 类型 0企业 1平台
        if (Objects.isNull(user.getCompanyId())) {
            queryWrapper.lambda().eq(Brand::getType,Constants.ONE);
        }else {
            queryWrapper.lambda().and(i->i.eq(Brand::getCompanyId,user.getCompanyId())
                    .or().eq(Brand::getType,Constants.ONE)
            );
        }
        queryWrapper.lambda().orderByAsc(Brand::getSortnum);
        IPage<Brand> brandIPage = brandMapper.selectPage(page, queryWrapper);
        String prefixUrl = systemDictDataBiz.queryByCode(Constants.OSS, Constants.RESOURCE_PATH).getCode()
                + systemDictDataBiz.queryByCode(Constants.OSS, Constants.BRAND_IMG).getCode();
        brandIPage.getRecords().forEach(i->{
            if(StringUtils.isNotBlank(i.getImgurl())){
                i.setImgfullurl(prefixUrl+i.getImgurl());
            }
        });
        return PageData.from(brandIPage);
    }
 
    @Override
    public long count(Brand brand) {
        QueryWrapper<Brand> wrapper = new QueryWrapper<>(brand);
        return brandMapper.selectCount(wrapper);
    }
}