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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package cn.ksource.web.facade.linkcate;
 
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
 
import javax.servlet.http.HttpServletRequest;
 
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import cn.ksource.beans.CMS_LINK_CATEGORY;
import cn.ksource.core.dao.BaseDao;
import cn.ksource.core.page.PageInfo;
import cn.ksource.core.util.AjaxUtil;
import cn.ksource.core.util.ConvertUtil;
import cn.ksource.core.util.JsonUtil;
import cn.ksource.core.util.StringUtil;
import cn.ksource.web.Constants;
 
@Service("linkCateFacade")
public class LinkCateFacadeImpl implements LinkCateFacade {
    
    @Autowired
    private BaseDao baseDao;
 
    @Override
    public int checkName(HttpServletRequest request) {
        String id = request.getParameter("id");
        Map map=new HashMap();
        String up_id=null;
        if(!"".equals(id)){
             String sql="select * from CMS_LINK_CATEGORY where ID="+Integer.parseInt(id);
             map=baseDao.queryForMap(sql);
             //获取上一级ID
            if(map!=null){
                up_id=map.get("ID").toString();
            }
        }
        String name = request.getParameter("category_name");
        if(name!=null&&!"".equals(name)){
            name = AjaxUtil.decode(name).trim();
        }
        
        StringBuilder t_sql = new StringBuilder();
        t_sql.append("select count(*) from CMS_LINK_CATEGORY where CATEGORY_NAME='").append(name).append("' ");
        if (StringUtils.isNotBlank(id)) {
            
            t_sql.append(" and ID <> ").append(Integer.parseInt(id)).append(" ");
        }
        if (StringUtils.isNotBlank(up_id)) {
            t_sql.append(" and UP_ID <> ").append(Integer.parseInt(up_id)).append(" ");
        }
        int count=baseDao.queryForInteger(t_sql.toString());
        return count;
    }
 
    @Override
    public PageInfo queryLinkcateData(PageInfo pageInfo,String pId) {
        StringBuilder builder = new StringBuilder("SELECT * FROM CMS_LINK_CATEGORY  WHERE 1 = 1 ");
        Map paramMap = new HashMap();
        if(StringUtil.notEmpty(pId)) {
            builder.append(" AND UP_ID = :pId ");
            paramMap.put("pId", pId);
        } else {
            builder.append(" AND LEVEL = 1 ");
        }
        
        builder.append(" ORDER BY LEVEL,ORDERNUM ");
        
        PageInfo info = baseDao.queryforSplitPageInfo(pageInfo, builder.toString(), paramMap);
        
        return info;
    }
 
    @Override
    public int queryLinkcateCount(String pId) {
        StringBuilder builder = new StringBuilder("SELECT COUNT(ID) FROM CMS_LINK_CATEGORY  WHERE 1 = 1 ");
        Map paramMap = new HashMap();
        if(StringUtil.notEmpty(pId)) {
            builder.append(" AND UP_ID = :pId ");
            paramMap.put("pId", pId);
        } else {
            builder.append(" AND LEVEL = 1 ");
        }
        
        int count = baseDao.queryForInteger(builder.toString(),paramMap);
        
        return count;
    }
 
    @Override
    public List<Map> queryLinkcateTree(HttpServletRequest request) {
        Map paramMap = new HashMap();
        String selectSubSql = "SELECT * FROM CMS_LINK_CATEGORY  ORDER BY LEVEL,ORDERNUM ";
        List<Map> cates = baseDao.queryForList(selectSubSql,paramMap);
        Map result = new HashMap();
        
        List<Map> resultList = new LinkedList<Map>();
        
        Map<String, Map> yjgnCache = new HashMap<String, Map>();
        Map<String, Map> ejgnCache = new HashMap<String, Map>();
        Map<String, Map> sjgnCache = new HashMap<String, Map>();
        
        for (Map map : cates) {
            String level = map.get("LEVEL").toString();
            String id = map.get("ID").toString();
            
            //一级树
            if (level.equalsIgnoreCase("1")) {
                yjgnCache.put(id, map);
                List<Map> ejgnList = new LinkedList<Map>();
                map.put("ejTree", ejgnList);
                resultList.add(map);
                continue;
            }
            //二级树
            if (level.equalsIgnoreCase("2")) {
                String pId = map.get("UP_ID").toString();
                if(yjgnCache.containsKey(pId)) {
                    Map yjgnMap = yjgnCache.get(pId);
                    List<Map> list = (List<Map>)yjgnMap.get("ejTree");
                    map.put("sjTree", new LinkedList<Map>());
                    list.add(map);
                    
                    ejgnCache.put(id, map);
                }
                continue;
            }
            //三级树
            if (level.equalsIgnoreCase("3")) {
                if(null!=map.get("UP_ID")){
                    String pId = map.get("UP_ID").toString();
                    if(ejgnCache.containsKey(pId)) {
                        Map ejgnMap = ejgnCache.get(map.get("UP_ID").toString());
                        List<Map> list = (List<Map>)ejgnMap.get("sjTree");
                        list.add(map);
                    }
                }
            }
        }
        System.out.println(JsonUtil.list2Json(resultList));
        return resultList;
    }
 
