| | |
| | | import cc.mrbird.febs.common.core.entity.FebsResponse; |
| | | import cc.mrbird.febs.common.core.entity.QueryRequest; |
| | | import cc.mrbird.febs.common.core.entity.constant.StringConstant; |
| | | import cc.mrbird.febs.common.core.entity.system.Dept; |
| | | import cc.mrbird.febs.common.core.entity.system.Eximport; |
| | | import cc.mrbird.febs.common.core.entity.system.LoginLog; |
| | | import cc.mrbird.febs.common.core.entity.system.SystemUser; |
| | | import cc.mrbird.febs.common.core.exception.FebsException; |
| | | import cc.mrbird.febs.common.core.utils.FebsUtil; |
| | | import cc.mrbird.febs.server.system.annotation.ControllerEndpoint; |
| | | import cc.mrbird.febs.server.system.service.IDeptService; |
| | | import cc.mrbird.febs.server.system.service.ILoginLogService; |
| | | import cc.mrbird.febs.server.system.service.IUserDataPermissionService; |
| | | import cc.mrbird.febs.server.system.service.IUserService; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.google.common.base.Stopwatch; |
| | | import com.google.common.collect.ImmutableMap; |
| | | import com.google.common.collect.Lists; |
| | | import com.wuwenze.poi.ExcelKit; |
| | | import com.wuwenze.poi.handler.ExcelReadHandler; |
| | | import com.wuwenze.poi.pojo.ExcelErrorField; |
| | | import lombok.RequiredArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.beanutils.ConvertUtils; |
| | | import org.apache.commons.beanutils.converters.DateConverter; |
| | | import org.apache.commons.collections4.CollectionUtils; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.springframework.security.access.prepost.PreAuthorize; |
| | | import org.springframework.security.crypto.password.PasswordEncoder; |
| | | import org.springframework.validation.annotation.Validated; |
| | | import org.springframework.web.bind.annotation.*; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import javax.validation.Valid; |
| | | import javax.validation.constraints.NotBlank; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.io.IOException; |
| | | import java.util.*; |
| | | import java.util.stream.IntStream; |
| | | |
| | | /** |
| | | * @author MrBird |
| | |
| | | @RequiredArgsConstructor |
| | | @RequestMapping("user") |
| | | public class UserController { |
| | | |
| | | private final IDeptService deptService; |
| | | private final IUserService userService; |
| | | private final IUserDataPermissionService userDataPermissionService; |
| | | private final ILoginLogService loginLogService; |
| | |
| | | @PostMapping |
| | | @PreAuthorize("hasAuthority('user:add')") |
| | | @ControllerEndpoint(operation = "新增用户", exceptionMessage = "新增用户失败") |
| | | public void addUser(@Valid SystemUser user) throws FebsException { |
| | | public void addUser(@Valid SystemUser user) throws FebsException { |
| | | this.userService.createUser(user); |
| | | } |
| | | |
| | |
| | | @PreAuthorize("hasAuthority('user:export')") |
| | | @ControllerEndpoint(operation = "导出用户数据", exceptionMessage = "导出Excel失败") |
| | | public void export(QueryRequest queryRequest, SystemUser user, HttpServletResponse response) { |
| | | int count = this.userService.count(); |
| | | queryRequest.setPageSize(count); |
| | | List<SystemUser> users = this.userService.findUserDetailList(user, queryRequest).getRecords(); |
| | | ExcelKit.$Export(SystemUser.class, response).downXlsx(users, false); |
| | | } |
| | | |
| | | @PostMapping("updateStatus") |
| | | public void updateStatus(@NotBlank(message = "{required}") String certificateNumb) { |
| | | this.userService.updateUserByCertificateNumb(certificateNumb); |
| | | String[] strNumbers = certificateNumb.split(","); |
| | | for (int i = 0,k=strNumbers.length; i < k; i++) { |
| | | this.userService.updateUserByCertificateNumb(strNumbers[i]); |
| | | } |
| | | } |
| | | private static final String XLSX = ".xlsx"; |
| | | @PostMapping("import") |
| | | public FebsResponse importExcels(MultipartFile file) throws IOException, FebsException { |
| | | if (file.isEmpty()) { |
| | | throw new FebsException("导入数据为空"); |
| | | } |
| | | String filename = file.getOriginalFilename(); |
| | | if (!StringUtils.endsWith(filename, XLSX)) { |
| | | throw new FebsException("只支持.xlsx类型文件导入"); |
| | | } |
| | | Stopwatch stopwatch = Stopwatch.createStarted(); |
| | | final List<SystemUser> data = Lists.newArrayList(); |
| | | final List<Map<String, Object>> error = Lists.newArrayList(); |
| | | ExcelKit.$Import(SystemUser.class).readXlsx(file.getInputStream(), new ExcelReadHandler<SystemUser>() { |
| | | @Override |
| | | public void onSuccess(int sheet, int row, SystemUser eximport) { |
| | | data.add(eximport); |
| | | } |
| | | @Override |
| | | public void onError(int sheet, int row, List<ExcelErrorField> errorFields) { |
| | | error.add(ImmutableMap.of("row", row, "errorFields", errorFields)); |
| | | } |
| | | }); |
| | | if (CollectionUtils.isNotEmpty(data)) { |
| | | for (SystemUser systemUser:data){ |
| | | Dept one = deptService.getOne(new QueryWrapper<Dept>().eq("DEPT_NAME", systemUser.getDeptName())); |
| | | if(one!=null){ |
| | | systemUser.setDeptId(one.getDeptId()); |
| | | } |
| | | this.userService.createUser(systemUser); |
| | | } |
| | | } |
| | | ImmutableMap<String, Object> result = ImmutableMap.of( |
| | | "time", stopwatch.stop().toString(), |
| | | "data", data, |
| | | "error", error |
| | | ); |
| | | return new FebsResponse().data(result); |
| | | } |
| | | @PostMapping("template") |
| | | public void generateImportTemplate(HttpServletResponse response) { |
| | | List<SystemUser> list = new ArrayList<>(); |
| | | ExcelKit.$Export(SystemUser.class, response).downXlsx(list, true); |
| | | } |
| | | } |