| | |
| | | package com.ruoyi.web.controller.api; |
| | | |
| | | |
| | | import cn.afterturn.easypoi.excel.ExcelExportUtil; |
| | | import cn.afterturn.easypoi.excel.ExcelImportUtil; |
| | | import cn.afterturn.easypoi.excel.entity.ExportParams; |
| | | import cn.afterturn.easypoi.excel.entity.ImportParams; |
| | | import cn.hutool.json.JSONObject; |
| | | import com.ruoyi.common.annotation.Log; |
| | | import com.ruoyi.common.basic.PageInfo; |
| | | import com.ruoyi.common.core.domain.R; |
| | | import com.ruoyi.common.core.domain.entity.SysUser; |
| | | import com.ruoyi.common.core.exception.ServiceException; |
| | | import com.ruoyi.common.enums.BusinessType; |
| | | import com.ruoyi.common.utils.WebUtils; |
| | | import com.ruoyi.system.dto.CleanerDTO; |
| | | import com.ruoyi.system.importExcel.TCleanerImportExcel; |
| | | import com.ruoyi.system.importExcel.TLocationImportExcel; |
| | | import com.ruoyi.system.model.TCleaner; |
| | | import com.ruoyi.system.model.TLocation; |
| | | import com.ruoyi.system.model.TLocationType; |
| | | import com.ruoyi.system.model.TProjectDept; |
| | | import com.ruoyi.system.query.CleanerListQuery; |
| | | import com.ruoyi.system.service.TCleanerService; |
| | | import com.ruoyi.system.service.TProjectDeptService; |
| | | import com.ruoyi.system.vo.system.CleanerListVO; |
| | | import io.swagger.annotations.Api; |
| | | import io.swagger.annotations.ApiOperation; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.poi.ss.usermodel.Workbook; |
| | | import org.springframework.transaction.annotation.Propagation; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.util.CollectionUtils; |
| | | import org.springframework.web.bind.annotation.*; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import javax.annotation.Resource; |
| | | import javax.servlet.ServletOutputStream; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.net.URLEncoder; |
| | | import java.util.ArrayList; |
| | | import java.util.Arrays; |
| | | import java.util.List; |
| | | import java.util.Objects; |
| | | |
| | | /** |
| | | * <p> |
| | |
| | | @Api(tags = "保洁员") |
| | | @RestController |
| | | @RequestMapping("/t-cleaner") |
| | | @Slf4j |
| | | public class TCleanerController { |
| | | @Resource |
| | | private TCleanerService cleanerService; |
| | | @Resource |
| | | private TProjectDeptService projectDeptService; |
| | | |
| | | |
| | | @ApiOperation(value = "保洁员分页列表") |
| | |
| | | cleanerService.removeBatchByIds(Arrays.asList(split)); |
| | | return R.ok(); |
| | | } |
| | | @Log(title = "保洁员导入", businessType = BusinessType.INSERT) |
| | | @ApiOperation(value = "保洁员导入") |
| | | @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRES_NEW) |
| | | @PostMapping("/importCleaner") |
| | | public R<String> importCleaner(@RequestPart("file") MultipartFile file) { |
| | | ImportParams params = new ImportParams(); |
| | | // params.setTitleRows(1); // 标题行数 |
| | | params.setHeadRows(1); //表头行数 |
| | | InputStream inputStream = null; |
| | | // List<CustomerImportFailedData> failedData = new ArrayList<>(); |
| | | List<TCleanerImportExcel> locationExcelList; |
| | | try { |
| | | inputStream = file.getInputStream(); |
| | | locationExcelList = ExcelImportUtil.importExcel(inputStream, |
| | | TCleanerImportExcel.class, params); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | log.error("保洁员导入失败:{}", e.getMessage()); |
| | | throw new ServiceException("保洁员导入失败!"); |
| | | } finally { |
| | | try { |
| | | inputStream.close(); |
| | | } catch (IOException e) { |
| | | throw new ServiceException(e.getMessage()); |
| | | } |
| | | } |
| | | if (CollectionUtils.isEmpty(locationExcelList)) { |
| | | throw new ServiceException("保洁员数据为空!"); |
| | | } |
| | | JSONObject result = new JSONObject(); |
| | | List<TProjectDept> projects = projectDeptService.list(); |
| | | for (TCleanerImportExcel locationExcel : locationExcelList) { |
| | | System.err.println(locationExcel); |
| | | TCleaner tCleaner = new TCleaner(); |
| | | TProjectDept projectDept = projects.stream().filter(e -> e.getProjectName().equals(locationExcel.getProjectName())).findFirst().orElse(null); |
| | | if (projectDept==null){ |
| | | result.append("保洁员:[", locationExcel.getCleanerName()+"]未查询到部门"); |
| | | continue; |
| | | }else{ |
| | | if (projectDept.getParentId().equals("0")){ |
| | | result.append("保洁员:[", locationExcel.getCleanerName()+"]所属片区不能指定为项目部"); |
| | | continue; |
| | | } |
| | | } |
| | | tCleaner.setCleanerCode(locationExcel.getCleanerCode()); |
| | | tCleaner.setCleanerName(locationExcel.getCleanerName()); |
| | | tCleaner.setProjectId(projectDept.getId()); |
| | | tCleaner.setDeptCode(locationExcel.getDeptCode()); |
| | | tCleaner.setProjectCode(locationExcel.getProjectCode()); |
| | | cleanerService.save(tCleaner); |
| | | } |
| | | if(!result.isEmpty()){ |
| | | return R.ok(result.toString()); |
| | | } |
| | | return R.ok(); |
| | | } |
| | | @ApiOperation(value = "保洁员导入模板下载") |
| | | @GetMapping("/import-cleaner") |
| | | public void importCleaner() { |
| | | List<TCleanerImportExcel> locationImportExcels = new ArrayList<>(); |
| | | TCleanerImportExcel tLocationImportExcel = new TCleanerImportExcel(); |
| | | locationImportExcels.add(tLocationImportExcel); |
| | | Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), TCleanerImportExcel.class, locationImportExcels); |
| | | HttpServletResponse response = WebUtils.response(); |
| | | ServletOutputStream outputStream = null; |
| | | try { |
| | | String fileName = URLEncoder.encode("保洁员导入模板.xls", "utf-8"); |
| | | response.setHeader("Content-Disposition", "attachment;filename=" + fileName); |
| | | response.setHeader("content-Type", "application/vnd.ms-excel"); |
| | | response.setHeader("Pragma", "no-cache"); |
| | | response.setHeader("Cache-Control", "no-cache"); |
| | | outputStream = response.getOutputStream(); |
| | | workbook.write(outputStream); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | System.out.println("保洁员导入模板下载失败!"); |
| | | } finally { |
| | | try { |
| | | outputStream.close(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |