From bd5af6eff63613ef4d25751d8105d2d82e2997fa Mon Sep 17 00:00:00 2001
From: zhongrj <646384940@qq.com>
Date: Thu, 09 Nov 2023 21:12:25 +0800
Subject: [PATCH] 租房统计查询修改

---
 src/main/java/org/springblade/modules/house/service/impl/HouseRentalServiceImpl.java |  141 ++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 139 insertions(+), 2 deletions(-)

diff --git a/src/main/java/org/springblade/modules/house/service/impl/HouseRentalServiceImpl.java b/src/main/java/org/springblade/modules/house/service/impl/HouseRentalServiceImpl.java
index 44bda12..b2494d1 100644
--- a/src/main/java/org/springblade/modules/house/service/impl/HouseRentalServiceImpl.java
+++ b/src/main/java/org/springblade/modules/house/service/impl/HouseRentalServiceImpl.java
@@ -16,19 +16,28 @@
  */
 package org.springblade.modules.house.service.impl;
 
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import org.springblade.core.secure.utils.AuthUtil;
 import org.springblade.modules.house.entity.HouseRentalEntity;
+import org.springblade.modules.house.entity.HouseTenantEntity;
 import org.springblade.modules.house.service.IHouseTenantService;
+import org.springblade.modules.house.vo.HouseRentalStatistics;
+import org.springblade.modules.house.vo.HouseRentalTenantVO;
 import org.springblade.modules.house.vo.HouseRentalVO;
 import org.springblade.modules.house.mapper.HouseRentalMapper;
 import org.springblade.modules.house.service.IHouseRentalService;
-import org.springblade.core.mp.base.BaseServiceImpl;
 import org.springblade.modules.house.vo.HouseTenantVO;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import com.baomidou.mybatisplus.core.metadata.IPage;
+import org.springframework.transaction.annotation.Transactional;
 
+import java.util.ArrayList;
+import java.util.Date;
 import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
 
 /**
  * 出租屋 服务实现类
@@ -49,7 +58,7 @@
 	 * @return
 	 */
 	@Override
-	public IPage<HouseRentalVO> selectHouseRentalPage(IPage<HouseRentalVO> page, HouseRentalVO houseRental) {
+	public IPage<HouseRentalTenantVO> selectHouseRentalPage(IPage<HouseRentalTenantVO> page, HouseRentalTenantVO houseRental) {
 		return page.setRecords(baseMapper.selectHouseRentalPage(page, houseRental));
 	}
 
@@ -64,4 +73,132 @@
 		// 返回
 		return houseRentalVOS;
 	}
