浏览代码

科室的批量导入和收入成本的批量导入

ljx 4 年之前
父节点
当前提交
36cc53d9db

+ 2 - 0
src/main/java/com/imed/costaccount/common/shiro/ShiroConfig.java

@@ -47,6 +47,8 @@ public class ShiroConfig {
         filterMap.put("/captcha.jpg", "anon");
         filterMap.put("/costAccount/getHospArea/**","anon");
         filterMap.put("/costAccount/excel/getImportUserTemplate","anon");
+        filterMap.put("/costAccount/excel/getDepartmentTemplate","anon");
+        filterMap.put("/costAccount/excel/getImportProductTemplate","anon");
         filterMap.put("/costAccount/login", "anon");
         filterMap.put("/**/*.jpg", "anon");
         filterMap.put("/**/*.png", "anon");

+ 9 - 0
src/main/java/com/imed/costaccount/service/DepartmentService.java

@@ -2,6 +2,7 @@ package com.imed.costaccount.service;
 
 import com.baomidou.mybatisplus.extension.service.IService;
 import com.imed.costaccount.common.util.PageUtils;
+import com.imed.costaccount.common.util.Result;
 import com.imed.costaccount.model.Department;
 import com.imed.costaccount.model.dto.DepartmentRequest;
 
@@ -46,5 +47,13 @@ public interface DepartmentService extends IService<Department> {
      * @param departmentRequest
      */
     void updateByDepartment(DepartmentRequest departmentRequest);
+
+    /**
+     * 导入科室信息
+     * @param read
+     * @param hospId
+     * @return
+     */
+    Result importDepartment(List<List<Object>> read, Integer hospId);
 }
 

+ 9 - 0
src/main/java/com/imed/costaccount/service/ProductService.java

@@ -2,6 +2,7 @@ package com.imed.costaccount.service;
 
 import com.baomidou.mybatisplus.extension.service.IService;
 import com.imed.costaccount.common.util.PageUtils;
+import com.imed.costaccount.common.util.Result;
 import com.imed.costaccount.model.Product;
 import com.imed.costaccount.model.dto.ProductDTO;
 import com.imed.costaccount.model.User;
@@ -49,5 +50,13 @@ public interface ProductService extends IService<Product> {
      * @return
      */
     List<CommonVO> getProducts(User user);
+
+    /**
+     * 批量导入收入成本信息
+     * @param read
+     * @param hospId
+     * @return
+     */
+    Result importProduct(List<List<Object>> read, Integer hospId);
 }
 

+ 86 - 0
src/main/java/com/imed/costaccount/service/impl/DepartmentServiceImpl.java

@@ -1,6 +1,8 @@
 package com.imed.costaccount.service.impl;
 
+import cn.hutool.core.collection.CollUtil;
 import cn.hutool.core.date.DateUtil;
+import cn.hutool.core.util.StrUtil;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
@@ -8,6 +10,8 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.imed.costaccount.common.exception.CostException;
 import com.imed.costaccount.common.util.BeanUtil;
 import com.imed.costaccount.common.util.PageUtils;
+import com.imed.costaccount.common.util.Result;
+import com.imed.costaccount.constants.NumberConstant;
 import com.imed.costaccount.mapper.DepartmentMapper;
 import com.imed.costaccount.model.Department;
 import com.imed.costaccount.model.Hospital;
@@ -15,6 +19,7 @@ import com.imed.costaccount.model.User;
 import com.imed.costaccount.model.dto.DepartmentRequest;
 import com.imed.costaccount.model.vo.DepartmentVO;
 import com.imed.costaccount.service.DepartmentService;
+import lombok.extern.slf4j.Slf4j;
 import org.apache.shiro.SecurityUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
@@ -23,11 +28,13 @@ import org.springframework.transaction.annotation.Transactional;
 import org.springframework.util.CollectionUtils;
 import org.springframework.util.StringUtils;
 
+import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
 import java.util.stream.Collectors;
 
+@Slf4j
 @Service("departmentService")
 public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements DepartmentService {
 
@@ -137,4 +144,83 @@ public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Departm
         departmentResponse.setHospId(hospId);
         baseMapper.insert(departmentResponse);
     }
+
+    /**
+     * 导入科室信息
+     *
+     * @param list
+     * @param hospId
+     * @return
+     */
+    @Override
+    public Result importDepartment(List<List<Object>> list, Integer hospId) {
+        for (int i = list.size() - 1; i >= 0; i--) {
+            if (i == NumberConstant.ZERO || i == NumberConstant.ONE || i == NumberConstant.TWO || i == NumberConstant.THREE) {
+                list.remove(list.get(i));
+            }
+        }
+        log.info("读取的数据为:{}", list);
+        List<Department> departmentList = new ArrayList<>();
+        List<String> errRowNums = new ArrayList<>();
+        for (int i = 0; i < list.size(); i++) {
+            List<Object> data = list.get(i);
+            log.info("得到用户输入的数据为:{}", data);
+            Department department = new Department();
+            department.setHospId(hospId);
+            department.setCreateTime(System.currentTimeMillis());
+            int size = data.size();
+            if (size != 5) {
+                // 补充读取的null问题
+                if (size == 4) {
+                    data.add(StrUtil.EMPTY);
+                }
+                for (int j = 0; j < 3; j++) {
+                    if (Objects.isNull(data.get(j))) {
+                        data.set(j, StrUtil.EMPTY);
+                    }
+                }
+            }
+            for (int j = 0; j < 3; j++) {
+                String str = String.valueOf(data.get(j));
+                log.info("得到的字符串{}", str);
+                if (StrUtil.isBlank(str)) {
+                    errRowNums.add("" + (i + 3));
+                }
+
+                if (j == 0) {
+                    //  Excel里面输入医院名称  根据医院名称解析
+                    Hospital hospital = hospitalService.getByName(str);
+                    department.setHospId(hospital.getId());
+                } else if (j == 1) {
+                    department.setDepartmentName(str);
+                } else {
+                    department.setDepartmentCode(str);
+                }
+            }
+            departmentList.add(department);
+        }
+        if (CollUtil.isNotEmpty(errRowNums)) {
+            String collect = errRowNums.stream().collect(Collectors.joining(StrUtil.COMMA));
+            throw new CostException(500, "第" + collect + "行数据异常");
+        }
+        // 校验输入的Department是否存在  输入的科室里面筛选出已经存在的科室信息
+        List<Department> realDepartments = new ArrayList<>();
+        departmentList.forEach(i -> {
+            Department one = getOne(
+                    new QueryWrapper<Department>().lambda()
+                            .eq(Department::getDepartmentCode, i.getDepartmentCode())
+                            .eq(Department::getHospId, i.getHospId())
+            );
+            if (Objects.nonNull(one)) {
+                realDepartments.add(one);
+            }
+        });
+        departmentList = departmentList.stream().filter(i -> !realDepartments.stream().map(Department::getDepartmentCode).collect(Collectors.toList()).contains(i.getDepartmentCode())).collect(Collectors.toList());
+        this.saveBatch(departmentList);
+        if (CollectionUtils.isEmpty(realDepartments)){
+            return Result.build(200,"数据导入成功",null);
+        }else {
+            return Result.build(200, "有" + realDepartments.size() + "条数据已存在,未被导入", null);
+        }
+    }
 }

