/*
 * Copyright ©2023 CJB-CNIT Team. All rights reserved
 */
package com.openhis.web.chargemanage.controller;

import java.util.List;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.core.common.core.domain.R;
import com.openhis.web.chargemanage.appservice.IOutpatientRefundAppService;
import com.openhis.web.chargemanage.dto.EncounterPatientPageParam;
import com.openhis.web.chargemanage.dto.RefundItemParam;

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

/**
 * 门诊退费 controller
 *
 * @author zwh
 * @date 2025-03-15
 */
@RestController
@RequestMapping("/charge-manage/refund")
@Slf4j
@AllArgsConstructor
public class OutpatientRefundController {

    @Resource
    private IOutpatientRefundAppService outpatientRefundAppService;

    /**
     * 门诊退费页面初始化
     *
     * @return 初始化信息
     */
    @GetMapping(value = "/init")
    public R<?> outpatientRefundInit() {
        return outpatientRefundAppService.outpatientRefundInit();
    }

    /**
     * 查询结算过的就诊患者分页列表
     *
     * @param encounterPatientPageParam 查询条件
     * @param searchKey 模糊查询关键字
     * @param pageNo 当前页
     * @param pageSize 每页多少条
     * @param request 请求
     * @return 就诊患者分页列表
     */
    @GetMapping(value = "/encounter-patient-page")
    public R<?> getBilledEncounterPatientPage(EncounterPatientPageParam encounterPatientPageParam,
        @RequestParam(value = "searchKey", defaultValue = "") String searchKey,
        @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
        @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest request) {
        return outpatientRefundAppService.getBilledEncounterPatientPage(encounterPatientPageParam, searchKey, pageNo,
            pageSize, request);
    }

    /**
     * 根据就诊id查询患者的账单（申请退费列表）
     *
     * @param encounterId 就诊id
     * @return 患者账单列表
     */
    @GetMapping(value = "/patient-payment")
    public R<?> getEncounterPatientPayment(@RequestParam Long encounterId) {
        return outpatientRefundAppService.getEncounterPatientPayment(encounterId);
    }

    /**
     * 根据账单申请退费
     *
     * @param refundItemList 退费项目id列表
     * @return 操作结果
     */
    @PostMapping(value = "/refund-payment")
    public R<?> refundPayment(@RequestBody List<RefundItemParam> refundItemList) {
        return outpatientRefundAppService.refundPayment(refundItemList);
    }

    /**
     * 根据就诊id查询患者的退费账单
     *
     * @param encounterId 就诊id
     * @param billDateSTime 收费时间开始
     * @param billDateETime 收费时间结束
     * @return 退费账单列表
     */
    @GetMapping(value = "/patient-refund")
    public R<?> getEncounterPatientRefund(@RequestParam Long encounterId, @RequestParam String billDateSTime,
        @RequestParam String billDateETime) {
        return outpatientRefundAppService.getEncounterPatientRefund(encounterId, billDateSTime, billDateETime);
    }

    /**
     * 根据就诊id查询患者因退费重新生成的账单
     *
     * @param encounterId 就诊id
     * @return 重新生成的账单列表
     */
    @GetMapping(value = "/regenerate_charge")
    public R<?> getRegenerateCharge(@RequestParam Long encounterId) {
        return outpatientRefundAppService.getRegenerateCharge(encounterId);
    }

    /**
     * 校验是否可以退费（耗材/药品是否已退，诊疗是否取消执行）
     *
     * @param chargeItemIdList 收费项目id列表
     * @return 是否可退
     */
    @GetMapping(value = "/verify_refund")
    public R<?> verifyRefundable(@RequestParam List<Long> chargeItemIdList) {
        return outpatientRefundAppService.verifyRefundable(chargeItemIdList);
    }
}
