Kaynağa Gözat

:zap: 行政区划增加导入导出功能

smallchill 6 yıl önce
ebeveyn
işleme
9bca74d823

+ 55 - 0
src/main/java/org/springblade/modules/system/controller/RegionController.java

@@ -16,23 +16,32 @@
  */
 package org.springblade.modules.system.controller;
 
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
 import io.swagger.annotations.*;
 import lombok.AllArgsConstructor;
 import org.springblade.core.boot.ctrl.BladeController;
+import org.springblade.core.excel.util.ExcelUtil;
 import org.springblade.core.launch.constant.AppConstant;
 import org.springblade.core.mp.support.Condition;
 import org.springblade.core.mp.support.Query;
 import org.springblade.core.tool.api.R;
+import org.springblade.core.tool.utils.DateUtil;
 import org.springblade.modules.system.entity.Region;
+import org.springblade.modules.system.excel.RegionExcel;
+import org.springblade.modules.system.excel.RegionImporter;
 import org.springblade.modules.system.service.IRegionService;
 import org.springblade.modules.system.vo.RegionVO;
 import org.springblade.modules.system.wrapper.RegionWrapper;
 import org.springframework.web.bind.annotation.*;
+import org.springframework.web.multipart.MultipartFile;
 import springfox.documentation.annotations.ApiIgnore;
 
+import javax.servlet.http.HttpServletResponse;
 import javax.validation.Valid;
+import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 
@@ -142,5 +151,51 @@ public class RegionController extends BladeController {
 		return R.status(regionService.removeRegion(id));
 	}
 
+	/**
+	 * 行政区划下拉数据源
+	 */
+	@GetMapping("/select")
+	@ApiOperationSupport(order = 9)
+	@ApiOperation(value = "下拉数据源", notes = "传入tenant")
+	public R<List<Region>> select(@RequestParam(required = false, defaultValue = "00") String code) {
+		List<Region> list = regionService.list(Wrappers.<Region>query().lambda().eq(Region::getParentCode, code));
+		return R.data(list);
+	}
+
+	/**
+	 * 导入行政区划数据
+	 */
+	@PostMapping("import-region")
+	@ApiOperationSupport(order = 10)
+	@ApiOperation(value = "导入行政区划", notes = "传入excel")
+	public R importRegion(MultipartFile file, Integer isCovered) {
+		RegionImporter regionImporter = new RegionImporter(regionService, isCovered == 1);
+		ExcelUtil.save(file, regionImporter, RegionExcel.class);
+		return R.success("操作成功");
+	}
+
+	/**
+	 * 导出行政区划数据
+	 */
+	@GetMapping("export-region")
+	@ApiOperationSupport(order = 11)
+	@ApiOperation(value = "导出行政区划", notes = "传入user")
+	public void exportRegion(@ApiIgnore @RequestParam Map<String, Object> region, HttpServletResponse response) {
+		QueryWrapper<Region> queryWrapper = Condition.getQueryWrapper(region, Region.class);
+		List<RegionExcel> list = regionService.exportRegion(queryWrapper);
+		ExcelUtil.export(response, "行政区划数据" + DateUtil.time(), "行政区划数据表", list, RegionExcel.class);
+	}
+
+	/**
+	 * 导出模板
+	 */
+	@GetMapping("export-template")
+	@ApiOperationSupport(order = 12)
+	@ApiOperation(value = "导出模板")
+	public void exportUser(HttpServletResponse response) {
+		List<RegionExcel> list = new ArrayList<>();
+		ExcelUtil.export(response, "行政区划模板", "行政区划表", list, RegionExcel.class);
+	}
+
 
 }

+ 90 - 0
src/main/java/org/springblade/modules/system/excel/RegionExcel.java

