package com.openhis.web.pharmacymanage.controller;

import java.util.List;

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.pharmacymanage.appservice.IMedicalDeviceDispenseAppService;
import com.openhis.web.pharmacymanage.dto.DispenseItemDto;
import com.openhis.web.pharmacymanage.dto.EncounterInfoSearchParam;
import com.openhis.web.pharmacymanage.dto.ItemDispenseOrderDto;

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

/**
 * 材料发送
 *
 * @author yuxj
 * @date 2025/8/21
 */
@RestController
@RequestMapping("/pharmacy-manage/device-dispense")
@Slf4j
@AllArgsConstructor
public class MedicalDeviceDispenseController {

    @Autowired
    public IMedicalDeviceDispenseAppService medicalDeviceDispenseAppService;

    /**
     * 获取页面初始化信息
     *
     * @return 初始化信息
     */
    @GetMapping(value = "/init")
    public R<?> deviceDispenseInit() {
        return medicalDeviceDispenseAppService.init();
    }

    /**
     * 分页查询病人列表
     *
     * @param encounterInfoSearchParam 查询条件
     * @param searchKey 模糊查询关键字
     * @param pageNo 当前页码
     * @param pageSize 查询条数
     * @param request 请求数据
     * @return 病人列表
     */
    @GetMapping("/encounter-list")
    public R<?> getEncounterInfoList(EncounterInfoSearchParam encounterInfoSearchParam, String searchKey,
        @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
        @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest request) {
        return medicalDeviceDispenseAppService.getEncounterInfoListPage(encounterInfoSearchParam, searchKey, pageNo,
            pageSize, request);
    }

    /**
     * 发耗材单查询
     *
     * @param itemDispenseOrderDto 查询信息
     * @param pageNo 当前页码
     * @param pageSize 查询条数
     * @return 发耗材单
     */
    @GetMapping("/device-order")
    public R<?> getDeviceDispenseOrderList(ItemDispenseOrderDto itemDispenseOrderDto,
        @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
        @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        return medicalDeviceDispenseAppService.getDeviceDispenseOrderList(itemDispenseOrderDto, pageNo, pageSize);
    }

    /**
     * 核对发耗材
     *
     * @param dispenseDeviceList 发耗材信息
     * @return 处理结果
     */
    @PutMapping("/device-dispense")
    public R<?> deviceDispense(@RequestBody List<DispenseItemDto> dispenseDeviceList) {
        return medicalDeviceDispenseAppService.deviceDispense(dispenseDeviceList);
    }

    /**
     * 发放耗材（长大专属）
     *
     * @param chargeItemIds 耗材收费单ids
     * @return 处理结果
     */
    @PutMapping("/consumables-dispense")
    public R<?> dispenseMedicalConsumables(@RequestBody List<Long> chargeItemIds) {
        return medicalDeviceDispenseAppService.dispenseMedicalConsumables(chargeItemIds);
    }

    /**
     * 作废
     *
     * @param dispenseDeviceList 作废信息
     * @return 处理结果
     */
    @PutMapping("/device-cancel")
    public R<?> deviceCancel(@RequestBody List<DispenseItemDto> dispenseDeviceList) {
        return medicalDeviceDispenseAppService.deviceCancel(dispenseDeviceList);
    }
}
