【尚庭公寓SpringBoot + Vue 项目实战】房间管理(十二)
文章目录
- 【尚庭公寓SpringBoot + Vue 项目实战】房间管理(十二)
- 1、业务介绍
- 2、逻辑模型介绍
- 3、接口开发
- 3.1、保存或更新房间信息
- 3.2、根据条件分页查询详细信息
- 3.3、根据id获取房间详细信息
- 3.4、根据ID删除房间信息
- 3.5、根据id修改房间发布状态
- 3.6、 根据公寓ID查询房间列表
1、业务介绍
房间管理共有六个接口,分别是
- 保存或更新房间信息
- 根据条件分页查询详细信息
- 根据ID获取房间详情信息
- 根据ID删除房间信息
- 根据ID修改房间发布状态
- 根据公寓ID查询房间信息列表
2、逻辑模型介绍
3、接口开发
3.1、保存或更新房间信息
查看接口
代码开发
-
查看请求的数据结构
查看web-admin模块中的
com.atguigu.lease.web.admin.vo.room.RoomSubmitVo
,内容如下@Data @Schema(description = "房间信息") public class RoomSubmitVo extends RoomInfo { @Schema(description = "图片列表") private List<GraphVo> graphVoList; @Schema(description = "属性信息列表") private List<Long> attrValueIds; @Schema(description = "配套信息列表") private List<Long> facilityInfoIds; @Schema(description = "标签信息列表") private List<Long> labelInfoIds; @Schema(description = "支付方式列表") private List<Long> paymentTypeIds; @Schema(description = "可选租期列表") private List<Long> leaseTermIds; }
-
编写Controller层逻辑
在
RoomController
中增加如下内容@Operation(summary = "保存或更新房间信息") @PostMapping("saveOrUpdate") public Result saveOrUpdate(@RequestBody RoomSubmitVo roomSubmitVo) { service.saveOrUpdateRoom(roomSubmitVo); return Result.ok(); }
-
编写Service 层逻辑
在
RoomInfoService
中增加如下内容void saveOrUpdateRoom(RoomSubmitVo roomSubmitVo);
在
RoomInfoServiceImpl
中增加如下内容@Override public void saveOrUpdateRoom(RoomSubmitVo roomSubmitVo) { boolean isUpdate = roomSubmitVo.getId() != null; super.saveOrUpdate(roomSubmitVo); //若为更新操作,则先删除与Room相关的各项信息列表 if (isUpdate) { //1.删除原有graphInfoList LambdaQueryWrapper<GraphInfo> graphQueryWrapper = new LambdaQueryWrapper<>(); graphQueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM); graphQueryWrapper.eq(GraphInfo::getItemId, roomSubmitVo.getId()); graphInfoService.remove(graphQueryWrapper); //2.删除原有roomAttrValueList LambdaQueryWrapper<RoomAttrValue> attrQueryMapper = new LambdaQueryWrapper<>(); attrQueryMapper.eq(RoomAttrValue::getRoomId, roomSubmitVo.getId()); roomAttrValueService.remove(attrQueryMapper); //3.删除原有roomFacilityList LambdaQueryWrapper<RoomFacility> facilityQueryWrapper = new LambdaQueryWrapper<>(); facilityQueryWrapper.eq(RoomFacility::getRoomId, roomSubmitVo.getId()); roomFacilityService.remove(facilityQueryWrapper); //4.删除原有roomLabelList LambdaQueryWrapper<RoomLabel> labelQueryWrapper = new LambdaQueryWrapper<>(); labelQueryWrapper.eq(RoomLabel::getRoomId, roomSubmitVo.getId()); roomLabelService.remove(labelQueryWrapper); //5.删除原有paymentTypeList LambdaQueryWrapper<RoomPaymentType> paymentQueryWrapper = new LambdaQueryWrapper<>(); paymentQueryWrapper.eq(RoomPaymentType::getRoomId, roomSubmitVo.getId()); roomPaymentTypeService.remove(paymentQueryWrapper); //6.删除原有leaseTermList LambdaQueryWrapper<RoomLeaseTerm> termQueryWrapper = new LambdaQueryWrapper<>(); termQueryWrapper.eq(RoomLeaseTerm::getRoomId, roomSubmitVo.getId()); roomLeaseTermService.remove(termQueryWrapper); } //1.保存新的graphInfoList List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList(); if (!CollectionUtils.isEmpty(graphVoList)) { ArrayList<GraphInfo> graphInfoList = new ArrayList<>(); for (GraphVo graphVo : graphVoList) { GraphInfo graphInfo = new GraphInfo(); graphInfo.setItemType(ItemType.ROOM); graphInfo.setItemId(roomSubmitVo.getId()); graphInfo.setName(graphVo.getName()); graphInfo.setUrl(graphVo.getUrl()); graphInfoList.add(graphInfo); } graphInfoService.saveBatch(graphInfoList); } //2.保存新的roomAttrValueList List<Long> attrValueIds = roomSubmitVo.getAttrValueIds(); if (!CollectionUtils.isEmpty(attrValueIds)) { List<RoomAttrValue> roomAttrValueList = new ArrayList<>(); for (Long attrValueId : attrValueIds) { RoomAttrValue roomAttrValue = RoomAttrValue.builder().roomId(roomSubmitVo.getId()).attrValueId(attrValueId).build(); roomAttrValueList.add(roomAttrValue); } roomAttrValueService.saveBatch(roomAttrValueList); } //3.保存新的facilityInfoList List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds(); if (!CollectionUtils.isEmpty(facilityInfoIds)) { List<RoomFacility> roomFacilityList = new ArrayList<>(); for (Long facilityInfoId : facilityInfoIds) { RoomFacility roomFacility = RoomFacility.builder().roomId(roomSubmitVo.getId()).facilityId(facilityInfoId).build(); roomFacilityList.add(roomFacility); } roomFacilityService.saveBatch(roomFacilityList); } //4.保存新的labelInfoList List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds(); if (!CollectionUtils.isEmpty(labelInfoIds)) { ArrayList<RoomLabel> roomLabelList = new ArrayList<>(); for (Long labelInfoId : labelInfoIds) { RoomLabel roomLabel = RoomLabel.builder().roomId(roomSubmitVo.getId()).labelId(labelInfoId).build(); roomLabelList.add(roomLabel); } roomLabelService.saveBatch(roomLabelList); } //5.保存新的paymentTypeList List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds(); if (!CollectionUtils.isEmpty(paymentTypeIds)) { ArrayList<RoomPaymentType> roomPaymentTypeList = new ArrayList<>(); for (Long paymentTypeId : paymentTypeIds) { RoomPaymentType roomPaymentType = RoomPaymentType.builder().roomId(roomSubmitVo.getId()).paymentTypeId(paymentTypeId).build(); roomPaymentTypeList.add(roomPaymentType); } roomPaymentTypeService.saveBatch(roomPaymentTypeList); } //6.保存新的leaseTermList List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds(); if (!CollectionUtils.isEmpty(leaseTermIds)) { ArrayList<RoomLeaseTerm> roomLeaseTerms = new ArrayList<>(); for (Long leaseTermId : leaseTermIds) { RoomLeaseTerm roomLeaseTerm = RoomLeaseTerm.builder().roomId(roomSubmitVo.getId()).leaseTermId(leaseTermId).build(); roomLeaseTerms.add(roomLeaseTerm); } roomLeaseTermService.saveBatch(roomLeaseTerms); } }
3.2、根据条件分页查询详细信息
查看接口
代码开发
-
查看请求和响应的数据结构
-
请求数据结构
-
current
和size
为分页相关参数,分别表示当前所处页面和每个页面的记录数。 -
RoomQueryVo
为房间的查询条件,详细结构如下:@Schema(description = "房间查询实体") @Data public class RoomQueryVo { @Schema(description = "省份Id") private Long provinceId; @Schema(description = "城市Id") private Long cityId; @Schema(description = "区域Id") private Long districtId; @Schema(description = "公寓Id") private Long apartmentId; }
-
-
响应数据结构
单个房间信息记录可查看
com.atguigu.lease.web.admin.vo.room.RoomItemVo
,内容如下:@Data @Schema(description = "房间信息") public class RoomItemVo extends RoomInfo { @Schema(description = "租约结束日期") private Date leaseEndDate; @Schema(description = "当前入住状态") private Boolean isCheckIn; @Schema(description = "所属公寓信息") private ApartmentInfo apartmentInfo; }
-
-
编写Controller层逻辑
在
RoomController
中增加如下内容@Operation(summary = "根据条件分页查询房间列表") @GetMapping("pageItem") public Result<IPage<RoomItemVo>> pageItem(@RequestParam long current, @RequestParam long size, RoomQueryVo queryVo) { IPage<RoomItemVo> page = new Page<>(current, size); IPage<RoomItemVo> result = service.pageRoomItemByQuery(page, queryVo); return Result.ok(result); }
-
编写Service 层逻辑
-
在
RoomInfoService
中增加如下内容IPage<RoomItemVo> pageRoomItemByQuery(IPage<RoomItemVo> page, RoomQueryVo queryVo);
-
在
RoomInfoServiceImpl
中增加如下内容@Override public IPage<RoomItemVo> pageRoomItemByQuery(IPage<RoomItemVo> page, RoomQueryVo queryVo) { return roomInfoMapper.pageRoomItemByQuery(page, queryVo); }
-
-
编写Mapper层逻辑
-
在
RoomInfoMapper
中增加如下内容IPage<RoomItemVo> pageRoomItemByQuery(IPage<RoomItemVo> page, RoomQueryVo queryVo);
-
在
RoomInfoMapper.xml
中增加如下内容<resultMap id="RoomItemVoMap" type="com.atguigu.lease.web.admin.vo.room.RoomItemVo" autoMapping="true"> <id property="id" column="id"/> <association property="apartmentInfo" javaType="com.atguigu.lease.model.entity.ApartmentInfo" autoMapping="true"> <id property="id" column="apart_id"/> <result property="isRelease" column="apart_is_release"/> </association> </resultMap> <select id="pageRoomItemByQuery" resultMap="RoomItemVoMap"> select ri.id, ri.room_number, ri.rent, ri.apartment_id, ri.is_release, la.room_id is not null is_check_in, la.lease_end_date, ai.id apart_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 apart_is_release from room_info ri left join lease_agreement la on ri.id = la.room_id and la.is_deleted = 0 and la.status in (2,5) left join apartment_info ai on ri.apartment_id = ai.id and ai.is_deleted = 0 <where> ri.is_deleted = 0 <if test="queryVo.provinceId != null"> apart.province_id = #{queryVo.provinceId} </if> <if test="queryVo.cityId != null"> and apart.city_id = #{queryVo.cityId} </if> <if test="queryVo.districtId != null"> and apart.district_id = #{queryVo.districtId} </if> <if test="queryVo.apartmentId != null"> and apartment_id = #{queryVo.apartmentId} </if> </where> </select>
-
3.3、根据id获取房间详细信息
查看接口
代码开发
-
查看响应数据结构
查看web-admin下的
com.atguigu.lease.web.admin.vo.room.RoomDetailVo
,内容如下@Schema(description = "房间信息") @Data public class RoomDetailVo extends RoomInfo { @Schema(description = "所属公寓信息") private ApartmentInfo apartmentInfo; @Schema(description = "图片列表") private List<GraphVo> graphVoList; @Schema(description = "属性信息列表") private List<AttrValueVo> attrValueVoList; @Schema(description = "配套信息列表") private List<FacilityInfo> facilityInfoList; @Schema(description = "标签信息列表") private List<LabelInfo> labelInfoList; @Schema(description = "支付方式列表") private List<PaymentType> paymentTypeList; @Schema(description = "可选租期列表") private List<LeaseTerm> leaseTermList; }
-
编写Controller层逻辑
在
RoomController
中增加如下内容@Operation(summary = "根据id获取房间详细信息") @GetMapping("getDetailById") public Result<RoomDetailVo> getDetailById(@RequestParam Long id) { RoomDetailVo roomInfo = service.getRoomDetailById(id); return Result.ok(roomInfo); }
-
编写Service 层逻辑
-
在
RoomInfoService
中增加如下内容RoomDetailVo getRoomDetailById(Long id);
-
在
RoomInfoServiceImpl
中增加如下内容@Override public RoomDetailVo getRoomDetailById(Long id) { //1.查询RoomInfo RoomInfo roomInfo = roomInfoMapper.selectById(id); //2.查询所属公寓信息 ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(roomInfo.getApartmentId()); //3.查询graphInfoList List<GraphVo> graphVoList = graphInfoMapper.selectListByItemTypeAndId(ItemType.ROOM, id); //4.查询attrValueList List<AttrValueVo> attrvalueVoList = attrValueMapper.selectListByRoomId(id); //5.查询facilityInfoList List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectListByRoomId(id); //6.查询labelInfoList List<LabelInfo> labelInfoList = labelInfoMapper.selectListByRoomId(id); //7.查询paymentTypeList List<PaymentType> paymentTypeList = paymentTypeMapper.selectListByRoomId(id); //8.查询leaseTermList List<LeaseTerm> leaseTermList = leaseTermMapper.selectListByRoomId(id); RoomDetailVo adminRoomDetailVo = new RoomDetailVo(); BeanUtils.copyProperties(roomInfo, adminRoomDetailVo); adminRoomDetailVo.setApartmentInfo(apartmentInfo); adminRoomDetailVo.setGraphVoList(graphVoList); adminRoomDetailVo.setAttrValueVoList(attrvalueVoList); adminRoomDetailVo.setFacilityInfoList(facilityInfoList); adminRoomDetailVo.setLabelInfoList(labelInfoList); adminRoomDetailVo.setPaymentTypeList(paymentTypeList); adminRoomDetailVo.setLeaseTermList(leaseTermList); return adminRoomDetailVo; }
-
编写Mapper层逻辑
-
编写房间属性查询逻辑
-
在
AttrValueMapper
中增加如下内容List<AttrValueVo> selectListByRoomId(Long id);
-
在
AttrValueMapper.xml
中增加如下内容<select id="selectListByRoomId" resultType="com.atguigu.lease.web.admin.vo.attr.AttrValueVo"> select v.id, v.name, v.attr_key_id, k.name attr_key_name from attr_value v join attr_key k on v.attr_key_id = k.id where v.is_deleted = 0 and k.is_deleted = 0 and v.id in (select attr_value_id from room_attr_value where is_deleted = 0 and room_id = #{id}) </select>
-
-
编写房间配套查询逻辑
-
在
FacilityInfoMapper
中增加如下内容List<FacilityInfo> selectListByRoomId(Long id);
-
在
FacilityInfoMapper.xml
中增加如下内容<select id="selectListByRoomId" resultType="com.atguigu.lease.model.entity.FacilityInfo"> select id, type, name, icon from facility_info where is_deleted = 0 and id in (select facility_id from room_facility where is_deleted = 0 and room_id = #{id}) </select>
-
-
编写房间标签查询逻辑
-
在
LabelInfoMapper
中增加如下内容List<LabelInfo> selectListByRoomId(Long id);
-
在
LabelInfoMapper.xml
中增加如下内容<select id="selectListByRoomId" resultType="com.atguigu.lease.model.entity.LabelInfo"> select id, type, name from label_info where is_deleted = 0 and id in (select label_id from room_label where is_deleted = 0 and room_id = #{id}) </select>
-
-
编写房间可选支付方式查询逻辑
-
在
PaymentTypeMapper
中增加如下内容List<PaymentType> selectListByRoomId(Long id);
-
在
PaymentTypeMapper.xml
中增加如下内容<select id="selectListByRoomId" resultType="com.atguigu.lease.model.entity.PaymentType"> select id, name, pay_month_count, additional_info from payment_type where is_deleted = 0 and id in (select payment_type_id from room_payment_type where is_deleted = 0 and room_id = #{id}) </select>
-
-
编写房间可选租期查询逻辑
-
在
Mapper
中增加如下内容List<LeaseTerm> selectListByRoomId(Long id);
-
在
Mapper.xml
中增加如下内容<select id="selectListByRoomId" resultType="com.atguigu.lease.model.entity.LeaseTerm"> select id, month_count, unit from lease_term where is_deleted = 0 and id in (select lease_term_id from room_lease_term where is_deleted = 0 and room_id = #{id}) </select>
-
-
-
-
编写Mapper层逻辑
-
编写房间属性查询逻辑
-
在
AttrValueMapper
中增加如下内容List<AttrValueVo> selectListByRoomId(Long id);
-
在
AttrValueMapper.xml
中增加如下内容<select id="selectListByRoomId" resultType="com.atguigu.lease.web.admin.vo.attr.AttrValueVo"> select v.id, v.name, v.attr_key_id, k.name attr_key_name from attr_value v join attr_key k on v.attr_key_id = k.id where v.is_deleted = 0 and k.is_deleted = 0 and v.id in (select attr_value_id from room_attr_value where is_deleted = 0 and room_id = #{id}) </select>
-
-
编写房间配套查询逻辑
-
在
FacilityInfoMapper
中增加如下内容List<FacilityInfo> selectListByRoomId(Long id);
-
在
FacilityInfoMapper.xml
中增加如下内容<select id="selectListByRoomId" resultType="com.atguigu.lease.model.entity.FacilityInfo"> select id, type, name, icon from facility_info where is_deleted = 0 and id in (select facility_id from room_facility where is_deleted = 0 and room_id = #{id}) </select>
-
-
编写房间标签查询逻辑
-
在
LabelInfoMapper
中增加如下内容List<LabelInfo> selectListByRoomId(Long id);
-
在
LabelInfoMapper.xml
中增加如下内容<select id="selectListByRoomId" resultType="com.atguigu.lease.model.entity.LabelInfo"> select id, type, name from label_info where is_deleted = 0 and id in (select label_id from room_label where is_deleted = 0 and room_id = #{id}) </select>
-
-
编写房间可选支付方式查询逻辑
-
在
PaymentTypeMapper
中增加如下内容List<PaymentType> selectListByRoomId(Long id);
-
在
PaymentTypeMapper.xml
中增加如下内容<select id="selectListByRoomId" resultType="com.atguigu.lease.model.entity.PaymentType"> select id, name, pay_month_count, additional_info from payment_type where is_deleted = 0 and id in (select payment_type_id from room_payment_type where is_deleted = 0 and room_id = #{id}) </select>
-
-
编写房间可选租期查询逻辑
-
在
Mapper
中增加如下内容List<LeaseTerm> selectListByRoomId(Long id);
-
在
Mapper.xml
中增加如下内容<select id="selectListByRoomId" resultType="com.atguigu.lease.model.entity.LeaseTerm"> select id, month_count, unit from lease_term where is_deleted = 0 and id in (select lease_term_id from room_lease_term where is_deleted = 0 and room_id = #{id}) </select>
-
-
3.4、根据ID删除房间信息
查看接口
代码开发
-
编写Controller层逻辑
在
RoomController
中增加如下内容@Operation(summary = "根据id删除房间信息") @DeleteMapping("removeById") public Result removeById(@RequestParam Long id) { service.removeRoomById(id); return Result.ok(); }
-
编写Service 层逻辑
-
在
RoomInfoService
中增加如下内容void removeRoomById(Long id);
-
在
RoomInfoServiceImpl
中增加如下内容@Override public void removeRoomById(Long id) { //1.删除RoomInfo super.removeById(id); //2.删除graphInfoList LambdaQueryWrapper<GraphInfo> graphQueryWrapper = new LambdaQueryWrapper<>(); graphQueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM); graphQueryWrapper.eq(GraphInfo::getItemId, id); graphInfoService.remove(graphQueryWrapper); //3.删除attrValueList LambdaQueryWrapper<RoomAttrValue> attrQueryWrapper = new LambdaQueryWrapper<>(); attrQueryWrapper.eq(RoomAttrValue::getRoomId, id); roomAttrValueService.remove(attrQueryWrapper); //4.删除facilityInfoList LambdaQueryWrapper<RoomFacility> facilityQueryWrapper = new LambdaQueryWrapper<>(); facilityQueryWrapper.eq(RoomFacility::getRoomId, id); roomFacilityService.remove(facilityQueryWrapper); //5.删除labelInfoList LambdaQueryWrapper<RoomLabel> labelQueryWrapper = new LambdaQueryWrapper<>(); labelQueryWrapper.eq(RoomLabel::getRoomId, id); roomLabelService.remove(labelQueryWrapper); //6.删除paymentTypeList LambdaQueryWrapper<RoomPaymentType> paymentQueryWrapper = new LambdaQueryWrapper<>(); paymentQueryWrapper.eq(RoomPaymentType::getRoomId, id); roomPaymentTypeService.remove(paymentQueryWrapper); //7.删除leaseTermList LambdaQueryWrapper<RoomLeaseTerm> termQueryWrapper = new LambdaQueryWrapper<>(); termQueryWrapper.eq(RoomLeaseTerm::getRoomId, id); roomLeaseTermService.remove(termQueryWrapper); }
-
3.5、根据id修改房间发布状态
查看接口
代码开发
在RoomController
中增加如下内容
@Operation(summary = "根据id修改房间发布状态")
@PostMapping("updateReleaseStatusById")
public Result updateReleaseStatusById(Long id, ReleaseStatus status) {
LambdaUpdateWrapper<RoomInfo> updateWrapper = new LambdaUpdateWrapper<>();
updateWrapper.eq(RoomInfo::getId, id);
updateWrapper.set(RoomInfo::getIsRelease, status);
service.update(updateWrapper);
return Result.ok();
}
3.6、 根据公寓ID查询房间列表
查看接口
代码开发
在RoomController
中增加如下内容
@GetMapping("listBasicByApartmentId")
@Operation(summary = "根据公寓id查询房间列表")
public Result<List<RoomInfo>> listBasicByApartmentId(Long id) {
LambdaQueryWrapper<RoomInfo> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(RoomInfo::getApartmentId, id);
queryWrapper.eq(RoomInfo::getIsRelease, ReleaseStatus.RELEASED);
List<RoomInfo> roomInfoList = service.list(queryWrapper);
return Result.ok(roomInfoList);
}