尚庭公寓-----day2 业务功能实现

发布于:2025-07-20 ⋅ 阅读:(11) ⋅ 点赞:(0)

公寓信息管理

需要实现六个接口

- 根据id修改公寓发布状态

controller

    @Operation(summary = "根据id修改公寓发布状态")
    @PostMapping("updateReleaseStatusById")
    public Result updateReleaseStatusById(@RequestParam Long id, @RequestParam ReleaseStatus status) {
        LambdaUpdateWrapper<ApartmentInfo> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(ApartmentInfo::getId,id);
        updateWrapper.set(ApartmentInfo::getIsRelease,status);
        apartmentInfoService.update(updateWrapper);
        return Result.ok();
    }

- 保存或更新公寓信息

controller


import java.util.List;

@Tag(name = "公寓信息管理")
@RestController
@RequestMapping("/admin/apartment")
public class ApartmentController {

    @Autowired
    private ApartmentInfoService apartmentInfoService;
    @Operation(summary = "保存或更新公寓信息")
    @PostMapping("saveOrUpdate")
    public Result saveOrUpdate(@RequestBody ApartmentSubmitVo apartmentSubmitVo) {
        apartmentInfoService.saveOrUpdateApartment(apartmentSubmitVo);
        return Result.ok();
    }

service

package com.nie.lease.web.admin.service;

import com.nie.lease.model.entity.ApartmentInfo;
import com.nie.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.nie.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.nie.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.nie.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;

/**
* @author liubo
* @description 针对表【apartment_info(公寓信息表)】的数据库操作Service
* @createDate 2023-07-24 15:48:00
*/
public interface ApartmentInfoService extends IService<ApartmentInfo> {

    void saveOrUpdateApartment(ApartmentSubmitVo apartmentSubmitVo);
}

service实现类


package com.nie.lease.web.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nie.lease.common.exception.LeaseException;
import com.nie.lease.common.result.ResultCodeEnum;
import com.nie.lease.model.entity.*;
import com.nie.lease.model.enums.ItemType;
import com.nie.lease.web.admin.mapper.*;
import com.nie.lease.web.admin.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nie.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.nie.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.nie.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.nie.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.nie.lease.web.admin.vo.fee.FeeValueVo;
import com.nie.lease.web.admin.vo.graph.GraphVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {

    @Autowired
    private GraphInfoService graphInfoService;

    @Autowired
    private ApartmentFacilityService apartmentFacilityService;

    @Autowired
    private ApartmentLabelService apartmentLabelService;

    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;

    @Autowired
    private GraphInfoMapper graphInfoMapper;

    @Autowired
    private LabelInfoMapper labelInfoMapper;

    @Autowired
    private FacilityInfoMapper facilityInfoMapper;

    @Autowired
    private FeeValueMapper feeValueMapper;

    @Autowired
    private RoomInfoMapper roomInfoMapper;

    @Override
    public void saveOrUpdateApartment(ApartmentSubmitVo apartmentSubmitVo) {
        boolean  IsUpdate=apartmentSubmitVo.getId()!=null;
        super.saveOrUpdate(apartmentSubmitVo);

        if (IsUpdate) {
            //删除图片列表
            LambdaUpdateWrapper<GraphInfo> graphInfoLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            graphInfoLambdaUpdateWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
            graphInfoLambdaUpdateWrapper.eq(GraphInfo::getItemId,apartmentSubmitVo.getId());
            graphInfoService.remove(graphInfoLambdaUpdateWrapper);

            //删除配套列表
            LambdaUpdateWrapper<ApartmentFacility> apartmentFacilityLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            apartmentFacilityLambdaUpdateWrapper.eq(ApartmentFacility::getApartmentId,apartmentSubmitVo.getId());
            apartmentFacilityService.remove(apartmentFacilityLambdaUpdateWrapper);

            //删除标签列表
            LambdaUpdateWrapper<ApartmentLabel> apartmentLabelLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            apartmentLabelLambdaUpdateWrapper.eq(ApartmentLabel::getApartmentId,apartmentSubmitVo.getId());
            apartmentLabelService.remove(apartmentLabelLambdaUpdateWrapper);

            //删除杂费列表
            LambdaUpdateWrapper<ApartmentFeeValue> apartmentFeeValueLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            apartmentFeeValueLambdaUpdateWrapper.eq(ApartmentFeeValue::getApartmentId,apartmentSubmitVo.getId());
            apartmentFeeValueService.remove(apartmentFeeValueLambdaUpdateWrapper);
        }
        //插入图片列表
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        if (!CollectionUtils.isEmpty(graphVoList)){
            ArrayList<GraphInfo> graphInfoList = new ArrayList<>();
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setItemType(ItemType.APARTMENT);
                graphInfo.setItemId(apartmentSubmitVo.getId());
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfoList.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfoList);
        }
        //插入配套列表
        List<Long> facilityInfoIdList = apartmentSubmitVo.getFacilityInfoIds();
        if (!CollectionUtils.isEmpty(facilityInfoIdList)){
            ArrayList<ApartmentFacility> facilityList = new ArrayList<>();
            for (Long facilityId : facilityInfoIdList) {
                ApartmentFacility apartmentFacility = new ApartmentFacility();
                apartmentFacility.setApartmentId(apartmentSubmitVo.getId());
                apartmentFacility.setFacilityId(facilityId);
                facilityList.add(apartmentFacility);
            }
            apartmentFacilityService.saveBatch(facilityList);
        }
        //插入标签列表
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        if (!CollectionUtils.isEmpty(labelIds)) {
            List<ApartmentLabel> apartmentLabelList = new ArrayList<>();
            for (Long labelId : labelIds) {
                ApartmentLabel apartmentLabel = new ApartmentLabel();
                apartmentLabel.setApartmentId(apartmentSubmitVo.getId());
                apartmentLabel.setLabelId(labelId);
                apartmentLabelList.add(apartmentLabel);
            }
            apartmentLabelService.saveBatch(apartmentLabelList);
        }

        //插入杂费列表
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        if (!CollectionUtils.isEmpty(feeValueIds)) {
            ArrayList<ApartmentFeeValue> apartmentFeeValueList = new ArrayList<>();
            for (Long feeValueId : feeValueIds) {
                ApartmentFeeValue apartmentFeeValue = new ApartmentFeeValue();
                apartmentFeeValue.setApartmentId(apartmentSubmitVo.getId());
                apartmentFeeValue.setFeeValueId(feeValueId);
                apartmentFeeValueList.add(apartmentFeeValue);
            }
            apartmentFeeValueService.saveBatch(apartmentFeeValueList);
        }

    }

- 根据条件分页查询公寓列表

controller

