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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
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.biz.vo.ElderlyVitalSignsRespVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.VitalSignsPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.VitalSignsSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyVitalSignsDO;
import cn.iocoder.yudao.module.system.service.biz.ElderlyVitalSignsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.validation.annotation.Validated;
import io.swagger.v3.oas.annotations.tags.Tag;

import java.io.IOException;
import java.util.List;
import java.util.Objects;

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

@Tag(name = "管理后台 - 生命体征")
@RestController
@RequestMapping("/elderly/vitalSigns")
@Validated
public class ElderlyVitalSignsController {

    @Resource
    private ElderlyVitalSignsService vitalSignsService;

    @PostMapping("/create")
    @Operation(summary = "创建生命体征")
    @TenantIgnore
    public CommonResult<Boolean> createVitalSigns(@Valid @RequestBody VitalSignsSaveReqVO createReqVO) {
        vitalSignsService.createVitalSigns(createReqVO);
        return success(true);
    }

    @PostMapping("/delete")
    @Operation(summary = "删除生命体征")
    public CommonResult<Boolean> deleteVitalSigns(@RequestBody Long[] ids) {
        vitalSignsService.deleteVitalSigns(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得生命体征")
    @Parameter(name = "id", description = "生命体征ID", required = true, example = "1024")
    public CommonResult<ElderlyVitalSignsDO> getVitalSigns(@RequestParam("id") Long id) {
        return success(vitalSignsService.getVitalSigns(id));
    }

    @GetMapping("/page")
    @Operation(summary = "获得长者生命体征分页")
    @TenantIgnore
    public CommonResult<PageResult<ElderlyVitalSignsDO>> getVitalSignsPage(@Valid VitalSignsPageReqVO pageReqVO) {
        pageReqVO.setTenantIds(Objects.isNull(pageReqVO.getTenantIds()) ? new Long[]{TenantContextHolder.getTenantId()} : pageReqVO.getTenantIds());
        if (!pageReqVO.isPageFlag()) {
            pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        }
        PageResult<ElderlyVitalSignsDO> pageResult = vitalSignsService.getVitalSignsPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/exportVitalSignsExcel")
    @Operation(summary = "导出生命体征 Excel")
    public void exportVitalSignsExcel(@Valid VitalSignsPageReqVO pageReqVO,
                                      HttpServletResponse response) throws IOException {
        if (!pageReqVO.isPageFlag()) {
            pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        }
        List<ElderlyVitalSignsRespVO> list = vitalSignsService.exportVitalSignsExcel(pageReqVO);
        // 导出 Excel
        ExcelUtils.write(response, "长者生命体征.xls", "数据", ElderlyVitalSignsRespVO.class,
                list);
    }

}