yujian
2019-03-14 4aadf484e193995c23ee1d5bb1971a497d2f9a0d
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
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.apache.commons.beanutils.PropertyUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.yeshi.utils.tencentcloud.COSManager;
 
import com.yeshi.fanli.dao.mybatis.homemodule.HomeNavbarMapper;
import com.yeshi.fanli.dao.mybatis.homemodule.SuperHomeNavbarMapper;
import com.yeshi.fanli.entity.bus.homemodule.HomeNavbar;
import com.yeshi.fanli.entity.bus.homemodule.SuperHomeNavbar;
import com.yeshi.fanli.entity.system.BusinessSystem;
import com.yeshi.fanli.service.inter.config.BusinessSystemService;
import com.yeshi.fanli.service.inter.homemodule.HomeNavbarService;
import com.yeshi.fanli.util.StringUtil;
 
 
@Service
public class HomeNavbarServiceImpl implements HomeNavbarService {
    
    @Resource
    private BusinessSystemService businessSystemService;
    @Resource
    private HomeNavbarMapper homeNavbarMapper;
    @Resource
    private SuperHomeNavbarMapper superHomeNavbarMapper;
 
    
    @Override
    public int deleteByPrimaryKey(Long id) {
        return homeNavbarMapper.deleteByPrimaryKey(id);
    }
 
    @Override
    public int insert(HomeNavbar record) {
        return homeNavbarMapper.insert(record);
    }
 
    @Override
    public int insertSelective(HomeNavbar record) {
        return homeNavbarMapper.insertSelective(record);
    }
 
    @Override
    public HomeNavbar selectByPrimaryKey(Long id) {
        return homeNavbarMapper.selectByPrimaryKey(id);
    }
 
    @Override
    public int updateByPrimaryKeySelective(HomeNavbar record) {
        return homeNavbarMapper.updateByPrimaryKeySelective(record);
    }
 
    @Override
    public int updateByPrimaryKey(HomeNavbar record) {
        return homeNavbarMapper.updateByPrimaryKey(record);
    }
 
    @Override
    @Transactional
    public int deleteBatchByPrimaryKey(List<Long> list) {
        superHomeNavbarMapper.deleteBatchByNavbarIds(list);
        for (Long id: list) {
            HomeNavbar homeNavbar = homeNavbarMapper.selectByPrimaryKey(id);
            if (homeNavbar !=null) {
                // 删除已存在图片 /
                String picture = homeNavbar.getPicture();
                if (!StringUtil.isNullOrEmpty(picture)) {
                    COSManager.getInstance().deleteFile(picture);
                }
            }
        }
        
        return homeNavbarMapper.deleteBatchByPrimaryKey(list);
    }
 
    @Override
    public int getMaxOrder() {
        return homeNavbarMapper.getMaxOrder();
    }
 
    @Override
    public List<HomeNavbar> getChangeOrder(Integer type, Integer order) {
        return homeNavbarMapper.getChangeOrder(type, order);
    }
 
    @Override
    public List<HomeNavbar> listQuery(long start, int count, String key) {
        
        List<HomeNavbar> listObj = homeNavbarMapper.listQuery(start, count, key);
        if (listObj == null || listObj.size() == 0) {
            return null;
        }
        
        List<BusinessSystem> systemList = businessSystemService.getBusinessSystems();
        
        List<Long> listId = new ArrayList<Long>();
        for (HomeNavbar homeNavbar: listObj) {
            listId.add(homeNavbar.getId());
        }
        List<SuperHomeNavbar> listSuper = superHomeNavbarMapper.listByNavbarIds(listId);
        
        if (listSuper == null || listSuper.size() == 0) {
            
            for (HomeNavbar homeNavbar: listObj) {
                homeNavbar.setSystemList(systemList);
            }
            
        } else {
            for (HomeNavbar homeNavbar: listObj) {
                
                Long id = homeNavbar.getId();
                List<BusinessSystem> newList = new ArrayList<BusinessSystem>();
                
                // 是否有关联系统选项
                for (BusinessSystem dsystem : systemList) {
                    
                    BusinessSystem newsystem = new BusinessSystem();
                    try {
                        PropertyUtils.copyProperties(newsystem, dsystem);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    
                    if (listSuper != null && listSuper.size() > 0) {
                        Long systemId = newsystem.getId();
                        
                        for (SuperHomeNavbar superHomeNavbar : listSuper) {
                            HomeNavbar navbar = superHomeNavbar.getHomeNavbar();
                            BusinessSystem system = superHomeNavbar.getSystem();
                            
                            // 当前专题 、当前系统
                            if (navbar != null && system != null && id == navbar.getId() 
                                    && systemId == system.getId()) {
                                newsystem.setCheck(1);
                                break;
                            }
                        }
                        
                    }
                    
                    if (newsystem.getCheck() != 1) {
                        newsystem.setCheck(0);
                    }
                    
                    newList.add(newsystem);
                }
                
                homeNavbar.setSystemList(newList);
            }
        }
        
        return listObj;
    }
 
    @Override
    public long countlistQuery(String key) {
        return homeNavbarMapper.countListQuery(key);
    }
 
 
    @Override
    public void uploadPicture(MultipartFile file, HomeNavbar homeNavbar) throws Exception {
        
        InputStream inputStream = file.getInputStream();
        String contentType = file.getContentType();
        String type = contentType.substring(contentType.indexOf("/") + 1);
        // 上传文件相对位置
        String fileUrl="HomeNavbar/"+UUID.randomUUID().toString().replace("-", "") + "." + type;
        
        /*  修改图片时,先删除已存在图片  */
        String picture = homeNavbar.getPicture();
        if (!StringUtil.isNullOrEmpty(picture)) {
            COSManager.getInstance().deleteFile(picture);
        }
        
        String uploadFilePath = COSManager.getInstance().uploadFile(inputStream, fileUrl).getUrl();
        /*  更新数据库信息  */
        if (!StringUtil.isNullOrEmpty(uploadFilePath)) {
            homeNavbar.setUpdatetime(new Date());
            homeNavbar.setPicture(uploadFilePath);
            updateByPrimaryKeySelective(homeNavbar);
        }
    }
    
 
}