linwe
2023-12-23 55d677758efadb6d42e6d4e595cecc2c50c20d5a
src/main/java/org/springblade/modules/house/service/impl/HouseRentalServiceImpl.java
@@ -16,19 +16,33 @@
 */
package org.springblade.modules.house.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springblade.common.utils.SpringUtils;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.modules.grid.service.IGridService;
import org.springblade.modules.house.entity.HouseRentalEntity;
import org.springblade.modules.house.service.IHouseTenantService;
import org.springblade.modules.house.vo.HouseRentalVO;
import org.springblade.modules.house.entity.HouseholdEntity;
import org.springblade.modules.house.excel.HouseRentalExcel;
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.springblade.modules.house.service.IHouseholdService;
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.vo.HouseholdVO;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.service.IUserService;
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.stream.Collectors;
/**
 * 出租屋 服务实现类
@@ -40,7 +54,11 @@
public class HouseRentalServiceImpl extends ServiceImpl<HouseRentalMapper, HouseRentalEntity> implements IHouseRentalService {
   @Autowired
   private IHouseTenantService houseTenantService;
   private IHouseholdService iHouseholdService;
   @Autowired
   private IGridService gridService;
   /**
    * 自定义分页查询
@@ -49,8 +67,38 @@
    * @return
    */
   @Override
   public IPage<HouseRentalVO> selectHouseRentalPage(IPage<HouseRentalVO> page, HouseRentalVO houseRental) {
      return page.setRecords(baseMapper.selectHouseRentalPage(page, houseRental));
   public IPage<HouseRentalTenantVO> selectHouseRentalPage(IPage<HouseRentalTenantVO> page, HouseRentalTenantVO houseRental) {
      List<String> list = new ArrayList<>();
      if (null!=houseRental.getRoleName() && !houseRental.getRoleName().equals("")){
         if (houseRental.getRoleName().equals("网格员")){
            // 查询对应的房屋地址code
            list = gridService.getAddressCodeListByUserId(AuthUtil.getUserId());
         }
      }
      if (null!=houseRental.getAuditStatus()){
         if (houseRental.getAuditStatus()==0){
            houseRental.setAuditStatus(2);
         }
      }
      List<HouseRentalTenantVO> houseRentalTenantVOS = baseMapper.selectHouseRentalPage(page, houseRental, list);
      for (HouseRentalTenantVO houseRentalTenantVO : houseRentalTenantVOS) {
         if(houseRentalTenantVO.getStatus().equals(1)){
            houseRentalTenantVO.setStatus(30);
         }
         if(houseRentalTenantVO.getStatus().equals(0) && houseRentalTenantVO.getAuditStatus().equals(0)){
            houseRentalTenantVO.setStatus(0);
         }
         if(houseRentalTenantVO.getStatus().equals(0) && houseRentalTenantVO.getAuditStatus().equals(1)){
            houseRentalTenantVO.setStatus(1);
         }
         if(houseRentalTenantVO.getStatus().equals(0) && houseRentalTenantVO.getDldType().equals(3)){
            houseRentalTenantVO.setStatus(20);
         }
         if(houseRentalTenantVO.getStatus().equals(0) && houseRentalTenantVO.getDldType().equals(2)){
            houseRentalTenantVO.setStatus(10);
         }
      }
      return page.setRecords(houseRentalTenantVOS);
   }
   /**
@@ -64,4 +112,214 @@
      // 返回
      return houseRentalVOS;
   }
   /**
    * 自定义房屋出租新增
    * @param houseRentalVO
    * @return
    */
   @Override
   @Transactional(rollbackFor = Exception.class)
   public Boolean add(HouseRentalVO houseRentalVO) {
//      // 判断添加人是否房屋业主
//      Long count = iHouseholdService.count(Wrappers.<HouseholdEntity>lambdaQuery()
//         .eq(HouseholdEntity::getHouseCode, houseRentalVO.getHouseCode())
//         .eq(HouseholdEntity::getAssociatedUserId, AuthUtil.getUserId())
//         .eq(HouseholdEntity::getRelationship, 1));
//      if (count == 0) {
//         return false;
//      }
      houseRentalVO.setCreateUser(AuthUtil.getUserId());
      houseRentalVO.setCreateTime(new Date());
      houseRentalVO.setUpdateUser(AuthUtil.getUserId());
      houseRentalVO.setUpdateTime(new Date());
      // 网格员新增默认是审核通过
      houseRentalVO.setAuditStatus(1);
      //保存自身
      boolean save = save(houseRentalVO);
      List<String> phoneList = new ArrayList<>();
      List<HouseholdEntity> householdEntityList = new ArrayList<>();
      houseRentalVO.getHouseholdVOList().forEach(e -> {
         e.setHouseCode(houseRentalVO.getHouseCode());
         e.setHousingRentalId(houseRentalVO.getId());
         e.setRelationship(18);
         e.setResidentialStatus(1);
         e.setRoleType(2);
         householdEntityList.add(e);
         phoneList.add(e.getPhoneNumber());
      });
      try {
         // 查询租户是否网格员身份的,给网格员设置居民角色
         IUserService bean = SpringUtils.getBean(IUserService.class);
         String str = "1717429261910528001";
         List<User> list = bean.list(Wrappers.<User>lambdaQuery()
            .in(User::getPhone, phoneList)
            .like(User::getRoleId, str));
         for (User user : list) {
            boolean contains = user.getRoleId().contains("1717429059648606209");
            if (contains) {
               continue;
            }
            user.setRoleId(user.getRoleId() + ",1717429059648606209");
         }
         bean.updateBatchById(list);
      } catch (Exception e) {
         log.error("保存用户角色为居民:", e);
      }
      boolean saveBatch = iHouseholdService.saveBatch(householdEntityList);
      return save && saveBatch;
   }
   /**
    * 出租屋 自定义删除
    * @param id
    * @return
    */
   @Override
   public Boolean removeHouseRental(Long id) {
      // 先删除出租屋信息
      boolean b = removeById(id);
      // 再删除租户信息
      boolean update = iHouseholdService.update(Wrappers.<HouseholdEntity>lambdaUpdate()
         .set(HouseholdEntity::getIsDeleted, 1)
         .eq(HouseholdEntity::getHousingRentalId, id));
      // 返回
      return b;
   }
   /**
    * 出租屋 自定义修改
    * @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<HouseholdEntity> wrapper = new QueryWrapper<>();
      wrapper.eq("housing_rental_id", houseRental.getId());
      List<HouseholdEntity> oldList = iHouseholdService.list(wrapper);
      List<HouseholdVO> list = houseRental.getHouseholdVOList();
      // 申明新增,修改,删除集合
      List<HouseholdEntity> newList = new ArrayList<>();
      List<HouseholdEntity> addList = new ArrayList<>();
      List<HouseholdEntity> updateList = new ArrayList<>();
      List<HouseholdEntity> removeList = new ArrayList<>();
      // 找出需要新增的,否则组成新集合进行比对
      List<String> phoneList = new ArrayList<>();
      for (HouseholdEntity householdEntity : list) {
         if (null == householdEntity.getId()) {
            // 新增
            householdEntity.setHouseCode(houseRental.getHouseCode());
            householdEntity.setHousingRentalId(houseRental.getId());
            householdEntity.setRelationship(18);
            householdEntity.setResidentialStatus(1);
            householdEntity.setRoleType(2);
            addList.add(householdEntity);
            phoneList.add(householdEntity.getPhoneNumber());
         } else {
            newList.add(householdEntity);
         }
      }
      // 遍历去差集,判断是新增还是删除还是更新
      // 取旧数据和新提交数据差集--删除
      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 = iHouseholdService.saveBatch(addList);
      }
      // 批量修改
      if (updateList.size() > 0) {
         updateFlag = iHouseholdService.updateBatchById(updateList);
         for (HouseholdEntity householdEntity : updateList) {
            phoneList.add(householdEntity.getPhoneNumber());
         }
      }
      // 批量删除
      if (removeList.size() > 0) {
         removeFlag = iHouseholdService.removeBatchByIds(removeList);
      }
      try {
         // 查询租户是否网格员身份的,给网格员设置居民角色
         IUserService bean = SpringUtils.getBean(IUserService.class);
         String str = "1717429261910528001";
         List<User> list2 = bean.list(Wrappers.<User>lambdaQuery()
            .in(User::getPhone, phoneList)
            .like(User::getRoleId, str));
         for (User user : list2) {
            boolean contains = user.getRoleId().contains("1717429059648606209");
            if (contains) {
               continue;
            }
            user.setRoleId(user.getRoleId() + ",1717429059648606209");
         }
         bean.updateBatchById(list2);
      } catch (Exception e) {
         log.error("保存用户角色为居民:", e);
      }
      // 返回
      return update && addFlag && updateFlag && removeFlag;
   }
   /**
    * 获取统计数据
    * @return
    */
   @Override
   public Object getStatistics(HouseRentalTenantVO houseRental) {
      List<String> list = new ArrayList<>();
      if (null!=houseRental.getRoleName() && !houseRental.getRoleName().equals("")){
         if (houseRental.getRoleName().equals("网格员")){
            // 查询对应的房屋地址code
            list = gridService.getAddressCodeListByUserId(AuthUtil.getUserId());
         }
      }
      // 查询
      List<HouseRentalStatistics> statistics = baseMapper.getStatistics(houseRental,list);
      // 返回
      return statistics;
   }
   /**
    * 出租屋 确认
    * @param houseRental
    * @return
    */
   @Override
   public Boolean confirmHouseRental(HouseRentalVO houseRental) {
      // 修改状态
      houseRental.setUpdateTime(new Date());
      // 修改
      return updateById(houseRental);
   }
   /**
    * 导出租赁信息
    * @param houseRentalVO
    * @return
    */
   @Override
   public List<HouseRentalExcel> export(HouseRentalTenantVO houseRentalVO) {
      List<HouseRentalExcel> houseRentalExcels = baseMapper.export(houseRentalVO);
      return houseRentalExcels;
   }
   @Override
   public Integer getStatisticsCount(HouseRentalTenantVO houseRental) {
      return baseMapper.getStatisticsCount(AuthUtil.getUserId());
   }
}