Cui Zhi Feng
2024-08-29 9f1412bc3afa8f16d13d9948b547c8748e02869a
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
package com.mzl.flower.service.point.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mzl.flower.config.exception.ValidationException;
import com.mzl.flower.config.security.SecurityUtils;
import com.mzl.flower.constant.Constants;
import com.mzl.flower.dto.BatchDTO;
import com.mzl.flower.dto.request.menber.MemberDTO;
import com.mzl.flower.dto.request.point.PointGoodDTO;
import com.mzl.flower.dto.request.point.PointGoodQueryDTO;
import com.mzl.flower.dto.response.content.AnnouncementDTO;
import com.mzl.flower.dto.response.point.PointGoodVO;
import com.mzl.flower.entity.content.Announcement;
import com.mzl.flower.entity.menber.Member;
import com.mzl.flower.entity.point.PointGood;
import com.mzl.flower.mapper.member.MemberMapper;
import com.mzl.flower.mapper.point.PointGoodMapper;
import com.mzl.flower.service.menber.MemberService;
import com.mzl.flower.service.point.PointGoodService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
 
import java.time.LocalDate;
import java.util.List;
 
/**
 * @author fanghaowei
 * @version version2.0
 * @className MemberServiceImpl
 * @date 2024/8/26
 * @description 会员管理功能逻辑层
 */
@Service
@Transactional
@RequiredArgsConstructor
public class PointGoodServiceImpl extends ServiceImpl<PointGoodMapper, PointGood> implements PointGoodService {
 
    private final PointGoodMapper pointGoodMapper;
 
    @Override
    public void save(PointGoodDTO pointGoodDTO) {
        if (pointGoodDTO.getId() != null) {
            throw new ValidationException("id必须为空");
        }
        if (pointGoodDTO.getStock() < 0) {
            throw new ValidationException("库存不能小于0");
        }
        if (pointGoodDTO.getPoint() < 0) {
            throw new ValidationException("积分不能小于0");
        }
        PointGood pointGood = new PointGood();
        BeanUtils.copyProperties(pointGoodDTO, pointGood);
        pointGood.setStatus(Constants.POINT_GOOD_STATUS.off.name());
        pointGood.create(SecurityUtils.getUserId());
        pointGoodMapper.insert(pointGood);
    }
 
    @Override
    public void update(PointGoodDTO pointGoodDTO) {
        if (pointGoodDTO.getId() == null || pointGoodDTO.getId() == 0) {
            throw new ValidationException("id不能为空");
        }
        if (pointGoodDTO.getStock() < 0) {
            throw new ValidationException("库存不能小于0");
        }
        if (pointGoodDTO.getPoint() < 0) {
            throw new ValidationException("积分不能小于0");
        }
        PointGood pointGood = pointGoodMapper.selectById(pointGoodDTO.getId());
        if (pointGood == null) {
            throw new ValidationException("找不到id为" + pointGood.getId() + "的商品");
        }
        BeanUtils.copyProperties(pointGoodDTO, pointGood, "id", "createTime", "createBy", "deleted", "status");
        pointGood.update(SecurityUtils.getUserId());
        pointGoodMapper.updateById(pointGood);
    }
 
    @Override
    public void batchDelete(BatchDTO batchDTO) {
        pointGoodMapper.deleteBatchIds(batchDTO.getIds());
    }
 
    @Override
    public void batchPublish(BatchDTO batchDTO) {
        List<PointGood> list = pointGoodMapper.selectList(new LambdaQueryWrapper<PointGood>().in(PointGood::getId, batchDTO.getIds()));
        for (PointGood pointGood : list) {
            pointGood.setStatus(Constants.POINT_GOOD_STATUS.up.name());
            pointGoodMapper.updateById(pointGood);
        }
    }
 
    @Override
    public void batchOff(BatchDTO batchDTO) {
        List<PointGood> list = pointGoodMapper.selectList(new LambdaQueryWrapper<PointGood>().in(PointGood::getId, batchDTO.getIds()));
        for (PointGood pointGood : list) {
            pointGood.setStatus(Constants.POINT_GOOD_STATUS.off.name());
            pointGoodMapper.updateById(pointGood);
        }
    }
 
    @Override
    public void delete(Long id) {
        PointGood pointGood = pointGoodMapper.selectById(id);
        if (pointGood == null) {
            throw new ValidationException("找不到id为" + id + "的商品");
        }
        pointGoodMapper.deleteById(id);
 
    }
 
    @Override
    public void copy(Long id) {
        PointGood pointGoodTmp = pointGoodMapper.selectById(id);
        if (pointGoodTmp == null) {
            throw new ValidationException("找不到id为" + id + "的商品");
        }
        PointGood pointGood = new PointGood();
        BeanUtils.copyProperties(pointGoodTmp, pointGood,"id","createTime", "createBy", "deleted", "status");
        pointGood.setStatus(Constants.POINT_GOOD_STATUS.off.name());
        pointGood.create(SecurityUtils.getUserId());
        pointGoodMapper.deleteById(id);
 
    }
 
    @Override
    public void changeStatus(Long id) {
        PointGood pointGood = pointGoodMapper.selectById(id);
        if (pointGood == null) {
            throw new ValidationException("找不到id为" + id + "的商品");
        }
        if (Constants.COMMON_PUBLISH_STATUS.published.name().equals(pointGood.getStatus())) {
            pointGood.setStatus(Constants.COMMON_PUBLISH_STATUS.unpublished.name());
        } else {
            pointGood.setStatus(Constants.COMMON_PUBLISH_STATUS.published.name());
        }
        pointGoodMapper.updateById(pointGood);
 
    }
 
    @Override
    public PointGoodVO detail(Long id) {
        PointGood pointGood = pointGoodMapper.selectById(id);
        if (pointGood == null) {
            return null;
        }
        PointGoodVO vo = new PointGoodVO();
        BeanUtils.copyProperties(pointGood, vo);
        return vo;
    }
 
    @Override
    public Page<PointGoodVO> queryPage(PointGoodQueryDTO pointGoodQueryDTO, Page page) {
        List<PointGoodVO> list = pointGoodMapper.queryPage(pointGoodQueryDTO, page);
        page.setRecords(list);
        return page;
    }
}