package com.ruoyi.inventory.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.inventory.domain.*;
import com.ruoyi.inventory.service.IHealthcareDrugFilingService;
import com.ruoyi.inventory.service.impl.HealthcareDrugArchiveServiceImpl;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.inventory.service.IHealthcareInventoryDrugentryService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 药品入库记录Controller
 *
 * @author 蒋松霖
 * @date 2024-09-27
 */
@RestController
@RequestMapping("/pasd-healthcare/drugentry")
public class HealthcareInventoryDrugentryController extends BaseController
{
    @Autowired
    private IHealthcareInventoryDrugentryService healthcareInventoryDrugentryService;

    @Autowired
    private IHealthcareDrugFilingService healthcareDrugFilingService;

    /**
     * 保存药品入库记录
     * @param healthcareInventoryDrugentry 药品入库记录对象
     * @return 操作结果
     */
    @Log(title = "药品入库记录", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermi('pasd-healthcare:drugentry:add')")
    @PostMapping("/save")
    public AjaxResult save(@RequestBody HealthcareInventoryDrugentry healthcareInventoryDrugentry) {
        return toAjax(healthcareInventoryDrugentryService.saveHealthcareInventoryDrugentry(healthcareInventoryDrugentry));
    }

    /**
     * 查询药品入库记录列表
     */
    @PreAuthorize("@ss.hasPermi('pasd-healthcare:drugentry:list')")
    @GetMapping("/list")
    public TableDataInfo list(HealthcareInventoryDrugentry healthcareInventoryDrugentry)
    {
        startPage();
        List<HealthcareInventoryDrugentry> list = healthcareInventoryDrugentryService.selectHealthcareInventoryDrugentryList(healthcareInventoryDrugentry);
        return getDataTable(list);
    }



    /**
     * 导出药品入库记录列表
     */
    @PreAuthorize("@ss.hasPermi('pasd-healthcare:drugentry:export')")
    @Log(title = "药品入库记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HealthcareInventoryDrugentry healthcareInventoryDrugentry)
    {
        List<HealthcareInventoryDrugentry> list = healthcareInventoryDrugentryService.selectHealthcareInventoryDrugentryList(healthcareInventoryDrugentry);
        ExcelUtil<HealthcareInventoryDrugentry> util = new ExcelUtil<HealthcareInventoryDrugentry>(HealthcareInventoryDrugentry.class);
        util.exportExcel(response, list, "药品入库记录数据");
    }


    /**
     * 获取药品入库记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('pasd-healthcare:drugentry:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id,
                              @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                              @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        // 启动分页
        PageHelper.startPage(pageNum, pageSize);
        return healthcareInventoryDrugentryService.selectHealthcareInventoryDrugentryById(id);
    }

    /**
     * 新增药品入库记录
     */
    @PreAuthorize("@ss.hasPermi('pasd-healthcare:drugentry:add')")
    @Log(title = "药品入库记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HealthcareInventoryDrugentry healthcareInventoryDrugentry)
    {
        return toAjax(healthcareInventoryDrugentryService.insertHealthcareInventoryDrugentry(healthcareInventoryDrugentry));
    }

    /**
     * 修改药品入库记录
     */
    @PreAuthorize("@ss.hasPermi('pasd-healthcare:drugentry:edit')")
    @Log(title = "药品入库记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HealthcareInventoryDrugentry healthcareInventoryDrugentry)
    {
        return toAjax(healthcareInventoryDrugentryService.updateHealthcareInventoryDrugentry(healthcareInventoryDrugentry));
    }

    /**
     * 删除药品入库记录
     */
    @PreAuthorize("@ss.hasPermi('pasd-healthcare:drugentry:remove')")
    @Log(title = "药品入库记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(healthcareInventoryDrugentryService.deleteHealthcareInventoryDrugentryByIds(ids));
    }

    /**
     * 导入Excel药品档案
     */
    @PreAuthorize("@ss.hasPermi('pasd-healthcare:drugentry:add')")
    @Log(title = "药品入库", businessType = BusinessType.IMPORT)
    @PostMapping("/entryExcel")
    public AjaxResult excelImport(MultipartFile file) throws Exception{
        ExcelUtil<HealthcareDrugFiling> util = new ExcelUtil<>(HealthcareDrugFiling.class);
        List<HealthcareDrugFiling> list = util.importExcel(file.getInputStream());
        return toAjax(healthcareDrugFilingService.insertHealthcareInventoryFilings(list));
    }
}
