/* 顶级分类默认值:100 一级分类:101100(自然数增加) 二级分类:101100100(前6位是父类ID,后3位取当前分类下的最大值加1,从100--999) 顶级分类:100 一级分类:101101(自然数增加) 二级分类:101101100(前6位是父类ID,后3位取当前分类下的最大值加1,从100--999) 每个二级分类下可添加900个子类。 */
1 //新增分类 2 public String addCategory(Category cate) { 3 try { 4 //1.取模块代码 5 String parentid=cate.getParentid(); 6 int begin=parentid.length()+1; 7 String sql1=""; 8 int flag = 0; 9 //顶级节点为100,一级节点 10 if(parentid.equals("100")){ 11 sql1="SELECT (CASE WHEN MAX(TO_NUMBER(CATID)) IS NULL THEN 100 ELSE MAX(TO_NUMBER(CATID)) END)+1 FROM MALL_CATEGORY WHERE PARENTID='100'"; 12 log.info("==addCategory:===="+sql1+"========"); 13 Long maxcatid=this.getJdbcTemplate().queryForLong(sql1); 14 cate.setCatid(maxcatid.toString()); 15 cate.setParentid("100"); 16 flag = 3; 17 } 18 //非一级节点 19 else{ 20 sql1 = "select min(catid) from mall_category where parentid='"+parentid+"' and status=2"; 21 Long mincatid=this.getJdbcTemplate().queryForLong(sql1); 22 if(mincatid != 0L){ 23 //使用删除的子分类ID作为新的ID,跟新数据 24 cate.setCatid(mincatid.toString()); 25 flag = 1; 26 }else{ 27 //获取所有分类ID 28 List<Category> listCatid = findAllCatid(parentid); 29 HashSet<String> set = new HashSet<String>(); 30 Category item = null; 31 if(!StringUtils.isEmpty(listCatid)){ 32 //该父分类下已有子分类,使用未使用过的子分类ID 33 for(int a = 0 ;a<listCatid.size();a++){ 34 item = listCatid.get(a); 35 set.add(item.getCatid()); 36 } 37 for(int a = 101;a <= 1000;a++){ 38 if(a==1000){ 39 flag = 2; 40 break; 41 } 42 if(!set.contains(a+"")){ 43 cate.setCatid(parentid+a); 44 flag = 3; 45 break; 46 } 47 } 48 }else{ 49 //该父分类下首次增加子分类 50 cate.setCatid(parentid+100); 51 flag = 3; 52 } 53 } 54 } 55 //2.取模块顺序 56 String sql2="SELECT (CASE WHEN MAX(TO_NUMBER(LISTORDER)) IS NULL THEN 0 ELSE MAX(TO_NUMBER(LISTORDER)) END)+1 FROM MALL_CATEGORY WHERE parentid='"+cate.getParentid()+"'"; 57 log.info("==addCategory:===="+sql2+"========"); 58 Integer listorder=this.getJdbcTemplate().queryForInt(sql2); 59 cate.setListorder(listorder); 60 cate.setStatus(1); 61 String sql = "",catid = ""; 62 if(flag != 2){ 63 if(flag == 3){ 64 //新增 65 sql = "INSERT INTO MALL_CATEGORY(CATID,CATNAME,PARENTID,LISTORDER,THUMB,STATUS,CATNAME2) VALUES('"+cate.getCatid()+"','"+cate.getCatname()+"','"+cate.getParentid()+"',"+cate.getListorder()+",'"+cate.getThumb()+"',"+cate.getStatus()+",'"+cate.getCatname2()+"')"; 66 }else if(flag == 1){ 67 //修改 68 sql = "update MALL_CATEGORY set CATNAME='"+cate.getCatname()+"',LISTORDER="+cate.getListorder()+",THUMB='"+cate.getThumb()+"',STATUS="+cate.getStatus()+",CATNAME2='"+cate.getCatname2()+"' where catid ='"+cate.getCatid()+"'"; 69 } 70 log.info("sql:"+sql); 71 this.getJdbcTemplate().execute(sql); 72 catid = cate.getCatid(); 73 }else{ 74 catid = ""; 75 } 76 return catid; 77 } catch (Exception e) { 78 log.info("==CategoryDao:addCategory==异常=====msg:"+e.getMessage()); 79 return ""; 80 } 81 }