package com.zhyc.module.biosafety.controller;

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

import com.zhyc.module.biosafety.domain.SwMedicineUsage;
import com.zhyc.module.biosafety.service.ISwMedicineUsageService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zhyc.common.annotation.Log;
import com.zhyc.common.core.controller.BaseController;
import com.zhyc.common.core.domain.AjaxResult;
import com.zhyc.common.enums.BusinessType;
import com.zhyc.common.utils.poi.ExcelUtil;
import com.zhyc.common.core.page.TableDataInfo;

/**
 * 药品使用记录Controller
 * 
 * @author ruoyi
 * @date 2025-07-12
 */
@RestController
@RequestMapping("/biosafety/usageInfo")
public class SwMedicineUsageController extends BaseController
{
    @Autowired
    private ISwMedicineUsageService swMedicineUsageService;

    /**
     * 查询药品使用记录列表
     */
    @PreAuthorize("@ss.hasPermi('biosafety:usageInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(SwMedicineUsage swMedicineUsage)
    {
        startPage();
        List<SwMedicineUsage> list = swMedicineUsageService.selectSwMedicineUsageList(swMedicineUsage);
        return getDataTable(list);
    }

    /**
     * 导出药品使用记录列表
     */
    @PreAuthorize("@ss.hasPermi('biosafety:usageInfo:export')")
    @Log(title = "药品使用记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SwMedicineUsage swMedicineUsage)
    {
        List<SwMedicineUsage> list = swMedicineUsageService.selectSwMedicineUsageList(swMedicineUsage);
        List<SwMedicineUsage> resultList = new ArrayList<>();
        for (SwMedicineUsage medicineUsage : list) {
            medicineUsage.setSwMedicineUsageDetailsList(swMedicineUsageService.selectSwMedicineUsageDetailsById(medicineUsage.getId()));
            resultList.add(medicineUsage);
        }
        ExcelUtil<SwMedicineUsage> util = new ExcelUtil<SwMedicineUsage>(SwMedicineUsage.class);
        util.exportExcel(response, resultList, "药品使用记录数据");
    }

    /**
     * 获取药品使用记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('biosafety:usageInfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(swMedicineUsageService.selectSwMedicineUsageById(id));
    }

    /**
     * 新增药品使用记录
     */
    @PreAuthorize("@ss.hasPermi('biosafety:usageInfo:add')")
    @Log(title = "药品使用记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SwMedicineUsage swMedicineUsage)
    {
        swMedicineUsage.setDeptId(getDeptId());
        swMedicineUsage.setUserId(getUserId());
        return toAjax(swMedicineUsageService.insertSwMedicineUsage(swMedicineUsage));
    }

    /**
     * 修改药品使用记录
     */
    @PreAuthorize("@ss.hasPermi('biosafety:usageInfo:edit')")
    @Log(title = "药品使用记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SwMedicineUsage swMedicineUsage)
    {
        return toAjax(swMedicineUsageService.updateSwMedicineUsage(swMedicineUsage));
    }

    /**
     * 删除药品使用记录
     */
    @PreAuthorize("@ss.hasPermi('biosafety:usageInfo:remove')")
    @Log(title = "药品使用记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(swMedicineUsageService.deleteSwMedicineUsageByIds(ids));
    }
}