    @Operation(summary = "根据条件分页查询公寓列表")
    @GetMapping("pageItem")
    public Result<IPage<ApartmentItemVo>> pageItem(@RequestParam long current, @RequestParam long size, ApartmentQueryVo queryVo) {
        Page<ApartmentItemVo> page = new Page<>(current, size);
        IPage<ApartmentItemVo> result=apartmentInfoService.pageItem(page, queryVo);
        return Result.ok(result);
    }

service接口

    IPage<ApartmentItemVo> pageItem(Page<ApartmentItemVo> page, ApartmentQueryVo queryVo);

service实现类

    @Override
    public IPage<ApartmentItemVo> pageItem(Page<ApartmentItemVo> page, ApartmentQueryVo queryVo) {
        return apartmentInfoMapper.pageItem(page, queryVo);
    }

mapper接口

package com.nie.lease.web.admin.mapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nie.lease.model.entity.ApartmentInfo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.nie.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.nie.lease.web.admin.vo.apartment.ApartmentQueryVo;

/**
* @author liubo
* @description 针对表【apartment_info(公寓信息表)】的数据库操作Mapper
* @createDate 2023-07-24 15:48:00
* @Entity com.nie.lease.model.ApartmentInfo
*/
public interface ApartmentInfoMapper extends BaseMapper<ApartmentInfo> {

    IPage<ApartmentItemVo> pageItem(Page<ApartmentItemVo> page, ApartmentQueryVo queryVo);
}

mapper实现

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.nie.lease.web.admin.mapper.ApartmentInfoMapper">

