From efd3674f931b0a2217c67efe88cfc559da67f125 Mon Sep 17 00:00:00 2001
From: zhongrj <646384940@qq.com>
Date: Wed, 01 Nov 2023 20:44:15 +0800
Subject: [PATCH] 租房,租户新增, 修改,删除修改

---
 src/main/java/org/springblade/modules/house/service/impl/HouseRentalServiceImpl.java |  101 +++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 98 insertions(+), 3 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 17a9095..71195c3 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,7 +16,9 @@
  */
 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;
@@ -31,7 +33,10 @@
 import org.springframework.transaction.annotation.Transactional;
 
 import java.util.ArrayList;
+import java.util.Date;
+import java.util.Iterator;
 import java.util.List;
+import java.util.stream.Collectors;
 
 /**
  * 出租屋 服务实现类
@@ -68,14 +73,21 @@
 		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();
 
@@ -85,7 +97,6 @@
 			houseTenant.setIdCard(e.getIdCard());
 			houseTenant.setDomicile(e.getDomicile());
 			houseTenant.setWorkUnit(e.getWorkUnit());
-
 			houseTenantEntities.add(houseTenant);
 
 		});
@@ -93,4 +104,88 @@
 
 		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;
+	}
 }

--
Gitblit v1.9.3