package com.iden.bms.controller; import com.baomidou.mybatisplus.core.metadata.IPage; import com.iden.bms.service.CommunityService; import com.iden.common.annotation.Permission; import com.iden.common.exception.BDException; import com.iden.common.logaspect.LogAnnotation; import com.iden.common.logaspect.OperateType; import com.iden.common.util.PageResult; import com.iden.common.util.Result; import com.iden.common.util.WebPageUtils; import com.iden.common.vo.PageReqVO; import com.iden.common.vo.CommunityVO; import com.iden.common.vo.UserLoginedConvertVO; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.List; /** * @Author: lilt * @Date: 2021/5/26 * @Desc: */ @RestController @Api(value = "CommunityController", tags = { "小区管理" }) @Slf4j @RequestMapping("/bms/community") @Permission public class CommunityController { @Autowired private CommunityService communityService; @GetMapping("/countCommunity") @ApiOperation(value = "当前小区个数查询") @ApiImplicitParams(value = { @ApiImplicitParam(paramType = "query", name = "district", value = "所属区域"), @ApiImplicitParam(paramType = "query", name = "subdistrict", value = "所属街道"), @ApiImplicitParam(paramType = "query", name = "name", value = "小区名称") }) public Result countCommunity(HttpServletRequest request, @RequestHeader(value = "token") String token, @RequestParam(value = "district", required = false) String district, @RequestParam(value = "subdistrict", required = false) String subdistrict, @RequestParam(value = "name", required = false) String name){ try { UserLoginedConvertVO loginUser = WebPageUtils.getCurrentLoginedUser(request); Integer count = this.communityService.countCommunity(district,subdistrict,name,loginUser); return Result.success("查询成功!",count); }catch (BDException e) { log.error("当前小区个数查询-出现异常",e); return PageResult.error(e.getMessage()); } catch (Exception e) { log.error("小区管理: 当前小区个数查询出现异常",e); return PageResult.error( "获取数据失败"); } } @GetMapping("/listCommunity") @ApiOperation(value = "小区列表分页 ") @ApiImplicitParams(value = { @ApiImplicitParam(paramType = "query", name = "district", value = "所属区域"), @ApiImplicitParam(paramType = "query", name = "subdistrict", value = "所属街道"), @ApiImplicitParam(paramType = "query", name = "name", value = "小区名称") }) public PageResult> listCommunity(HttpServletRequest request, @RequestHeader(value = "token") String token, @RequestParam(value = "district", required = false) String district, @RequestParam(value = "subdistrict", required = false) String subdistrict, @RequestParam(value = "name", required = false) String name, PageReqVO pageReqVo){ try { UserLoginedConvertVO loginUser = WebPageUtils.getCurrentLoginedUser(request); IPage pageResponse = this.communityService.listCommunity(district,subdistrict,name,loginUser ,pageReqVo); return PageResult.success(pageResponse.getRecords(),pageResponse.getCurrent(),pageResponse.getSize(),pageResponse.getTotal()); }catch (BDException e) { log.error("小区列表查询-分页列表出现异常",e); return PageResult.error(e.getMessage()); } catch (Exception e) { log.error("小区管理: 小区列表查询出现异常",e); return PageResult.error( "获取列表失败"); } } /** * 导出列表 * @param * @return */ @ApiOperation(value = "导出Excel") @ApiImplicitParams(value = { @ApiImplicitParam(paramType = "query", name = "district", value = "所属区域"), @ApiImplicitParam(paramType = "query", name = "subdistrict", value = "所属街道"), @ApiImplicitParam(paramType = "query", name = "name", value = "小区名称") }) @GetMapping({"/exportToExcel"}) @LogAnnotation( type = OperateType.EXPORT, moduleName = "导出小区列表", description = "导出小区列表" ) public void exportToExcel(HttpServletRequest request,@RequestHeader(name = "token", required = true) String token, @RequestParam(value = "district", required = false) String district, @RequestParam(value = "subdistrict", required = false) String subdistrict, @RequestParam(value = "name", required = false) String name, HttpServletResponse response) { try { UserLoginedConvertVO loginUser = WebPageUtils.getCurrentLoginedUser(request); this.communityService.exportToExcel(district,subdistrict,name,loginUser ,response); } catch (BDException e) { log.error("导出小区列表出现异常",e); } catch (Exception e) { log.error("小区管理: 导出小区列表出现异常",e); } } @ApiOperation(value = "导入Excel") @PostMapping(value = "/importWithExcel", headers = "content-type=multipart/form-data") public Result importWithExcel( @RequestHeader(name = "token") String token, @RequestParam(value = "file") MultipartFile multipartFile) { try { boolean flag = communityService.importWithExcel(multipartFile); if(flag){ return Result.success("导入成功!"); } else { return Result.error("导入失败"); } } catch (BDException e) { log.error("导入小区列表出现异常",e); return Result.error(e.getMessage()); } catch (Exception e) { log.error("小区管理: 导入小区列表出现异常",e); return Result.error("导入小区失败!"); } } @PostMapping("/addCommunity") @ApiOperation(value = "新增小区") @LogAnnotation( type = OperateType.ADD, moduleName = "新增小区", description = "新增小区" ) public Result addCommunity(HttpServletRequest request,@RequestHeader("token") String token, @RequestBody CommunityVO vo){ try { UserLoginedConvertVO loginUser = WebPageUtils.getCurrentLoginedUser(request); int flag = this.communityService.createCommunity(vo,loginUser); if (flag == 1) { return Result.error("名称已存在!"); } if (flag == 2) { return Result.error("编码已存在!"); } else if (flag == 0) { return Result.success("新增成功!"); } else { return Result.error("新增失败!"); } }catch (BDException e) { log.error("新增小区-出现异常",e); return Result.error(e.getMessage()); } catch (Exception e) { log.error("小区管理: 新增小区出现异常",e); return Result.error("新增失败!"); } } @PostMapping("/updateCommunity") @ApiOperation(value = "修改小区") @LogAnnotation( type = OperateType.EDIT, moduleName = "修改小区", description = "修改小区" ) public Result updateCommunity(HttpServletRequest request,@RequestHeader("token") String token, @RequestBody CommunityVO vo){ try { this.communityService.updateCommunity(vo); return Result.success("修改成功!"); }catch (BDException e) { log.error("修改小区-出现异常",e); return Result.error(e.getMessage()); } catch (Exception e) { log.error("小区管理: 修改小区出现异常",e); return Result.error("修改失败!"); } } @GetMapping("/getCommunityInfo/{id}") @ApiOperation(value = "小区详情") public Result getCommunityInfo(HttpServletRequest request, @RequestHeader("token") String token, @PathVariable("id") Long id){ CommunityVO orderInfo = this.communityService.getCommunityById(id); return Result.success("查询成功!",orderInfo); } @PostMapping("/deleteCommunity/{id}") @ApiOperation(value = "删除小区") @LogAnnotation( type = OperateType.REMOVE, moduleName = "删除小区", description = "删除小区" ) public Result deleteCommunity(HttpServletRequest request,@RequestHeader("token") String token, @PathVariable("id") Long id){ try { if( this.communityService.deleteById(id)){ return Result.success("删除成功!"); } else { return Result.error("小区被引用,删除失败!"); } } catch (Exception e) { log.error("小区管理: 删除出现异常",e); return Result.error("删除失败!"); } } }