@@ -0,0 +1,90 @@
+/*
+ *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions are met:
+ *
+ *  Redistributions of source code must retain the above copyright notice,
+ *  this list of conditions and the following disclaimer.
+ *  Redistributions in binary form must reproduce the above copyright
+ *  notice, this list of conditions and the following disclaimer in the
+ *  documentation and/or other materials provided with the distribution.
+ *  Neither the name of the dreamlu.net developer nor the names of its
+ *  contributors may be used to endorse or promote products derived from
+ *  this software without specific prior written permission.
+ *  Author: Chill 庄骞 (smallchill@163.com)
+ */
+package org.springblade.modules.system.excel;
+
+import com.alibaba.excel.annotation.ExcelProperty;
+import com.alibaba.excel.annotation.write.style.ColumnWidth;
+import com.alibaba.excel.annotation.write.style.ContentRowHeight;
+import com.alibaba.excel.annotation.write.style.HeadRowHeight;
+import lombok.Data;
+
+import java.io.Serializable;
+
+/**
+ * RegionExcel
+ *
+ * @author Chill
+ */
+@Data
+@ColumnWidth(16)
+@HeadRowHeight(20)
+@ContentRowHeight(18)
+public class RegionExcel implements Serializable {
+	private static final long serialVersionUID = 1L;
+
+	@ExcelProperty("区划编号")
+	private String code;
+
+	@ExcelProperty("父区划编号")
+	private String parentCode;
+
+	@ExcelProperty("祖区划编号")
+	private String ancestors;
+
+	@ExcelProperty("区划名称")
+	private String name;
+
+	@ExcelProperty("省级区划编号")
+	private String provinceCode;
+
+	@ExcelProperty("省级名称")
+	private String provinceName;
+
+	@ExcelProperty("市级区划编号")
+	private String cityCode;
+
+	@ExcelProperty("市级名称")
+	private String cityName;
+
+	@ExcelProperty("区级区划编号")
+	private String districtCode;
+
+	@ExcelProperty("区级名称")
+	private String districtName;
+
+	@ExcelProperty("镇级区划编号")
+	private String townCode;
+
+	@ExcelProperty("镇级名称")
+	private String townName;
+
+	@ExcelProperty("村级区划编号")
+	private String villageCode;
+
+	@ExcelProperty("村级名称")
+	private String villageName;
+
+	@ExcelProperty("层级")
+	private Integer level;
+
+	@ExcelProperty("排序")
+	private Integer sort;
+
+	@ExcelProperty("备注")
+	private String remark;
+
+}

+ 40 - 0
src/main/java/org/springblade/modules/system/excel/RegionImporter.java

@@ -0,0 +1,40 @@
+/*
+ *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions are met:
+ *
+ *  Redistributions of source code must retain the above copyright notice,
+ *  this list of conditions and the following disclaimer.
+ *  Redistributions in binary form must reproduce the above copyright
+ *  notice, this list of conditions and the following disclaimer in the
+ *  documentation and/or other materials provided with the distribution.
+ *  Neither the name of the dreamlu.net developer nor the names of its
+ *  contributors may be used to endorse or promote products derived from
+ *  this software without specific prior written permission.
+ *  Author: Chill 庄骞 (smallchill@163.com)
+ */
+package org.springblade.modules.system.excel;
+
+import lombok.RequiredArgsConstructor;
+import org.springblade.core.excel.support.ExcelImporter;
+import org.springblade.modules.system.service.IRegionService;
+
+import java.util.List;
+
+/**
+ * 行政区划数据导入类
+ *
+ * @author Chill
+ */
+@RequiredArgsConstructor
+public class RegionImporter implements ExcelImporter<RegionExcel> {
+
+	private final IRegionService service;
+	private final Boolean isCovered;
+
+	@Override
+	public void save(List<RegionExcel> data) {
+		service.importRegion(data, isCovered);
+	}
+}

+ 1 - 1
src/main/java/org/springblade/modules/system/excel/UserExcel.java

@@ -27,7 +27,7 @@ import java.io.Serializable;
 import java.util.Date;
 
 /**
- * UserDTO
+ * UserExcel
  *
  * @author Chill
  */

+ 13 - 0
src/main/java/org/springblade/modules/system/mapper/RegionMapper.java

@@ -16,8 +16,11 @@
  */
 package org.springblade.modules.system.mapper;
 
+import com.baomidou.mybatisplus.core.conditions.Wrapper;
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import org.apache.ibatis.annotations.Param;
 import org.springblade.modules.system.entity.Region;
+import org.springblade.modules.system.excel.RegionExcel;
 import org.springblade.modules.system.vo.RegionVO;
 
 import java.util.List;
@@ -32,6 +35,7 @@ public interface RegionMapper extends BaseMapper<Region> {
 
 	/**
 	 * 懒加载列表
+	 *
 	 * @param parentCode
 	 * @param param
 	 * @return
@@ -40,10 +44,19 @@ public interface RegionMapper extends BaseMapper<Region> {
 
 	/**
 	 * 懒加载列表
+	 *
 	 * @param parentCode
 	 * @param param
 	 * @return
 	 */
 	List<RegionVO> lazyTree(String parentCode, Map<String, Object> param);
 
+	/**
+	 * 导出区划数据
+	 *
+	 * @param queryWrapper
+	 * @return
+	 */
+	List<RegionExcel> exportRegion(@Param("ew") Wrapper<Region> queryWrapper);
+
 }

