package com.openhis.web.inventorymanage.controller;

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

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

import com.core.common.core.domain.R;
import com.openhis.web.inventorymanage.appservice.IProductDetailAppService;
import com.openhis.web.inventorymanage.dto.ProductDetailSearchParam;

import lombok.extern.slf4j.Slf4j;

/**
 * 库存商品明细 controller
 *
 * @author yuanzs
 * @date 2025-04-24
 */
@RestController
@RequestMapping("/inventory-manage/product")
@Slf4j
public class ProductDetailController {

    @Resource
    private IProductDetailAppService productDetailsAppService;

    /**
     * 库存商品明细初始化
     *
     * @return 下拉列表值
     */
    @GetMapping("/product-init")
    public R<?> getMedicationInit() {
        return productDetailsAppService.getInit();
    }

    /**
     * 查询库存商品明细分页列表
     *
     * @param productDetailSearchParam 查询条件
     * @param pageNo 当前页码
     * @param pageSize 查询条数
     * @param searchKey 模糊查询关键字
     * @param request 请求数据
     * @return 库存商品明细分页列表
     */
    @GetMapping(value = "/product-page")
    public R<?> getProductDetailPage(ProductDetailSearchParam productDetailSearchParam,
        @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
        @RequestParam(value = "pageSize", defaultValue = "1000") Integer pageSize,
        @RequestParam(name = "searchKey", required = false) String searchKey, HttpServletRequest request) {
        return productDetailsAppService.getProductDetailPage(productDetailSearchParam, pageNo, pageSize, searchKey,
            request);
    }

    /**
     * 查询库存商品明细统计数据
     *
     * @param productDetailSearchParam 查询条件
     * @param searchKey 模糊查询关键字
     * @param request 请求数据
     * @return 库存商品明细统计数据
     */
    @GetMapping(value = "/product-statistics")
    public R<?> getProductDetailStatistics(ProductDetailSearchParam productDetailSearchParam,
        @RequestParam(name = "searchKey", required = false) String searchKey, HttpServletRequest request) {
        return productDetailsAppService.getProductDetailStatistics(productDetailSearchParam, searchKey, request);
    }

    /**
     * 操作：停供
     *
     * @param inventoryId 库存项目管理ID
     * @return 操作结果
     */
    @PutMapping("/stop-supply")
    public R<?> stopSupplyById(@RequestBody Long inventoryId) {
        return productDetailsAppService.stopSupplyById(inventoryId);
    }

    /**
     * 操作：取消停供
     *
     * @param inventoryId 库存项目管理ID
     * @return 操作结果
     */
    @PutMapping("/cancel-supply")
    public R<?> cancelSupplyById(@RequestBody Long inventoryId) {
        return productDetailsAppService.cancelSupplyById(inventoryId);
    }

    /**
     * 库存明细表导出
     *
     * @param productDetailSearchParam 查询条件
     * @param pageNo 当前页码
     * @param pageSize 查询条数
     * @param searchKey 模糊查询关键字
     * @param request 请求数据
     * @param response 响应数据
     */
    @GetMapping(value = "/excel-out")
    public void makeExcelFile(ProductDetailSearchParam productDetailSearchParam,
        @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
        @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
        @RequestParam(name = "searchKey", required = false) String searchKey, HttpServletRequest request,
        HttpServletResponse response) {
        productDetailsAppService.makeExcelFile(productDetailSearchParam, pageNo, pageSize, searchKey, request,
            response);
    }
}
