在ViewAppointmenController中增加内容
@Operation(summary = "根据id更新预约状态")
@PostMapping("updateStatusById")
public Result updateStatusById(@RequestParam Long id, @RequestParam AppointmentStatus status) {
LambdaUpdateWrapper<ViewAppointment> updateWrapper = new LambdaUpdateWrapper<>();
updateWrapper.eq(ViewAppointment::getId,id);
updateWrapper.set(ViewAppointment::getAppointmentStatus,status);
service.update(updateWrapper);
return Result.ok();
}
编写Controller层逻辑
在ViewAppointmentController
中增加如下内容
@Operation(summary = "分页查询预约信息")
@GetMapping("page")
public Result<IPage<AppointmentVo>> page(@RequestParam long current, @RequestParam long size, AppointmentQueryVo queryVo) {
IPage<AppointmentVo> page = new Page<>(current, size);
IPage<AppointmentVo> list = service.pageAppointmentByQuery(page, queryVo);
return Result.ok(list);
}
编写Service层逻辑
在ViewAppointmentService
中增加如下内容
IPage<AppointmentVo> pageAppointmentByQuery(IPage<AppointmentVo> page, AppointmentQueryVo queryVo);
在ViewAppointmentServiceImpl
中增加如下内容
@Override
public IPage<AppointmentVo> pageAppointmentByQuery(IPage<AppointmentVo> page, AppointmentQueryVo queryVo) {
return viewAppointmentMapper.pageAppointmentByQuery(page, queryVo);
}
编写Mapper层逻辑
在ViewAppointmentMapper
中增加如下内容
IPage<AppointmentVo> pageAppointmentByQuery(IPage<AppointmentVo> page, AppointmentQueryVo queryVo);
在ViewAppointmentMapper.xml
中增加如下内容
<resultMap id="AppointmentVoMap" type="com.atguigu.lease.web.admin.vo.appointment.AppointmentVo" autoMapping="true">
<id property="id" column="id"/>
<association property="apartmentInfo" javaType="com.atguigu.lease.model.entity.ApartmentInfo" autoMapping="true">
<id property="id" column="apartment_id"/>
<result property="name" column="apartment_name"/>
association>
resultMap>
<select id="pageAppointmentByQuery" resultMap="AppointmentVoMap">
select va.id,
va.user_id,
va.name,
va.phone,
va.appointment_time,
va.additional_info,
va.appointment_status,
ai.id apartment_id,
ai.name apartment_name,
ai.district_id,
ai.district_name,
ai.city_id,
ai.city_name,
ai.province_id,
ai.province_name
from view_appointment va
left join
apartment_info ai
on va.apartment_id = ai.id and ai.is_deleted=0
<where>
va.is_deleted = 0
<if test="queryVo.provinceId != null">
and ai.province_id = #{queryVo.provinceId}
if>
<if test="queryVo.cityId != null">
and ai.city_id = #{queryVo.cityId}
if>
<if test="queryVo.districtId != null">
and ai.district_id = #{queryVo.districtId}
if>
<if test="queryVo.apartmentId != null">
and va.apartment_id = #{queryVo.apartmentId}
if>
<if test="queryVo.name != null and queryVo.name != ''">
and va.name like concat('%',#{queryVo.name},'%')
if>
<if test="queryVo.phone != null and queryVo.phone != ''">
and va.phone like concat('%',#{queryVo.phone},'%')
if>
where>
select>
Date类型格式和时区问题
格式
在指定字段增加@JsonFormat
注解,如下
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date appointmentTime;
全局配置
在application.yml
中增加如下内容
spring:
jackson:
date-format: yyyy-MM-dd HH:mm:ss
时区
单独配置
在指定字段增加@JsonFormat
注解,如下
@JsonFormat(timezone = "GMT+8")
private Date appointmentTime;
全局配置
spring:
jackson:
time-zone: GMT+8
在LeaseAgreementController
中增加如下内容
@Operation(summary = "保存或修改租约信息")
@PostMapping("saveOrUpdate")
public Result saveOrUpdate(@RequestBody LeaseAgreement leaseAgreement) {
service.saveOrUpdate(leaseAgreement);
return Result.ok();
}
查看请求和响应的数据结构
请求数据结构
current
和size
为分页相关参数,分别表示当前所处页面和每个页面的记录数。
AgreementQueryVo
为公寓的查询条件,详细结构如下:
@Data
@Schema(description = "租约查询实体")
public class AgreementQueryVo {
@Schema(description = "公寓所处省份id")
private Long provinceId;
@Schema(description = "公寓所处城市id")
private Long cityId;
@Schema(description = "公寓所处区域id")
private Long districtId;
@Schema(description = "公寓id")
private Long apartmentId;
@Schema(description = "房间号")
private String roomNumber;
@Schema(description = "用户姓名")
private String name;
@Schema(description = "用户手机号码")
private String phone;
}
响应数据结构
单个租约信息的结构可查看com.atguigu.lease.web.admin.vo.agreement.AgreementVo
,内容如下:
@Data
@Schema(description = "租约信息")
public class AgreementVo extends LeaseAgreement {
@Schema(description = "签约公寓信息")
private ApartmentInfo apartmentInfo;
@Schema(description = "签约房间信息")
private RoomInfo roomInfo;
@Schema(description = "支付方式")
private PaymentType paymentType;
@Schema(description = "租期")
private LeaseTerm leaseTerm;
}
编写Controller层逻辑
在LeaseAgreementController
中增加如下内容
@Operation(summary = "根据条件分页查询租约列表")
@GetMapping("page")
public Result<IPage<AgreementVo>> page(@RequestParam long current, @RequestParam long size, AgreementQueryVo queryVo) {
IPage<AgreementVo> page = new Page<>(current, size);
IPage<AgreementVo> list = service.pageAgreementByQuery(page, queryVo);
return Result.ok(list);
}
编写Service层逻辑
在LeaseAgreementService
中增加如下内容
IPage<AgreementVo> pageAgreementByQuery(IPage<AgreementVo> page, AgreementQueryVo queryVo);
在LeaseAgreementServiceImpl
中增加如下内容
@Override
public IPage<AgreementVo> pageAgreementByQuery(IPage<AgreementVo> page, AgreementQueryVo queryVo) {
return leaseAgreementMapper.pageAgreementByQuery(page, queryVo);
}
编写Mapper层逻辑
在LeaseAgreementMapper
中增加如下内容
IPage<AgreementVo> pageAgreementByQuery(IPage<AgreementVo> page, AgreementQueryVo queryVo);
在LeaseAgreementMapper.xml
中增加如下内容
<resultMap id="agreementVoMap" type="com.atguigu.lease.web.admin.vo.agreement.AgreementVo" autoMapping="true">
<id property="id" column="id"/>
<association property="apartmentInfo" javaType="com.atguigu.lease.model.entity.ApartmentInfo" autoMapping="true">
<id property="id" column="apartment_id"/>
<result property="name" column="apartment_name"/>
</association>
<association property="roomInfo" javaType="com.atguigu.lease.model.entity.RoomInfo" autoMapping="true">
<id property="id" column="room_id"/>
</association>
<association property="paymentType" javaType="com.atguigu.lease.model.entity.PaymentType" autoMapping="true">
<id property="id" column="payment_type_id"/>
<result property="name" column="payment_type_name"/>
</association>
<association property="leaseTerm" javaType="com.atguigu.lease.model.entity.LeaseTerm" autoMapping="true">
<id property="id" column="lease_term_id"/>
</association>
</resultMap>
<select id="pageAgreementByQuery" resultMap="agreementVoMap">
select la.id,
la.phone,
la.name,
la.identification_number,
la.lease_start_date,
la.lease_end_date,
la.rent,
la.deposit,
la.status,
la.source_type,
la.additional_info,
ai.id apartment_id,
ai.name apartment_name,
ai.district_id,
ai.district_name,
ai.city_id,
ai.city_name,
ai.province_id,
ai.province_name,
ri.id room_id,
ri.room_number,
pt.id payment_type_id,
pt.name payment_type_name,
pt.pay_month_count,
lt.id lease_term_id,
lt.month_count,
lt.unit
from lease_agreement la
left join
apartment_info ai
on la.apartment_id = ai.id and ai.is_deleted=0
left join
room_info ri
on la.room_id = ri.id and ri.is_deleted=0
left join
payment_type pt
on la.payment_type_id = pt.id and pt.is_deleted=0
left join
lease_term lt
on la.lease_term_id = lt.id and lt.is_deleted=0
<where>
la.is_deleted = 0
<if test="queryVo.provinceId != null">
and ai.province_id = #{queryVo.provinceId}
</if>
<if test="queryVo.cityId != null">
and ai.city_id = #{queryVo.cityId}
</if>
<if test="queryVo.districtId != null">
and ai.district_id = #{queryVo.districtId}
</if>
<if test="queryVo.apartmentId != null">
and la.apartment_id = #{queryVo.apartmentId}
</if>
<if test="queryVo.roomNumber != null and queryVo.roomNumber != ''">
and ri.room_number like concat('%',#{queryVo.roomNumber},'%')
</if>
<if test="queryVo.name != null and queryVo.name != ''">
and la.name like concat('%',#{queryVo.name},'%')
</if>
<if test="queryVo.phone != null and queryVo.phone != ''">
and la.phone like concat('%',#{queryVo.phone},'%')
</if>
</where>
</select>
编写Controller层逻辑
在LeaseAgreementController
中增加如下内容
@Operation(summary = "根据id查询租约信息")
@GetMapping(name = "getById")
public Result<AgreementVo> getById(@RequestParam Long id) {
AgreementVo apartment = service.getAgreementById(id);
return Result.ok(apartment);
}
编写Service层逻辑
在LeaseAgreementService
中增加如下内容
AgreementVo getAgreementById(Long id);
在LeaseAgreementServiceImpl
中增加如下内容
@Override
public AgreementVo getAgreementById(Long id) {
//1.查询租约信息
LeaseAgreement leaseAgreement = leaseAgreementMapper.selectById(id);
//2.查询公寓信息
ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(leaseAgreement.getApartmentId());
//3.查询房间信息
RoomInfo roomInfo = roomInfoMapper.selectById(leaseAgreement.getRoomId());
//4.查询支付方式
PaymentType paymentType = paymentTypeMapper.selectById(leaseAgreement.getPaymentTypeId());
//5.查询租期
LeaseTerm leaseTerm = leaseTermMapper.selectById(leaseAgreement.getLeaseTermId());
AgreementVo adminAgreementVo = new AgreementVo();
BeanUtils.copyProperties(leaseAgreement, adminAgreementVo);
adminAgreementVo.setApartmentInfo(apartmentInfo);
adminAgreementVo.setRoomInfo(roomInfo);
adminAgreementVo.setPaymentType(paymentType);
adminAgreementVo.setLeaseTerm(leaseTerm);
return adminAgreementVo;
}
在LeaseAgreementController
中增加如下内容
@Operation(summary = "根据id删除租约信息")
@DeleteMapping("removeById")
public Result removeById(@RequestParam Long id) {
service.removeById(id);
return Result.ok();
}
后台管理系统需要多个修改租约状态的接口,例如修改租约状态为已取消、修改租约状态为已退租等等。为省去重复编码,此处将多个接口合并为一个如下,注意,在生产中应避免这样的写法。
在LeaseAgreementController
中增加如下内容
@Operation(summary = "根据id更新租约状态")
@PostMapping("updateStatusById")
public Result updateStatusById(@RequestParam Long id, @RequestParam LeaseStatus status) {
LambdaUpdateWrapper<LeaseAgreement> updateWrapper = new LambdaUpdateWrapper<>();
updateWrapper.eq(LeaseAgreement::getId, id);
updateWrapper.set(LeaseAgreement::getStatus, status);
service.update(updateWrapper);
return Result.ok();
}
内容是通过定时任务定时检查租约是否到期。SpringBoot内置了定时任务,具体实现如下:
启用Spring Boot定时任务
在SpringBoot启动类上增加@EnableScheduling
注解,如下
@SpringBootApplication
@EnableScheduling
public class AdminWebApplication {
public static void main(String[] args) {
SpringApplication.run(AdminWebApplication.class, args);
}
}
编写定时逻辑
在web-admin模块下创建com.atguigu.lease.web.admin.schedule.ScheduledTasks
类,内容如下
@Component
public class ScheduledTasks {
@Autowired
private LeaseAgreementService leaseAgreementService;
@Scheduled(cron = "0 0 0 * * *")
public void checkLeaseStatus() {
LambdaUpdateWrapper<LeaseAgreement> updateWrapper = new LambdaUpdateWrapper<>();
Date now = new Date();
updateWrapper.le(LeaseAgreement::getLeaseEndDate, now);
updateWrapper.eq(LeaseAgreement::getStatus, LeaseStatus.SIGNED);
updateWrapper.in(LeaseAgreement::getStatus, LeaseStatus.SIGNED, LeaseStatus.WITHDRAWING);
leaseAgreementService.update(updateWrapper);
}
}