    <select id="pageItem" resultType="com.nie.lease.web.admin.vo.apartment.ApartmentItemVo">
        select ai.id,
               ai.name,
               ai.introduction,
               ai.district_id,
               ai.district_name,
               ai.city_id,
               ai.city_name,
               ai.province_id,
               ai.province_name,
               ai.address_detail,
               ai.latitude,
               ai.longitude,
               ai.phone,
               ai.is_release,
               ifnull(tc.cnt,0) total_room_count,
        ifnull(tc.cnt,0) - ifnull(cc.cnt,0) free_room_count
        from (select id,
                     name,
                     introduction,
                     district_id,
                     district_name,
                     city_id,
                     city_name,
                     province_id,
                     province_name,
                     address_detail,
                     latitude,
                     longitude,
                     phone,
                     is_release
              from apartment_info
        <where>
            is_deleted=0
            <if test="queryVo.provinceId !=null">
                and province_id=#{queryVo.provinceId}
            </if>
            <if test="queryVo.cityId !=null">
                and city_id=#{queryVo.cityId}
            </if>
            <if test="queryVo.districtId !=null">
                and district_id=#{queryVo.districtId}
            </if>
        </where>
              ) ai
                 left join
             (select apartment_id,
                     COUNT(*) cnt
              from room_info
              where is_deleted = 0
                and is_release = 1
              group by apartment_id) tc
             on ai.id = tc.apartment_id

                 left join
             (select apartment_id,
                     COUNT(*) cnt
              from lease_agreement
              where is_deleted = 0
                and status in (2, 5)
              group by apartment_id) cc
             on ai.id = cc.apartment_id

    </select>
</mapper>

- 根据区县id查询公寓信息列表

controller

    @Operation(summary = "根据区县id查询公寓信息列表")
    @GetMapping("listInfoByDistrictId")
    public Result<List<ApartmentInfo>> listInfoByDistrictId(@RequestParam Long id) {
        LambdaQueryWrapper<ApartmentInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApartmentInfo::getDistrictId,id);
        List<ApartmentInfo> list = apartmentInfoService.list(queryWrapper);
        return Result.ok(list);
    }

service接口

  package com.nie.lease.web.admin.service;

import com.nie.lease.model.entity.ApartmentInfo;
import com.nie.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.nie.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.nie.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.nie.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;

/**
* @author liubo
* @description 针对表【apartment_info(公寓信息表)】的数据库操作Service
* @createDate 2023-07-24 15:48:00
*/
public interface ApartmentInfoService extends IService<ApartmentInfo> {

    void saveOrUpdateApartment(ApartmentSubmitVo apartmentSubmitVo);

    IPage<ApartmentItemVo> pageItem(Page<ApartmentItemVo> page, ApartmentQueryVo queryVo);

    ApartmentDetailVo getDetailById(Long id);

    void removeApartmentById(Long id);
}

- 根据ID获取公寓详细信息

controller

    @Operation(summary = "根据ID获取公寓详细信息")
    @GetMapping("getDetailById")
    public Result<ApartmentDetailVo> getDetailById(@RequestParam Long id) {
        ApartmentDetailVo result=apartmentInfoService.getDetailById(id);
        return Result.ok(result);
    }

service接口

package com.nie.lease.web.admin.service;

import com.nie.lease.model.entity.ApartmentInfo;
import com.nie.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.nie.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.nie.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.nie.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;

/**
* @author liubo
* @description 针对表【apartment_info(公寓信息表)】的数据库操作Service
* @createDate 2023-07-24 15:48:00
*/
public interface ApartmentInfoService extends IService<ApartmentInfo> {

    void saveOrUpdateApartment(ApartmentSubmitVo apartmentSubmitVo);

    IPage<ApartmentItemVo> pageItem(Page<ApartmentItemVo> page, ApartmentQueryVo queryVo);

    ApartmentDetailVo getDetailById(Long id);

    void removeApartmentById(Long id);
}

service实现类

 public ApartmentDetailVo getDetailById(Long id) {
        //查询公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(id);
        //查询图片列表
        List<GraphVo> graphVoList=graphInfoMapper.selectListByItemTypeAndItemId(ItemType.APARTMENT,id);
        //查询标签列表
        List<LabelInfo> labelInfoList=labelInfoMapper.selectListByApartmentId(id);
        //查询配套列表
        List<FacilityInfo> facilityInfoList=facilityInfoMapper.selectListByApartmentId(id);
        //查询杂费列表
        List<FeeValueVo> feeValueVoList=feeValueMapper.selectListByApartmentId(id);

        //组装VO
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        BeanUtils.copyProperties(apartmentInfo, apartmentDetailVo);
        apartmentDetailVo.setGraphVoList(graphVoList);
        apartmentDetailVo.setLabelInfoList(labelInfoList);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        apartmentDetailVo.setFeeValueVoList(feeValueVoList);

        return apartmentDetailVo;
    }

