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

import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.ImportResultVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.MedicalRecordsRespVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.RecordsPageReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.MedicalRecordsDO;
import cn.iocoder.yudao.module.system.service.biz.MedicalRecordsService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

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

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import javax.validation.*;


import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

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


@Tag(name = "管理后台 - 医疗记录")
@RestController
@RequestMapping("/medical/records")
@Validated
public class MedicalRecordsController {

    @Resource
    private MedicalRecordsService recordsService;

    @PostMapping("/create")
    @Operation(summary = "创建医疗记录")
    @TenantIgnore
    public CommonResult<Boolean> createRecords(@Valid @RequestBody MedicalRecordsDO createReqVO) {
        recordsService.createRecords(createReqVO);
        return success(true);
    }

    @PutMapping("/update")
    @Operation(summary = "更新医疗记录")
    @TenantIgnore
    public CommonResult<Boolean> updateRecords(@Valid @RequestBody MedicalRecordsDO updateReqVO) {
        recordsService.updateRecords(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除医疗记录")
    @Parameter(name = "id", description = "编号", required = true)
    @TenantIgnore
    public CommonResult<Boolean> deleteRecords(@RequestParam("id") Integer id) {
        recordsService.deleteRecords(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得医疗记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @TenantIgnore
    public CommonResult<MedicalRecordsDO> getRecords(@RequestParam("id") Integer id) {
        MedicalRecordsDO records = recordsService.getRecords(id);
        return success(records);
    }

    @GetMapping("/page")
    @Operation(summary = "获得医疗记录分页")
    @TenantIgnore
    public CommonResult<PageResult<MedicalRecordsRespVO>> getRecordsPage(@Valid RecordsPageReqVO pageReqVO) {
        PageResult<MedicalRecordsRespVO> pageResult = recordsService.getRecordsPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/import-template")
    @Operation(summary = "下载医疗记录导入模板")
    @TenantIgnore
    public void importTemplate(HttpServletResponse response) throws IOException {
        recordsService.exportImportTemplate(response);
    }

    @PostMapping("/import")
    @Operation(summary = "导入医疗记录")
    @TenantIgnore
    public CommonResult<ImportResultVO> importCareRecords(
            @RequestParam("file") MultipartFile file) throws Exception {
        ImportResultVO result = recordsService.importMedicalRecords(file);
        return success(result);
    }
//    @GetMapping("/export-excel")
//    @Operation(summary = "导出医疗记录主 Excel")
//    @PreAuthorize("@ss.hasPermission('medical:records:export')")
//    @ApiAccessLog(operateType = EXPORT)
//    public void exportRecordsExcel(@Valid RecordsPageReqVO pageReqVO,
//              HttpServletResponse response) throws IOException {
//        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
//        List<RecordsDO> list = recordsService.getRecordsPage(pageReqVO).getList();
//        // 导出 Excel
//        ExcelUtils.write(response, "医疗记录主.xls", "数据", RecordsRespVO.class,
//                        BeanUtils.toBean(list, RecordsRespVO.class));
//    }

}