yujian
2019-04-08 1da17d215d48e3e3aa9e8d7a3ef526904764f408
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package com.yeshi.fanli.service.impl.homemodule;
 
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
 
import javax.annotation.Resource;
import javax.transaction.Transactional;
 
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.yeshi.utils.JsonUtil;
import org.yeshi.utils.tencentcloud.COSManager;
 
import com.yeshi.fanli.dao.mybatis.homemodule.SpecialMapper;
import com.yeshi.fanli.entity.bus.homemodule.Special;
import com.yeshi.fanli.entity.bus.homemodule.SpecialPlace;
import com.yeshi.fanli.service.inter.homemodule.SpecialPlaceService;
import com.yeshi.fanli.service.inter.homemodule.SpecialService;
import com.yeshi.fanli.util.StringUtil;
 
import net.sf.json.JSONObject;
 
 
@Service
public class SpecialServiceImpl implements SpecialService {
    
    @Resource
    private SpecialMapper specialMapper;
    
    @Resource
    private SpecialPlaceService specialPlaceService;
 
    
    @Override
    public int deleteByPrimaryKey(Long id) {
        return specialMapper.deleteByPrimaryKey(id);
    }
 
    @Override
    public int insert(Special record) {
        return specialMapper.insert(record);
    }
 
    @Override
    public int insertSelective(Special record) {
        return specialMapper.insertSelective(record);
    }
 
    @Override
    public Special selectByPrimaryKey(Long id) {
        return specialMapper.selectByPrimaryKey(id);
    }
 
    @Override
    public int updateByPrimaryKeySelective(Special record) {
        return specialMapper.updateByPrimaryKeySelective(record);
    }
 
    @Override
    public int updateByPrimaryKey(Special record) {
        return specialMapper.updateByPrimaryKey(record);
    }
    
    @Override
    @Transactional
    public int deleteBatchByPrimaryKey(List<Long> list) throws Exception{
        
        List<Special> listSpecial = specialMapper.queryByListPrimaryKey(list);
        for (Special special: listSpecial) {
            String src = special.getPicture();
            if (!StringUtil.isNullOrEmpty(src)) {
                COSManager.getInstance().deleteFile(src);
            }
        }
        
        return specialMapper.deleteBatchByPrimaryKey(list);
    }
    
    @Override
    public int deleteBatchByCardID(List<Long> list) throws Exception{
        List<Special> listSpecial = specialMapper.queryByListCardID(list);
        for (Special special: listSpecial) {
            String src = special.getPicture();
            if (!StringUtil.isNullOrEmpty(src)) {
                COSManager.getInstance().deleteFile(src);
            }
        }
        
        return specialMapper.deleteBatchByCardID(list);
    }
    
    
    @Override
    public List<Special> listQueryByCard(long start, int count, Long card, String key){
        return specialMapper.listQueryByCard(start, count, card, key);
    }
    
    @Override
    public long countlistQueryByCard(Long card, String key) {
        return specialMapper.countlistQueryByCard(card, key);
    }
    
    @Override
    public List<Special> getOrderByCardID(Long cardId, Integer type, Integer order) {
        return specialMapper.getOrderByCardID(cardId, type, order);
    }
    
    @Override
    public int getMaxOrderByCard(Long card) {
        return specialMapper.getMaxOrderByCard(card);
    }
 
