package com.ruoyi.os.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.os.domain.DrugFiling;
import com.ruoyi.os.service.IDrugFilingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
 *
 *  @author 何顺
 *  @date 2024-09-17
 */
@RestController
@RequestMapping("/healthcare/inventory/drugFiling")
public class DrugFilingController extends BaseController {

    @Autowired
    private IDrugFilingService drugFilingService;

    @GetMapping("/list")
    public TableDataInfo list(DrugFiling drugFiling) {
        startPage();
        List<DrugFiling> list = drugFilingService.selectDrugFilingList(drugFiling);
        return getDataTable(list);
    }

    @PutMapping
    public AjaxResult update(@RequestBody DrugFiling drugFiling) {
        if (drugFiling.getId() == null) {
            return error("批次ID不能为空");
        }
        if (drugFiling.getArchiveId() == null) {
            return error("药品档案ID不能为空");
        }
        return toAjax(drugFilingService.updateDrugFiling(drugFiling));
    }

    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        DrugFiling drugFiling = drugFilingService.selectDrugFilingById(id);
        if (drugFiling == null) {
            return error("批次不存在");
        }
        if (hasRelatedBusinessData(id)) {
            return error("该批次存在关联业务数据，不能删除");
        }
        return toAjax(drugFilingService.deleteDrugFilingById(id));
    }

    private boolean hasRelatedBusinessData(Long filingId) {
        // 实现检查关联业务数据的逻辑
        return false;
    }
}