+ 4 - 0
src/main/java/org/springblade/modules/system/mapper/RegionMapper.xml

@@ -97,4 +97,8 @@
         </where>
     </select>
 
+    <select id="exportRegion" resultType="org.springblade.modules.system.excel.RegionExcel">
+        SELECT * FROM blade_region ${ew.customSqlSegment}
+    </select>
+
 </mapper>

+ 19 - 0
src/main/java/org/springblade/modules/system/service/IRegionService.java

@@ -16,8 +16,10 @@
  */
 package org.springblade.modules.system.service;
 
+import com.baomidou.mybatisplus.core.conditions.Wrapper;
 import com.baomidou.mybatisplus.extension.service.IService;
 import org.springblade.modules.system.entity.Region;
+import org.springblade.modules.system.excel.RegionExcel;
 import org.springblade.modules.system.vo.RegionVO;
 
 import java.util.List;
@@ -64,4 +66,21 @@ public interface IRegionService extends IService<Region> {
 	 */
 	List<RegionVO> lazyTree(String parentCode, Map<String, Object> param);
 
+	/**
+	 * 导入区划数据
+	 *
+	 * @param data
+	 * @param isCovered
+	 * @return
+	 */
+	void importRegion(List<RegionExcel> data, Boolean isCovered);
+
+	/**
+	 * 导出区划数据
+	 *
+	 * @param queryWrapper
+	 * @return
+	 */
+	List<RegionExcel> exportRegion(Wrapper<Region> queryWrapper);
+
 }

+ 2 - 1
src/main/java/org/springblade/modules/system/service/IUserService.java

@@ -144,12 +144,13 @@ public interface IUserService extends BaseService<User> {
 	 * 导入用户数据
 	 *
 	 * @param data
+	 * @param isCovered
 	 * @return
 	 */
 	void importUser(List<UserExcel> data, Boolean isCovered);
 
 	/**
-	 * 获取导出用户数据
+	 * 导出用户数据
 	 *
 	 * @param queryWrapper
 	 * @return

+ 32 - 1
src/main/java/org/springblade/modules/system/service/impl/RegionServiceImpl.java

@@ -16,15 +16,21 @@
  */
 package org.springblade.modules.system.service.impl;
 
+import com.baomidou.mybatisplus.core.conditions.Wrapper;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import org.springblade.core.log.exception.ServiceException;
+import org.springblade.core.tool.utils.BeanUtil;
+import org.springblade.core.tool.utils.Func;
+import org.springblade.core.tool.utils.StringPool;
 import org.springblade.modules.system.entity.Region;
+import org.springblade.modules.system.excel.RegionExcel;
 import org.springblade.modules.system.mapper.RegionMapper;
 import org.springblade.modules.system.service.IRegionService;
 import org.springblade.modules.system.vo.RegionVO;
 import org.springframework.stereotype.Service;
 
+import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 
@@ -44,7 +50,13 @@ public class RegionServiceImpl extends ServiceImpl<RegionMapper, Region> impleme
 		if (cnt > 0) {
 			return this.updateById(region);
 		}
-		// 增加省、市、区、镇、村的冗余字段
+		// 设置祖区划编号
+		Region parent = getByCode(region.getParentCode());
+		if (Func.isNotEmpty(parent) || Func.isNotEmpty(parent.getCode())) {
+			String ancestors = parent.getAncestors() + StringPool.COMMA + parent.getCode();
+			region.setAncestors(ancestors);
+		}
+		// 设置省、市、区、镇、村
 		Integer level = region.getLevel();
 		String code = region.getCode();
 		String name = region.getName();
@@ -85,4 +97,23 @@ public class RegionServiceImpl extends ServiceImpl<RegionMapper, Region> impleme
 	public List<RegionVO> lazyTree(String parentCode, Map<String, Object> param) {
 		return baseMapper.lazyTree(parentCode, param);
 	}
+
+	@Override
+	public void importRegion(List<RegionExcel> data, Boolean isCovered) {
+		List<Region> list = new ArrayList<>();
+		data.forEach(regionExcel -> {
+			Region region = BeanUtil.copy(regionExcel, Region.class);
+			list.add(region);
+		});
+		if (isCovered) {
+			this.saveOrUpdateBatch(list);
+		} else {
+			this.saveBatch(list);
+		}
+	}
+
+	@Override
+	public List<RegionExcel> exportRegion(Wrapper<Region> queryWrapper) {
+		return baseMapper.exportRegion(queryWrapper);
+	}
 }