/* * Copyright (c) 2018-2028, Chill Zhuang All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * Neither the name of the dreamlu.net developer nor the names of its * contributors may be used to endorse or promote products derived from * this software without specific prior written permission. * Author: Chill 庄骞 (smallchill@163.com) */ 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.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.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 org.springframework.transaction.annotation.Transactional; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.stream.Collectors; /** * 出租屋 服务实现类 * * @author BladeX * @since 2023-10-28 */ @Service public class HouseRentalServiceImpl extends ServiceImpl implements IHouseRentalService { @Autowired private IHouseholdService iHouseholdService; @Autowired private IGridService gridService; /** * 自定义分页查询 * @param page * @param houseRental * @return */ @Override public IPage selectHouseRentalPage(IPage page, HouseRentalTenantVO houseRental) { List 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 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); } /** * 查询房屋出租情况 * @param code * @return */ @Override public List getHouseRentalListByCode(String code) { List houseRentalVOS = baseMapper.getHouseRentalListByCode(code); // 返回 return houseRentalVOS; } /** * 自定义房屋出租新增 * @param houseRentalVO * @return */ @Override @Transactional(rollbackFor = Exception.class) public Boolean add(HouseRentalVO houseRentalVO) { // // 判断添加人是否房屋业主 // Long count = iHouseholdService.count(Wrappers.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 phoneList = new ArrayList<>(); List 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 list = bean.list(Wrappers.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.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 wrapper = new QueryWrapper<>(); wrapper.eq("housing_rental_id", houseRental.getId()); List oldList = iHouseholdService.list(wrapper); List list = houseRental.getHouseholdVOList(); // 申明新增,修改,删除集合 List newList = new ArrayList<>(); List addList = new ArrayList<>(); List updateList = new ArrayList<>(); List removeList = new ArrayList<>(); // 找出需要新增的,否则组成新集合进行比对 List 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 list2 = bean.list(Wrappers.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 list = new ArrayList<>(); if (null!=houseRental.getRoleName() && !houseRental.getRoleName().equals("")){ if (houseRental.getRoleName().equals("网格员")){ // 查询对应的房屋地址code list = gridService.getAddressCodeListByUserId(AuthUtil.getUserId()); } } // 查询 List 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 export(HouseRentalTenantVO houseRentalVO) { List houseRentalExcels = baseMapper.export(houseRentalVO); return houseRentalExcels; } @Override public Integer getStatisticsCount(HouseRentalTenantVO houseRental) { return baseMapper.getStatisticsCount(AuthUtil.getUserId()); } }