admin
2020-05-20 98b1a0affd69bbe63223c21fdd2c404e8bedfccb
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
package com.yeshi.fanli.service.impl.user;
 
import java.io.IOException;
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.UserComplaintMapper;
import com.yeshi.fanli.entity.bus.user.UserComplaint;
import com.yeshi.fanli.entity.bus.user.UserInfo;
import com.yeshi.fanli.exception.user.UserComplaintException;
import com.yeshi.fanli.service.inter.user.UserComplaintService;
import com.yeshi.fanli.util.FilePathEnum;
 
import net.sf.json.JSONArray;
 
@Service
public class UserComplaintServiceImpl implements UserComplaintService {
    
    @Resource
    private UserComplaintMapper userComplaintMapper;
    
    @Override
    public void addUserComplaint(List<MultipartFile> fileList, Long uid, String content) 
            throws UserComplaintException, IOException, Exception {
        
        if (content == null || content.trim().length() == 0) {
            throw new UserComplaintException(1, "吐槽内容不能为空");
        }
        
        // 图片最多五张、可以为空
        if (fileList != null && fileList.size() > 5 ) {
            throw new UserComplaintException(1, "图片最多可上传五张");
        }
        
        // 图片列表
        String pictureList = null;
        if (fileList != null && fileList.size() > 0) {
            
            JSONArray array = new JSONArray();
            // 文件上传
            for (MultipartFile file: fileList) {
                
                InputStream inputStream = file.getInputStream();
                String contentType = file.getContentType();
                String type = contentType.substring(contentType.indexOf("/") + 1);
                // 上传文件相对位置
                String fileUrl= FilePathEnum.userComplaint.getPath() +UUID.randomUUID().toString().replace("-", "") + "." + type;
                
                String uploadFilePath = COSManager.getInstance().uploadFile(inputStream, fileUrl).getUrl();
                array.add(uploadFilePath);
            }
            
            pictureList = array.toString();
        }
        
        UserInfo userInfo = null;
        if (uid != null) {
            userInfo = new UserInfo();
            userInfo.setId(uid);
        }
        
        UserComplaint userComplaint = new UserComplaint();
        userComplaint.setContent(content);
        userComplaint.setPictureList(pictureList);
        userComplaint.setUserInfo(userInfo);
        userComplaint.setCreateTime(new Date());
        userComplaintMapper.insertSelective(userComplaint);
    }
    
    
}