| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333 |
- 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;
- /**
- * <p>
- * 前端控制器
- * </p>
- *
- * @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<RoomTypeVo> roomTypeList = houseService.roomType();
- // 全日
- ArrayList<RoomTypeVo> day = new ArrayList<>();
- // 钟点
- ArrayList<RoomTypeVo> hour = new ArrayList<>();
- for (RoomTypeVo roomTypeVo : roomTypeList) {
- String roomType = roomTypeVo.getRoomType();
- if ("1".equals(roomType)) {
- day.add(roomTypeVo);
- } else {
- hour.add(roomTypeVo);
- }
- }
- HashMap<String, List<RoomTypeVo>> 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<HousePrice> housePrices = new ArrayList<>();
- // 获取改价内容
- List<AlterDto> list = alterPriceDto.getAlterDtoList();
- for (AlterDto alterDto : list) {
- int type = alterDto.getType();
- String name = alterDto.getName();
- LambdaQueryWrapper<House> 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<AlterPriceRecordVo> 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<String, Object> map = new HashMap<>();
- // 时间日期
- ArrayList<String> 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<House> wrapper = new LambdaQueryWrapper<>();
- wrapper.eq(ObjectUtils.isNotEmpty(houseName), House::getRoomName, houseName)
- .eq(ObjectUtils.isNotEmpty(houseType), House::getRoomType, houseType);
- IPage<House> houseIPage = houseService.page(new Page<>(page, size), wrapper);
- List<House> 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<HousePrice> housePrices = housePriceService.housePrice(localDate, endLocalDate, ids);
- ArrayList<HousePricePageVo> housePricePageVos = new ArrayList<>();
- // 组合数据
- for (House house : houseList) {
- HousePricePageVo housePricePageVo = new HousePricePageVo();
- housePricePageVo.setHouseName(house.getRoomName());
- housePricePageVo.setHouseType(house.getRoomType());
- ArrayList<HousePriceVo> 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<HousePricePageVo> 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<AlterPriceRecordVo> 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);
- }
- }
|