import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import java.lang.reflect.Field; import java.util.Date; import java.util.List; import java.util.stream.Collectors; /** * 小区Controller * <p> * Created by chenqh on 2021-03-26 16:25:01. */ @Controller @Api(tags = "SubdistrictController", description = "小区配置") @RequestMapping("/subdistrict") public class SubdistrictController extends BaseController { @Autowired private SubdistrictService subdistrictService; @ApiOperation("添加小区") @RequestMapping(value = "/create", method = RequestMethod.POST) @ResponseBody public CommonResult create(@Validated @RequestBody Subdistrict param) { SysSubdistrict subdistrict = getSysSubdistrict(param, false); int count = subdistrictService.create(subdistrict); if (count > 0) { return CommonResult.success(count); } return CommonResult.failed(); } @ApiOperation("修改小区") @RequestMapping(value = "/update/{id}", method = RequestMethod.PUT) @ResponseBody public CommonResult update(@PathVariable Long id, @Validated @RequestBody SubdistrictEnable param) { SysSubdistrict subdistrict = getSysSubdistrict(param, true); int count = subdistrictService.update(id, subdistrict); if (count > 0) { return CommonResult.success(count); } return CommonResult.failed(); } @ApiOperation("删除小区") @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE) @ResponseBody public CommonResult delete(@PathVariable Long id) { int count = subdistrictService.delete(id); if (count > 0) { return CommonResult.success(count); } return CommonResult.failed(); } @ApiOperation("分页查询全部小区") @RequestMapping(value = "/list", method = RequestMethod.GET) @ResponseBody public CommonResult<CommonPage<SubdistrictVo>> list( @ApiParam("小区名称") @RequestParam(value = "subDisName", required = false) String subDisName, @ApiParam("片区名称") @RequestParam(value = "secName", required = false) String secName, @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) { List<SubdistrictVo> reasonList = subdistrictService.list(subDisName, secName, pageSize, pageNum); return CommonResult.success(CommonPage.restPage(reasonList)); } @ApiOperation("根据小区ID获取片区") @RequestMapping(value = "/getSectionsById", method = RequestMethod.GET) @ResponseBody public CommonResult<List<BaseVo>> getSectionsById( @ApiParam("小区ID") @RequestParam("id") Long id) { return CommonResult.success(subdistrictService.getSectionsById(id)); } @ApiOperation("获取所有小区") @RequestMapping(value = "/listAll", method = RequestMethod.GET) @ResponseBody public CommonResult<List<BaseVo>> listAll() { return CommonResult.success(subdistrictService.listAll()); } /** * 获取小区信息 * * @param param * @return */ private SysSubdistrict getSysSubdistrict(SubdistrictBase param, boolean edit) { String userName = getUserName(); SysSubdistrict subdistrict = new SysSubdistrict(); if (edit) {//编辑 SubdistrictEnable p = (SubdistrictEnable) param; subdistrict.setName(null); subdistrict.setSections(p.getSections().stream().map(e -> { BaseEnableVo enableVo = new BaseEnableVo(); enableVo.setName(e.getName()); enableVo.setIsEnable(e.getIsEnable()); return enableVo; }).collect(Collectors.toList())); } else { Subdistrict p = (Subdistrict) param; subdistrict.setCreateTime(new Date()); subdistrict.setCreateBy(userName); subdistrict.setName(p.getName()); subdistrict.setSections(p.getSectionNames().stream().map(e -> { BaseEnableVo enableVo = new BaseEnableVo(); enableVo.setName(e); return enableVo; }).collect(Collectors.toList())); } subdistrict.setDeleteStatus(false); subdistrict.setUpdateTime(new Date()); subdistrict.setUpdateBy(userName); subdistrict.setDirector(param.getDirector()); subdistrict.setPrincipal(param.getPrincipal()); return subdistrict; } }
import org.springframework.transaction.annotation.Transactional; import java.util.List; /** * 小区管理Service * Created by chenqh on 2021-03-26 16:25:01. */ public interface SubdistrictService { /** * 添加小区 */ @Transactional int create(SysSubdistrict subdistrict); /** * 修改小区 */ @Transactional int update(Long id, SysSubdistrict subdistrict); /** * 删除小区 */ @Transactional int delete(Long id); /** * 分页获取小区 */ List<SubdistrictVo> list(String subDisName, String secName, Integer pageSize, Integer pageNum); /** * 根据ID获取所有区域列表 */ List<BaseVo> getSectionsById(Long id); List<BaseVo> listAll(); }
import com.github.pagehelper.PageHelper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; import java.util.*; import java.util.stream.Collectors; /** * 小区Service实现类 * Created by chenqh on 2021-03-26 16:25:01. */ @Service public class SubdistrictServiceImpl implements SubdistrictService { @Autowired private SysSubdistrictMapper subdistrictMapper; @Autowired private SysSectionMapper sectionMapper; @Autowired private SysAreaMapper areaMapper; @Override public int create(SysSubdistrict subdistrict) { //验证名称不能重复 SysSubdistrictExample example = new SysSubdistrictExample(); example.createCriteria().andNameEqualTo(subdistrict.getName()); List<SysSubdistrict> sysSubdistricts = subdistrictMapper.selectByExample(example); if (!CollectionUtils.isEmpty(sysSubdistricts)) { Asserts.fail("小区名称已存在!"); } List<BaseEnableVo> sections = subdistrict.getSections(); List<String> neams = sections.stream().map(e -> e.getName()).collect(Collectors.toList()); Set<String> sNeams = sections.stream().map(e -> e.getName()).collect(Collectors.toSet()); if (sNeams.size() != neams.size()) { Asserts.fail("片区名称重复,请核对数据!"); } subdistrictMapper.insert(subdistrict); addOrUpdateSecionts(subdistrict, false); return 1; } @Override public int update(Long id, SysSubdistrict subdistrict) { subdistrict.setId(id); addOrUpdateSecionts(subdistrict, true); return subdistrictMapper.updateByPrimaryKeySelective(subdistrict); } /** * 添加/更新片区 * * @param subdistrict */ private void addOrUpdateSecionts(SysSubdistrict subdistrict, boolean edit) { List<BaseEnableVo> pSections = subdistrict.getSections(); String userBy = subdistrict.getUpdateBy(); Long id = subdistrict.getId(); if (edit) { SysSectionExample example = new SysSectionExample(); example.createCriteria().andSubdisIdEqualTo(id); List<SysSection> sections = sectionMapper.selectByExample(example); List<Long> secIds = sections.stream().map(e -> e.getId()) .collect(Collectors.toList()); List<String> secNames = sections.stream().map(e -> e.getName()) .collect(Collectors.toList()); //验证是否关联 SysAreaExample areaExample = new SysAreaExample(); areaExample.createCriteria().andSecIdIn(secIds); List<SysArea> sysAreas = areaMapper.selectByExample(areaExample); List<Long> relSelIds = sysAreas