浏览代码

教学点管理

haogh 1 年之前
父节点
当前提交
b748bb5753

+ 1 - 1
src/main/java/com/qmth/exam/reserve/bean/stdapply/CategoryVO.java

@@ -15,6 +15,6 @@ public class CategoryVO implements IModel {
     @ApiModelProperty("id")
     @ApiModelProperty("id")
     private Long id;
     private Long id;
 
 
-    @ApiModelProperty("任务名称")
+    @ApiModelProperty("类别名称")
     private String name;
     private String name;
 }
 }

+ 54 - 4
src/main/java/com/qmth/exam/reserve/controller/admin/TeachingController.java

@@ -1,12 +1,16 @@
 package com.qmth.exam.reserve.controller.admin;
 package com.qmth.exam.reserve.controller.admin;
 
 
+import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
+import com.qmth.boot.core.exception.StatusException;
 import com.qmth.exam.reserve.bean.login.LoginUser;
 import com.qmth.exam.reserve.bean.login.LoginUser;
+import com.qmth.exam.reserve.bean.stdapply.CategoryVO;
 import com.qmth.exam.reserve.bean.teaching.TeachingSaveReq;
 import com.qmth.exam.reserve.bean.teaching.TeachingSaveReq;
+import com.qmth.exam.reserve.enums.Role;
+import com.qmth.exam.reserve.util.ResourceUtil;
+import io.swagger.annotations.ApiModelProperty;
+import io.swagger.annotations.ApiParam;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.RequestBody;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
 
 
 import com.qmth.boot.api.annotation.Aac;
 import com.qmth.boot.api.annotation.Aac;
 import com.qmth.boot.api.constant.ApiConstant;
 import com.qmth.boot.api.constant.ApiConstant;
@@ -18,6 +22,14 @@ import com.qmth.exam.reserve.service.CategoryService;
 
 
 import io.swagger.annotations.Api;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import io.swagger.annotations.ApiOperation;
+import org.springframework.web.multipart.MultipartFile;
+
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
 
 
 @RestController
 @RestController
 @Api(tags = "教学点管理")
 @Api(tags = "教学点管理")
@@ -34,6 +46,13 @@ public class TeachingController extends BaseController {
         return categoryService.page(req);
         return categoryService.page(req);
     }
     }
 
 
+    @ApiOperation(value = "教学点所属城市选择")
+    @PostMapping(value = "/city/list")
+    public List<CategoryVO> listCity() {
+        LoginUser user = curLoginUser();
+        return categoryService.listCity(user);
+    }
+
     @ApiOperation(value = "教学点新增/编辑")
     @ApiOperation(value = "教学点新增/编辑")
     @PostMapping(value = "/save")
     @PostMapping(value = "/save")
     public void save(@RequestBody TeachingSaveReq req) {
     public void save(@RequestBody TeachingSaveReq req) {
@@ -41,6 +60,37 @@ public class TeachingController extends BaseController {
         categoryService.saveTeaching(user, req);
         categoryService.saveTeaching(user, req);
     }
     }
 
 
+    @ApiOperation(value = "教学点启用/禁用")
+    @PostMapping(value = "/enable")
+    public void enable(@ApiParam("教学点ID") @RequestParam Long id, @ApiParam("启用/禁用") @RequestParam Boolean enable) {
+        categoryService.enable(id, enable);
+    }
+
+
+    @ApiOperation(value = "教学点模版下载")
+    @PostMapping(value = "/import/template")
+    public void download(HttpServletResponse response) {
+        exportFile("教学点模板.xlsx", ResourceUtil.getStream("templates/teachingImport.xlsx"));
+    }
 
 
 
 
+    @ApiOperation(value = "教学点导入")
+    @PostMapping(value = "/import")
+    public Map<String, Object> importPreExamStd(@RequestParam MultipartFile file) {
+        LoginUser user = this.curLoginUser();
+        if (!Role.ADMIN.equals(user.getRole())) {
+            throw new StatusException("没有权限");
+        }
+        List<Map<String, Object>> failRecords;
+        try {
+            failRecords = categoryService.importTeaching(user,file.getInputStream());
+        } catch (IOException e) {
+            throw new StatusException("文件读取出错", e);
+        }
+        Map<String, Object> map = new HashMap<>();
+        map.put("hasError", CollectionUtils.isNotEmpty(failRecords));
+        map.put("failRecords", failRecords);
+        return map;
+    }
+
 }
 }

