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

import com.alibaba.fastjson.JSONArray;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.MedicineInventoryListReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.MedicineInventoryPageReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.MedicineInventoryDO;
import cn.iocoder.yudao.module.system.service.biz.MedicineInventoryService;
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.stereotype.Controller;
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 java.util.Map;

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

/**
 * 药品库存数量表 Controller
 *
 * @author 系统管理员
 */
@Tag(name = "管理后台 - 药品库存")
@RestController
@RequestMapping("/medicine/inventory")
@Validated
public class MedicineInventoryController {

    @Resource
    private MedicineInventoryService medicineInventoryService;

    @PostMapping("/create")
    @Operation(summary = "创建库存记录")
    public CommonResult<Long> createInventory(@Valid @RequestBody MedicineInventoryDO inventory) {
        Long id = medicineInventoryService.createInventory(inventory);
        return success(id);
    }

    @PutMapping("/update")
    @Operation(summary = "更新库存记录")
    @TenantIgnore
    public CommonResult<Boolean> updateInventory(@Valid @RequestBody MedicineInventoryDO inventory) {
        medicineInventoryService.updateInventory(inventory);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除库存记录")
    @Parameter(name = "id", description = "记录ID", required = true)
    @TenantIgnore
    public CommonResult<Boolean> deleteInventory(@RequestParam("id") Long id) {
        medicineInventoryService.deleteInventory(id);
        return success(true);
    }

    @PostMapping("/delete-batch")
    @Operation(summary = "批量删除库存记录")
    @TenantIgnore
    public CommonResult<Boolean> deleteInventories(@RequestBody Long[] ids) {
        medicineInventoryService.deleteInventories(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得库存记录详情")
    @Parameter(name = "id", description = "记录ID", required = true, example = "1024")
    @TenantIgnore
    public CommonResult<MedicineInventoryDO> getInventory(@RequestParam("id") Long id) {
        MedicineInventoryDO inventory = medicineInventoryService.getInventory(id);
        return success(inventory);
    }

    @GetMapping("/get-by-barcode")
    @Operation(summary = "根据药品标识码查询库存信息")
    @Parameter(name = "barcode", description = "药品标识码", required = true)
    public CommonResult<MedicineInventoryDO> getInventoryByBarcode(@RequestParam("barcode") String barcode) {
        MedicineInventoryDO inventory = medicineInventoryService.getInventoryByBarcode(barcode);
        return success(inventory);
    }

    @GetMapping("/list-by-batch-number")
    @Operation(summary = "根据批次号查询库存信息")
    public CommonResult<List<MedicineInventoryDO>> getInventoriesByBatchNumber(@RequestParam("batchNumber") String batchNumber) {
        List<MedicineInventoryDO> inventories = medicineInventoryService.getInventoriesByBatchNumber(batchNumber);
        return success(inventories);
    }

    @GetMapping("/list-by-status")
    @Operation(summary = "根据库存状态查询库存信息")
    @Parameter(name = "inventoryStatus", description = "库存状态：1-正常 2-近效期 3-短缺 4-积压 5-过期 6-停用", required = true)
    public CommonResult<List<MedicineInventoryDO>> getInventoriesByStatus(@RequestParam("inventoryStatus") Integer inventoryStatus) {
        List<MedicineInventoryDO> inventories = medicineInventoryService.getInventoriesByStatus(inventoryStatus);
        return success(inventories);
    }

    @GetMapping("/list-by-tenant")
    @Operation(summary = "根据机构ID查询库存信息")
    @TenantIgnore
    public CommonResult<JSONArray> getInventoriesByTenantId(@RequestParam("tenantId") Long tenantId) {
        return success(medicineInventoryService.getInventoriesByTenantId(tenantId));
    }

    @GetMapping("/list-by-drug-name")
    @Operation(summary = "根据药品名称模糊查询库存信息")
    @Parameter(name = "drugName", description = "药品名称", required = true)
    public CommonResult<List<MedicineInventoryDO>> getInventoriesByDrugName(@RequestParam("drugName") String drugName) {
        List<MedicineInventoryDO> inventories = medicineInventoryService.getInventoriesByDrugName(drugName);
        return success(inventories);
    }

    @GetMapping("/page")
    @Operation(summary = "获得药品库存分页")
    @TenantIgnore
    public CommonResult<PageResult<MedicineInventoryDO>> getInventoryPage(@Valid MedicineInventoryPageReqVO pageReqVO) {
        PageResult<MedicineInventoryDO> pageResult = medicineInventoryService.getInventoryPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/list")
    @Operation(summary = "获得药品库存列表")
    @TenantIgnore
    public CommonResult<List<MedicineInventoryDO>> getInventoryList(@Valid MedicineInventoryListReqVO reqVO) {
        List<MedicineInventoryDO> list = medicineInventoryService.getInventoryList(reqVO);
        return success(list);
    }

}

