package com.openhis.web.inpatientmanage.controller;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import com.core.common.core.domain.R;
import com.openhis.web.inpatientmanage.appservice.IPatientHomeAppService;
import com.openhis.web.inpatientmanage.dto.PatientHomeSearchParam;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 患者首页 应用实现
 *
 * @author SY
 * @since 2025/05/19
 */
@RestController
@RequestMapping("/patient-home-manage")
@Slf4j
@AllArgsConstructor
public class PatientHomeController {

    @Resource
    private IPatientHomeAppService patientHomeAppService;

    /**
     * 获取患者首页信息初期数据列表
     *
     * @return 患者首页初期数据列表
     */
    @GetMapping("/init")
    public R<?> getPatientInfoInit(PatientHomeSearchParam patientHomeSearchParam,
        @RequestParam(value = "searchKey", defaultValue = "") String searchKey,
        @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
        @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest request) {
        return patientHomeAppService.getPatientInfoInit(patientHomeSearchParam, searchKey, pageNo, pageSize, request);
    }

    /**
     * 患者首页-病区的空病床信息
     *
     * @return 空病床列表
     */
    @GetMapping("/empty-bed")
    public R<?> getEmptyBed(Long organizationId) {
        return patientHomeAppService.getEmptyBed(organizationId);
    }

    /**
     * 患者首页-转床保存
     *
     * @return 转床结果
     */
    @PutMapping("/bed-transfer")
    public R<?> saveBedTransfer(Long encounterLocationId, Long encounterId, Long oldLocationId, Long newLocationId) {
        return R
            .ok(patientHomeAppService.saveBedTransfer(encounterLocationId, encounterId, oldLocationId, newLocationId));
    }

    /**
     * 患者首页-科室信息
     *
     * @return 科室列表
     */
    @GetMapping("/caty")
    public R<?> getCaty() {
        return R.ok(patientHomeAppService.getCaty());
    }

    /**
     * 患者首页-转科保存
     *
     * @return 转科结果
     */
    @PutMapping("/department-transfer")
    public R<?> saveDepartmentTransfer(Long encounterLocationId, Long patientId, Long encounterId, Long organizationId,
        @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date DepartmentTransferYmd) {
        return R.ok(patientHomeAppService.saveDepartmentTransfer(encounterLocationId, patientId, encounterId,
            organizationId, DepartmentTransferYmd));
    }

    /**
     * 患者首页-出院信息
     *
     * @return 出院原因列表
     */
    @PutMapping("/discharge-from-hospital")
    public R<?> saveDischargeHospital(Long encounterLocationId, Long patientId, Long encounterId, Long locationId,
        @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date DischargeHospitalYmd) {
        return R.ok(patientHomeAppService.saveDischargeHospital(encounterLocationId, patientId, encounterId, locationId,
            DischargeHospitalYmd));
    }
}
