本文共 9259 字,大约阅读时间需要 30 分钟。
package com.learn.company.controller;import com.learn.common.controller.BaseController;import com.learn.common.entity.Result;import com.learn.common.entity.ResultCode;import com.learn.company.service.CompanyService;import com.learn.company.service.DepartmentService;import com.learn.domain.company.Company;import com.learn.domain.company.Department;import com.learn.domain.company.response.DeptListResult;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.*;import java.util.List;//1.解决跨域@CrossOrigin//2.声明restContoller@RestController//3.设置父路径@RequestMapping(value="/company") // company/deparmentpublic class DepartmentController extends BaseController{ @Autowired private DepartmentService departmentService; @RequestMapping(value="/department/search",method = RequestMethod.POST) public Department findByCode(@RequestParam(value="code") String code,@RequestParam(value="companyId") String companyId) { Department dept = departmentService.findByCode(code,companyId); return dept; }}
package com.learn.company.service;import com.learn.common.service.BaseService;import com.learn.common.utils.IdWorker;import com.learn.company.dao.DepartmentDao;import com.learn.domain.company.Department;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.jpa.domain.Specification;import org.springframework.stereotype.Service;import javax.persistence.criteria.CriteriaBuilder;import javax.persistence.criteria.CriteriaQuery;import javax.persistence.criteria.Predicate;import javax.persistence.criteria.Root;import java.util.List;@Servicepublic class DepartmentService extends BaseService { @Autowired private DepartmentDao departmentDao; /** * 根据部门编码和企业id查询部门 */ public Department findByCode(String code, String companyId) { return departmentDao.findByCodeAndCompanyId(code,companyId); }}
package com.learn.company.dao;import com.learn.domain.company.Department;import org.springframework.data.jpa.repository.JpaRepository;import org.springframework.data.jpa.repository.JpaSpecificationExecutor;/** * 部门dao接口 */public interface DepartmentDao extends JpaRepository,JpaSpecificationExecutor { Department findByCodeAndCompanyId(String code, String companyId);}
package com.learn.system.client;import com.learn.common.entity.Result;import com.learn.domain.company.Department;import org.springframework.cloud.openfeign.FeignClient;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;/** * 声明接口,通过feign调用其他微服务 *///声明调用的微服务名称@FeignClient("learn-company")public interface DepartmentFeignClient { /** * 调用微服务的接口 */ @RequestMapping(value="/company/department/{id}",method = RequestMethod.GET) public Result findById(@PathVariable(value="id") String id); @RequestMapping(value="/company/department/search",method = RequestMethod.POST) public Department findByCode(@RequestParam(value="code") String code, @RequestParam(value="companyId") String companyId);}
package com.learn.system.service;import com.learn.common.utils.IdWorker;import com.learn.domain.company.Department;import com.learn.domain.system.Role;import com.learn.domain.system.User;import com.learn.system.client.DepartmentFeignClient;import com.learn.system.dao.RoleDao;import com.learn.system.dao.UserDao;import org.apache.shiro.crypto.hash.Md5Hash;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.domain.Page;import org.springframework.data.domain.PageRequest;import org.springframework.data.jpa.domain.Specification;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Transactional;import org.springframework.util.StringUtils;import javax.persistence.criteria.CriteriaBuilder;import javax.persistence.criteria.CriteriaQuery;import javax.persistence.criteria.Predicate;import javax.persistence.criteria.Root;import java.lang.annotation.Target;import java.util.*;@Servicepublic class UserService { @Autowired private UserDao userDao; @Autowired private RoleDao roleDao; @Autowired private IdWorker idWorker; @Autowired private DepartmentFeignClient departmentFeignClient; /** * 批量保存用户 */ @Transactional public void saveAll(Listlist ,String companyId,String companyName){ for (User user : list) { //默认密码 user.setPassword(new Md5Hash("123456",user.getMobile(),3).toString()); //id user.setId(idWorker.nextId()+""); //基本属性 user.setCompanyId(companyId); user.setCompanyName(companyName); user.setInServiceStatus(1); user.setEnableState(1); user.setLevel("user"); //填充部门的属性 Department department = departmentFeignClient.findByCode(user.getDepartmentId(), companyId); if(department != null) { user.setDepartmentId(department.getId()); user.setDepartmentName(department.getName()); } userDao.save(user); } }}
package com.learn.system.controller;import com.learn.common.controller.BaseController;import com.learn.common.entity.PageResult;import com.learn.common.entity.Result;import com.learn.common.entity.ResultCode;import com.learn.common.exception.CommonException;import com.learn.common.utils.JwtUtils;import com.learn.common.utils.PermissionConstants;import com.learn.domain.system.Permission;import com.learn.domain.system.Role;import com.learn.domain.system.response.ProfileResult;import com.learn.domain.system.User;import com.learn.domain.system.response.UserResult;import com.learn.system.client.DepartmentFeignClient;import com.learn.system.service.PermissionService;import com.learn.system.service.RoleService;import com.learn.system.service.UserService;import io.jsonwebtoken.Claims;import org.apache.poi.ss.usermodel.*;import org.apache.poi.xssf.usermodel.XSSFWorkbook;import org.apache.shiro.SecurityUtils;import org.apache.shiro.authc.UsernamePasswordToken;import org.apache.shiro.authz.annotation.RequiresPermissions;import org.apache.shiro.crypto.hash.Md5Hash;import org.apache.shiro.subject.PrincipalCollection;import org.apache.shiro.subject.Subject;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.domain.Page;import org.springframework.util.StringUtils;import org.springframework.web.bind.annotation.*;import org.springframework.web.multipart.MultipartFile;import javax.servlet.http.HttpServletRequest;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;//1.解决跨域@CrossOrigin//2.声明restContoller@RestController//3.设置父路径@RequestMapping(value="/sys")public class UserController extends BaseController { @Autowired private UserService userService; @Autowired private PermissionService permissionService; @Autowired private JwtUtils jwtUtils; @Autowired private DepartmentFeignClient departmentFeignClient; /** * 导入Excel,添加用户 * 文件上传:springboot */ @RequestMapping(value="/user/import",method = RequestMethod.POST) public Result importUser(@RequestParam(name="file") MultipartFile file) throws Exception { //1.解析Excel //1.1.根据Excel文件创建工作簿 Workbook wb = new XSSFWorkbook(file.getInputStream()); //1.2.获取Sheet Sheet sheet = wb.getSheetAt(0);//参数:索引 //1.3.获取Sheet中的每一行,和每一个单元格 //2.获取用户数据列表 Listlist = new ArrayList<>(); System.out.println(sheet.getLastRowNum()); for (int rowNum = 1; rowNum<= sheet.getLastRowNum() ;rowNum ++) { Row row = sheet.getRow(rowNum);//根据索引获取每一个行 Object [] values = new Object[row.getLastCellNum()]; for(int cellNum=1;cellNum< row.getLastCellNum(); cellNum ++) { Cell cell = row.getCell(cellNum); Object value = getCellValue(cell); values[cellNum] = value; } User user = new User(values); list.add(user); } //3.批量保存用户 userService.saveAll(list,companyId,companyName); return new Result(ResultCode.SUCCESS); } public static Object getCellValue(Cell cell) { //1.获取到单元格的属性类型 CellType cellType = cell.getCellType(); //2.根据单元格数据类型获取数据 Object value = null; switch (cellType) { case STRING: value = cell.getStringCellValue(); break; case BOOLEAN: value = cell.getBooleanCellValue(); break; case NUMERIC: if(DateUtil.isCellDateFormatted(cell)) { //日期格式 value = cell.getDateCellValue(); }else{ //数字 value = cell.getNumericCellValue(); } break; case FORMULA: //公式 value = cell.getCellFormula(); break; default: break; } return value; }}
转载地址:http://wqkzb.baihongyu.com/