/* * 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 liquibase.repackaged.org.apache.commons.lang3.StringUtils; import org.apache.logging.log4j.util.Strings; import org.springblade.common.constant.CommonConstant; import org.springblade.common.param.CommonParamSet; import org.springblade.common.utils.SpringUtils; import org.springblade.core.secure.utils.AuthUtil; import org.springblade.core.tool.utils.DigestUtil; 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.springblade.modules.system.vo.UserVO; 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.Arrays; 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) { if (null != houseRental.getAuditStatus()) { if (houseRental.getAuditStatus() == 0) { houseRental.setAuditStatus(2); } } CommonParamSet commonParamSet = new CommonParamSet<>().invoke(HouseRentalTenantVO.class, houseRental); List houseRentalTenantVOS = baseMapper.selectHouseRentalPage(page, houseRental, commonParamSet.getGridCodeList(), commonParamSet.getRegionChildCodesList(), commonParamSet.getIsAdministrator()); 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) { boolean flag = false; houseRentalVO.setCreateUser(AuthUtil.getUserId()); houseRentalVO.setCreateTime(new Date()); houseRentalVO.setUpdateUser(AuthUtil.getUserId()); houseRentalVO.setUpdateTime(new Date()); // 网格员新增默认是审核通过 houseRentalVO.setAuditStatus(1); // 获取请求头中的角色别名 String roleName = SpringUtils.getRequestParam("roleName"); // 居民 if (!Strings.isBlank(roleName) && roleName.equals("inhabitant")) { // 待审核 houseRentalVO.setAuditStatus(0); } //保存自身 flag = save(houseRentalVO); //保存到住户 flag = saveHousehold(houseRentalVO, flag); return flag; } /** * 保存租户信息 * * @param houseRentalVO * @param flag * @return */ public boolean saveHousehold(HouseRentalVO houseRentalVO, boolean flag) { if (flag && houseRentalVO.getHouseholdVOList().size() > 0) { 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); List list = bean.list(Wrappers.lambdaQuery() .in(User::getPhone, phoneList) .eq(User::getIsDeleted, 0)); for (User user : list) { if (StringUtils.isNoneBlank(user.getRoleId())) { boolean contains = user.getRoleId().contains("1717429059648606209"); if (contains) { continue; } user.setRoleId(user.getRoleId() + ",1717429059648606209"); } else { user.setRoleId("1717429059648606209"); } } bean.updateBatchById(list); } catch (Exception e) { log.error("保存用户角色为居民:", e); } flag = iHouseholdService.saveBatch(householdEntityList); } return flag; } /** * 出租屋 自定义删除 * * @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<>(); IUserService userService = SpringUtils.getBean(IUserService.class); for (HouseholdEntity householdEntity : list) { if (null == householdEntity.getId()) { User user = new User(); // 新增或更新用户 boolean save = isSave(userService, householdEntity, user); // 新增住户 householdEntity.setHouseCode(houseRental.getHouseCode()); householdEntity.setHousingRentalId(houseRental.getId()); householdEntity.setRelationship(18); householdEntity.setResidentialStatus(1); // householdEntity.setIdCard(houseRental.getid); householdEntity.setRoleType(2); // householdEntity.setName(); if (save) { householdEntity.setAssociatedUserId(user.getId()); } 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) { for (HouseholdEntity householdEntity : updateList) { phoneList.add(householdEntity.getPhoneNumber()); User user = new User(); // 修改用户,用户存在则更新用户,不存在则新增用户 boolean save = isSave(userService, householdEntity, user); if (save) { householdEntity.setAssociatedUserId(user.getId()); } } updateFlag = iHouseholdService.updateBatchById(updateList); } // 批量删除 if (removeList.size() > 0) { removeFlag = iHouseholdService.removeBatchByIds(removeList); // 删除用户。判断用户是否有其他角色, for (HouseholdEntity householdEntity : removeList) { if (householdEntity.getAssociatedUserId() != null) { UserVO userVO = userService.getuserById(householdEntity.getAssociatedUserId()); if (userVO.getRoleId().contains("1717429059648606209")) { List stringList = Arrays.asList(userVO.getRoleId().split(",")); // 有其他角色 if (stringList.size() >= 2) { List collect = stringList.stream().filter(item -> !item.equals("1717429059648606209")).collect(Collectors.toList()); userVO.setRoleId(StringUtils.join(collect, ",")); userService.updateById(userVO); } else { // 没有其他角色 userService.removeById(userVO.getId()); } } } } } // 返回 return update && addFlag && updateFlag && removeFlag; } /** * 保存或更新用户 * @param userService * @param householdEntity * @param user * @return */ private boolean isSave(IUserService userService, HouseholdEntity householdEntity, User user) { boolean save; // 查询该住户是否有账号 IUserService userService1 = SpringUtils.getBean(IUserService.class); User user1 = userService1.getOne(Wrappers.lambdaQuery() .eq(User::getPhone, householdEntity.getPhoneNumber()) .eq(User::getIsDeleted, 0).last("limit 1")); if (user1 != null) { user = user1; user.setName(householdEntity.getName()); if (StringUtils.isNoneBlank(user.getRoleId())) { boolean contains = user.getRoleId().contains("1717429059648606209"); if (!contains) { user.setRoleId(user.getRoleId() + ",1717429059648606209"); } } else { user.setRoleId("1717429059648606209"); } // 更新用户 save = userService.updateById(user); return save; } // 新增用户 user.setAccount(householdEntity.getPhoneNumber()); user.setUserType(1); user.setPhone(householdEntity.getPhoneNumber()); user.setRealName(householdEntity.getName()); user.setName(householdEntity.getName()); user.setPassword(DigestUtil.encrypt(CommonConstant.DEFAULT_PASSWORD)); user.setRoleId("1717429059648606209"); save = userService.save(user); return save; } /** * 获取统计数据 * * @return */ @Override public Object getStatistics(HouseRentalTenantVO houseRental) { CommonParamSet commonParamSet = new CommonParamSet<>().invoke(HouseRentalTenantVO.class, houseRental); // 查询 List statistics = baseMapper.getStatistics(houseRental, commonParamSet.getGridCodeList(), commonParamSet.getRegionChildCodesList(), commonParamSet.getIsAdministrator()); // 返回 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) { CommonParamSet commonParamSet = new CommonParamSet<>().invoke(HouseRentalTenantVO.class, houseRental); return baseMapper.getStatisticsCount(houseRental, commonParamSet.getGridCodeList(), commonParamSet.getRegionChildCodesList(), commonParamSet.getIsAdministrator()); } }