controller
package com.umh.doctorreferral.api.controller
import com.umh.doctorreferral.api.model.BookingDto
import com.umh.doctorreferral.api.model.BookingParam
import com.umh.doctorreferral.api.service.ApiBookingCaseService
import io.swagger.annotations.ApiResponse
import io.swagger.annotations.ApiResponses
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.web.bind.annotation.*
import java.util.*
/**
* @Description
* @Author hunagzp
* @Date 2021/7/22 17:26
*
*/
@RestController
@Tag(name = "预约记录API", description = "预约记录API")
class BookingController {
@Autowired
private lateinit var apiBookingCaseService : ApiBookingCaseService
@Operation(summary = "获取已绑定预约记录", description = "获取已绑定预约记录")
@GetMapping("/referral-cases/{referralCaseId}/bookings", params = ["page", "size"])
@ApiResponses(
ApiResponse(code = 200, message = "OK", response = BookingDto::class)
)
fun referralCaseBookings(@PathVariable referralCaseId: UUID, pageable: Pageable): Page<BookingDto> {
return apiBookingCaseService.getBooking(referralCaseId,pageable)
}
}
service
package com.umh.doctorreferral.api.service
import com.umh.doctorreferral.api.model.BookingDto
import com.umh.doctorreferral.core.model.doctorreferralcase.DoctorReferralCaseBookingMap
import com.umh.doctorreferral.core.model.doctorreferralcase.DoctorReferralCaseBookingMapRepository
import io.swagger.v3.oas.annotations.Operation
import org.slf4j.LoggerFactory
import org.springframework.beans.BeanUtils
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.domain.Page
import org.springframework.data.domain.PageRequest
import org.springframework.data.domain.Pageable
import org.springframework.data.domain.Sort
import org.springframework.stereotype.Service
import java.util.*
import javax.persistence.EntityManager
import javax.persistence.PersistenceContext
/**
* @Description
* @Author hunagzp
* @Date 2021/7/22 16:04
*
*/
@Service
class ApiBookingCaseService {
@Autowired
private lateinit var doctorReferralCaseBookingMapRepository: DoctorReferralCaseBookingMapRepository
@PersistenceContext
private lateinit var em: EntityManager
private val logger = LoggerFactory.getLogger(this.javaClass)
@Operation(summary = "获取已绑定预约记录", description = "获取已绑定预约记录")
fun getBooking(referralCaseId: UUID, pageable: Pageable): Page<BookingDto> {
//定义分页类型
val page : Pageable
val orders = ArrayList<Sort.Order>()
orders.add(Sort.Order(Sort.Direction.DESC, "createdDate"))
page = PageRequest.of(pageable.pageNumber, pageable.pageSize, Sort.by(orders))
//查找数据并分页
val booking = doctorReferralCaseBookingMapRepository.pageBooking(referralCaseId,page)
//return booking.map { convertToBookingDto(it) } 方法一
//方法二
return booking.map { BookingDto().apply {
BeanUtils.copyProperties(it, this)
this.referralCaseId = it.doctorReferralCaseId
} }
}
fun convertToBookingDto(doctorReferralCaseBookingMap: DoctorReferralCaseBookingMap): BookingDto {
return BookingDto().apply {
BeanUtils.copyProperties(doctorReferralCaseBookingMap, this)
//TODO bookingDate, bookingTime, clinicName, doctoeName, services
this.referralCaseId = doctorReferralCaseBookingMap.doctorReferralCaseId
}
}
}
JpaRepository
package com.umh.doctorreferral.core.model.doctorreferralcase
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Query
import java.util.*
interface DoctorReferralCaseBookingMapRepository : JpaRepository<DoctorReferralCaseBookingMap, UUID> {
@Query("select b from DoctorReferralCaseBookingMap b where b.doctorReferralCaseId=?1")
fun pageBooking(referralCaseId: UUID, page: Pageable): Page<DoctorReferralCaseBookingMap>
}