HousePriceController.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. package com.template.controller;
  2. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  3. import com.baomidou.mybatisplus.core.metadata.IPage;
  4. import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
  5. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  6. import com.template.api.HousePriceAPI;
  7. import com.template.common.utils.DateUtil;
  8. import com.template.common.utils.ExcelUtils2;
  9. import com.template.model.dto.AlterDto;
  10. import com.template.model.dto.AlterPriceDto;
  11. import com.template.model.pojo.House;
  12. import com.template.model.pojo.HousePrice;
  13. import com.template.model.pojo.PermissionSetting;
  14. import com.template.model.result.CommonResult;
  15. import com.template.model.result.PageUtils;
  16. import com.template.model.vo.AlterPriceRecordVo;
  17. import com.template.model.vo.HousePricePageVo;
  18. import com.template.model.vo.HousePriceVo;
  19. import com.template.model.vo.RoomTypeVo;
  20. import com.template.services.HousePriceService;
  21. import com.template.services.HouseService;
  22. import com.template.services.PermissionSettingService;
  23. import org.apache.poi.ss.usermodel.Row;
  24. import org.apache.poi.ss.usermodel.Sheet;
  25. import org.apache.poi.ss.usermodel.Workbook;
  26. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  27. import org.springframework.beans.BeanUtils;
  28. import org.springframework.beans.factory.annotation.Autowired;
  29. import org.springframework.web.bind.annotation.RestController;
  30. import javax.servlet.http.HttpServletResponse;
  31. import java.math.BigDecimal;
  32. import java.time.LocalDateTime;
  33. import java.time.ZoneId;
  34. import java.time.format.DateTimeFormatter;
  35. import java.util.ArrayList;
  36. import java.util.Date;
  37. import java.util.HashMap;
  38. import java.util.List;
  39. /**
  40. * <p>
  41. * 前端控制器
  42. * </p>
  43. *
  44. * @author ceshi
  45. * @since 2023-11-09
  46. */
  47. @RestController
  48. public class HousePriceController implements HousePriceAPI {
  49. @Autowired
  50. HouseService houseService;
  51. @Autowired
  52. PermissionSettingService permissionSettingService;
  53. @Autowired
  54. HousePriceService housePriceService;
  55. @Override
  56. public CommonResult roomType() {
  57. List<RoomTypeVo> roomTypeList = houseService.roomType();
  58. // 全日
  59. ArrayList<RoomTypeVo> day = new ArrayList<>();
  60. // 钟点
  61. ArrayList<RoomTypeVo> hour = new ArrayList<>();
  62. for (RoomTypeVo roomTypeVo : roomTypeList) {
  63. String roomType = roomTypeVo.getRoomType();
  64. if ("1".equals(roomType)) {
  65. day.add(roomTypeVo);
  66. } else {
  67. hour.add(roomTypeVo);
  68. }
  69. }
  70. HashMap<String, List<RoomTypeVo>> map = new HashMap<>();
  71. map.put("day", day);
  72. map.put("hour", hour);
  73. return CommonResult.ok(map);
  74. }
  75. @Override
  76. public CommonResult alterPrice(AlterPriceDto alterPriceDto) {
  77. if (ObjectUtils.isEmpty(alterPriceDto) && ObjectUtils.isEmpty(alterPriceDto.getAdminId())) {
  78. return CommonResult.fail();
  79. }
  80. int adminId = alterPriceDto.getAdminId();
  81. PermissionSetting permissionSetting = permissionSettingService.getById(adminId);
  82. if (ObjectUtils.isEmpty(permissionSetting)) {
  83. return CommonResult.fail("非法进入");
  84. }
  85. // 判断该用户是否拥有权限
  86. String houseTypeManagement = permissionSetting.getHouseTypeManagement();
  87. if (!"0".equals(houseTypeManagement) && !houseTypeManagement.contains("4")) {
  88. return CommonResult.fail("此账号暂无该权限");
  89. }
  90. ArrayList<HousePrice> housePrices = new ArrayList<>();
  91. // 获取改价内容
  92. List<AlterDto> list = alterPriceDto.getAlterDtoList();
  93. for (AlterDto alterDto : list) {
  94. int type = alterDto.getType();
  95. String name = alterDto.getName();
  96. LambdaQueryWrapper<House> wrapper = new LambdaQueryWrapper<>();
  97. wrapper.eq(House::getRoomType, type)
  98. .eq(House::getRoomName, name);
  99. House house = houseService.getOne(wrapper);
  100. if (ObjectUtils.isNotEmpty(house)) {
  101. BigDecimal roomPrice = house.getRoomPrice();
  102. HousePrice housePrice = new HousePrice();
  103. BigDecimal price = alterDto.getPrice();
  104. // 改价后的价格
  105. housePrice.setPrice(price);
  106. // 原价
  107. housePrice.setOriginalPrice(roomPrice);
  108. // 房型id
  109. housePrice.setHouseId(house.getId() + "");
  110. // 设置日期
  111. housePrice.setSetDate(alterDto.getDate());
  112. housePrices.add(housePrice);
  113. }
  114. }
  115. boolean b = housePriceService.saveBatch(housePrices);
  116. if (b) {
  117. return CommonResult.ok();
  118. }
  119. return CommonResult.fail();
  120. }
  121. @Override
  122. public CommonResult alterPriceRecord(int adminId, String type, String houseName, String operatingTime, String priceTime, String operatingName, int page, int size) {
  123. if (ObjectUtils.isEmpty(adminId)) {
  124. return CommonResult.fail();
  125. }
  126. PermissionSetting permissionSetting = permissionSettingService.getById(adminId);
  127. if (ObjectUtils.isEmpty(permissionSetting)) {
  128. return CommonResult.fail("非法进入");
  129. }
  130. // 判断该用户是否拥有权限
  131. String houseTypeManagement = permissionSetting.getHouseTypeManagement();
  132. if (!"0".equals(houseTypeManagement) && ! houseTypeManagement.contains("5")) {
  133. return CommonResult.fail("此账号暂无该权限");
  134. }
  135. if (ObjectUtils.isEmpty(page) && page <= 0) {
  136. page = 1;
  137. }
  138. if (ObjectUtils.isEmpty(size) && size <= 0) {
  139. size = 10;
  140. }
  141. if (ObjectUtils.isNotEmpty(operatingTime)) {
  142. if (operatingTime.split(",").length != 2) {
  143. return CommonResult.fail("参数异常");
  144. }
  145. }
  146. if (ObjectUtils.isNotEmpty(priceTime)) {
  147. if (priceTime.split(",").length != 2) {
  148. return CommonResult.fail("参数异常");
  149. }
  150. }
  151. PageUtils<AlterPriceRecordVo> voPageUtils = housePriceService.alterPriceRecord(type, houseName, operatingTime, priceTime, operatingName, page, size);
  152. return CommonResult.ok(voPageUtils);
  153. }
  154. @Override
  155. public CommonResult housePricePage(String date, String houseName, String houseType, int page, int size) {
  156. if (ObjectUtils.isEmpty(page) && page <= 0) {
  157. page = 1;
  158. }
  159. if (ObjectUtils.isEmpty(size) && size <= 0) {
  160. size = 10;
  161. }
  162. // 获取当时时间
  163. LocalDateTime localDate;
  164. DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  165. if (ObjectUtils.isEmpty(date)) {
  166. localDate = LocalDateTime.now();
  167. } else {
  168. localDate = LocalDateTime.parse(date, dateTimeFormatter1);
  169. }
  170. // 需获取21天内的所有改价信息
  171. LocalDateTime endLocalDate = localDate.plusDays(20);
  172. // 返回格式
  173. HashMap<String, Object> map = new HashMap<>();
  174. // 时间日期
  175. ArrayList<String> list = new ArrayList<>();
  176. for (int i = 0; i < 21; i++) {
  177. LocalDateTime localDate1 = localDate.plusDays(i);
  178. String format = localDate1.format(dateTimeFormatter1);
  179. list.add(format);
  180. }
  181. map.put("dateTime", list);
  182. // 先获取房型数据
  183. LambdaQueryWrapper<House> wrapper = new LambdaQueryWrapper<>();
  184. wrapper.eq(ObjectUtils.isNotEmpty(houseName), House::getRoomName, houseName)
  185. .eq(ObjectUtils.isNotEmpty(houseType), House::getRoomType, houseType);
  186. IPage<House> houseIPage = houseService.page(new Page<>(page, size), wrapper);
  187. List<House> houseList = houseIPage.getRecords();
  188. if (ObjectUtils.isEmpty(houseList) && houseList.size() <= 0) {
  189. map.put("page", houseIPage);
  190. return CommonResult.ok(map);
  191. }
  192. // 获取当前房型的所有id
  193. String ids = "";
  194. for (int i = 0; i < houseList.size(); i++) {
  195. String id = houseList.get(i).getId() + "";
  196. if (i == 0) {
  197. ids = ids + id;
  198. } else {
  199. ids = ids + "," + id;
  200. }
  201. }
  202. System.out.println("ids = " + ids);
  203. // 获取在这21天内所属房型的所有改价记录
  204. List<HousePrice> housePrices = housePriceService.housePrice(localDate, endLocalDate, ids);
  205. ArrayList<HousePricePageVo> housePricePageVos = new ArrayList<>();
  206. // 组合数据
  207. for (House house : houseList) {
  208. HousePricePageVo housePricePageVo = new HousePricePageVo();
  209. housePricePageVo.setHouseName(house.getRoomName());
  210. housePricePageVo.setHouseType(house.getRoomType());
  211. ArrayList<HousePriceVo> housePriceVos = new ArrayList<>();
  212. for (int i = 0; i < 21; i++) {
  213. HousePriceVo housePriceVo = new HousePriceVo();
  214. LocalDateTime localDate1 = localDate.plusDays(i);
  215. String format = localDate1.format(dateTimeFormatter1);
  216. housePriceVo.setDate(format);
  217. housePriceVo.setPrice(house.getRoomPrice());
  218. // 判断这个时间内是否有
  219. for (HousePrice housePrice : housePrices) {
  220. if (housePrice.getHouseId().equals(house.getId() + "")) {
  221. String setDate = housePrice.getSetDate();
  222. String[] split = setDate.split(",");
  223. Date startDate = DateUtil.parseDateOnly(split[0]);
  224. Date endDate = DateUtil.parseDateOnly(split[1]);
  225. Date date1 = Date.from(localDate1.atZone(ZoneId.systemDefault()).toInstant());
  226. // 判断当前时间是否在[startTime, endTime]区间
  227. assert startDate != null;
  228. boolean effectiveDate = DateUtil.isEffectiveDate(date1, startDate, endDate);
  229. if (effectiveDate) {
  230. BigDecimal price = housePrice.getPrice();
  231. housePriceVo.setPrice(price);
  232. }
  233. }
  234. }
  235. housePriceVos.add(housePriceVo);
  236. }
  237. housePricePageVo.setHousePriceVos(housePriceVos);
  238. housePricePageVos.add(housePricePageVo);
  239. }
  240. IPage<HousePricePageVo> housePricePageVoIPage = new Page<>();
  241. BeanUtils.copyProperties(houseIPage, housePricePageVoIPage);
  242. housePricePageVoIPage.setRecords(housePricePageVos);
  243. map.put("page", housePricePageVoIPage);
  244. return CommonResult.ok(map);
  245. }
  246. @Override
  247. public void queryExport(HttpServletResponse response, String type, String houseName, String operatingTime, String priceTime, String operatingName) {
  248. String fileName = "改价记录.xls";
  249. List<AlterPriceRecordVo> vos = housePriceService.queryExport(type, houseName, operatingTime, priceTime, operatingName);
  250. // 导出
  251. Workbook workbook = new XSSFWorkbook();
  252. Sheet sheet = workbook.createSheet("改价记录表");
  253. Row headerRow = sheet.createRow(0);
  254. headerRow.createCell(0).setCellValue("类型");
  255. headerRow.createCell(1).setCellValue("房型名称");
  256. headerRow.createCell(2).setCellValue("价格设置日期");
  257. headerRow.createCell(3).setCellValue("修改后价格");
  258. headerRow.createCell(4).setCellValue("原价");
  259. headerRow.createCell(5).setCellValue("操作人");
  260. headerRow.createCell(6).setCellValue("操作时间");
  261. if (ObjectUtils.isNotEmpty(vos) && vos.size() > 0) {
  262. for (int i = 0; i < vos.size(); i++) {
  263. AlterPriceRecordVo vo = vos.get(i);
  264. Row dataRow1 = sheet.createRow(i + 1);
  265. dataRow1.createCell(0).setCellValue(vo.getHouseType());
  266. dataRow1.createCell(1).setCellValue(vo.getHouseName());
  267. dataRow1.createCell(2).setCellValue(vo.getSetDate());
  268. dataRow1.createCell(3).setCellValue(vo.getAlterPrice().toString());
  269. dataRow1.createCell(4).setCellValue(vo.getOriginalPrice().toString());
  270. dataRow1.createCell(5).setCellValue(vo.getName());
  271. dataRow1.createCell(6).setCellValue(vo.getDate());
  272. }
  273. }
  274. // 将工作簿写入文件
  275. ExcelUtils2.excelDownload(workbook, "改价记录表.xlsx", response);
  276. }
  277. }