cy
2022-06-21 129904537f66509f97b285e7eb4f42b3dc349dd0
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package cn.ksource.web.facade.link;
 
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import javax.servlet.http.HttpServletRequest;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import com.lowagie.tools.handout_pdf;
 
import cn.ksource.core.dao.BaseDao;
import cn.ksource.core.dao.SqlParameter;
import cn.ksource.core.page.PageInfo;
import cn.ksource.core.util.ConvertUtil;
import cn.ksource.core.util.FileUtil;
import cn.ksource.core.util.MyFileUploadException;
import cn.ksource.core.util.StringUtil;
import cn.ksource.web.Constants;
import cn.ksource.web.entity.link.CmsLink;
 
@Service("linkFacade")
public class LinkFacadeImpl implements LinkFacade {
 
    @Autowired
    private BaseDao baseDao;
    
    
    /**
     * 查询指定分类下的列表信息
     */
    @Override
    public Map queryLinkListJson(String categoryId, int page, int pageSize) {
        StringBuilder builder = new StringBuilder("SELECT * FROM CMS_LINK WHERE CATEGORY_ID = :categoryId");
        StringBuilder countBuilder = new StringBuilder("SELECT COUNT(ID) FROM CMS_LINK WHERE CATEGORY_ID = :categoryId");
        Map paramMap = new HashMap();
        builder.append(" ORDER BY THETOP ASC,ORDERNUM ASC");
        builder.append(" LIMIT :begin,:size");
        page = page==0?1:page;
        pageSize = pageSize==0?10:pageSize;
        int begin = (page-1)*pageSize;
        paramMap.put("begin", begin);
        paramMap.put("size", pageSize);
        paramMap.put("categoryId", categoryId);
        int count = baseDao.queryForInteger(countBuilder.toString(), paramMap);
        List<Map> resultList = baseDao.queryForList(builder.toString(),paramMap);
        
        
        Map resultMap = new HashMap();
        resultMap.put("total", count); //信息总数
        resultMap.put("rows", resultList);
        return resultMap;
    }
    
    
 
 
    @Override
    public Map queryLinkCategoryById(String categoryId) {
        String sql = "SELECT * FROM CMS_LINK_CATEGORY WHERE ID = :id";
        return baseDao.queryForMap(sql, new SqlParameter().addValue("id", categoryId));
    }
 
 
 
 
    @Override
    public boolean save(CmsLink link, HttpServletRequest request) {
        String[] allowType =  new String[]{"gif","jpg","jpeg","png"};
        int categoryId = link.getCategoryId();
        System.out.println("categoryId:"+categoryId);
        Map linkCategory = queryLinkCategoryById(ConvertUtil.obj2StrBlank(categoryId));
        //判断是否需要标题图片
        int titleimage = ConvertUtil.obj2Int(linkCategory.get("TITLEIMAGE"));
        
        String uploadUrl = new String();
        if(titleimage==1) {
            try {
                uploadUrl = FileUtil.uploadFile4SpringMVC(request, "image" ,"/upload/classify"+categoryId,allowType);
                //判断是否需要生成缩略图
                int thumbnail = ConvertUtil.obj2Int(linkCategory.get("THUMBNAIL"));
                if(thumbnail!=1){
                    //获得生成的缩略图的等比或者强制宽和高
                    String widthStr = ConvertUtil.obj2StrBlank(linkCategory.get("TITLEIMAGE_WIDTH"));
                    String heightStr = ConvertUtil.obj2StrBlank(linkCategory.get("TITLEIMAGE_HEIGHT"));
                    if(!widthStr.equals("") && !heightStr.equals("") && !widthStr.equals("0") && !heightStr.equals("0")) {
                        int width = ConvertUtil.obj2Int(widthStr);
                        int height = ConvertUtil.obj2Int(heightStr);
                        String pathPix = request.getSession().getServletContext().getRealPath("/");
                        boolean b = false;
                        if(thumbnail==3) {
                            b = true;
                        }
                        String newName = FileUtil.resizeForEqualImg(new File(pathPix+uploadUrl), width, height, b);
                        uploadUrl = uploadUrl.replace(uploadUrl.substring(uploadUrl.lastIndexOf("/")+1), newName);
                    }
                }
                link.setTitleimage(uploadUrl);
                
            } catch (MyFileUploadException e) {
                return false;
            }
        }
        
        //保存信息
        
        String idSql = "SELECT MAX(ID) FROM CMS_LINK";
        Integer idObj = baseDao.queryForInteger(idSql);
        int id = 1;
        if(null != idObj) {
            id = idObj;
        }
        
        String sql = "INSERT INTO CMS_LINK(ID,CATEGORY_ID,TITLE,TITLEIMAGE,THETOP,SUMMARY,CONTENT,THEURL,ORDERNUM,POSITION_ID,LINK_STATUS) " +
                     "VALUES (:id,:categoryId,:title,:titleimage,:thetop,:summary,:content,:theurl,:ordernum,:positionId,:status)";
 
        Map paramMap = new HashMap();
        paramMap.put("id", id+1);
        paramMap.put("categoryId", link.getCategoryId());
        paramMap.put("title", link.getTitle());
        paramMap.put("titleimage", link.getTitleimage());
        paramMap.put("thetop", link.getThetop());
        paramMap.put("summary", link.getSummary());
        paramMap.put("content", link.getContent());
        paramMap.put("theurl", link.getTheurl());
        paramMap.put("ordernum", link.getOrdernum());
        paramMap.put("positionId", link.getPositionId());
        paramMap.put("status", 1);
        baseDao.execute(sql, paramMap);
        return true;
    }
 
 
 
 
    @Override
    public Map queryLinkById(String linkId) {
        String sql = "SELECT * FROM CMS_LINK WHERE ID = :id";
        return baseDao.queryForMap(sql, new SqlParameter().addValue("id", linkId));
    }
 
 
 
 
    @Override
    public boolean updateLink(CmsLink link, HttpServletRequest request) {
        String[] allowType =  new String[]{"gif","jpg","jpeg","png"};
        int categoryId = link.getCategoryId();
        Map linkCategory = queryLinkCategoryById(ConvertUtil.obj2StrBlank(categoryId));
        //判断是否需要标题图片
        int titleimage = ConvertUtil.obj2Int(linkCategory.get("TITLEIMAGE"));
        
        String uploadUrl = new String();
        if(titleimage==1) {
            try {
                uploadUrl = FileUtil.uploadFile4SpringMVC(request, "image" ,"/upload/classify"+categoryId,allowType);
                if(null!=uploadUrl) {
                    //判断是否需要生成缩略图
                    int thumbnail = ConvertUtil.obj2Int(linkCategory.get("THUMBNAIL"));
                    if(thumbnail!=1){
                        //获得生成的缩略图的等比或者强制宽和高
                        String widthStr = ConvertUtil.obj2StrBlank(linkCategory.get("TITLEIMAGE_WIDTH"));
                        String heightStr = ConvertUtil.obj2StrBlank(linkCategory.get("TITLEIMAGE_HEIGHT"));
                        if(!widthStr.equals("") && !heightStr.equals("") && !widthStr.equals("0") && !heightStr.equals("0")) {
                            int width = ConvertUtil.obj2Int(widthStr);
                            int height = ConvertUtil.obj2Int(heightStr);
                            String pathPix = request.getSession().getServletContext().getRealPath("/");
                            boolean b = false;
                            if(thumbnail==3) {
                                b = true;
                            }
                            String newName = FileUtil.resizeForEqualImg(new File(pathPix+uploadUrl), width, height, b);
                            uploadUrl = uploadUrl.replace(uploadUrl.substring(uploadUrl.lastIndexOf("/")+1), newName);
                        }
                    }
                    link.setTitleimage(uploadUrl);
                }
                
            } catch (MyFileUploadException e) {
                return false;
            }
        }
        
        String sql = "UPDATE CMS_LINK SET TITLE = :title,TITLEIMAGE = :titleimage,THETOP = :thetop,SUMMARY = :summary,CONTENT = :content,THEURL = :theurl,ORDERNUM = :ordernum,POSITION_ID=:positionId WHERE ID = :id";
        Map parmaMap = new HashMap();
        parmaMap.put("title", link.getTitle());
        parmaMap.put("titleimage", link.getTitleimage());
        parmaMap.put("thetop", link.getThetop());
        parmaMap.put("content", link.getContent());
        parmaMap.put("summary", link.getSummary());
        parmaMap.put("theurl", link.getTheurl());
        parmaMap.put("ordernum", link.getOrdernum());
        parmaMap.put("positionId", link.getPositionId());
        parmaMap.put("id", link.getId());
        
        baseDao.execute(sql, parmaMap);
        return true;
        
    }
 
 
 
 
    @Override
    public void deleteLink(String id,String index) {
        String sql = "UPDATE CMS_LINK SET LINK_STATUS = :link_status WHERE ID = :id";
        Map param = new HashMap();
        param.put("link_status", index);
        param.put("id", id);
        baseDao.execute(sql,param);
    }
 
 
 
 
    @Override
    public Boolean queryMsgCountByCategoryId(String categoryId) {
        String sql = "SELECT COUNT(ID) FROM CMS_LINK WHERE CATEGORY_ID = :categoryId";
        int count = baseDao.queryForInteger(sql,new SqlParameter("categoryId",categoryId));
        if(count>0) {
            return false;
        }
        return true;
    }
 
 
 
 
    @Override
    public void updateState(int id, int state) {
        String sql = "UPDATE CMS_LINK SET LINK_STATUS=:state WHERE ID=:id";
        
        baseDao.execute(sql, new SqlParameter().addValue("state", state).addValue("id", id));
    }
 
 
 
 
    @Override
    public int querylinkCount(String categoryId) {
        StringBuilder builder = new StringBuilder("SELECT COUNT(ID) FROM CMS_LINK WHERE CATEGORY_ID = :categoryId ");
        Map paramMap = new HashMap();
        paramMap.put("categoryId", categoryId);
        
        
        int count = baseDao.queryForInteger(builder.toString(), paramMap);
        
        return count;
    }
 
 
 
 
    @Override
    public PageInfo querylinkData(PageInfo pageInfo,String categoryId) {
        StringBuilder builder = new StringBuilder("SELECT * FROM CMS_LINK WHERE CATEGORY_ID = :categoryId ");
        Map paramMap = new HashMap();
        paramMap.put("categoryId", categoryId);
        
        builder.append(" ORDER BY THETOP ASC,ORDERNUM ASC ");
        
        PageInfo info = baseDao.queryforSplitPageInfo(pageInfo, builder.toString(), paramMap);
        
        return info;
    }
}