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.PageParam;
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.module.system.controller.admin.biz.vo.MedicationPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.MedicationRespVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.MedicationSaveReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.excel.MedicationExcelVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.MedicationDO;
import cn.iocoder.yudao.module.system.service.biz.MedicationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.security.access.prepost.PreAuthorize;
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 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/medication")
@Validated
public class MedicationController {

    @Resource
    private MedicationService medicationService;

    @PostMapping("/createMedication")
    @Operation(summary = "创建长者用药")
    public CommonResult<Boolean> createMedication(@Valid @RequestBody MedicationSaveReqVO saveReqVO) {
        medicationService.createMedication(saveReqVO);
        return success(true);
    }

    @DeleteMapping("/deleteMedication")
    @Operation(summary = "删除长者用药")
    @Parameter(name = "id", description = "用药记录ID", required = true)
    public CommonResult<Boolean> deleteMedication(@RequestParam("id") Long id) {
        medicationService.deleteMedication(id);
        return success(true);
    }

    @GetMapping("/getMedication")
    @Operation(summary = "获得长者用药详情")
    @Parameter(name = "elderId", description = "长者ID", required = true, example = "1024")
    public CommonResult<MedicationRespVO> getMedication(@RequestParam("elderId") Long elderId) {
        MedicationRespVO medication = medicationService.getMedication(elderId);
        return success(medication);
    }


    @PostMapping("/updateUsingMedical")
    @Operation(summary = "停用或者启用长者用药")
    public CommonResult<Boolean> updateUsingMedical(@RequestBody MedicationDO medicationDO) {
        medicationService.updateUsingMedical(medicationDO);
        return success(true);
    }


    @GetMapping("/getMedicationPage")
    @Operation(summary = "获得长者用药分页")
    public CommonResult<PageResult<MedicationDO>> getMedicationPage(@Valid MedicationPageReqVO pageReqVO) {
        PageResult<MedicationDO> pageResult = medicationService.getMedicationPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/exportMedicationExcel")
    @Operation(summary = "导出长者用药 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMedicationExcel(@Valid MedicationPageReqVO pageReqVO,
                                      HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MedicationExcelVO> medicationExcel = medicationService.exportMedicationExcel(pageReqVO);
        // 导出 Excel
        ExcelUtils.write(response, "长者用药记录.xls", "数据", MedicationExcelVO.class, medicationExcel);
    }

}