jiangping
2023-09-15 b459b874c062b1124200f8a591b86d0558c3ffce
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
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.PinYinUtil;
import com.doumee.core.utils.Utils;
import com.doumee.dao.business.*;
import com.doumee.dao.business.join.BaseGoodsJoinMapper;
import com.doumee.dao.business.model.*;
import com.doumee.dao.business.model.dto.BaseGoodsCreateOrUpdateRequest;
import com.doumee.dao.business.model.dto.BaseGoodsDTO;
import com.doumee.dao.business.model.dto.BaseGoodsParamCreatRequest;
import com.doumee.service.business.BaseGoodsService;
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 com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
 
import java.util.Date;
import java.util.List;
import java.util.Objects;
 
/**
 * 素材库-商品信息表Service实现
 * @author 江蹄蹄
 * @date 2023/09/07 11:41
 */
@Service
public class BaseGoodsServiceImpl implements BaseGoodsService {
 
    @Autowired
    private BaseGoodsMapper baseGoodsMapper;
 
 
    @Autowired
    private BaseGoodsJoinMapper baseGoodsJoinMapper;
 
    @Autowired
    private BaseGoodsParamMapper baseGoodsParamMapper;
 
 
    @Autowired
    private MultifileMapper multifileMapper;
 
    @Autowired
    @Lazy
    private SystemDictDataBiz systemDictDataBiz;
 
    @Autowired
    private BrandMapper brandMapper;
 
    @Autowired
    private GoodsMapper goodsMapper;
 
    @Override
    public Integer create(BaseGoodsCreateOrUpdateRequest baseGoods) {
        LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        if(baseGoodsMapper.selectCount(new QueryWrapper<BaseGoods>().eq("ISDELETED", Constants.ZERO).eq("name",baseGoods.getName()))>0){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"【"+baseGoods.getName()+"】已存在");
        }
 
        Integer integer = baseGoodsMapper.selectCount(new QueryWrapper<BaseGoods>().eq("ISDELETED", Constants.ZERO));
        BaseGoods insert = new BaseGoods();
        insert.setCreator(user.getId());
        insert.setCreateDate(new Date());
        insert.setEditor(user.getId());
        insert.setEditDate(new Date());
        insert.setIsdeleted(Constants.ZERO);
        insert.setRemark(baseGoods.getRemark());
        insert.setName(baseGoods.getName());
        insert.setStatus(Constants.ZERO);
        insert.setSortnum(integer+Constants.ONE);
        insert.setImgurl(baseGoods.getImgurl());
        insert.setCategoryId(baseGoods.getCategoryId());
        insert.setBrandId(baseGoods.getBrandId());
        insert.setZdPrice(baseGoods.getZdPrice());
        insert.setPrice(baseGoods.getPrice());
        insert.setContent(baseGoods.getContent());
        insert.setPinyin(PinYinUtil.getFullSpell(insert.getName()));
        insert.setShortPinyin(PinYinUtil.getFirstSpell(insert.getName()));
        baseGoodsMapper.insert(insert);
 
 
        List<Multifile> multifileList = baseGoods.getMultifileList();
        if(!Objects.isNull(multifileList)&&multifileList.size()>Constants.ZERO){
            for (int i = 0; i < multifileList.size(); i++) {
                Multifile multifile = multifileList.get(i);
                multifile.setCreator(user.getId());
                multifile.setCreateDate(new Date());
                multifile.setIsdeleted(Constants.ZERO);
                multifile.setSortnum(i+Constants.ONE);
                multifile.setObjId(insert.getId());
                multifile.setType(Constants.ZERO);
                multifile.setObjType(Constants.ONE);
                multifileMapper.insert(multifile);
            }
        }
 
 
        List<BaseGoodsParamCreatRequest> goodsParamList = baseGoods.getBaseGoodsParamList();
        if(!Objects.isNull(goodsParamList)&&goodsParamList.size()>0){
            for (int i = 0; i < goodsParamList.size(); i++) {
                BaseGoodsParamCreatRequest param = goodsParamList.get(i);
                BaseGoodsParam baseGoodsParam = new BaseGoodsParam();
                baseGoodsParam.setCreator(user.getId());
                baseGoodsParam.setCreateDate(new Date());
                baseGoodsParam.setEditor(user.getId());
                baseGoodsParam.setEditDate(new Date());
                baseGoodsParam.setIsdeleted(Constants.ZERO);
                baseGoodsParam.setName(param.getName());
                baseGoodsParam.setRemark(param.getRemark());
                baseGoodsParam.setStatus(Constants.ZERO);
                baseGoodsParam.setSortnum(i+Constants.ONE);
                baseGoodsParam.setPramaId(param.getPramaId());
                baseGoodsParam.setVal(param.getVal());
                baseGoodsParam.setGoodsId(insert.getId());
                baseGoodsParamMapper.insert(baseGoodsParam);
            }
        }
 
