yujian
2019-03-12 4a05f1c9c508ab7f10c5eae22a5c716f5454ca02
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
package com.yeshi.fanli.service.impl.homemodule;
 
import java.io.InputStream;
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.tencentcloud.COSManager;
 
import com.yeshi.fanli.dao.mybatis.homemodule.SwiperPictureMapper;
import com.yeshi.fanli.entity.bus.homemodule.SwiperPicture;
import com.yeshi.fanli.exception.NotExistObjectException;
import com.yeshi.fanli.exception.banner.SwiperPictureException;
import com.yeshi.fanli.service.inter.config.SystemConfigService;
import com.yeshi.fanli.service.inter.homemodule.SwiperPictureService;
import com.yeshi.fanli.util.StringUtil;
 
@Service
public class SwiperPictureServiceImpl implements SwiperPictureService {
 
    @Resource
    private SwiperPictureMapper swiperPictureMapper;
    
    @Resource
    private SystemConfigService systemConfigService;
    
    @Override
    public int deleteByPrimaryKey(Long id) throws SwiperPictureException{
        return swiperPictureMapper.deleteByPrimaryKey(id);
    }
 
    @Override
    public int insert(SwiperPicture record) throws SwiperPictureException{
        return swiperPictureMapper.insert(record);
    }
 
    @Override
    public int insertSelective(SwiperPicture record) throws SwiperPictureException{
        return swiperPictureMapper.insertSelective(record);
    }
 
    @Override
    public SwiperPicture selectByPrimaryKey(Long id) throws SwiperPictureException{
        return swiperPictureMapper.selectByPrimaryKey(id);
    }
 
    @Override
    public int updateByPrimaryKeySelective(SwiperPicture record) throws SwiperPictureException{
        return swiperPictureMapper.updateByPrimaryKeySelective(record);
    }
 
    @Override
    public int updateByPrimaryKey(SwiperPicture record) throws SwiperPictureException{
        return swiperPictureMapper.updateByPrimaryKey(record);
    }
 
    @Override
    public List<SwiperPicture> queryByBannerID(long start, int count, Long bannerId) throws SwiperPictureException{
        return swiperPictureMapper.queryByBannerID(start, count, bannerId);
    }
 
    @Override
    public long countQueryByBannerID(Long bannerId) throws SwiperPictureException{
        return swiperPictureMapper.countQueryByBannerID(bannerId);
    }
 
    
    
    @Override
    public void save(SwiperPicture record) throws SwiperPictureException{
        
        if (record == null) {
            throw new SwiperPictureException(1, "参数不能为空");
        }
        
        Long bannerId = record.getBannerId();
        if (bannerId == null) {
            throw new SwiperPictureException(1, "标识管理ID不能为空");
        }
        
        String params = record.getParams();
        if (!StringUtil.isNullOrEmpty(params)) {
            try {
                String jumpValue = systemConfigService.get("jump");
                if (StringUtil.isNullOrEmpty(jumpValue)) {
                    jumpValue = "{\"url\":\"#\"}";
                }
                params = jumpValue.replace("#", params);
                
            } catch (NotExistObjectException e) {
                e.printStackTrace();
            }
        }
        
        Long id = record.getId();
        
        if (id == null) {
            // 新增
            
            int maxOrder = swiperPictureMapper.getMaxOrderByBannerID(bannerId);
            record.setOrder(maxOrder + 1);
            // 默认停用
            record.setState(1);
            // 默认非系统控制
            record.setAutoControl(1);
            // 默认非登陆
            record.setJumpNeedLogin(false);
            record.setCreatetime(new Date());
            record.setUpdatetime(new Date());
            
            swiperPictureMapper.insert(record);
        } else {
            // 修改
            SwiperPicture resultObj = swiperPictureMapper.selectByPrimaryKey(id);
            if (resultObj == null) {
                throw new SwiperPictureException(1, "参数不能为空");
            }
            
            record.setOrder(resultObj.getOrder());
            record.setCreatetime(resultObj.getCreatetime());
            record.setUpdatetime(new Date());
            swiperPictureMapper.updateByPrimaryKey(record);
        }
    }
    
    
    @Override
    @Transactional
    public int deleteBatchByPrimaryKey(List<Long> list) throws SwiperPictureException{
        
        List<SwiperPicture> listSwiper = swiperPictureMapper.queryByListPrimaryKey(list);
        for (SwiperPicture swiperPicture: listSwiper) {
            String src = swiperPicture.getSrc();
            if (!StringUtil.isNullOrEmpty(src)) {
                COSManager.getInstance().deleteFile(src);
            }
        }
        
        return swiperPictureMapper.deleteBatchByPrimaryKey(list);
    }
 
    @Override
    public int deleteBatchByBannerID(List<Long> list) throws SwiperPictureException{
        return swiperPictureMapper.deleteBatchByBannerID(list);
    }
    
    @Override
    public List<SwiperPicture> queryByListBannerID(List<Long> list) throws Exception{
        return swiperPictureMapper.queryByListBannerID(list);
    }
 
    @Override
    public void uploadPicture(MultipartFile file, SwiperPicture record) throws Exception {
        
        InputStream inputStream = file.getInputStream();
        String contentType = file.getContentType();
        String type = contentType.substring(contentType.indexOf("/") + 1);
        // 上传文件相对位置
        String fileUrl="swiperPic/"+UUID.randomUUID().toString().replace("-", "") + "." + type;
        
        /*  修改图片时,先删除已存在图片  */
        String src = record.getSrc();
        if (!StringUtil.isNullOrEmpty(src)) {
            COSManager.getInstance().deleteFile(src);
        }
        
        String uploadFilePath = COSManager.getInstance().uploadFile(inputStream, fileUrl).getUrl();
        
        /*  更新数据库信息  */
        if (!StringUtil.isNullOrEmpty(uploadFilePath)) {
            record.setSrc(uploadFilePath);
            record.setUpdatetime(new Date());
            updateByPrimaryKeySelective(record);
        }
    }
 
    @Override
    public List<SwiperPicture> getOrderByBannerID(Long bannerId, Integer type, Integer order) throws SwiperPictureException {
        return swiperPictureMapper.getOrderByBannerID(bannerId, type, order);
    }
 
    @Override
    public int getMaxOrderByBannerID(Long bannerId) throws SwiperPictureException {
        return swiperPictureMapper.getMaxOrderByBannerID(bannerId);
    }
    
    @Override
    @Cacheable(value = "bannerCache", key = "'getByBannerCard-'+#card")
    public List<SwiperPicture> getByBannerCard(String card) throws SwiperPictureException {
        return swiperPictureMapper.getByBannerCard(card);
    }
    
}