package com.gxwebsoft.clinic.controller;

import com.gxwebsoft.clinic.entity.ClinicMedicine;
import com.gxwebsoft.clinic.param.ClinicMedicineParam;
import com.gxwebsoft.clinic.service.ClinicMedicineService;
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")
public class ClinicMedicineController extends BaseController {
    @Resource
    private ClinicMedicineService clinicMedicineService;

    @Operation(summary = "分页查询药品库")
    @GetMapping("/page")
    public ApiResult<PageResult<ClinicMedicine>> page(ClinicMedicineParam param) {
        // 使用关联查询
        return success(clinicMedicineService.pageRel(param));
    }

    @Operation(summary = "查询全部药品库")
    @GetMapping()
    public ApiResult<List<ClinicMedicine>> list(ClinicMedicineParam param) {
        // 使用关联查询
        return success(clinicMedicineService.listRel(param));
    }

    @Operation(summary = "根据id查询药品库")
    @GetMapping("/{id}")
    public ApiResult<ClinicMedicine> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(clinicMedicineService.getByIdRel(id));
    }


    @Operation(summary = "添加药品库")
    @PostMapping()
    public ApiResult<?> save(@RequestBody ClinicMedicine clinicMedicine) {
        // 记录当前登录用户id
        // User loginUser = getLoginUser();
        // if (loginUser != null) {
        // clinicMedicine.setUserId(loginUser.getUserId());
        // }
        if (clinicMedicineService.save(clinicMedicine)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }


    @Operation(summary = "修改药品库")
    @PutMapping()
    public ApiResult<?> update(@RequestBody ClinicMedicine clinicMedicine) {
        if (clinicMedicineService.updateById(clinicMedicine)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }


    @Operation(summary = "删除药品库")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (clinicMedicineService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }


    @Operation(summary = "批量添加药品库")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<ClinicMedicine> list) {
        if (clinicMedicineService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }


    @Operation(summary = "批量修改药品库")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<ClinicMedicine> batchParam) {
        if (batchParam.update(clinicMedicineService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }


    @Operation(summary = "批量删除药品库")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (clinicMedicineService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
