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.context.TenantContextHolder;
import cn.iocoder.yudao.module.system.controller.admin.elderly.vo.ElderlySafeCheckElderPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.elderly.vo.ElderlySafeCheckElderPageRespVO;
import cn.iocoder.yudao.module.system.controller.admin.elderly.vo.ElderlySafeCheckPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.elderly.vo.ElderlySafeCheckRespVO;
import cn.iocoder.yudao.module.system.controller.admin.elderly.vo.ElderlySafeCheckSaveReqVO;
import cn.iocoder.yudao.module.system.service.elderly.ElderlySafeCheckService;
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.List;
import java.util.Objects;

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

@Tag(name = "管理后台 - 长者安全检查")
@RestController
@RequestMapping("/elderly-safe-check")
@Validated
public class ElderlySafeCheckController {

    @Resource
    private ElderlySafeCheckService safeCheckService;

    @PostMapping("/create")
    @Operation(summary = "创建长者安全检查")
    @PermitAll
    public CommonResult<Integer> create(@Valid @RequestBody List<ElderlySafeCheckSaveReqVO> createReqList) {
        for (ElderlySafeCheckSaveReqVO createReqVO : createReqList) {
            createReqVO.setTenantId(Objects.isNull(createReqVO.getTenantId())
                    ? TenantContextHolder.getTenantId() : createReqVO.getTenantId());
        }
        return success(safeCheckService.createBatch(createReqList));
    }

    @PutMapping("/update")
    @Operation(summary = "更新长者安全检查")
    public CommonResult<Boolean> update(@Valid @RequestBody ElderlySafeCheckSaveReqVO updateReqVO) {
        updateReqVO.setTenantId(Objects.isNull(updateReqVO.getTenantId())
                ? TenantContextHolder.getTenantId() : updateReqVO.getTenantId());
        safeCheckService.update(updateReqVO);
        return success(Boolean.TRUE);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得长者安全检查")
    @Parameter(name = "id", description = "记录编号", required = true, example = "1024")
    public CommonResult<ElderlySafeCheckRespVO> get(@RequestParam("id") Long id) {
        return success(safeCheckService.get(id));
    }

    @GetMapping("/page")
    @Operation(summary = "获得长者安全检查分页（按长者分组）")
    public CommonResult<PageResult<ElderlySafeCheckElderPageRespVO>> getElderPage(@Valid ElderlySafeCheckElderPageReqVO pageReqVO) {
        pageReqVO.setTenantIds(Objects.isNull(pageReqVO.getTenantIds())
                ? new Long[]{TenantContextHolder.getTenantId()} : pageReqVO.getTenantIds());
        return success(safeCheckService.getElderPage(pageReqVO));
    }

    @GetMapping("/page-record")
    @Operation(summary = "获得长者安全检查记录分页")
    public CommonResult<PageResult<ElderlySafeCheckRespVO>> getPage(@Valid ElderlySafeCheckPageReqVO pageReqVO) {
        pageReqVO.setTenantIds(Objects.isNull(pageReqVO.getTenantIds())
                ? new Long[]{TenantContextHolder.getTenantId()} : pageReqVO.getTenantIds());
        return success(safeCheckService.getPage(pageReqVO));
    }
}