+ 90 - 1
src/main/java/com/imed/costaccount/service/impl/ProductServiceImpl.java

@@ -3,11 +3,15 @@ package com.imed.costaccount.service.impl;
 import cn.hutool.core.collection.CollUtil;
 import cn.hutool.core.util.StrUtil;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.imed.costaccount.common.exception.CostException;
 import com.imed.costaccount.common.util.PageUtils;
+import com.imed.costaccount.common.util.Result;
+import com.imed.costaccount.constants.NumberConstant;
 import com.imed.costaccount.mapper.ProductMapper;
+import com.imed.costaccount.model.Hospital;
 import com.imed.costaccount.model.Product;
 import com.imed.costaccount.model.User;
 import com.imed.costaccount.model.dto.ProductDTO;
@@ -16,19 +20,25 @@ import com.imed.costaccount.model.vo.CommonVO;
 import com.imed.costaccount.model.vo.ProductVO;
 import com.imed.costaccount.service.ProductService;
 import com.imed.costaccount.utils.BeanUtil;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Propagation;
 import org.springframework.transaction.annotation.Transactional;
+import org.springframework.util.CollectionUtils;
 
+import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
 import java.util.Objects;
 import java.util.stream.Collectors;
 
-
+@Slf4j
 @Service("productService")
 public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {
 
+    @Autowired
+    private HospitalServiceImpl hospitalService;
 
     /**
      * 分页查询
@@ -128,4 +138,83 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> impl
             return commonVO;
         }).collect(Collectors.toList());
     }
+
+    /**
+     * 批量导入收入成本信息
+     *
+     * @param list
+     * @param hospId
+     * @return
+     */
+    @Override
+    public Result importProduct(List<List<Object>> list, Integer hospId) {
+        for (int i = list.size() - 1; i >= 0; i--) {
+            if (i == NumberConstant.ZERO || i == NumberConstant.ONE || i == NumberConstant.TWO || i == NumberConstant.THREE) {
+                list.remove(list.get(i));
+            }
+        }
+        log.info("读取的数据为:{}", list);
+        List<Product> productList = new ArrayList<>();
+        List<String> errRowNums = new ArrayList<>();
+        for (int i = 0; i < list.size(); i++) {
+            List<Object> data = list.get(i);
+            log.info("得到用户输入的数据为:{}", data);
+            Product product = new Product();
+            product.setHospId(hospId);
+            product.setCreateTime(System.currentTimeMillis());
+            int size = data.size();
+            if (size != 5) {
+                // 补充读取的null问题
+                if (size == 4) {
+                    data.add(StrUtil.EMPTY);
+                }
+                for (int j = 0; j < 3; j++) {
+                    if (Objects.isNull(data.get(j))) {
+                        data.set(j, StrUtil.EMPTY);
+                    }
+                }
+            }
+            for (int j = 0; j < 3; j++) {
+                String str = String.valueOf(data.get(j));
+                log.info("得到的字符串{}", str);
+                if (StrUtil.isBlank(str)) {
+                    errRowNums.add("" + (i + 3));
+                }
+
+                if (j == 0) {
+                    //  Excel里面输入医院名称  根据医院名称解析
+                    Hospital hospital = hospitalService.getByName(str);
+                    product.setHospId(hospital.getId());
+                } else if (j == 1) {
+                    product.setProductName(str);
+                } else {
+                    product.setProductCode(str);
+                }
+            }
+            productList.add(product);
+        }
+        if (CollUtil.isNotEmpty(errRowNums)) {
+            String collect = errRowNums.stream().collect(Collectors.joining(StrUtil.COMMA));
+            throw new CostException(500, "第" + collect + "行数据异常");
+        }
+        // 校验输入的Department是否存在  输入的科室里面筛选出已经存在的科室信息
+        List<Product> realProducts = new ArrayList<>();
+        productList.forEach(i -> {
+            Product one = getOne(
+                    new QueryWrapper<Product>().lambda()
+                            .eq(Product::getProductCode, i.getProductCode())
+                            .eq(Product::getHospId, i.getHospId())
+            );
+            if (Objects.nonNull(one)) {
+                realProducts.add(one);
+            }
+        });
+        productList = productList.stream().filter(i -> !realProducts.stream().map(Product::getProductCode).collect(Collectors.toList()).contains(i.getProductCode())).collect(Collectors.toList());
+        this.saveBatch(productList);
+        if (CollectionUtils.isEmpty(realProducts)){
+            return Result.build(200,"数据导入成功",null);
+        }else {
+            return Result.build(200, "有" + realProducts.size() + "条数据已存在,未被导入", null);
+        }
+    }
 }

