mitao
2025-02-21 31573d6180d15ef65ed0df9c2732495f40b12663
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
package com.panzhihua.service_community.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.community.convenient.ConvenientGoodsCategoryDTO;
import com.panzhihua.common.model.dtos.community.convenient.ConvenientServiceCategoryDTO;
import com.panzhihua.common.model.dtos.community.convenient.PageConvenientGoodsCategoryDTO;
import com.panzhihua.common.model.dtos.community.convenient.PageConvenientServiceCategoryDTO;
import com.panzhihua.common.model.vos.R;
import com.panzhihua.common.model.vos.community.convenient.ConvenientGoodsCategoryVO;
import com.panzhihua.common.model.vos.community.convenient.ConvenientServiceCategoryVO;
import com.panzhihua.service_community.dao.ConvenientGoodsCategoryDAO;
import com.panzhihua.service_community.dao.ConvenientServiceCategoryDAO;
import com.panzhihua.service_community.model.dos.ConvenientGoodsCategoryDO;
import com.panzhihua.service_community.model.dos.ConvenientServiceCategoryDO;
import com.panzhihua.service_community.service.ConvenientGoodsCategoryService;
import com.panzhihua.service_community.service.ConvenientServiceCategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
import static java.util.Objects.isNull;
 
/**
 * @title: ConvenientGoodsCategoryServiceImpl
 * @projectName: 成都呐喊信息技术有限公司-智慧社区项目
 * @description: 商品分类服务实现类
 * @author: yh
 * @date: 2022-10-21 09:36:09
 */
@Service
public class ConvenientGoodsCategoryServiceImpl extends ServiceImpl<ConvenientGoodsCategoryDAO, ConvenientGoodsCategoryDO>
        implements ConvenientGoodsCategoryService {
 
    @Override
    public R addGoodsCategory(ConvenientGoodsCategoryDTO convenientGoodsCategoryDTO) {
        ConvenientGoodsCategoryDO convenientGoodsCategoryDO = new ConvenientGoodsCategoryDO();
        BeanUtils.copyProperties(convenientGoodsCategoryDTO, convenientGoodsCategoryDO);
        convenientGoodsCategoryDO.setCreatedAt(new Date());
        int result = this.baseMapper.insert(convenientGoodsCategoryDO);
        if (result > 0) {
            return R.ok();
        }
        return R.fail("添加失败");
    }
 
    @Override
    public R putGoodsCategory(ConvenientGoodsCategoryDTO convenientGoodsCategoryDTO) {
        ConvenientGoodsCategoryDO convenientGoodsCategoryDO = this.baseMapper.selectById(convenientGoodsCategoryDTO.getId());
        if (isNull(convenientGoodsCategoryDO)) {
            return R.fail("分类id不存在");
        }
        BeanUtils.copyProperties(convenientGoodsCategoryDTO, convenientGoodsCategoryDO);
        int result = this.baseMapper.updateById(convenientGoodsCategoryDO);
        if (result > 0) {
            return R.ok();
        }
        return R.fail("更新失败");
    }
 
    @Override
    public R deleteGoodsCategoryById(Long categoryId, Long operator) {
        if (isNull(categoryId)) {
            return R.fail("分类id不能为空");
        }
        ConvenientGoodsCategoryDO convenientGoodsCategoryDO = this.baseMapper.selectById(categoryId);
        if (isNull(convenientGoodsCategoryDO)) {
            return R.fail("分类id不存在");
        }
        int count = this.baseMapper.checkCategoryIsUsing(categoryId);
        if (count > 0) {
            return R.fail("该分类名称已被引用,无法删除!");
        }
        convenientGoodsCategoryDO.setIsDel(true);
        convenientGoodsCategoryDO.setUpdatedBy(operator);
        int result = this.baseMapper.updateById(convenientGoodsCategoryDO);
        if (result > 0) {
            return R.ok();
        }
        return R.fail("删除失败");
    }
 
    @Override
    public R getGoodsCategoryById(Long goodsId) {
        if (isNull(goodsId)) {
            return R.fail("分类id不能为空");
        }
        ConvenientGoodsCategoryDO convenientGoodsCategoryDO = this.baseMapper.selectById(goodsId);
        if (isNull(convenientGoodsCategoryDO)) {
            return R.fail("分类id不存在");
        }
        ConvenientGoodsCategoryVO convenientGoodsCategoryVO = new ConvenientGoodsCategoryVO();
        BeanUtils.copyProperties(convenientGoodsCategoryDO, convenientGoodsCategoryVO);
        return R.ok(convenientGoodsCategoryVO);
    }
 
    @Override
    public R pageGoodsCategory(PageConvenientGoodsCategoryDTO pageConvenientGoodsCategoryDTO) {
        Page page = new Page<>();
        page.setSize(pageConvenientGoodsCategoryDTO.getPageSize());
        page.setCurrent(pageConvenientGoodsCategoryDTO.getPageNum());
        IPage<ConvenientGoodsCategoryVO> iPage = this.baseMapper.pageGoodsCategory(page, pageConvenientGoodsCategoryDTO);
        return R.ok(iPage);
    }
 
    @Override
    public R getAllGoodsCategories() {
        List<ConvenientGoodsCategoryVO> categoryVOList = new ArrayList<>();
        List<ConvenientGoodsCategoryDO> categoryDOS = this.baseMapper.selectList(new QueryWrapper<ConvenientGoodsCategoryDO>()
                .lambda().eq(ConvenientGoodsCategoryDO::getIsDel,0).orderByDesc(ConvenientGoodsCategoryDO::getWeight));
        if (!ObjectUtils.isEmpty(categoryDOS)) {
            categoryDOS.forEach(categoryDO -> {
                ConvenientGoodsCategoryVO categoryVO = new ConvenientGoodsCategoryVO();
                BeanUtils.copyProperties(categoryDO, categoryVO);
                categoryVOList.add(categoryVO);
            });
        }
        return R.ok(categoryVOList);
    }
 
}