cloudroam
12 小时以前 b0fee54f8475336b05c50ef3154c51f60ebd63aa
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
package com.mzl.flower.service.film.impl;
 
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.request.film.HomeConfigDTO;
import com.mzl.flower.dto.request.film.HomeConfigQueryDTO;
import com.mzl.flower.dto.response.content.AnnouncementDTO;
import com.mzl.flower.dto.response.film.HomeConfigVO;
import com.mzl.flower.entity.content.Announcement;
import com.mzl.flower.entity.film.HomeConfig;
import com.mzl.flower.mapper.film.HomeConfigMapper;
import com.mzl.flower.service.film.HomeConfigService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.util.List;
 
 
@Service
@Transactional
@RequiredArgsConstructor
public class HomeConfigServiceImpl extends ServiceImpl<HomeConfigMapper, HomeConfig> implements HomeConfigService {
 
    @Resource
    private HomeConfigMapper homeConfigMapper;
 
    @Override
    public void saveHomeConfig(HomeConfigDTO homeConfigDTO) {
        HomeConfig homeConfig = new HomeConfig();
        BeanUtils.copyProperties(homeConfigDTO, homeConfig);
        homeConfig.setStatus(Constants.COMMON_PUBLISH_STATUS.unpublished.name());
        homeConfig.create(SecurityUtils.getUserId());
        homeConfigMapper.insert(homeConfig);
    }
 
    @Override
    public void updateHomeConfig(HomeConfigDTO homeConfigDTO) {
        if(homeConfigDTO.getId()==null || homeConfigDTO.getId()==0){
            throw new ValidationException("id不能为空");
        }
        HomeConfig homeConfig = homeConfigMapper.selectById(homeConfigDTO.getId());
        if(homeConfig==null){
            throw new ValidationException("找不到id为"+homeConfigDTO.getId()+"的配置");
        }
        BeanUtils.copyProperties(homeConfigDTO, homeConfig);
        homeConfig.update(SecurityUtils.getUserId());
        homeConfigMapper.updateById(homeConfig);
    }
 
    @Override
    public void deleteHomeConfig(String id) {
        HomeConfig homeConfig = homeConfigMapper.selectById(id);
        if (homeConfig == null) {
            throw new ValidationException("主页配置不存在");
        }
        //已发布的不能删除
        if(Constants.COMMON_PUBLISH_STATUS.published.name().equals(homeConfig.getStatus())){
            throw new ValidationException("已发布的不能删除");
        }
        homeConfigMapper.deleteById(id);
    }
 
    @Override
    public Page<HomeConfigVO> queryPage(HomeConfigQueryDTO homeConfigQueryDTO, Page page) {
        List<HomeConfigVO> list = homeConfigMapper.queryPage(homeConfigQueryDTO, page);
        return page.setRecords(list);
    }
 
    @Override
    public void changeStatus(Long id) {
        HomeConfig homeConfig = homeConfigMapper.selectById(id);
        if(homeConfig==null){
            throw new ValidationException("找不到id为"+id+"的公告");
        }
        //判断所有列表中是否已经存在已发布
        List<HomeConfig> list = homeConfigMapper.queryList(Constants.COMMON_PUBLISH_STATUS.published.name());
        if(list.size()==1 && Constants.COMMON_PUBLISH_STATUS.published.name().equals(homeConfig.getStatus())){
            homeConfig.setStatus(Constants.COMMON_PUBLISH_STATUS.unpublished.name());
            homeConfigMapper.updateById(homeConfig);
        }else {
            if (list.size() > 0) {
                throw new ValidationException("已存在已发布");
            }
            if (Constants.COMMON_PUBLISH_STATUS.published.name().equals(homeConfig.getStatus())) {
                homeConfig.setStatus(Constants.COMMON_PUBLISH_STATUS.unpublished.name());
            } else {
                homeConfig.setStatus(Constants.COMMON_PUBLISH_STATUS.published.name());
            }
            homeConfigMapper.updateById(homeConfig);
        }
    }
 
    @Override
    public HomeConfigVO detail(Long id) {
        HomeConfig homeConfig = homeConfigMapper.selectById(id);
        if(homeConfig==null){
            return null;
        }
        HomeConfigVO vo = new HomeConfigVO();
        BeanUtils.copyProperties(homeConfig,vo);
        return vo;
    }
 
    @Override
    public HomeConfigVO getHomeConfigInfo() {
        return homeConfigMapper.getHomeConfigInfo();
    }
}