huanghongfa
2021-04-17 eb261d529b71f922cb78971ab0b329f6c323a9bf
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
package com.panzhihua.service_community.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.panzhihua.common.model.dtos.shop.ComShopGoodsDTO;
import com.panzhihua.common.model.vos.R;
import com.panzhihua.common.model.vos.shop.ComShopGoodsAttrVO;
import com.panzhihua.common.model.vos.shop.ComShopGoodsVO;
import com.panzhihua.common.model.vos.shop.ComShopStoreVO;
import com.panzhihua.service_community.dao.ComShopGoodsAttrDAO;
import com.panzhihua.common.model.dtos.shop.PageComShopGoodsDTO;
import com.panzhihua.common.model.vos.shop.ComShopStoreVO;
import com.panzhihua.common.model.vos.shop.ShopGoodsVO;
import com.panzhihua.service_community.dao.ComShopGoodsDAO;
import com.panzhihua.service_community.dao.ComShopStoreDAO;
import com.panzhihua.service_community.model.dos.ComShopGoodsAttrDO;
import com.panzhihua.service_community.model.dos.ComShopGoodsDO;
import com.panzhihua.service_community.model.dos.ComShopStoreDO;
import com.panzhihua.service_community.service.ComShopGoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
 
/**
 * @auther lyq
 * @create 2021-04-14 15:02:35
 * @describe 商品表服务实现类
 */
@Slf4j
@Service
public class ComShopGoodsServiceImpl extends ServiceImpl<ComShopGoodsDAO, ComShopGoodsDO> implements ComShopGoodsService {
    @Override
    public R saveShopGoods(ShopGoodsVO shopGoodsVO) {
        if (shopGoodsVO == null) {
            return R.ok("500", "数据为空!");
        }
        ComShopGoodsDO shopGoodsDO = new ComShopGoodsDO();
        BeanUtils.copyProperties(shopGoodsVO, shopGoodsDO);
        shopGoodsDO.setSale(0);
        this.baseMapper.insert(shopGoodsDO);
        return R.ok();
    }
 
    @Override
    public R pageStoreList(PageComShopGoodsDTO pageComShopGoodsDTO) {
        Page page = new Page<>(pageComShopGoodsDTO.getPageNum(), pageComShopGoodsDTO.getPageSize());
        IPage<ComShopStoreVO> comShopStoreVOIPage = this.baseMapper.pageShopGoods(page, pageComShopGoodsDTO);
        return R.ok(comShopStoreVOIPage);
    }
 
    @Resource
    private ComShopGoodsAttrDAO shopGoodsAttrDAO;
    @Resource
    private ComShopStoreDAO shopStoreDAO;
 
    /**
     * 分页查询商品列表
     * @param comShopGoodsDTO   请求参数
     * @return  商品列表
     */
    @Override
    public R pageShopGoods(ComShopGoodsDTO comShopGoodsDTO){
        Page page = new Page<>(comShopGoodsDTO.getPageNum(),comShopGoodsDTO.getPageSize());
        return R.ok(this.baseMapper.pageShopGoods(page,comShopGoodsDTO));
    }
 
    /**
     * 根据商品id查询商品信息
     * @param goodsId   商品id
     * @return  商品信息
     */
    @Override
    public R shopGoodsDetail(Long goodsId){
 
        //根据id查询商品信息
        ComShopGoodsDO goodsDO = this.baseMapper.selectById(goodsId);
        if(goodsDO == null || goodsDO.getDeleteStatus().equals(ComShopGoodsDO.deleteStatus.yes)){
            return R.fail(403,"商品不存在");
        }
        if(!goodsDO.getStatus().equals(ComShopGoodsDO.status.sell)){
            return R.fail(405,"商品已下架");
        }
 
        //根据商品id查询商品规格信息
        List<ComShopGoodsAttrVO> goodsAttrList = new ArrayList<>();
        List<ComShopGoodsAttrDO> goodsAttrDOS = shopGoodsAttrDAO.selectList(new QueryWrapper<ComShopGoodsAttrDO>().eq("goods_id",goodsId));
        goodsAttrDOS.forEach(attrDO->{
            ComShopGoodsAttrVO goodsAttrVO = new ComShopGoodsAttrVO();
            BeanUtils.copyProperties(attrDO,goodsAttrVO);
            goodsAttrList.add(goodsAttrVO);
        });
 
        //查询商品店铺信息
        ComShopStoreDO shopStoreDO = shopStoreDAO.selectById(goodsDO.getStoreId());
        ComShopStoreVO shopStoreVO = new ComShopStoreVO();
        BeanUtils.copyProperties(shopStoreDO,shopStoreVO);
 
        //设置值
        ComShopGoodsVO shopGoods = new ComShopGoodsVO();
        BeanUtils.copyProperties(goodsDO,shopGoods);
        shopGoods.setGoodsAttrList(goodsAttrList);
        shopGoods.setShopStoreVO(shopStoreVO);
        return R.ok(shopGoods);
    }
 
    @Override
    public R editShopGoods(Long id, ShopGoodsVO shopGoodsVO) {
        if (shopGoodsVO == null) {
            return R.ok("500", "数据为空!");
        }
        LambdaQueryWrapper<ComShopGoodsDO> query = new LambdaQueryWrapper<ComShopGoodsDO>().eq(ComShopGoodsDO::getId, id);
        ComShopGoodsDO shopGoodsDO = new ComShopGoodsDO();
        BeanUtils.copyProperties(shopGoodsVO,shopGoodsDO);
        int update = this.baseMapper.update(shopGoodsDO, query);
        return update > 0 ? R.ok() : R.fail();
    }
 
    @Override
    public R deleteShopGoods(Long[] id) {
        ComShopGoodsDO storeDO = new ComShopGoodsDO();
        storeDO.setDeleteStatus(2);
        LambdaQueryWrapper<ComShopGoodsDO> query = new LambdaQueryWrapper<ComShopGoodsDO>().eq(ComShopGoodsDO::getId, id);
        int update = this.baseMapper.update(storeDO, query);
        return update > 0 ? R.ok() : R.fail();
    }
}