+
+	/**
+	 * 自定义房屋出租新增
+	 * @param houseRentalVO
+	 * @return
+	 */
+	@Override
+	@Transactional(rollbackFor = Exception.class)
+	public Boolean add(HouseRentalVO houseRentalVO) {
+		houseRentalVO.setCreateUser(AuthUtil.getUserId());
+		houseRentalVO.setCreateTime(new Date());
+		houseRentalVO.setUpdateUser(AuthUtil.getUserId());
+		houseRentalVO.setUpdateTime(new Date());
+		//保存自身
+		boolean save = save(houseRentalVO);
+		List<HouseTenantEntity> houseTenantEntities = new ArrayList<>();
+		houseRentalVO.getHouseTenantVOList().forEach(e->{
+			HouseTenantEntity houseTenant= new HouseTenantEntity();
+
+			houseTenant.setHousingRentalId(houseRentalVO.getId());
+			houseTenant.setName(e.getName());
+			houseTenant.setPhone(e.getPhone());
+			houseTenant.setIdCard(e.getIdCard());
+			houseTenant.setDomicile(e.getDomicile());
+			houseTenant.setWorkUnit(e.getWorkUnit());
+			houseTenantEntities.add(houseTenant);
+
+		});
+		boolean saveBatch = houseTenantService.saveBatch(houseTenantEntities);
+
+		return save&&saveBatch;
+	}
+
+	/**
+	 * 出租屋 自定义删除
+	 * @param id
+	 * @return
+	 */
+	@Override
+	public Boolean removeHouseRental(Long id) {
+		// 先删除出租屋信息
+		boolean b = removeById(id);
+		// 再删除租户信息
+		int i = houseTenantService.removeByHousingRentalId(id);
+		if (i>0){
+			return true && b;
+		}
+		return false;
+	}
+
+	/**
+	 * 出租屋 自定义修改
+	 * @param houseRental
+	 * @return
+	 */
+	@Override
+	@Transactional(rollbackFor = Exception.class)
+	public Boolean updateHouseRental(HouseRentalVO houseRental) {
+		boolean addFlag = true;
+		boolean updateFlag = true;
+		boolean removeFlag = true;
+		houseRental.setUpdateUser(AuthUtil.getUserId());
+		houseRental.setUpdateTime(new Date());
+		//更新自身
+		boolean update = updateById(houseRental);
+		// 查询对应已存在的租户
+		QueryWrapper<HouseTenantEntity> wrapper = new QueryWrapper<>();
+		wrapper.eq("housing_rental_id",houseRental.getId());
+		List<HouseTenantEntity> oldList = houseTenantService.list(wrapper);
+		List<HouseTenantVO> list = houseRental.getHouseTenantVOList();
+		// 申明新增,修改,删除集合
+		List<HouseTenantEntity> newList = new ArrayList<>();
+		List<HouseTenantEntity> addList = new ArrayList<>();
+		List<HouseTenantEntity> updateList = new ArrayList<>();
+		List<HouseTenantEntity> removeList = new ArrayList<>();
+		// 找出需要新增的,否则组成新集合进行比对
+		for (HouseTenantVO houseTenantVO : list) {
+			houseTenantVO.setHousingRentalId(houseRental.getId());
+			if (null==houseTenantVO.getId()){
+				// 新增
+				HouseTenantEntity houseTenant= new HouseTenantEntity();
+
+				houseTenant.setHousingRentalId(houseRental.getId());
+				houseTenant.setName(houseTenantVO.getName());
+				houseTenant.setPhone(houseTenantVO.getPhone());
+				houseTenant.setIdCard(houseTenantVO.getIdCard());
+				houseTenant.setDomicile(houseTenantVO.getDomicile());
+				houseTenant.setWorkUnit(houseTenantVO.getWorkUnit());
+				addList.add(houseTenant);
+			}else {
+				newList.add(houseTenantVO);
+			}
+		}
+		// 遍历去差集,判断是新增还是删除还是更新
+		// 取旧数据和新提交数据差集--删除
+		removeList = oldList.stream().filter(vo -> !newList.stream().map(e ->
+			e.getId()).collect(Collectors.toList()).contains(vo.getId())).collect(Collectors.toList());
+		// 取旧数据和新提交数据交集--更新
+		updateList = newList.stream().filter(vo -> oldList.stream().map(e ->
+			e.getId()).collect(Collectors.toList()).contains(vo.getId())).collect(Collectors.toList());
+
+		// 批量新增
+		if (addList.size()>0) {
+			addFlag = houseTenantService.saveBatch(addList);
+		}
+		// 批量修改
+		if (updateList.size()>0) {
+			updateFlag = houseTenantService.updateBatchById(updateList);
+		}
+		// 批量删除
+		if (removeList.size()>0) {
+			removeFlag = houseTenantService.removeBatchByIds(removeList);
+		}
+		// 返回
+		return update && addFlag && updateFlag && removeFlag;
+	}
+
+	/**
+	 * 获取统计数据
+	 * @return
+	 */
+	@Override
+	public Object getStatistics(HouseRentalTenantVO houseRental) {
+		// 查询
+		List<HouseRentalStatistics> statistics = baseMapper.getStatistics(houseRental);
+		// 返回
+		return statistics;
+	}
 }

--
Gitblit v1.9.3