yujian
2019-09-03 2ddf5b8a1290123e2d1c05bcc851ec35cd217afc
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package com.yeshi.fanli.service.impl.help;
 
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.help.HelpClassMapper;
import com.yeshi.fanli.entity.bus.help.HelpClass;
import com.yeshi.fanli.exception.config.HelpClassException;
import com.yeshi.fanli.service.inter.help.HelpClassService;
import com.yeshi.fanli.util.StringUtil;
 
@Service
public class HelpClassServiceImpl implements HelpClassService {
 
    @Resource
    private HelpClassMapper helpClassMapper;
 
    @Override
    public int deleteByPrimaryKeyBatch(List<Long> list) {
        return helpClassMapper.deleteByPrimaryKeyBatch(list);
    }
 
    @Override
    public HelpClass selectByPrimaryKey(Long id) {
        return helpClassMapper.selectByPrimaryKey(id);
    }
    
    @Override
    public void save(HelpClass helpClass) throws HelpClassException {
        if (helpClass == null) {
            throw new HelpClassException(1, "参数不能为空");
        }
        
        if (helpClass.getId() == null) {
            
            String name = helpClass.getName();
            if (StringUtil.isNullOrEmpty(name)) {
                throw new HelpClassException(1, "名称不能为空");
            }
            
            // 默认不显示
            helpClass.setShowState(0);
            
            // 排序-末尾
            int maxOrder = helpClassMapper.getMaxOrder();
            helpClass.setSort(maxOrder + 1);
            
            helpClass.setCreateTime(new Date());
            helpClass.setUpdateTime(new Date());
            helpClassMapper.insertSelective(helpClass);
            
        } else {
            helpClass.setUpdateTime(new Date());
            helpClassMapper.updateByPrimaryKeySelective(helpClass);
        }
    }
    
    
    @Override
    public int updateShowState(Long id) throws HelpClassException {
        
        if (id == null) {
            throw new HelpClassException(1, "传递参数不能为空");
        }
        
        HelpClass helpClass = helpClassMapper.selectByPrimaryKey(id);
        if (helpClass == null) {
            throw new HelpClassException(1, "更新数据已不存在");
        }
        
        Integer showState = helpClass.getShowState();
        if (showState == null || showState == 0) {
            showState = 1;
        } else {
            showState = 0;
        }
        
        HelpClass help = new HelpClass();
        help.setId(id);
        help.setShowState(showState);
        help.setUpdateTime(new Date());
        
        helpClassMapper.updateByPrimaryKeySelective(help);
        
        return showState;
    }
    
    
    @Override
    public void updateSort(Integer moveType, Long id) throws HelpClassException {
        
        if (id == null || moveType == null) {
            throw new HelpClassException(1, "传递参数不能为空");
        }
        
        HelpClass helpClass = helpClassMapper.selectByPrimaryKey(id);
        if (helpClass == null) {
            throw new HelpClassException(1, "更新数据已不存在");
        }
        
        HelpClass changeObjct = helpClassMapper.getChangeOrder(moveType, helpClass.getSort());
        
        if (changeObjct == null) {
            throw new HelpClassException(1, "已经是最边缘位置");
        } else {
            Integer changeSort = changeObjct.getSort();
            
            // 交换排序序号
            changeObjct.setSort(helpClass.getSort());
            helpClassMapper.updateByPrimaryKeySelective(changeObjct);
            
            helpClass.setSort(changeSort);
            helpClassMapper.updateByPrimaryKeySelective(helpClass);
        }
    }
    
    @Override
    public void uploadPic(MultipartFile file, Long id) throws HelpClassException, IOException {
        
        if (id == null || file == null) {
            throw new HelpClassException(1, "传递参数不能为空");
        }
        
        HelpClass helpClass = helpClassMapper.selectByPrimaryKey(id);
        if (helpClass == null) {
            throw new HelpClassException(1, "更新数据已不存在");
        }
        
        InputStream inputStream = file.getInputStream();
        String contentType = file.getContentType();
        String type = contentType.substring(contentType.indexOf("/") + 1);
        
        String filePath ="/img/HelpClass/" + UUID.randomUUID().toString().replace("-", "") + "." + type;
        
        /*  修改图片时,先删除已存在图片  */
        String picture = helpClass.getPicture();
        if (!StringUtil.isNullOrEmpty(picture)) {
            COSManager.getInstance().deleteFile(picture);
        }
        
        String fileUrl = COSManager.getInstance().uploadFile(inputStream, filePath).getUrl();
        
        /*  更新数据库信息  */
        if (!StringUtil.isNullOrEmpty(fileUrl)) {
            helpClass.setUpdateTime(new Date());
            helpClass.setPicture(fileUrl);
            helpClassMapper.updateByPrimaryKeySelective(helpClass);
        }
    }
    
    
    @Override
    public List<HelpClass> query(int start, int count, String key, Integer state)
            throws HelpClassException {
        return helpClassMapper.listQuery(start, count, key, state);
    }
 
    @Override
    public long countQuery(String key, Integer state) {
        return helpClassMapper.countQuery(key, state);
    }
    
    @Override
    public List<HelpClass> getClassByState(Integer state) throws HelpClassException {
        return helpClassMapper.getClassByState(state);
    }
    
    @Override
    public List<HelpClass> getProvidedClass() throws HelpClassException {
        return helpClassMapper.getProvidedClass();
    }
    
    
}