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.ElderlyNursingLogListReqVO;
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.ElderlyNursingLogSimpleRespVO;
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.format.annotation.DateTimeFormat;
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.time.LocalDate;
import java.util.List;
import java.util.Objects;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY;

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

    @Resource
    private ElderlyNursingLogService nursingLogService;

    @PostMapping("/create")
    @Operation(summary = "创建长者护理记录")
    @PermitAll
    @TenantIgnore
    public CommonResult<Boolean> create(@Valid @RequestBody List<ElderlyNursingLogSaveReqVO> createReqVO) {
        return success(nursingLogService.create(createReqVO));
    }

    @PostMapping("/executeNursingLog")
    @Operation(summary = "执行护理记录")
    @PermitAll
    @TenantIgnore
    public CommonResult<Boolean> executeNursingLog(@Valid @RequestBody ElderlyNursingLogSaveReqVO updateReqVO) {
        updateReqVO.setTenantId(Objects.isNull(updateReqVO.getTenantId())
                ? TenantContextHolder.getTenantId() : updateReqVO.getTenantId());
        nursingLogService.executeNursingLog(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<ElderlyNursingLogSimpleRespVO>> getPage(@Valid ElderlyNursingLogPageReqVO pageReqVO) {
        pageReqVO.setTenantIds(Objects.isNull(pageReqVO.getTenantIds())
                ? new Long[]{TenantContextHolder.getTenantId()} : pageReqVO.getTenantIds());
        return success(nursingLogService.getPage(pageReqVO));
    }

    @GetMapping("/list")
    @Operation(summary = "获取长者护理记录列表")
    @TenantIgnore
    @PermitAll
    public CommonResult<List<ElderlyNursingLogRespVO>> getList(@Valid ElderlyNursingLogListReqVO listReqVO) {
        return success(nursingLogService.getList(listReqVO));
    }

    @GetMapping("/detail")
    @Operation(summary = "获得长者护理记录详情")
    @Parameter(name = "elderId", description = "长者编号", required = true, example = "1")
    @Parameter(name = "start", description = "开始日期", required = true, example = "2026-01-02")
    @Parameter(name = "end", description = "结束日期", required = true, example = "2026-01-02")
    @Parameter(name = "pageNo", description = "页码，从 1 开始", required = true, example = "1")
    @Parameter(name = "pageSize", description = "每页条数", required = true, example = "10")
    @PermitAll
    public CommonResult<ElderlyNursingLogDetailRespVO> getDetail(@RequestParam("elderId") Long elderId,
                                                                 @RequestParam("start")
                                                                 @DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY) LocalDate start,
                                                                 @RequestParam("end")
                                                                 @DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY) LocalDate end,
                                                                 @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                                                                 @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        return success(nursingLogService.getDetailByElderId(elderId, start, end, pageNo, pageSize));
    }
}