    @Override
    @Cacheable(value = "configCache", key = "'listBySystemAndCard-'+#card+'-'+#systemId")
    public List<Special> listBySystemAndCard(String card, Long systemId) {
        return specialMapper.listBySystemAndCard(card, systemId);
    }
 
    
    @Override
    @Cacheable(value = "configCache", key = "'listPageBySystemAndCard-'+#start+'-'+#count+'-'+#card+'-'+#systemId")
    public List<Special> listPageBySystemAndCard(long start, int count, String card, Long systemId) {
        return specialMapper.listPageBySystemAndCard(start, count, card, systemId);
    }
 
    
    @Override
    @Cacheable(value = "configCache", key = "'getSpecialListCache-'+#card+'-'+#systemId")
    public JSONObject getSpecialListCache(String card, Long systemId) throws Exception{
        
        // 两行圆形专题
        List<Special> arcList = new ArrayList<Special>();
        // 活动列表
        List<Special> activityList = new ArrayList<Special>();
        // 方块部分
        List<Special> blockList = new ArrayList<Special>();
        
        
        List<Special> list = specialMapper.listBySystemAndCard(card, systemId);
        if (list != null && list.size() > 0) {
            for (Special special : list) {
                if (Special.SHOWTYPE_ARC.equals(special.getShowType())) {
                    // 两行圆形专题
                    arcList.add(special);
                } else if (Special.SHOWTYPE_BLOCK.equals(special.getShowType())) {
                    // 方块部分
                    blockList.add(special);
                } else if (Special.SHOWTYPE_ACTIVITY.equals(special.getShowType())) {
                    // 动态
                    activityList.add(special);
                }
            }
        }
        
        JSONObject arcMap = new JSONObject();
        arcMap.put("list", JsonUtil.getApiCommonGson().toJson(arcList));
        
        JSONObject activityMap = new JSONObject();
        activityMap.put("list", JsonUtil.getApiCommonGson().toJson(activityList));
        
        JSONObject blockJsonMap = new JSONObject();
        blockJsonMap.put("list", JsonUtil.getApiCommonGson().toJson(blockList));
        
        // 背景图片
        List<SpecialPlace> listPlace = specialPlaceService.getList();
        if (listPlace != null && listPlace.size() > 0) {
            for (SpecialPlace specialPlace: listPlace) {
                String bottomPicture = specialPlace.getBottomPicture();
                if (bottomPicture != null && bottomPicture.trim().length() > 0) {
                    if (Special.SHOWTYPE_ARC.equals(specialPlace.getKey())) {
                        arcMap.put("bottomPicture", bottomPicture);
                    } else if (Special.SHOWTYPE_BLOCK.equals(specialPlace.getKey())) {
                        blockJsonMap.put("bottomPicture", bottomPicture);
                    } else if (Special.SHOWTYPE_ACTIVITY.equals(specialPlace.getKey())) {
                        activityMap.put("bottomPicture", bottomPicture);
                    }
                }
            }
        }
        
        JSONObject rootMap = new JSONObject();
        rootMap.put("arcArea", arcMap);
        rootMap.put("activityArea", activityMap);
        rootMap.put("blockArea", blockJsonMap);
        
        return rootMap;
    }
    
    
    @Override
    public void uploadPicture(MultipartFile file, Special record, Long cardId) throws Exception {
        
        InputStream inputStream = file.getInputStream();
        String contentType = file.getContentType();
        String type = contentType.substring(contentType.indexOf("/") + 1);
        // 上传文件相对位置
        String fileUrl="SpecialPic/"+UUID.randomUUID().toString().replace("-", "") + "." + type;
        
        /*  修改图片时,先删除已存在图片  */
        if (record != null) { 
            String src = record.getPicture();
            if (!StringUtil.isNullOrEmpty(src)) {
                COSManager.getInstance().deleteFile(src);
            }
        }
        
        String uploadFilePath = COSManager.getInstance().uploadFile(inputStream, fileUrl).getUrl();
        
        /*  更新数据库信息  */
        if (!StringUtil.isNullOrEmpty(uploadFilePath)) {
            
            if (record != null) {
                record.setUpdatetime(new Date());
                record.setPicture(uploadFilePath);
                updateByPrimaryKeySelective(record);
            } else {
                record = new Special();
                record.setPicture(uploadFilePath);
                record.setCreatetime(new Date());
                record.setUpdatetime(new Date());
                record.setCardId(cardId);
                record.setState(0L);
                
                int maxOrder = getMaxOrderByCard(cardId);
                record.setOrderby(maxOrder + 1);
                record.setShowType("default");
                insertSelective(record);
            }
            
        }
    }
 
    
}