+ 8 - 0
src/main/java/com/qmth/exam/reserve/service/CategoryService.java

@@ -1,6 +1,8 @@
 package com.qmth.exam.reserve.service;
 package com.qmth.exam.reserve.service;
 
 
+import java.io.InputStream;
 import java.util.List;
 import java.util.List;
+import java.util.Map;
 
 
 import com.baomidou.mybatisplus.extension.service.IService;
 import com.baomidou.mybatisplus.extension.service.IService;
 import com.qmth.boot.core.collection.PageResult;
 import com.qmth.boot.core.collection.PageResult;
@@ -21,4 +23,10 @@ public interface CategoryService extends IService<CategoryEntity> {
     PageResult<TeachingVO> page(TeachingReq req);
     PageResult<TeachingVO> page(TeachingReq req);
 
 
     void saveTeaching(LoginUser user, TeachingSaveReq req);
     void saveTeaching(LoginUser user, TeachingSaveReq req);
+
+    void enable(Long id, Boolean enable);
+
+    List<CategoryVO> listCity(LoginUser user);
+
+    List<Map<String, Object>> importTeaching(LoginUser user, InputStream inputStream);
 }
 }

+ 151 - 9
src/main/java/com/qmth/exam/reserve/service/impl/CategoryServiceImpl.java

@@ -3,10 +3,14 @@ package com.qmth.exam.reserve.service.impl;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.qmth.boot.core.collection.PageResult;
 import com.qmth.boot.core.collection.PageResult;
 import com.qmth.boot.core.exception.StatusException;
 import com.qmth.boot.core.exception.StatusException;
+import com.qmth.boot.tools.excel.ExcelReader;
+import com.qmth.boot.tools.excel.enums.ExcelType;
+import com.qmth.boot.tools.excel.model.DataMap;
 import com.qmth.exam.reserve.bean.applytask.CurrentApplyTaskVO;
 import com.qmth.exam.reserve.bean.applytask.CurrentApplyTaskVO;
 import com.qmth.exam.reserve.bean.category.CategoryCacheBean;
 import com.qmth.exam.reserve.bean.category.CategoryCacheBean;
 import com.qmth.exam.reserve.bean.category.CategoryInfo;
 import com.qmth.exam.reserve.bean.category.CategoryInfo;
@@ -21,6 +25,7 @@ import com.qmth.exam.reserve.cache.impl.CategoryCacheService;
 import com.qmth.exam.reserve.dao.CategoryDao;
 import com.qmth.exam.reserve.dao.CategoryDao;
 import com.qmth.exam.reserve.entity.CategoryEntity;
 import com.qmth.exam.reserve.entity.CategoryEntity;
 import com.qmth.exam.reserve.enums.CategoryLevel;
 import com.qmth.exam.reserve.enums.CategoryLevel;
+import com.qmth.exam.reserve.enums.ImportStatus;
 import com.qmth.exam.reserve.enums.Role;
 import com.qmth.exam.reserve.enums.Role;
 import com.qmth.exam.reserve.service.CategoryService;
 import com.qmth.exam.reserve.service.CategoryService;
 import com.qmth.exam.reserve.util.PageUtil;
 import com.qmth.exam.reserve.util.PageUtil;