mapper接口

package com.nie.lease.web.admin.mapper;

import com.nie.lease.model.entity.GraphInfo;
import com.nie.lease.model.enums.ItemType;
import com.nie.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import java.util.List;

/**
* @author liubo
* @description 针对表【graph_info(图片信息表)】的数据库操作Mapper
* @createDate 2023-07-24 15:48:00
* @Entity com.atguigu.lease.model.GraphInfo
*/
public interface GraphInfoMapper extends BaseMapper<GraphInfo> {

    List<GraphVo> selectListByItemTypeAndItemId(ItemType itemType, Long id);
}

package com.nie.lease.web.admin.mapper;

import com.nie.lease.model.entity.LabelInfo;
import com.nie.lease.model.enums.ItemType;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import java.util.List;

/**
* @author liubo
* @description 针对表【label_info(标签信息表)】的数据库操作Mapper
* @createDate 2023-07-24 15:48:00
* @Entity com.atguigu.lease.model.LabelInfo
*/
public interface LabelInfoMapper extends BaseMapper<LabelInfo> {

    List<LabelInfo> selectListByApartmentId(Long id);
}


package com.nie.lease.web.admin.mapper;

import com.nie.lease.model.entity.LabelInfo;
import com.nie.lease.model.enums.ItemType;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import java.util.List;

/**
* @author liubo
* @description 针对表【label_info(标签信息表)】的数据库操作Mapper
* @createDate 2023-07-24 15:48:00
* @Entity com.atguigu.lease.model.LabelInfo
*/
public interface LabelInfoMapper extends BaseMapper<LabelInfo> {

    List<LabelInfo> selectListByApartmentId(Long id);
}





package com.nie.lease.web.admin.mapper;

import com.nie.lease.model.entity.FacilityInfo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import java.util.List;

/**
* @author liubo
* @description 针对表【facility_info(配套信息表)】的数据库操作Mapper
* @createDate 2023-07-24 15:48:00
* @Entity com.atguigu.lease.model.FacilityInfo
*/
public interface FacilityInfoMapper extends BaseMapper<FacilityInfo> {

    List<FacilityInfo> selectListByApartmentId(Long id);
}

package com.nie.lease.web.admin.mapper;

import com.nie.lease.model.entity.FeeValue;
import com.nie.lease.web.admin.vo.fee.FeeValueVo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import java.util.List;

/**
* @author liubo
* @description 针对表【fee_value(杂项费用值表)】的数据库操作Mapper
* @createDate 2023-07-24 15:48:00
* @Entity com.atguigu.lease.model.FeeValue
*/
public interface FeeValueMapper extends BaseMapper<FeeValue> {

    List<FeeValueVo> selectListByApartmentId(Long id);
}





