package com.gxwebsoft.clinic.controller;

import com.gxwebsoft.clinic.entity.ClinicPrescriptionItem;
import com.gxwebsoft.clinic.param.ClinicPrescriptionItemParam;
import com.gxwebsoft.clinic.service.ClinicPrescriptionItemService;
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:01:13
 */
@Tag(name = "处方明细表管理")
@RestController
@RequestMapping("/api/clinic/clinic-prescription-item")
public class ClinicPrescriptionItemController extends BaseController {
    @Resource
    private ClinicPrescriptionItemService clinicPrescriptionItemService;

    @Operation(summary = "分页查询处方明细表")
    @GetMapping("/page")
    public ApiResult<PageResult<ClinicPrescriptionItem>> page(ClinicPrescriptionItemParam param) {
        // 使用关联查询
        return success(clinicPrescriptionItemService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('clinic:clinicPrescription:list')")
    @Operation(summary = "查询全部处方明细表")
    @GetMapping()
    public ApiResult<List<ClinicPrescriptionItem>> list(ClinicPrescriptionItemParam param) {
        // 使用关联查询
        return success(clinicPrescriptionItemService.listRel(param));
    }

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

    @PreAuthorize("hasAuthority('clinic:clinicPrescription:save')")
    
    @Operation(summary = "添加处方明细表")
    @PostMapping()
    public ApiResult<?> save(@RequestBody ClinicPrescriptionItem clinicPrescriptionItem) {
        if (clinicPrescriptionItemService.save(clinicPrescriptionItem)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('clinic:clinicPrescription:update')")
    
    @Operation(summary = "修改处方明细表")
    @PutMapping()
    public ApiResult<?> update(@RequestBody ClinicPrescriptionItem clinicPrescriptionItem) {
        if (clinicPrescriptionItemService.updateById(clinicPrescriptionItem)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('clinic:clinicPrescription:remove')")
    
    @Operation(summary = "删除处方明细表")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (clinicPrescriptionItemService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('clinic:clinicPrescription:save')")
    
    @Operation(summary = "批量添加处方明细表")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<ClinicPrescriptionItem> list) {
        if (clinicPrescriptionItemService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('clinic:clinicPrescription:update')")
    
    @Operation(summary = "批量修改处方明细表")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<ClinicPrescriptionItem> batchParam) {
        if (batchParam.update(clinicPrescriptionItemService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('clinic:clinicPrescription:remove')")
    
    @Operation(summary = "批量删除处方明细表")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (clinicPrescriptionItemService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
