admin
2025-02-25 30d8e227e8d823b6c38c3b9c90ac2df03b63befe
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
package com.yeshi.fanli.service.impl.config;
 
import java.io.InputStream;
import java.util.Date;
import java.util.List;
import java.util.UUID;
 
import javax.annotation.Resource;
 
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.yeshi.utils.tencentcloud.COSManager;
 
import com.yeshi.fanli.dao.mybatis.user.SpreadImgMapper;
import com.yeshi.fanli.entity.bus.invite.SpreadImg;
import com.yeshi.fanli.service.inter.config.SpreadImgService;
import com.yeshi.fanli.util.FilePathEnum;
 
@Service
public class SpreadImgServiceImpl implements SpreadImgService {
 
    @Resource
    private SpreadImgMapper spreadImgMapper;
 
    
    @Override
    public void deleteInviteFriendImg(long id) {
        spreadImgMapper.deleteByPrimaryKey(id);
    }
 
    @Override
    public SpreadImg getInviteSpreadImg(long id) {
        return spreadImgMapper.selectByPrimaryKey(id);
    }
 
    @Override
    public void saveObject(MultipartFile file, SpreadImg record) throws Exception{
        
        String picture = null;
        if (file != null) {
            picture = uploadPicture(file);
        }
        
        Long id = record.getId();
        if (id == null) {
            record.setUrl(picture);
            record.setCreatetime(new Date());
            spreadImgMapper.insert(record);
        } else {
            // 修改
            SpreadImg resultObj = spreadImgMapper.selectByPrimaryKey(id);
            if (resultObj == null) {
                throw new Exception("修改内容已不存在");
            }
            
            if (picture != null && picture.trim().length() > 0) {
                // 删除已存在图片
                removePicture(resultObj.getUrl());
                record.setUrl(picture);
            } else {
                record.setUrl(resultObj.getUrl());
            }
            
            record.setCreatetime(resultObj.getCreatetime());
            spreadImgMapper.updateByPrimaryKey(record);
        }
    }
 
    /**
     * 上传图片
     * @param file
     * @return
     * @throws Exception
     */
    public String uploadPicture(MultipartFile file) throws Exception {
        // 文件解析 
        InputStream inputStream = file.getInputStream();
        String contentType = file.getContentType();
        String type = contentType.substring(contentType.indexOf("/") + 1);
    
        // 文件路径
        String filePath=FilePathEnum.invite.getPath() + UUID.randomUUID().toString().replace("-", "") + "." + type;
        // 执行上传
        return COSManager.getInstance().uploadFile(inputStream, filePath).getUrl();
    }
 
    /**
     * 删除图片
     * @param record
     * @throws Exception
     */
    public void removePicture(String picture) throws Exception {
        if (picture != null && picture.trim().length() > 0) {
            COSManager.getInstance().deleteFile(picture);
        }
    }
 
    @Override
    public List<SpreadImg> listQuery(int start, int pageSize, String key) {
        return spreadImgMapper.listQuery(start, pageSize, key);
    }
 
    @Override
    public long countQuery(String key) {
        return spreadImgMapper.countQuery(key);
    }
 
    @Override
    public List<SpreadImg> listAll() {
        return spreadImgMapper.listAll();
    }
    
    @Override
    public long countAll() {
        return spreadImgMapper.countAll();
    }
}