package com.gxwebsoft.clinic.controller;

import com.gxwebsoft.clinic.entity.ClinicMedicineInout;
import com.gxwebsoft.clinic.param.ClinicMedicineInoutParam;
import com.gxwebsoft.clinic.service.ClinicMedicineInoutService;
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-inout")
public class ClinicMedicineInoutController extends BaseController {
    @Resource
    private ClinicMedicineInoutService clinicMedicineInoutService;

    @PreAuthorize("hasAuthority('clinic:clinicMedicineInout:list')")
    @Operation(summary = "分页查询出入库")
    @GetMapping("/page")
    public ApiResult<PageResult<ClinicMedicineInout>> page(ClinicMedicineInoutParam param) {
        // 使用关联查询
        return success(clinicMedicineInoutService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('clinic:clinicMedicineInout:list')")
    @Operation(summary = "查询全部出入库")
    @GetMapping()
    public ApiResult<List<ClinicMedicineInout>> list(ClinicMedicineInoutParam param) {
        // 使用关联查询
        return success(clinicMedicineInoutService.listRel(param));
    }

    @PreAuthorize("hasAuthority('clinic:clinicMedicineInout:list')")
    @Operation(summary = "根据id查询出入库")
    @GetMapping("/{id}")
    public ApiResult<ClinicMedicineInout> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(clinicMedicineInoutService.getByIdRel(id));
    }

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

    @PreAuthorize("hasAuthority('clinic:clinicMedicineInout:update')")
    
    @Operation(summary = "修改出入库")
    @PutMapping()
    public ApiResult<?> update(@RequestBody ClinicMedicineInout clinicMedicineInout) {
        if (clinicMedicineInoutService.updateById(clinicMedicineInout)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('clinic:clinicMedicineInout:remove')")
    
    @Operation(summary = "删除出入库")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (clinicMedicineInoutService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('clinic:clinicMedicineInout:save')")
    
    @Operation(summary = "批量添加出入库")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<ClinicMedicineInout> list) {
        if (clinicMedicineInoutService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('clinic:clinicMedicineInout:update')")
    
    @Operation(summary = "批量修改出入库")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<ClinicMedicineInout> batchParam) {
        if (batchParam.update(clinicMedicineInoutService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

}
