package cn.iocoder.yudao.module.system.controller.admin.elderly;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.module.system.controller.admin.elderly.vo.ElderlyNursingLogDetailRespVO;
import cn.iocoder.yudao.module.system.controller.admin.elderly.vo.ElderlyNursingLogPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.elderly.vo.ElderlyNursingLogRespVO;
import cn.iocoder.yudao.module.system.controller.admin.elderly.vo.ElderlyNursingLogSaveReqVO;
import cn.iocoder.yudao.module.system.service.elderly.ElderlyNursingLogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.validation.Valid;
import java.util.Objects;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 长者护理记录")
@RestController
@RequestMapping("/elderly-nursing-log")
@Validated
public class ElderlyNursingLogController {

    @Resource
    private ElderlyNursingLogService nursingLogService;

    @PostMapping("/create")
    @Operation(summary = "创建长者护理记录")
    @PermitAll
    @TenantIgnore
    public CommonResult<Long> create(@Valid @RequestBody ElderlyNursingLogSaveReqVO createReqVO) {
        createReqVO.setTenantId(Objects.isNull(createReqVO.getTenantId())
                ? TenantContextHolder.getTenantId() : createReqVO.getTenantId());
        return success(nursingLogService.create(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新长者护理记录")
    public CommonResult<Boolean> update(@Valid @RequestBody ElderlyNursingLogSaveReqVO updateReqVO) {
        updateReqVO.setTenantId(Objects.isNull(updateReqVO.getTenantId())
                ? TenantContextHolder.getTenantId() : updateReqVO.getTenantId());
        nursingLogService.update(updateReqVO);
        return success(Boolean.TRUE);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除长者护理记录")
    @Parameter(name = "id", description = "记录编号", required = true)
    public CommonResult<Boolean> delete(@RequestParam("id") Long id) {
        nursingLogService.delete(id);
        return success(Boolean.TRUE);
    }

    @GetMapping("/get")
    @Operation(summary = "获得长者护理记录")
    @Parameter(name = "id", description = "记录编号", required = true, example = "1024")
    public CommonResult<ElderlyNursingLogRespVO> get(@RequestParam("id") Long id) {
        return success(nursingLogService.get(id));
    }

    @GetMapping("/page")
    @Operation(summary = "获得长者护理记录分页")
    public CommonResult<PageResult<ElderlyNursingLogRespVO>> getPage(@Valid ElderlyNursingLogPageReqVO pageReqVO) {
        pageReqVO.setTenantIds(Objects.isNull(pageReqVO.getTenantIds())
                ? new Long[]{TenantContextHolder.getTenantId()} : pageReqVO.getTenantIds());
        return success(nursingLogService.getPage(pageReqVO));
    }

    @GetMapping("/detail")
    @Operation(summary = "获得长者护理记录详情")
    @Parameter(name = "elderId", description = "长者编号", required = true, example = "1")
    public CommonResult<ElderlyNursingLogDetailRespVO> getDetail(@RequestParam("elderId") Long elderId) {
        return success(nursingLogService.getDetailByElderId(elderId));
    }
}
