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

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
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.elderlybloodsugarrecord.ElderlyBloodSugarRecordExportExcelVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.elderlybloodsugarrecord.ElderlyBloodSugarRecordPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.elderlybloodsugarrecord.ElderlyBloodSugarRecordRespVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.elderlybloodsugarrecord.ElderlyBloodSugarRecordSaveReqVO;
import cn.iocoder.yudao.module.system.service.biz.ElderlyBloodSugarRecordService;
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.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 长者血糖记录")
@RestController
@RequestMapping("/elderly-blood-sugar-record")
@Validated
public class ElderlyBloodSugarRecordController {

    @Resource
    private ElderlyBloodSugarRecordService elderlyBloodSugarRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建长者血糖记录")
    public CommonResult<Long> create(@Valid @RequestBody ElderlyBloodSugarRecordSaveReqVO createReqVO) {
        if (createReqVO.getTenantId() == null) {
            createReqVO.setTenantId(TenantContextHolder.getTenantId());
        }
        return success(elderlyBloodSugarRecordService.createElderlyBloodSugarRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新长者血糖记录")
    @TenantIgnore
    public CommonResult<Boolean> update(@Valid @RequestBody ElderlyBloodSugarRecordSaveReqVO updateReqVO) {
        if (updateReqVO.getTenantId() == null) {
            updateReqVO.setTenantId(TenantContextHolder.getTenantId());
        }
        elderlyBloodSugarRecordService.updateElderlyBloodSugarRecord(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得长者血糖记录详情")
    @Parameter(name = "id", description = "编号", required = true, example = "1")
    @TenantIgnore
    public CommonResult<ElderlyBloodSugarRecordRespVO> get(@RequestParam("id") Long id) {
        return success(elderlyBloodSugarRecordService.getElderlyBloodSugarRecord(id));
    }

    @GetMapping("/page")
    @Operation(summary = "获得长者血糖记录分页")
    @TenantIgnore
    public CommonResult<PageResult<ElderlyBloodSugarRecordRespVO>> page(@Valid ElderlyBloodSugarRecordPageReqVO pageReqVO) {
        if (pageReqVO.getTenantIds() == null) {
            pageReqVO.setTenantIds(new Long[]{TenantContextHolder.getTenantId()});
        }
        return success(elderlyBloodSugarRecordService.getElderlyBloodSugarRecordPage(pageReqVO));
    }

    @GetMapping("/exportExcel")
    @Operation(summary = "导出长者血糖记录 Excel")
    @ApiAccessLog(operateType = EXPORT)
    @TenantIgnore
    public void exportExcel(@Valid ElderlyBloodSugarRecordPageReqVO pageReqVO,
                            HttpServletResponse response) throws IOException {
        if (pageReqVO.getTenantIds() == null) {
            pageReqVO.setTenantIds(new Long[]{TenantContextHolder.getTenantId()});
        }
        List<ElderlyBloodSugarRecordRespVO> list = elderlyBloodSugarRecordService.getElderlyBloodSugarRecordList(pageReqVO);
        List<ElderlyBloodSugarRecordExportExcelVO> exportList = list.stream().map(vo -> {
            ElderlyBloodSugarRecordExportExcelVO excelVO = new ElderlyBloodSugarRecordExportExcelVO();
            excelVO.setId(vo.getId());
            excelVO.setElderName(vo.getElderName());
            excelVO.setRoomName(vo.getRoomName());
            excelVO.setBedName(vo.getBedName());
            excelVO.setElderSexDisplay(vo.getElderSex() == null ? "" : (vo.getElderSex() == 1 ? "男" : "女"));
            excelVO.setRecordDate(vo.getRecordDate());
            excelVO.setBloodSugar(vo.getBloodSugar());
            excelVO.setCreator(vo.getCreator());
            excelVO.setRemark(vo.getRemark());
            return excelVO;
        }).collect(Collectors.toList());
        ExcelUtils.write(response, "长者血糖记录.xls", "数据", ElderlyBloodSugarRecordExportExcelVO.class, exportList);
    }
}