mapper实现类

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.nie.lease.web.admin.mapper.LabelInfoMapper">

    <select id="selectListByApartmentId" resultType="com.nie.lease.model.entity.LabelInfo">
        select id,
               type,
               name
        from label_info
        where is_deleted = 0
          and id in (select label_id
                     from apartment_label
                     where is_deleted = 0
                       and apartment_id = #{id})
    </select>
</mapper>

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.nie.lease.web.admin.mapper.GraphInfoMapper">

    <select id="selectListByItemTypeAndItemId" resultType="com.nie.lease.web.admin.vo.graph.GraphVo">
        select
            name,
            url
            from graph_info
        where  is_deleted=0
        and item_type=#{itemType}
        and item_id=#{id}
    </select>
</mapper>

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.nie.lease.web.admin.mapper.LabelInfoMapper">

    <select id="selectListByApartmentId" resultType="com.nie.lease.model.entity.LabelInfo">
        select id,
               type,
               name
        from label_info
        where is_deleted = 0
          and id in (select label_id
                     from apartment_label
                     where is_deleted = 0
                       and apartment_id = #{id})
    </select>
</mapper>

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.nie.lease.web.admin.mapper.FacilityInfoMapper">

    <select id="selectListByApartmentId" resultType="com.nie.lease.model.entity.FacilityInfo">
        select id,
               type,
               name,
               icon
        from facility_info
        where is_deleted = 0
          and id in (select facility_id
                     from apartment_facility
                     where is_deleted = 0
                       and apartment_id = #{id})
    </select>
</mapper>

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.nie.lease.web.admin.mapper.FeeValueMapper">

    <select id="selectListByApartmentId" resultType="com.nie.lease.web.admin.vo.fee.FeeValueVo">
        select fv.id,
               fv.name,
               fv.unit,
               fv.fee_key_id,
               fk.id,
               fk.name fee_key_name
        from fee_value fv
                 join fee_key fk on fv.fee_key_id = fk.id
        where fk.is_deleted = 0
          and fv.is_deleted = 0
          and fv.id in (select fee_value_id
                        from apartment_fee_value
                        where is_deleted = 0
                          and apartment_id = #{id})
    </select>
</mapper>

- 根据id删除公寓信息

controller

    @Operation(summary = "根据id删除公寓信息")
    @DeleteMapping("removeById")
    public Result removeById(@RequestParam Long id) {
        apartmentInfoService.removeApartmentById(id);
        return Result.ok();
    }

service接口

package com.nie.lease.web.admin.service;

import com.nie.lease.model.entity.ApartmentInfo;
import com.nie.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.nie.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.nie.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.nie.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;

/**
* @author liubo
* @description 针对表【apartment_info(公寓信息表)】的数据库操作Service
* @createDate 2023-07-24 15:48:00
*/
public interface ApartmentInfoService extends IService<ApartmentInfo> {

    void removeApartmentById(Long id);
}

service实现类

 public void removeApartmentById(Long id) {
        LambdaQueryWrapper<RoomInfo> roomQueryWrapper = new LambdaQueryWrapper<>();
        roomQueryWrapper.eq(RoomInfo::getApartmentId,id);
        Long count = roomInfoMapper.selectCount(roomQueryWrapper);
        if (count>0){
            //有房间,不能删除
            throw new LeaseException(ResultCodeEnum.ADMIN_APARTMENT_DELETE_ERROR);

        }
        super.removeById(id);
        //删除图片列表
        LambdaUpdateWrapper<GraphInfo> graphInfoLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        graphInfoLambdaUpdateWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
        graphInfoLambdaUpdateWrapper.eq(GraphInfo::getItemId,id);
        graphInfoService.remove(graphInfoLambdaUpdateWrapper);

        //删除配套列表
        LambdaUpdateWrapper<ApartmentFacility> apartmentFacilityLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        apartmentFacilityLambdaUpdateWrapper.eq(ApartmentFacility::getApartmentId,id);
        apartmentFacilityService.remove(apartmentFacilityLambdaUpdateWrapper);

        //删除标签列表
        LambdaUpdateWrapper<ApartmentLabel> apartmentLabelLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        apartmentLabelLambdaUpdateWrapper.eq(ApartmentLabel::getApartmentId,id);
        apartmentLabelService.remove(apartmentLabelLambdaUpdateWrapper);

        //删除杂费列表
        LambdaUpdateWrapper<ApartmentFeeValue> apartmentFeeValueLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        apartmentFeeValueLambdaUpdateWrapper.eq(ApartmentFeeValue::getApartmentId,id);
        apartmentFeeValueService.remove(apartmentFeeValueLambdaUpdateWrapper);
    }

定义异常处理类

package com.nie.lease.common.exception;

import com.nie.lease.common.result.ResultCodeEnum;
import lombok.Data;
import org.springframework.context.annotation.Configuration;

@Data
public class LeaseException extends RuntimeException{

    private Integer code;

    public LeaseException(Integer code,String message){
        super(message);
        this.code=code;
    }

    public LeaseException(ResultCodeEnum resultCodeEnum){
        super(resultCodeEnum.getMessage());
        this.code=resultCodeEnum.getCode();
    }
}

package com.nie.lease.common.exception;


import com.nie.lease.common.result.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result handle(Exception e){
        e.printStackTrace();
        return Result.fail();
    }


    @ExceptionHandler(LeaseException.class)
    @ResponseBody
    public Result handle(LeaseException e){
        e.printStackTrace();
        return Result.fail(e.getCode(),e.getMessage());
    }

}


网站公告

今日签到

点亮在社区的每一天
去签到