From 044c3d2c6cd0936f9c1e29788505bbda3c4b65ad Mon Sep 17 00:00:00 2001 From: zeb <936152864@qq.com> Date: Thu, 24 Jun 2021 15:13:44 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=82=E6=8B=8D=E8=AE=B0=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/AuctionRecordController.java | 27 +++- .../com/air/land/entity/AuctionRecord.java | 2 + .../land/service/AuctionRecordService.java | 14 ++ .../impl/AuctionRecordServiceImpl.java | 137 ++++++++++++++++++ 4 files changed, 175 insertions(+), 5 deletions(-) diff --git a/air/src/main/java/com/air/land/controller/AuctionRecordController.java b/air/src/main/java/com/air/land/controller/AuctionRecordController.java index e825ca8..225e76b 100644 --- a/air/src/main/java/com/air/land/controller/AuctionRecordController.java +++ b/air/src/main/java/com/air/land/controller/AuctionRecordController.java @@ -12,10 +12,14 @@ import com.cinderella.framework.common.data.mybatis.QueryPage; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.AllArgsConstructor; +import org.springframework.transaction.annotation.Transactional; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; +import javax.validation.Valid; +import java.util.List; + /** * 参拍记录表 @@ -25,6 +29,7 @@ import org.springframework.web.multipart.MultipartFile; */ @RestController @AllArgsConstructor +@Validated @RequestMapping("/auction_record") @Api(value = "auction_record", tags = "参拍记录表管理") public class AuctionRecordController { @@ -55,7 +60,7 @@ public class AuctionRecordController { * @param auctionRecordVo 保存参拍记录 * @return R */ - @ApiOperation(value = "保存参拍记录", notes = "保存参拍记录") + @ApiOperation(value = "保存或更新参拍记录", notes = "保存参拍记录") @PostMapping public R save(@RequestBody @Validated AuctionRecordVo auctionRecordVo) { AuctionRecord auctionRecord = new AuctionRecord(); @@ -63,6 +68,18 @@ public class AuctionRecordController { return R.ok(auctionRecordService.saveOrUpdate(auctionRecord)); } + @ApiOperation(value = "保存或更新参拍记录(多个)", notes = "保存参拍记录") + @PostMapping("/multi") + @Transactional(rollbackFor = Exception.class) + public R saveBatch(@RequestBody @Valid List auctionRecords) { + auctionRecords.forEach(a -> { + AuctionRecord auctionRecord = new AuctionRecord(); + BeanUtil.copyProperties(a, auctionRecord); + auctionRecordService.saveOrUpdate(auctionRecord); + }); + return R.ok(); + } + /** * 通过id删除参拍记录表 * @@ -76,13 +93,13 @@ public class AuctionRecordController { } /** - * 导入已挂牌地块土拍举牌记录 + * 导入已挂牌地块土拍举牌记录,返回数据,前端调接口保存 * @author peihao * @date 2021/5/25 **/ @ApiOperation(value = "导入已挂牌地块土拍举牌记录", notes = "导入已挂牌地块土拍举牌记录") - @GetMapping(value = "/importRecordExcel") - public R importRecordExcel(@RequestParam(value="uploadFile") MultipartFile file){ - return R.ok(); + @PostMapping(value = "/importRecordExcel") + public R> importRecordExcel(@RequestParam(value="uploadFile") MultipartFile file){ + return R.ok(auctionRecordService.readRecordExcel(file)); } } diff --git a/air/src/main/java/com/air/land/entity/AuctionRecord.java b/air/src/main/java/com/air/land/entity/AuctionRecord.java index 82da10f..68680d3 100644 --- a/air/src/main/java/com/air/land/entity/AuctionRecord.java +++ b/air/src/main/java/com/air/land/entity/AuctionRecord.java @@ -7,6 +7,7 @@ import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data; import lombok.EqualsAndHashCode; +import lombok.experimental.Accessors; import java.time.LocalDateTime; @@ -20,6 +21,7 @@ import java.time.LocalDateTime; @Data @TableName("auction_record") @EqualsAndHashCode(callSuper = true) +@Accessors(chain = true) @ApiModel(value = "参拍记录表") public class AuctionRecord extends Model { private static final long serialVersionUID = 1L; diff --git a/air/src/main/java/com/air/land/service/AuctionRecordService.java b/air/src/main/java/com/air/land/service/AuctionRecordService.java index a6a7502..b8bc65b 100644 --- a/air/src/main/java/com/air/land/service/AuctionRecordService.java +++ b/air/src/main/java/com/air/land/service/AuctionRecordService.java @@ -2,6 +2,9 @@ package com.air.land.service; import com.air.land.entity.AuctionRecord; import com.baomidou.mybatisplus.extension.service.IService; +import org.springframework.web.multipart.MultipartFile; + +import java.util.List; /** * 参拍记录表 @@ -10,5 +13,16 @@ import com.baomidou.mybatisplus.extension.service.IService; * @date 2021-05-18 09:39:41 */ public interface AuctionRecordService extends IService { + /** + * 导入zip,多文件,未使用 + * @param file + */ + void importRecordExcel(MultipartFile file); + /** + * 根据excel读取土拍记录 + * @param file + * @return + */ + List readRecordExcel(MultipartFile file); } diff --git a/air/src/main/java/com/air/land/service/impl/AuctionRecordServiceImpl.java b/air/src/main/java/com/air/land/service/impl/AuctionRecordServiceImpl.java index c16a9b2..2d04418 100644 --- a/air/src/main/java/com/air/land/service/impl/AuctionRecordServiceImpl.java +++ b/air/src/main/java/com/air/land/service/impl/AuctionRecordServiceImpl.java @@ -1,10 +1,29 @@ package com.air.land.service.impl; +import cn.hutool.core.date.DateUtil; +import cn.hutool.core.io.FileUtil; +import cn.hutool.core.io.IoUtil; +import cn.hutool.core.util.RandomUtil; +import cn.hutool.core.util.StrUtil; +import cn.hutool.core.util.ZipUtil; +import cn.hutool.json.JSONUtil; import com.air.land.entity.AuctionRecord; import com.air.land.mapper.AuctionRecordMapper; import com.air.land.service.AuctionRecordService; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import lombok.SneakyThrows; +import lombok.extern.slf4j.Slf4j; +import org.apache.poi.xssf.usermodel.XSSFCell; +import org.apache.poi.xssf.usermodel.XSSFRow; +import org.apache.poi.xssf.usermodel.XSSFSheet; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.springframework.stereotype.Service; +import org.springframework.web.multipart.MultipartFile; + +import java.io.*; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; /** * 参拍记录表 @@ -13,6 +32,124 @@ import org.springframework.stereotype.Service; * @date 2021-05-18 09:39:41 */ @Service +@Slf4j public class AuctionRecordServiceImpl extends ServiceImpl implements AuctionRecordService { + @Override + public void importRecordExcel(MultipartFile file) { + String tempPath = RandomUtil.randomString(6); + File dir = FileUtil.mkdir(tempPath); + File tempFile = FileUtil.createTempFile("record-", "tmp", dir, true); + try { + IoUtil.copy(file.getInputStream(), new FileOutputStream(tempFile)); + } catch (IOException e) { + e.printStackTrace(); + } + File zipFile = new File(dir.getPath() + "/record.zip"); + FileUtil.move(tempFile, zipFile, true); + ZipUtil.unzip(zipFile); + + List files = new ArrayList<>(); + getAllDirFiles(dir, files); + files.forEach(f -> { + if (!f.getName().startsWith(StrUtil.DOT) && f.getName().endsWith(".xlsx")) { + log.info(f.getName()); + List lists = null; + try { + lists = readAuction(new FileInputStream(f), 0, 7, 21); + } catch (IOException e) { + e.printStackTrace(); + } + System.out.println(JSONUtil.toJsonPrettyStr(lists)); + } + }); + dir.delete(); + } + + @Override + @SneakyThrows + public List readRecordExcel(MultipartFile file) { + return readAuction(file.getInputStream(), 0, 7, 21); + } + + private void getAllDirFiles(File file, List files) { + for (File f : Objects.requireNonNull(file.listFiles())) { + if (f.isDirectory()) { + getAllDirFiles(f, files); + } else { + files.add(f); + } + } + } + + private List readAuction(InputStream file, int startSheet, int startRow, int startCell) throws IOException { + // 读取excel模板 + XSSFWorkbook wb = new XSSFWorkbook(file); + XSSFSheet sheet = wb.getSheetAt(startSheet); + int rowSum = sheet.getPhysicalNumberOfRows(); + //取值 + String city = sheet.getRow(2).getCell(1).toString(); + String landName = sheet.getRow(0).getCell(0).toString(); + String landCode = sheet.getRow(2).getCell(2).getRawValue(); + String date = DateUtil.formatDate(sheet.getRow(2).getCell(10).getDateCellValue()); + String winnerEnterprise = getCellValue(sheet.getRow(startCell).getCell(startCell + 1)); + //获取数据 + List result = new ArrayList<>(); + for (int i = startRow; i < rowSum; i++) { + XSSFRow sheetRow = sheet.getRow(i); + String rank = getCellValue(sheetRow.getCell(startCell)); + if (StrUtil.isBlank(rank)) { + break; + } + AuctionRecord record = new AuctionRecord().setCity(city).setLandName(landName).setWinnerEnterprises(winnerEnterprise); + //排名 + record.setRanking(rank); + //举牌企业 + record.setRaiseEnterprise(getCellValue(sheetRow.getCell(startCell + 1))); + //举牌价 + record.setRaisePrice(getCellPrice(sheetRow.getCell(startCell + 2))); + //名义楼面价 + record.setNominalFloorPrice(getCellPrice(sheetRow.getCell(startCell + 3))); + //无偿移交比例 + record.setPercentUnpaidTransfers(getCellPercent(sheetRow.getCell(startCell + 4))); + //实际楼面价 + record.setActualFloorPrice(getCellPrice(sheetRow.getCell(startCell + 5))); + //溢价率 + record.setPremiumRate(getCellPercent(sheetRow.getCell(startCell + 6))); + //最后轮次 + record.setFinalRound(getCellValue(sheetRow.getCell(startCell + 7))); + result.add(record); + } + + return result; + } + + private String getCellValue(XSSFCell cell) { + String value; + try { + cell.getCellFormula(); + value = cell.getRawValue(); + } catch (IllegalStateException e){ + value = cell.toString(); + } + return value; + } + + private String getCellPrice(XSSFCell cell) { + String price = getCellValue(cell); + if ("未举牌".equals(price)) { + return price; + } + return String.format("%.0f", Double.valueOf(price)); + } + + private String getCellPercent(XSSFCell cell) { + String price = getCellValue(cell); + if ("未举牌".equals(price)) { + return price; + } + Double d = Double.valueOf(price); + d = d * 100; + return String.format("%.2f", d); + } }