@@ -29,15 +34,17 @@ import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.transaction.annotation.Transactional;
+import org.springframework.transaction.interceptor.TransactionAspectSupport;
 
 
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
+import java.io.InputStream;
+import java.util.*;
 import java.util.stream.Collectors;
 import java.util.stream.Collectors;
 
 
 @Service
 @Service
 public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {
 public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {
 
 
+    private static final String[] EXCEL_HEADER = new String[]{"教学点代码", "教学点名称", "教学点所在城市"};
+
     @Autowired
     @Autowired
     private ApplyTaskCacheService applyTaskCacheService;
     private ApplyTaskCacheService applyTaskCacheService;
 
 
@@ -187,7 +194,7 @@ public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity
 
 
     @Override
     @Override
     public PageResult<TeachingVO> page(TeachingReq req) {
     public PageResult<TeachingVO> page(TeachingReq req) {
-        if(req.getLevel() == null){
+        if (req.getLevel() == null) {
             req.setLevel(CategoryLevel.TEACHING.getValue());
             req.setLevel(CategoryLevel.TEACHING.getValue());
         }
         }
         IPage<TeachingVO> iPage = baseMapper.page(new Page<>(req.getPageNumber(), req.getPageSize()),
         IPage<TeachingVO> iPage = baseMapper.page(new Page<>(req.getPageNumber(), req.getPageSize()),
@@ -207,24 +214,159 @@ public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity
         saveOrUpdate(category);
         saveOrUpdate(category);
     }
     }
 
 
+    @Override
+    public void enable(Long id, Boolean enable) {
+        CategoryEntity category = getById(id);
+        category.setEnable(enable);
+        updateById(category);
+    }
+
     private void checkTeaching(TeachingSaveReq req) {
     private void checkTeaching(TeachingSaveReq req) {
-        if(StringUtils.isBlank(req.getCode())) {
+        if (StringUtils.isBlank(req.getCode())) {
             throw new StatusException("教学点代码不能为空");
             throw new StatusException("教学点代码不能为空");
         }
         }
-        if(StringUtils.isBlank(req.getName())) {
+        if (StringUtils.isBlank(req.getName())) {
             throw new StatusException("教学点名称不能为空");
             throw new StatusException("教学点名称不能为空");
         }
         }
-        if(req.getCityId() == null) {
+        if (req.getCityId() == null) {
             throw new StatusException("请选择教学点所在城市");
             throw new StatusException("请选择教学点所在城市");
         }
         }
-        if(req.getId() == null) {
+        if (req.getId() == null) {
             LambdaQueryWrapper<CategoryEntity> wrapper = new LambdaQueryWrapper<>();
             LambdaQueryWrapper<CategoryEntity> wrapper = new LambdaQueryWrapper<>();
             wrapper.eq(CategoryEntity::getCode, req.getCode());
             wrapper.eq(CategoryEntity::getCode, req.getCode());
             List<CategoryEntity> list = baseMapper.selectList(wrapper);
             List<CategoryEntity> list = baseMapper.selectList(wrapper);
-            if(list.size() > 0) {
+            if (list.size() > 0) {
                 throw new StatusException("教学点代码已经存在");
                 throw new StatusException("教学点代码已经存在");
             }
             }
         }
         }
     }
     }
 
 
+    @Override
+    public List<CategoryVO> listCity(LoginUser user) {
+        LambdaQueryWrapper<CategoryEntity> wrapper = new LambdaQueryWrapper<>();
+        wrapper.eq(CategoryEntity::getOrgId, user.getOrgId());
+        wrapper.eq(CategoryEntity::getEnable, Boolean.TRUE);
+        wrapper.eq(CategoryEntity::getLevel, CategoryLevel.CITY.getValue());
+        wrapper.orderByAsc(CategoryEntity::getId);
+        List<CategoryEntity> cityList = this.list(wrapper);
+        List<CategoryVO> cityVOList = new ArrayList<>();
+        for (CategoryEntity city : cityList) {
+            CategoryVO cityVO = new CategoryVO();
+            cityVO.setId(city.getId());
+            cityVO.setName(city.getName());
+            cityVOList.add(cityVO);
+        }
+        return cityVOList;
+    }
+
+    @Override
+    public List<Map<String, Object>> importTeaching(LoginUser user, InputStream inputStream) {
+        List<DataMap> lineList = null;
+        ExcelReader reader = ExcelReader.create(ExcelType.XLSX, inputStream, 0);
+        try {
+            lineList = reader.getDataMapList();
+        } catch (Exception e) {
+            throw new StatusException("Excel 解析失败");
+        }
+        if (CollectionUtils.isEmpty(lineList)) {
+            throw new StatusException("Excel无内容");
+        }
+        List<Map<String, Object>> failRecords = new ArrayList<Map<String, Object>>();
+        List<CategoryEntity> categoryList = new ArrayList<>();
+        List<CategoryVO> cityList = listCity(user);
+        for (int i = 0; i < lineList.size(); i++) {
+            CategoryEntity category = new CategoryEntity();
+            DataMap line = lineList.get(i);
+            StringBuilder msg = new StringBuilder();
+            String code = trimAndNullIfBlank(line.get(EXCEL_HEADER[0]));
+            if (StringUtils.isBlank(code)) {
+                msg.append(" 教学点代码不能为空");
+            } else {
+                category.setCode(code);
+            }
+            String name = trimAndNullIfBlank(line.get(EXCEL_HEADER[1]));
+            if (StringUtils.isBlank(name)) {
+                msg.append(" 教学点名称不能为空");
+            } else {
+                category.setName(name);
+            }
+            String cityName = trimAndNullIfBlank(line.get(EXCEL_HEADER[2]));
+            Long cityId = null;
+            if (StringUtils.isBlank(cityName)) {
+                msg.append(" 教学点所在城市不能为空");
+            } else {
+                cityId = getIdByName(cityName, cityList);
+                if (cityId == null) {
+                    msg.append(" 教学点所在城市不存在");
+                } else {
+                    category.setParentId(cityId);
+                }
+            }
+            if (msg.length() > 0) {
+                failRecords.add(newError(i + 1, msg.toString()));
+            } else {
+                category.setEnable(Boolean.TRUE);
+                category.setLevel(CategoryLevel.TEACHING.getValue());
+                category.setOrgId(user.getOrgId());
+                categoryList.add(category);
+            }
+        }
+
+        if (CollectionUtils.isNotEmpty(failRecords)) {
+            return failRecords;
+        }
+        for (int i = 0; i < categoryList.size(); i++) {
+            CategoryEntity category = categoryList.get(i);
+            try {
+                saveTeaching(category);
+            } catch (Exception e) {
+                failRecords.add(newError(i + 1, " 系统异常"));
+                log.error("导入异常", e);
+            }
+        }
+        if (CollectionUtils.isNotEmpty(failRecords)) {
+             TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
+            return failRecords;
+        }
+        return failRecords;
+    }
+
+    private void saveTeaching(CategoryEntity category) {
+        LambdaQueryWrapper<CategoryEntity> wrapper = new LambdaQueryWrapper<>();
+        wrapper.eq(CategoryEntity::getCode, category.getCode());
+        wrapper.eq(CategoryEntity::getOrgId, category.getOrgId());
+        CategoryEntity existCategory = baseMapper.selectOne(wrapper);
+        if (existCategory != null) {
+            existCategory.setParentId(category.getParentId());
+            existCategory.setName(category.getName());
+            existCategory.setEnable(Boolean.TRUE);
+            baseMapper.updateById(existCategory);
+        } else {
+            baseMapper.insert(category);
+        }
+    }
+
+    private Long getIdByName(String cityName, List<CategoryVO> cityList) {
+        for (CategoryVO city : cityList) {
+            if (city.getName().equals(cityName)) {
+                return city.getId();
+            }
+        }
+        return null;
+    }
+
+    private String trimAndNullIfBlank(String s) {
+        if (StringUtils.isBlank(s)) {
+            return null;
+        }
+        return s.trim();
+    }
+
+    private Map<String, Object> newError(int lineNum, String msg) {
+        Map<String, Object> map = new HashMap<>();
+        map.put("lineNum", lineNum);
+        map.put("msg", msg);
+        return map;
+    }
+
 }
 }

二进制
src/main/resources/templates/teachingImport.xlsx