package com.template.controller; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.toolkit.ObjectUtils; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.template.api.HousePriceAPI; import com.template.common.utils.DateUtil; import com.template.common.utils.ExcelUtils2; import com.template.model.dto.AlterDto; import com.template.model.dto.AlterPriceDto; import com.template.model.pojo.House; import com.template.model.pojo.HousePrice; import com.template.model.pojo.PermissionSetting; import com.template.model.result.CommonResult; import com.template.model.result.PageUtils; import com.template.model.vo.AlterPriceRecordVo; import com.template.model.vo.HousePricePageVo; import com.template.model.vo.HousePriceVo; import com.template.model.vo.RoomTypeVo; import com.template.services.HousePriceService; import com.template.services.HouseService; import com.template.services.PermissionSettingService; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletResponse; import java.math.BigDecimal; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; /** *

* 前端控制器 *

* * @author ceshi * @since 2023-11-09 */ @RestController public class HousePriceController implements HousePriceAPI { @Autowired HouseService houseService; @Autowired PermissionSettingService permissionSettingService; @Autowired HousePriceService housePriceService; @Override public CommonResult roomType() { List roomTypeList = houseService.roomType(); // 全日 ArrayList day = new ArrayList<>(); // 钟点 ArrayList hour = new ArrayList<>(); for (RoomTypeVo roomTypeVo : roomTypeList) { String roomType = roomTypeVo.getRoomType(); if ("1".equals(roomType)) { day.add(roomTypeVo); } else { hour.add(roomTypeVo); } } HashMap> map = new HashMap<>(); map.put("day", day); map.put("hour", hour); return CommonResult.ok(map); } @Override public CommonResult alterPrice(AlterPriceDto alterPriceDto) { if (ObjectUtils.isEmpty(alterPriceDto) && ObjectUtils.isEmpty(alterPriceDto.getAdminId())) { return CommonResult.fail(); } int adminId = alterPriceDto.getAdminId(); PermissionSetting permissionSetting = permissionSettingService.getById(adminId); if (ObjectUtils.isEmpty(permissionSetting)) { return CommonResult.fail("非法进入"); } // 判断该用户是否拥有权限 String houseTypeManagement = permissionSetting.getHouseTypeManagement(); if (!"0".equals(houseTypeManagement) && !houseTypeManagement.contains("4")) { return CommonResult.fail("此账号暂无该权限"); } ArrayList housePrices = new ArrayList<>(); // 获取改价内容 List list = alterPriceDto.getAlterDtoList(); for (AlterDto alterDto : list) { int type = alterDto.getType(); String name = alterDto.getName(); LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); wrapper.eq(House::getRoomType, type) .eq(House::getRoomName, name); House house = houseService.getOne(wrapper); if (ObjectUtils.isNotEmpty(house)) { BigDecimal roomPrice = house.getRoomPrice(); HousePrice housePrice = new HousePrice(); BigDecimal price = alterDto.getPrice(); // 改价后的价格 housePrice.setPrice(price); // 原价 housePrice.setOriginalPrice(roomPrice); // 房型id housePrice.setHouseId(house.getId() + ""); // 设置日期 housePrice.setSetDate(alterDto.getDate()); housePrices.add(housePrice); } } boolean b = housePriceService.saveBatch(housePrices); if (b) { return CommonResult.ok(); } return CommonResult.fail(); } @Override public CommonResult alterPriceRecord(int adminId, String type, String houseName, String operatingTime, String priceTime, String operatingName, int page, int size) { if (ObjectUtils.isEmpty(adminId)) { return CommonResult.fail(); } PermissionSetting permissionSetting = permissionSettingService.getById(adminId); if (ObjectUtils.isEmpty(permissionSetting)) { return CommonResult.fail("非法进入"); } // 判断该用户是否拥有权限 String houseTypeManagement = permissionSetting.getHouseTypeManagement(); if (!"0".equals(houseTypeManagement) && ! houseTypeManagement.contains("5")) { return CommonResult.fail("此账号暂无该权限"); } if (ObjectUtils.isEmpty(page) && page <= 0) { page = 1; } if (ObjectUtils.isEmpty(size) && size <= 0) { size = 10; } if (ObjectUtils.isNotEmpty(operatingTime)) { if (operatingTime.split(",").length != 2) { return CommonResult.fail("参数异常"); } } if (ObjectUtils.isNotEmpty(priceTime)) { if (priceTime.split(",").length != 2) { return CommonResult.fail("参数异常"); } } PageUtils voPageUtils = housePriceService.alterPriceRecord(type, houseName, operatingTime, priceTime, operatingName, page, size); return CommonResult.ok(voPageUtils); } @Override public CommonResult housePricePage(String date, String houseName, String houseType, int page, int size) { if (ObjectUtils.isEmpty(page) && page <= 0) { page = 1; } if (ObjectUtils.isEmpty(size) && size <= 0) { size = 10; } // 获取当时时间 LocalDateTime localDate; DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); if (ObjectUtils.isEmpty(date)) { localDate = LocalDateTime.now(); } else { localDate = LocalDateTime.parse(date, dateTimeFormatter1); } // 需获取21天内的所有改价信息 LocalDateTime endLocalDate = localDate.plusDays(20); // 返回格式 HashMap map = new HashMap<>(); // 时间日期 ArrayList list = new ArrayList<>(); for (int i = 0; i < 21; i++) { LocalDateTime localDate1 = localDate.plusDays(i); String format = localDate1.format(dateTimeFormatter1); list.add(format); } map.put("dateTime", list); // 先获取房型数据 LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); wrapper.eq(ObjectUtils.isNotEmpty(houseName), House::getRoomName, houseName) .eq(ObjectUtils.isNotEmpty(houseType), House::getRoomType, houseType); IPage houseIPage = houseService.page(new Page<>(page, size), wrapper); List houseList = houseIPage.getRecords(); if (ObjectUtils.isEmpty(houseList) && houseList.size() <= 0) { map.put("page", houseIPage); return CommonResult.ok(map); } // 获取当前房型的所有id String ids = ""; for (int i = 0; i < houseList.size(); i++) { String id = houseList.get(i).getId() + ""; if (i == 0) { ids = ids + id; } else { ids = ids + "," + id; } } System.out.println("ids = " + ids); // 获取在这21天内所属房型的所有改价记录 List housePrices = housePriceService.housePrice(localDate, endLocalDate, ids); ArrayList housePricePageVos = new ArrayList<>(); // 组合数据 for (House house : houseList) { HousePricePageVo housePricePageVo = new HousePricePageVo(); housePricePageVo.setHouseName(house.getRoomName()); housePricePageVo.setHouseType(house.getRoomType()); ArrayList housePriceVos = new ArrayList<>(); for (int i = 0; i < 21; i++) { HousePriceVo housePriceVo = new HousePriceVo(); LocalDateTime localDate1 = localDate.plusDays(i); String format = localDate1.format(dateTimeFormatter1); housePriceVo.setDate(format); housePriceVo.setPrice(house.getRoomPrice()); // 判断这个时间内是否有 for (HousePrice housePrice : housePrices) { if (housePrice.getHouseId().equals(house.getId() + "")) { String setDate = housePrice.getSetDate(); String[] split = setDate.split(","); Date startDate = DateUtil.parseDateOnly(split[0]); Date endDate = DateUtil.parseDateOnly(split[1]); Date date1 = Date.from(localDate1.atZone(ZoneId.systemDefault()).toInstant()); // 判断当前时间是否在[startTime, endTime]区间 assert startDate != null; boolean effectiveDate = DateUtil.isEffectiveDate(date1, startDate, endDate); if (effectiveDate) { BigDecimal price = housePrice.getPrice(); housePriceVo.setPrice(price); } } } housePriceVos.add(housePriceVo); } housePricePageVo.setHousePriceVos(housePriceVos); housePricePageVos.add(housePricePageVo); } IPage housePricePageVoIPage = new Page<>(); BeanUtils.copyProperties(houseIPage, housePricePageVoIPage); housePricePageVoIPage.setRecords(housePricePageVos); map.put("page", housePricePageVoIPage); return CommonResult.ok(map); } @Override public void queryExport(HttpServletResponse response, String type, String houseName, String operatingTime, String priceTime, String operatingName) { String fileName = "改价记录.xls"; List vos = housePriceService.queryExport(type, houseName, operatingTime, priceTime, operatingName); // 导出 Workbook workbook = new XSSFWorkbook(); Sheet sheet = workbook.createSheet("改价记录表"); Row headerRow = sheet.createRow(0); headerRow.createCell(0).setCellValue("类型"); headerRow.createCell(1).setCellValue("房型名称"); headerRow.createCell(2).setCellValue("价格设置日期"); headerRow.createCell(3).setCellValue("修改后价格"); headerRow.createCell(4).setCellValue("原价"); headerRow.createCell(5).setCellValue("操作人"); headerRow.createCell(6).setCellValue("操作时间"); if (ObjectUtils.isNotEmpty(vos) && vos.size() > 0) { for (int i = 0; i < vos.size(); i++) { AlterPriceRecordVo vo = vos.get(i); Row dataRow1 = sheet.createRow(i + 1); dataRow1.createCell(0).setCellValue(vo.getHouseType()); dataRow1.createCell(1).setCellValue(vo.getHouseName()); dataRow1.createCell(2).setCellValue(vo.getSetDate()); dataRow1.createCell(3).setCellValue(vo.getAlterPrice().toString()); dataRow1.createCell(4).setCellValue(vo.getOriginalPrice().toString()); dataRow1.createCell(5).setCellValue(vo.getName()); dataRow1.createCell(6).setCellValue(vo.getDate()); } } // 将工作簿写入文件 ExcelUtils2.excelDownload(workbook, "改价记录表.xlsx", response); } }