package cn.iocoder.yudao.module.system.controller.admin.biz;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.MedicineInOutRecordsDO;
import cn.iocoder.yudao.module.system.service.biz.MedicineInOutRecordsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * 药品出入库记录 Controller
 *
 * @author 系统管理员
 */
@Tag(name = "管理后台 - 药品出入库记录")
@RestController
@RequestMapping("/medicine/in-out-records")
@Validated
public class MedicineInOutRecordsController {

    @Resource
    private MedicineInOutRecordsService medicineInOutRecordsService;

    @PostMapping("/create")
    @Operation(summary = "创建出入库记录")
    @PreAuthorize("@ss.hasPermission('medicine:in-out-records:create')")
    public CommonResult<Long> createRecord(@Valid @RequestBody MedicineInOutRecordsDO record) {
        Long id = medicineInOutRecordsService.createRecord(record);
        return success(id);
    }

    @PutMapping("/update")
    @Operation(summary = "更新出入库记录")
    @PreAuthorize("@ss.hasPermission('medicine:in-out-records:update')")
    public CommonResult<Boolean> updateRecord(@Valid @RequestBody MedicineInOutRecordsDO record) {
        medicineInOutRecordsService.updateRecord(record);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除出入库记录")
    @Parameter(name = "id", description = "记录ID", required = true)
    @PreAuthorize("@ss.hasPermission('medicine:in-out-records:delete')")
    public CommonResult<Boolean> deleteRecord(@RequestParam("id") Long id) {
        medicineInOutRecordsService.deleteRecord(id);
        return success(true);
    }

    @PostMapping("/delete-batch")
    @Operation(summary = "批量删除出入库记录")
    @PreAuthorize("@ss.hasPermission('medicine:in-out-records:delete')")
    public CommonResult<Boolean> deleteRecords(@RequestBody Long[] ids) {
        medicineInOutRecordsService.deleteRecords(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得出入库记录详情")
    @Parameter(name = "id", description = "记录ID", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('medicine:in-out-records:query')")
    public CommonResult<MedicineInOutRecordsDO> getRecord(@RequestParam("id") Long id) {
        MedicineInOutRecordsDO record = medicineInOutRecordsService.getRecord(id);
        return success(record);
    }

    @GetMapping("/list-by-barcode")
    @Operation(summary = "根据药品标识码查询出入库记录")
    @Parameter(name = "barcode", description = "药品标识码", required = true)
    @PreAuthorize("@ss.hasPermission('medicine:in-out-records:query')")
    public CommonResult<List<MedicineInOutRecordsDO>> getRecordsByBarcode(@RequestParam("barcode") String barcode) {
        List<MedicineInOutRecordsDO> records = medicineInOutRecordsService.getRecordsByBarcode(barcode);
        return success(records);
    }

    @GetMapping("/list-by-batch-number")
    @Operation(summary = "根据批次号查询出入库记录")
    @Parameter(name = "batchNumber", description = "批次号", required = true)
    @PreAuthorize("@ss.hasPermission('medicine:in-out-records:query')")
    public CommonResult<List<MedicineInOutRecordsDO>> getRecordsByBatchNumber(@RequestParam("batchNumber") String batchNumber) {
        List<MedicineInOutRecordsDO> records = medicineInOutRecordsService.getRecordsByBatchNumber(batchNumber);
        return success(records);
    }

    @GetMapping("/list-by-type")
    @Operation(summary = "根据出入库类型查询记录")
    @Parameter(name = "inOrOut", description = "出入库类型：1-入库，2-出库", required = true)
    @PreAuthorize("@ss.hasPermission('medicine:in-out-records:query')")
    public CommonResult<List<MedicineInOutRecordsDO>> getRecordsByInOrOut(@RequestParam("inOrOut") Integer inOrOut) {
        List<MedicineInOutRecordsDO> records = medicineInOutRecordsService.getRecordsByInOrOut(inOrOut);
        return success(records);
    }

    @GetMapping("/list-by-tenant")
    @Operation(summary = "根据机构ID查询出入库记录")
    @Parameter(name = "tenantId", description = "机构ID", required = true)
    @PreAuthorize("@ss.hasPermission('medicine:in-out-records:query')")
    public CommonResult<List<MedicineInOutRecordsDO>> getRecordsByTenantId(@RequestParam("tenantId") Long tenantId) {
        List<MedicineInOutRecordsDO> records = medicineInOutRecordsService.getRecordsByTenantId(tenantId);
        return success(records);
    }
}




















































