cloudroam
3 天以前 3a819e4f668c15e8b77b188b322470da12bb7a43
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
package com.mzl.flower.service.content;
 
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.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.content.AddAdvertisementDTO;
import com.mzl.flower.dto.request.content.UpdateAdvertisementDTO;
import com.mzl.flower.dto.response.content.AdvertisementDTO;
import com.mzl.flower.entity.content.Advertisement;
import com.mzl.flower.mapper.content.AdvertisementMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.List;
import java.util.stream.Collectors;
 
@Service
@Transactional
public class AdvertisementService {
 
    private final AdvertisementMapper advertisementMapper;
 
    public AdvertisementService(AdvertisementMapper advertisementMapper) {
        this.advertisementMapper = advertisementMapper;
    }
 
    public void add(AddAdvertisementDTO dto) {
        Advertisement advertisement = new Advertisement();
        advertisement.setContent(dto.getContent());
        advertisement.setTitle(dto.getTitle());
        advertisement.setCover(dto.getCover());
        advertisement.create(SecurityUtils.getUserId());
        advertisement.setStatus(Constants.COMMON_PUBLISH_STATUS.unpublished.name());
 
        advertisementMapper.insert(advertisement);
    }
 
    public void update(UpdateAdvertisementDTO dto) {
        Advertisement advertisement = advertisementMapper.selectById(dto.getId());
        if (advertisement == null) {
            throw new ValidationException("广告不存在");
        }
        advertisement.setContent(dto.getContent());
        advertisement.setTitle(dto.getTitle());
        advertisement.setCover(dto.getCover());
        advertisement.update(SecurityUtils.getUserId());
 
        advertisementMapper.updateById(advertisement);
    }
 
    public void delete(Long id) {
        advertisementMapper.deleteById(id);
    }
 
    public AdvertisementDTO detail(Long id) {
        Advertisement advertisement = advertisementMapper.selectById(id);
        if (advertisement == null) {
             return null;
        }
        AdvertisementDTO advertisementDTO = new AdvertisementDTO();
        BeanUtils.copyProperties(advertisement, advertisementDTO);
        return advertisementDTO;
    }
 
    public IPage<AdvertisementDTO> queryPage(String title, String status, Page page) {
        LambdaQueryWrapper<Advertisement> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Advertisement::getCreateTime);
        if (StringUtils.isNotBlank(title)) {
             wrapper.like(Advertisement::getTitle, title);
        }
        if (StringUtils.isNotBlank(status)) {
              wrapper.eq(Advertisement::getStatus, status);
        }
        return advertisementMapper.selectPage(page, wrapper).convert(item -> {
               AdvertisementDTO advertisementDTO = new AdvertisementDTO();
               BeanUtils.copyProperties(item, advertisementDTO);
               advertisementDTO.setContent(null);
               return advertisementDTO;
        });
    }
 
    public void changeStatus(Long id) {
        Advertisement advertisement = advertisementMapper.selectById(id);
        if (advertisement == null) {
             throw new ValidationException("广告不存在");
        }
        if(Constants.COMMON_PUBLISH_STATUS.published.name().equals(advertisement.getStatus())){
            advertisement.setStatus(Constants.COMMON_PUBLISH_STATUS.unpublished.name());
        }else{
            advertisement.setStatus(Constants.COMMON_PUBLISH_STATUS.published.name());
        }
        advertisementMapper.updateById(advertisement);
    }
 
    public List<AdvertisementDTO> queryList(String title, String status) {
        LambdaQueryWrapper<Advertisement> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Advertisement::getCreateTime);
        if (StringUtils.isNotBlank(title)) {
            wrapper.like(Advertisement::getTitle, title);
        }
        if (StringUtils.isNotBlank(status)) {
            wrapper.eq(Advertisement::getStatus, status);
        }
        return advertisementMapper.selectList(wrapper).stream().map(item -> {
            AdvertisementDTO advertisementDTO = new AdvertisementDTO();
            BeanUtils.copyProperties(item, advertisementDTO);
            return advertisementDTO;
        }).collect(Collectors.toList());
    }
 
    public void batchDelete(BatchDTO dto) {
        advertisementMapper.deleteBatchIds(dto.getIds());
    }
 
    public void batchPublish(BatchDTO dto) {
        List<Advertisement> list = advertisementMapper.selectList(new LambdaQueryWrapper<Advertisement>().in(Advertisement::getId, dto.getIds()));
        for (Advertisement advertisement : list) {
            advertisement.setStatus(Constants.COMMON_PUBLISH_STATUS.published.name());
            advertisementMapper.updateById(advertisement);
        }
    }
}