package com.gxwebsoft.clinic.controller;

import com.gxwebsoft.clinic.entity.ClinicMedicineStock;
import com.gxwebsoft.clinic.param.ClinicMedicineStockParam;
import com.gxwebsoft.clinic.service.ClinicMedicineStockService;
import com.gxwebsoft.common.core.annotation.OperationLog;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.core.web.BatchParam;
import com.gxwebsoft.common.core.web.PageResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 药品库存控制器
 *
 * @author 科技小王子
 * @since 2025-10-22 02:06:32
 */
@Tag(name = "药品库存管理")
@RestController
@RequestMapping("/api/clinic/clinic-medicine-stock")
public class ClinicMedicineStockController extends BaseController {
    @Resource
    private ClinicMedicineStockService clinicMedicineStockService;

    @PreAuthorize("hasAuthority('clinic:clinicMedicineStock:list')")
    @Operation(summary = "分页查询药品库存")
    @GetMapping("/page")
    public ApiResult<PageResult<ClinicMedicineStock>> page(ClinicMedicineStockParam param) {
        // 使用关联查询
        return success(clinicMedicineStockService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('clinic:clinicMedicineStock:list')")
    @Operation(summary = "查询全部药品库存")
    @GetMapping()
    public ApiResult<List<ClinicMedicineStock>> list(ClinicMedicineStockParam param) {
        // 使用关联查询
        return success(clinicMedicineStockService.listRel(param));
    }

    @PreAuthorize("hasAuthority('clinic:clinicMedicineStock:list')")
    @Operation(summary = "根据id查询药品库存")
    @GetMapping("/{id}")
    public ApiResult<ClinicMedicineStock> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(clinicMedicineStockService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('clinic:clinicMedicineStock:save')")
    @OperationLog
    @Operation(summary = "添加药品库存")
    @PostMapping()
    public ApiResult<?> save(@RequestBody ClinicMedicineStock clinicMedicineStock) {
        // 记录当前登录用户id
        // User loginUser = getLoginUser();
        // if (loginUser != null) {
        // clinicMedicineStock.setUserId(loginUser.getUserId());
        // }
        if (clinicMedicineStockService.save(clinicMedicineStock)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('clinic:clinicMedicineStock:update')")
    @OperationLog
    @Operation(summary = "修改药品库存")
    @PutMapping()
    public ApiResult<?> update(@RequestBody ClinicMedicineStock clinicMedicineStock) {
        if (clinicMedicineStockService.updateById(clinicMedicineStock)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('clinic:clinicMedicineStock:remove')")
    @OperationLog
    @Operation(summary = "删除药品库存")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (clinicMedicineStockService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('clinic:clinicMedicineStock:save')")
    @OperationLog
    @Operation(summary = "批量添加药品库存")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<ClinicMedicineStock> list) {
        if (clinicMedicineStockService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('clinic:clinicMedicineStock:update')")
    @OperationLog
    @Operation(summary = "批量修改药品库存")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<ClinicMedicineStock> batchParam) {
        if (batchParam.update(clinicMedicineStockService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('clinic:clinicMedicineStock:remove')")
    @OperationLog
    @Operation(summary = "批量删除药品库存")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (clinicMedicineStockService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
