| | |
| | | package com.whyc.service; |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; |
| | | import com.whyc.dto.Response; |
| | | import com.whyc.mapper.AreaInfMapper; |
| | | import com.whyc.pojo.db_area.AreaInf; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import java.util.List; |
| | | |
| | |
| | | List<AreaInf> list=mapper.selectList(wrapper); |
| | | return new Response().setII(1,list!=null,list,"查询所有区域信息"); |
| | | } |
| | | //添加区域 |
| | | public Response addArea(Integer parentId, String areaName,String areaUname,String areaDescript) { |
| | | AreaInf ainf=new AreaInf(); |
| | | ainf.setParentId(0); |
| | | ainf.setAreaLevel(1); |
| | | ainf.setAreaPath(areaName); |
| | | ainf.setAreaName(areaName); |
| | | if(areaUname!=null){ |
| | | ainf.setAreaUname(areaUname); |
| | | } |
| | | if(areaDescript!=null){ |
| | | ainf.setAreaDescript(areaDescript); |
| | | } |
| | | if(parentId!=null){ |
| | | //获取上一级信息 |
| | | QueryWrapper wrapper=new QueryWrapper(); |
| | | wrapper.eq("id",parentId); |
| | | wrapper.last("limit 1"); |
| | | AreaInf painf=mapper.selectOne(wrapper); |
| | | ainf.setParentId(parentId); |
| | | ainf.setAreaLevel(painf.getAreaLevel()+1); |
| | | ainf.setAreaPath(painf.getAreaName()+"_"+areaName); |
| | | } |
| | | int flag= mapper.insert(ainf); |
| | | return new Response().set(1,flag>0); |
| | | } |
| | | //删除区域 |
| | | public Response delArea(Integer id) { |
| | | delAll(id); |
| | | return new Response().set(1,true); |
| | | } |
| | | @Transactional |
| | | public void delAll(Integer id) { |
| | | //获取上一级信息 |
| | | QueryWrapper wrapper=new QueryWrapper(); |
| | | wrapper.eq("parent_id",id); |
| | | wrapper.last("limit 1"); |
| | | AreaInf painf=mapper.selectOne(wrapper); |
| | | if(painf!=null){ |
| | | delAll(painf.getId()); |
| | | } |
| | | UpdateWrapper wrapper1=new UpdateWrapper(); |
| | | wrapper1.eq("id",id); |
| | | mapper.delete(wrapper1); |
| | | } |
| | | //修改区域 |
| | | public Response updateArea(Integer id, String areaName, String areaUname, String areaDescript) { |
| | | QueryWrapper wrapper=new QueryWrapper(); |
| | | wrapper.eq("id",id); |
| | | wrapper.last("limit 1"); |
| | | AreaInf ainf=mapper.selectOne(wrapper); |
| | | ainf.setAreaUname(areaUname); |
| | | ainf.setAreaDescript(areaDescript); |
| | | ainf.setAreaName(areaName); |
| | | String oldPath=ainf.getAreaPath(); |
| | | String newPath=oldPath.substring(0,oldPath.lastIndexOf("_")+1)+areaName; |
| | | UpdateWrapper wrapper1=new UpdateWrapper(); |
| | | wrapper1.eq("id",id); |
| | | mapper.update(ainf,wrapper1); |
| | | updateAll(id,newPath); |
| | | return new Response().set(1,true); |
| | | } |
| | | public void updateAll(Integer id, String newPath) { |
| | | QueryWrapper wrapper=new QueryWrapper(); |
| | | wrapper.eq("parent_id",id); |
| | | wrapper.last("limit 1"); |
| | | AreaInf painf=mapper.selectOne(wrapper); |
| | | if(painf!=null){ |
| | | String path=newPath+"_"+painf.getAreaName(); |
| | | updateAll(painf.getId(),path); |
| | | } |
| | | UpdateWrapper wrapper2=new UpdateWrapper(); |
| | | wrapper2.set("area_path",newPath); |
| | | wrapper2.eq("id",id); |
| | | mapper.update(null,wrapper2); |
| | | } |
| | | } |