        return insert.getId();
    }
 
    @Override
    public void deleteById(Integer id) {
        baseGoodsMapper.deleteById(id);
    }
 
    @Override
    public void delete(BaseGoods baseGoods) {
        UpdateWrapper<BaseGoods> deleteWrapper = new UpdateWrapper<>(baseGoods);
        baseGoodsMapper.delete(deleteWrapper);
    }
 
    @Override
    public void deleteByIdInBatch(List<Integer> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return;
        }
        baseGoodsMapper.deleteBatchIds(ids);
    }
 
 
    @Transactional(rollbackFor = {Exception.class,BusinessException.class})
    @Override
    public void update(BaseGoodsCreateOrUpdateRequest baseGoods) {
 
        LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        if(baseGoodsMapper.selectCount(new QueryWrapper<BaseGoods>()
                .eq("ISDELETED",Constants.ZERO).ne("id",baseGoods.getId())
                .eq("name",baseGoods.getName()))>0){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"【"+baseGoods.getName()+"】已存在");
        }
 
        UpdateWrapper<BaseGoods> wrapper = new UpdateWrapper<>();
        wrapper.lambda()
                .eq(BaseGoods::getId,baseGoods.getId())
                .set(BaseGoods::getName,baseGoods.getName())
                .set(BaseGoods::getBrandId,baseGoods.getBrandId())
                .set(BaseGoods::getCategoryId,baseGoods.getCategoryId())
                .set(BaseGoods::getImgurl,baseGoods.getImgurl())
                .set(BaseGoods::getPrice,baseGoods.getPrice())
                .set(BaseGoods::getZdPrice,baseGoods.getZdPrice());
        baseGoodsMapper.update(null,wrapper);
 
        multifileMapper.delete(new QueryWrapper<Multifile>().eq("OBJ_ID",baseGoods.getId())
                                                            .eq("OBJ_TYPE",Constants.ONE));
        List<Multifile> multifileList = baseGoods.getMultifileList();
        if(!Objects.isNull(multifileList)&&multifileList.size()>Constants.ZERO){
            for (int i = 0; i < multifileList.size(); i++) {
                Multifile multifile = multifileList.get(i);
                multifile.setCreator(user.getId());
                multifile.setCreateDate(new Date());
                multifile.setIsdeleted(Constants.ZERO);
                multifile.setSortnum(i+Constants.ONE);
                multifile.setObjId(baseGoods.getId());
                multifile.setType(Constants.ZERO);
                multifile.setObjType(Constants.ONE);
                multifileMapper.insert(multifile);
            }
        }
        baseGoodsParamMapper.delete(new QueryWrapper<BaseGoodsParam>().eq("GOODS_ID",baseGoods.getId()));
        List<BaseGoodsParamCreatRequest> goodsParamList = baseGoods.getBaseGoodsParamList();
        if(!Objects.isNull(goodsParamList)&&goodsParamList.size()>0){
            for (int i = 0; i < goodsParamList.size(); i++) {
                BaseGoodsParamCreatRequest param = goodsParamList.get(i);
                BaseGoodsParam baseGoodsParam = new BaseGoodsParam();
                baseGoodsParam.setCreator(user.getId());
                baseGoodsParam.setCreateDate(new Date());
                baseGoodsParam.setEditor(user.getId());
                baseGoodsParam.setEditDate(new Date());
                baseGoodsParam.setIsdeleted(Constants.ZERO);
                baseGoodsParam.setName(param.getName());
                baseGoodsParam.setRemark(param.getRemark());
                baseGoodsParam.setStatus(Constants.ZERO);
                baseGoodsParam.setSortnum(i+Constants.ONE);
                baseGoodsParam.setPramaId(param.getPramaId());
                baseGoodsParam.setVal(param.getVal());
                baseGoodsParam.setGoodsId(baseGoods.getId());
                baseGoodsParamMapper.insert(baseGoodsParam);
            }
        }
    }
 
    @Transactional(rollbackFor = {Exception.class,BusinessException.class})
    @Override
    public void updateStatusByIds(List<Integer> idList, Integer status) {
        if (CollectionUtils.isEmpty(idList)){
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"商品状态不能为空");
        }
        idList.forEach(s->updateStatusById(s,status));
 
    }
 
    @Transactional(rollbackFor = {Exception.class,BusinessException.class})
    @Override
    public void updateStatusById(Integer id, Integer status) {
 
        if (Objects.isNull(status)){
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"商品状态不能为空");
        }
        if (Constants.equalsInteger(status,Constants.ZERO)){
            BaseGoods baseGoods = new BaseGoods();
            baseGoods.setId(id);
            baseGoods.setStatus(status);
            baseGoodsMapper.updateById(baseGoods);
        }else if (Constants.equalsInteger(status,Constants.ONE)){
            BaseGoods baseGoods = new BaseGoods();
            baseGoods.setId(id);
            baseGoods.setStatus(status);
            baseGoodsMapper.updateById(baseGoods);
 
            UpdateWrapper<Goods> goodsUpdate = new UpdateWrapper<>();
            goodsUpdate.lambda()
                        .eq(Goods::getType,Constants.ONE)
                        .eq(Goods::getGoodsId,id)
                        .set(Goods::getStatus,Constants.ONE);
            goodsMapper.update(null,goodsUpdate);
        }
    }
 
    @Override
    public void updateById(BaseGoods baseGoods) {
        baseGoodsMapper.updateById(baseGoods);
    }
 
    @Override
    public void updateByIdInBatch(List<BaseGoods> baseGoodss) {
        if (CollectionUtils.isEmpty(baseGoodss)) {
            return;
        }
        for (BaseGoods baseGoods: baseGoodss) {
            this.updateById(baseGoods);
        }
    }
 
    @Override
    public BaseGoods findById(Integer id) {
        return baseGoodsMapper.selectById(id);
    }
 
    @Override
    public BaseGoodsDTO findByIdBaseGoods(Integer id) {
 
        MPJLambdaWrapper<BaseGoods> queryWrapper = new MPJLambdaWrapper<>();
 
        queryWrapper.leftJoin(Brand.class,Brand::getId,BaseGoods::getBrandId)
                .leftJoin(BaseCategory.class,BaseCategory::getId,BaseGoods::getBaseDataId)
                .selectAll(BaseGoods.class)
                .selectAs(Brand::getName,BaseGoodsDTO::getBrandName)
                .selectAs(BaseCategory::getSortnum,BaseGoodsDTO::getCategoryName)
                .eq(BaseGoods::getId,id);
 
        BaseGoodsDTO baseGoodsDTO = baseGoodsJoinMapper.selectJoinOne(BaseGoodsDTO.class, queryWrapper);
        String prefixUrl = systemDictDataBiz.queryByCode(Constants.SYSTEM, Constants.FILE_DIR).getCode()
                + systemDictDataBiz.queryByCode(Constants.SYSTEM, Constants.PROJECTS).getCode();
        baseGoodsDTO.setImgfullurl(prefixUrl+baseGoodsDTO.getImgurl());
 
        List<Multifile> multifiles = multifileMapper.selectList(new QueryWrapper<Multifile>()
                .eq("OBJ_ID", id)
                .eq("OBJ_TYPE", Constants.ONE));
        if (!CollectionUtils.isEmpty(multifiles)){
            multifiles.forEach(s->s.setFilefullurl(prefixUrl+s.getFileurl()));
        }
        baseGoodsDTO.setMultifileList(multifiles);
 
        QueryWrapper<BaseGoodsParam> baseGoodsParamQuery = new QueryWrapper<>();
        baseGoodsParamQuery.select("base_goods_param.*,(select name from base_cate_param bcp where (bcp.id = base_goods_param.PRAMA_ID)) as paramName")
                            .lambda()
                            .eq(BaseGoodsParam::getGoodsId,id)
                            .orderByAsc(BaseGoodsParam::getSortnum);
        List<BaseGoodsParam> baseGoodsParams = baseGoodsParamMapper.selectList(baseGoodsParamQuery);
        baseGoodsDTO.setBaseGoodsParamList(baseGoodsParams);
        return baseGoodsDTO;
 
    }
 
    @Override
    public BaseGoods findOne(BaseGoods baseGoods) {
        QueryWrapper<BaseGoods> wrapper = new QueryWrapper<>(baseGoods);
        return baseGoodsMapper.selectOne(wrapper);
    }
 
    @Override
    public List<BaseGoods> findList(BaseGoods baseGoods) {
        QueryWrapper<BaseGoods> wrapper = new QueryWrapper<>(baseGoods);
        return baseGoodsMapper.selectList(wrapper);
    }
 
    @Override
    public PageData<BaseGoods> findPage(PageWrap<BaseGoods> pageWrap) {
        pageWrap.getModel().setIsdeleted(Constants.ZERO);
        IPage<BaseGoods> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
        MPJLambdaWrapper<BaseGoods> queryWrapper = new MPJLambdaWrapper<>();
        Utils.MP.blankToNull(pageWrap.getModel());
        queryWrapper.leftJoin(Brand.class,Brand::getId,BaseGoods::getBrandId)
                    .leftJoin(BaseCategory.class,BaseCategory::getId,BaseGoods::getCategoryId)
                    .selectAll(BaseGoods.class)
                    .selectAs(Brand::getName,BaseGoods::getBrandName)
                    .selectAs(BaseCategory::getName,BaseGoods::getCategoryName)
                    .eq(BaseGoods::getIsdeleted, Constants.ZERO)
                    .like(StringUtils.isNotBlank(pageWrap.getModel().getName()), BaseGoods::getName,pageWrap.getModel().getName())
                    .eq(pageWrap.getModel().getId()!=null,BaseGoods::getId, pageWrap.getModel().getId())
                    .eq(pageWrap.getModel().getCategoryId()!=null,BaseGoods::getCategoryId, pageWrap.getModel().getCategoryId())
                    .eq(pageWrap.getModel().getBrandId()!=null,BaseGoods::getBrandId, pageWrap.getModel().getBrandId())
                    .eq(pageWrap.getModel().getStatus()!=null,BaseGoods::getStatus, pageWrap.getModel().getStatus());
 
        queryWrapper.orderByDesc(Goods::getId);
        PageData<BaseGoods> pageData =PageData.from(baseGoodsJoinMapper.selectJoinPage(page,BaseGoods.class,queryWrapper));
        String prefixUrl = systemDictDataBiz.queryByCode(Constants.OSS, Constants.RESOURCE_PATH).getCode()
                + systemDictDataBiz.queryByCode(Constants.OSS, Constants.GOODS_IMG).getCode();
        pageData.getRecords().forEach(i->{
            i.setFullImgUrl(prefixUrl + i.getImgurl());
        });
        return pageData;
    }
 
    @Override
    public long count(BaseGoods baseGoods) {
        QueryWrapper<BaseGoods> wrapper = new QueryWrapper<>(baseGoods);
        return baseGoodsMapper.selectCount(wrapper);
    }
}