    @Override
    public void saveLinkcate(HttpServletRequest request) {
        int level=0;
        String id = request.getParameter("id");
        String pId = request.getParameter("up_id");
        String category_name = request.getParameter("category_name");
        String ordernum = request.getParameter("ordernum");
        String titleimage = request.getParameter("titleimage");
        String content = request.getParameter("content");
        String summary = request.getParameter("summary");
        String titleimage_width = request.getParameter("titleimage_width");
        String titleimage_height = request.getParameter("titleimage_height");
        String thumbnail = request.getParameter("thumbnail");
        String note = request.getParameter("note");
        if(StringUtil.isBlank(id)){
            if (StringUtil.isEmpty(pId)||Constants.TOP_ID.equals(pId)) {
                level=1;
            } else {
                level=2;
            }
            int num=Integer.parseInt(ordernum);
            int title=Integer.parseInt(titleimage);
            int con=Integer.parseInt(content);
            int sum = Integer.parseInt(summary);
            int width=0;
            int height=0;
            if(titleimage_width!=null){
                width=Integer.parseInt(titleimage_width);
                height=Integer.parseInt(titleimage_height);
            }
            
            String idSql = "SELECT MAX(ID) FROM CMS_LINK_CATEGORY";
            Integer idObj = baseDao.queryForInteger(idSql);
            int saveId = 1;
            if(null != idObj) {
                saveId = idObj;
            }
            
            int thumb=Integer.parseInt(thumbnail);
            Map map=new HashMap();
            map.put("id", saveId+1);
            map.put("category_name", category_name);
            map.put("level", level);
            if(StringUtil.isEmpty(pId)){
                
                map.put("up_id", "-1");
            }else{
                map.put("up_id", pId);
            }
            map.put("ordernum", num);
            map.put("titleimage", title);
            map.put("content", con);
            map.put("summary", sum);
            map.put("titleimage_width",width);
            map.put("titleimage_height",height);
            map.put("thumbnail", thumb);
            map.put("note", note);
            StringBuffer sBuffer = new StringBuffer("insert into CMS_LINK_CATEGORY(");
            StringBuffer fileds = new StringBuffer("ID,CATEGORY_NAME,");
            StringBuffer values = new StringBuffer(":id,:category_name,");        
                fileds.append("LEVEL,");
                values.append(":level,");
                fileds.append("UP_ID,");
                values.append(":up_id,");
                fileds.append("ORDERNUM,");
                values.append(":ordernum,");
                fileds.append("TITLEIMAGE,");
                values.append(":titleimage,");
                fileds.append("TITLEIMAGE_WIDTH,");
                values.append(":titleimage_width,");
                fileds.append("TITLEIMAGE_HEIGHT,");
                values.append(":titleimage_height,");
                fileds.append("CONTENT,");
                values.append(":content,");
                fileds.append("SUMMARY,");
                values.append(":summary,");
                fileds.append("THUMBNAIL,");
                values.append(":thumbnail,");
                fileds.append("NOTE,");
                values.append(":note,");
            sBuffer.append(StringUtils.removeEnd(fileds.toString(), ",") + ") values("+StringUtils.removeEnd(values.toString(), ",")+")");
            
            baseDao.execute(sBuffer.toString(),map);
        }else{
            int ID=Integer.parseInt(id);
            int num=Integer.parseInt(ordernum);
            int title=Integer.parseInt(titleimage);
            int con=Integer.parseInt(content);
            int sum = Integer.parseInt(summary);
            int width=0;
            int height=0;
            if(titleimage_width!=null){
                width=Integer.parseInt(titleimage_width);
                height=Integer.parseInt(titleimage_height);
            }
            
            int thumb=Integer.parseInt(thumbnail);
            Map map=new HashMap();
            map.put("category_name", category_name);
            map.put("ordernum", num);
            map.put("titleimage", title);
            map.put("titleimage_width",width);
            map.put("titleimage_height",height);
            map.put("content", con);
            map.put("summary", sum);
            map.put("thumbnail", thumb);
            map.put("note", note);
            StringBuffer sBuffer = new StringBuffer("update CMS_LINK_CATEGORY set ");
                sBuffer.append("CATEGORY_NAME=:category_name,");
                sBuffer.append("ORDERNUM=:ordernum,");
                sBuffer.append("TITLEIMAGE=:titleimage,");
                sBuffer.append("TITLEIMAGE_WIDTH=:titleimage_width,");
                sBuffer.append("TITLEIMAGE_HEIGHT=:titleimage_height,");
                sBuffer.append("CONTENT=:content,");
                sBuffer.append("SUMMARY=:summary,");
                sBuffer.append("THUMBNAIL=:thumbnail,");
                sBuffer.append("NOTE=:note,");
            String sql=StringUtils.removeEnd(sBuffer.toString(), ",") + " where id="+ID;
            baseDao.execute(sql,map);
        }
        
    }
 
    @Override
    public Map deletelinkcate(HttpServletRequest request) {
        String id = request.getParameter("id");
        String sql = "SELECT COUNT(ID) FROM CMS_LINK_CATEGORY WHERE UP_ID = :up_id ";
        Map param = new HashMap();
        param.put("up_id", id);
        int count = baseDao.queryForInteger(sql, param);
        
        String firstCate = new String();
        String secondCate = new String();
        CMS_LINK_CATEGORY cate = new CMS_LINK_CATEGORY(id).getInstanceById();
        String sjbh = ConvertUtil.obj2StrBlank(cate.getUp_id());
        if(StringUtil.notEmpty(sjbh)&&!Constants.TOP_ID.equals(sjbh)) {
            CMS_LINK_CATEGORY pcate = new CMS_LINK_CATEGORY(sjbh).getInstanceById();
            int level = pcate.getLevel();
            if(level==2) {
                firstCate = ConvertUtil.obj2StrBlank(pcate.getUp_id());
                secondCate = pcate.getId();
            } else {
                firstCate = pcate.getId();
            }
        }
        Map result = new HashMap();
        if(count > 0){
            result.put("msg", "存在子分类,不能删除");
        }else{
            cate.deleteById();
            result.put("msg", "1");
        }
        result.put("firstCate", firstCate);
        result.put("secondCate", secondCate);
        return result;
    }
 
}