陶杰
2024-08-22 ee9032d9baf5f33e376d2d2699136e0a7b26bec7
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
package com.mzl.flower.service.system;
 
import com.mzl.flower.config.exception.ValidationException;
import com.mzl.flower.config.security.SecurityUtils;
import com.mzl.flower.dto.request.system.ContentSaveDTO;
import com.mzl.flower.dto.response.system.ContentDTO;
import com.mzl.flower.dto.response.system.ContentListDTO;
import com.mzl.flower.entity.system.ConfigContent;
import com.mzl.flower.mapper.system.ConfigContentMapper;
import com.mzl.flower.service.BaseService;
import io.micrometer.core.instrument.util.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.ArrayList;
import java.util.List;
 
@Service
@Transactional
public class ConfigContentService extends BaseService {
 
    @Autowired
    private ConfigContentMapper contentMapper;
 
    public String saveContent(ContentSaveDTO dto) {
        String id = dto.getId();
        if(StringUtils.isEmpty(id)){
            throw new ValidationException("id不能为空");
        }
        ConfigContent cc = contentMapper.selectById(id);
 
        if(cc == null){
            cc = new ConfigContent();
            BeanUtils.copyProperties(dto, cc);
            cc.create(SecurityUtils.getUserId());
            contentMapper.insert(cc);
        } else {
            BeanUtils.copyProperties(dto, cc);
            cc.update(SecurityUtils.getUserId());
            contentMapper.updateById(cc);
        }
 
        return id;
    }
 
    public List<ContentListDTO> selectContentList(){
        List<ConfigContent> ls = contentMapper.selectList(null);
 
        List<ContentListDTO> result = new ArrayList<>();
        if(ls != null && ls.size() > 0){
            for(ConfigContent cc : ls){
                ContentListDTO dto = new ContentListDTO();
                BeanUtils.copyProperties(cc, dto);
 
                result.add(dto);
            }
        }
 
        return result;
    }
 
    public ContentDTO getContent(String id){
        ConfigContent cc = contentMapper.selectById(id);
        ContentDTO dto = new ContentDTO();
        if(cc!=null){
            BeanUtils.copyProperties(cc, dto);
        }
        return dto;
    }
}