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 queryLinkcateTree(HttpServletRequest request) { Map paramMap = new HashMap(); String selectSubSql = "SELECT * FROM CMS_LINK_CATEGORY ORDER BY LEVEL,ORDERNUM "; List cates = baseDao.queryForList(selectSubSql,paramMap); Map result = new HashMap(); List resultList = new LinkedList(); Map yjgnCache = new HashMap(); Map ejgnCache = new HashMap(); Map sjgnCache = new HashMap(); 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 ejgnList = new LinkedList(); 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 list = (List)yjgnMap.get("ejTree"); map.put("sjTree", new LinkedList()); 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 list = (List)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; } }