+ 120 - 1
src/main/java/com/imed/costaccount/web/ExcelController.java

@@ -9,11 +9,14 @@ import com.imed.costaccount.common.exception.CostException;
 import com.imed.costaccount.common.util.Result;
 import com.imed.costaccount.model.User;
 import com.imed.costaccount.service.UserService;
+import com.imed.costaccount.service.impl.DepartmentServiceImpl;
+import com.imed.costaccount.service.impl.ProductServiceImpl;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.poi.ss.usermodel.Sheet;
 import org.apache.shiro.SecurityUtils;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.multipart.MultipartFile;
 
@@ -37,12 +40,18 @@ public class ExcelController {
 
     private UserService userService;
 
+    @Autowired
+    private DepartmentServiceImpl departmentService;
+
+    @Autowired
+    private ProductServiceImpl productService;
+
     public ExcelController(UserService userService) {
         this.userService = userService;
     }
 
     @ApiOperation("用户导出模板设置")
-    @GetMapping("/getImportUserTemplate")
+    @GetMapping("/getcurrentTemplate")
     public void getImportUserTemplate(HttpServletResponse response) throws IOException {
         User user = (User) SecurityUtils.getSubject().getPrincipal();
         // TODO: 2021/7/26 暂时没有登录
@@ -89,6 +98,116 @@ public class ExcelController {
             throw new CostException(500, "导入失败");
         }
     }
+    /**
+     * 科室模板导出功能设置
+     */
+    @ApiOperation("科室导出模板设置")
+    @GetMapping("/getDepartmentTemplate")
+    public void getImportDepartmentTemplate(HttpServletResponse response) throws IOException {
+        User user = (User) SecurityUtils.getSubject().getPrincipal();
+        String uuid = UUID.randomUUID().toString();
+        String url = System.getProperty("java.io.tmpdir") + File.separator + uuid + File.separator + uuid + ".xls";
+        FileUtil.del(FileUtil.file(url));
+
+        ExcelWriter writer = new ExcelWriter(url);
+        // 样式
+        Sheet sheet = writer.getSheet();
+        // 内容
+        writer.merge(0, 1, 0, 2, "为了保证成功导入,请勿修改模板格式", false);
+        writer.passCurrentRow();
+        writer.passCurrentRow();
+        writer.merge(2, 2, 0, 2, "医院科室批量导入", false);
+        writer.passCurrentRow();
+        writer.setColumnWidth(0,20);
+        writer.setColumnWidth(1,15);
+        writer.setColumnWidth(2,15);
+        writer.writeRow(Arrays.asList("院区","科室名称", "科室代码"));
+
+        // 写入响应
+        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
+        response.setHeader("Content-Disposition", "attachment;filename=" + uuid + ".xls");
+        ServletOutputStream out = null;
+
+        out = response.getOutputStream();
+        writer.flush(out, true);
+        writer.close();
+        IoUtil.close(out);
+    }
+    /**
+     * 批量导入科室信息
+     */
+    @PostMapping("/importDepartment")
+    @ApiOperation("导入科室信息")
+    public Result importDepartment(@RequestParam("file") MultipartFile file){
+        InputStream in;
+        try {
+            in = file.getInputStream();
+            ExcelReader reader = ExcelUtil.getReader(in);
+            List<List<Object>> read = reader.read();
+            log.info("最开始:read={}",read);
+            log.info("-------------------------------------------------------------------");
+            User user = (User) SecurityUtils.getSubject().getPrincipal();
+            Integer hospId = user.getHospId();
+            return departmentService.importDepartment(read, hospId);
+        }catch (IOException e){
+            e.printStackTrace();;
+            throw new CostException(500, "导入失败");
+        }
+    }
+    /**
+     *  收入成本项目批量导入模板
+     */
+    @ApiOperation("收入成本导出模板设置")
+    @GetMapping("/getImportProductTemplate")
+    public void getImportProductTemplate(HttpServletResponse response) throws IOException {
+        User user = (User) SecurityUtils.getSubject().getPrincipal();
+        String uuid = UUID.randomUUID().toString();
+        String url = System.getProperty("java.io.tmpdir") + File.separator + uuid + File.separator + uuid + ".xls";
+        FileUtil.del(FileUtil.file(url));
 
+        ExcelWriter writer = new ExcelWriter(url);
+        // 样式
+        Sheet sheet = writer.getSheet();
+        // 内容
+        writer.merge(0, 1, 0, 2, "为了保证成功导入,请勿修改模板格式", false);
+        writer.passCurrentRow();
+        writer.passCurrentRow();
+        writer.merge(2, 2, 0, 2, "医院成本收入批量导入", false);
+        writer.passCurrentRow();
+        writer.setColumnWidth(0,20);
+        writer.setColumnWidth(1,15);
+        writer.setColumnWidth(2,15);
+        writer.writeRow(Arrays.asList("院区","成本项目名", "成本项目编号"));
 
+        // 写入响应
+        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
+        response.setHeader("Content-Disposition", "attachment;filename=" + uuid + ".xls");
+        ServletOutputStream out = null;
+
+        out = response.getOutputStream();
+        writer.flush(out, true);
+        writer.close();
+        IoUtil.close(out);
+    }
+    /**
+     *  收入成本项目批量导入
+     */
+    @PostMapping("/importProduct")
+    @ApiOperation("批量导入收入成本信息")
+    public Result importProduct(@RequestParam("file") MultipartFile file){
+        InputStream in;
+        try {
+            in = file.getInputStream();
+            ExcelReader reader = ExcelUtil.getReader(in);
+            List<List<Object>> read = reader.read();
+            log.info("最开始:read={}",read);
+            log.info("-------------------------------------------------------------------");
+            User user = (User) SecurityUtils.getSubject().getPrincipal();
+            Integer hospId = user.getHospId();
+            return productService.importProduct(read, hospId);
+        }catch (IOException e){
+            e.printStackTrace();;
+            throw new CostException(500, "导入失败");
+        }
+    }
 }