package com.gxwebsoft.clinic.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.clinic.service.PrescriptionOrderItemService;
import com.gxwebsoft.clinic.entity.PrescriptionOrderItem;
import com.gxwebsoft.clinic.param.PrescriptionOrderItemParam;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.BatchParam;
import com.gxwebsoft.common.core.annotation.OperationLog;
import com.gxwebsoft.common.system.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 处方明细表
控制器
 *
 * @author LX
 * @since 2025-12-10 11:28:03
 */
@Api(tags = "处方明细表管理")
@RestController
@RequestMapping("/api/clinic/prescription-order-item")
public class PrescriptionOrderItemController extends BaseController {
    @Resource
    private PrescriptionOrderItemService prescriptionOrderItemService;

    @ApiOperation("分页查询处方明细表")
    @GetMapping("/page")
    public ApiResult<PageResult<PrescriptionOrderItem>> page(PrescriptionOrderItemParam param) {
        // 使用关联查询
        return success(prescriptionOrderItemService.pageRel(param));
    }

    @ApiOperation("查询全部处方明细表")
    @GetMapping()
    public ApiResult<List<PrescriptionOrderItem>> list(PrescriptionOrderItemParam param) {
        // 使用关联查询
        return success(prescriptionOrderItemService.listRel(param));
    }

    @ApiOperation("根据id查询处方明细表")
    @GetMapping("/{id}")
    public ApiResult<PrescriptionOrderItem> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(prescriptionOrderItemService.getByIdRel(id));
    }

    @ApiOperation("添加处方明细表")
    @PostMapping()
    public ApiResult<?> save(@RequestBody PrescriptionOrderItem prescriptionOrderItem) {
        if (prescriptionOrderItemService.save(prescriptionOrderItem)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @ApiOperation("修改处方明细表")
    @PutMapping()
    public ApiResult<?> update(@RequestBody PrescriptionOrderItem prescriptionOrderItem) {
        if (prescriptionOrderItemService.updateById(prescriptionOrderItem)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @ApiOperation("删除处方明细表")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (prescriptionOrderItemService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @ApiOperation("批量添加处方明细表")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<PrescriptionOrderItem> list) {
        if (prescriptionOrderItemService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @ApiOperation("批量修改处方明细表")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<PrescriptionOrderItem> batchParam) {
        if (batchParam.update(prescriptionOrderItemService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @ApiOperation("批量删除处方明细表")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (prescriptionOrderItemService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
