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.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.ElderlyMedicationExecutionRespVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.MedicationExecutionPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.MedicationExecutionRecordResVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.MedicationExecutionDO;
import cn.iocoder.yudao.module.system.service.biz.MedicationExecutionService;
import io.swagger.v3.oas.annotations.Operation;
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.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - 长者用药执行记录")
@RestController
@RequestMapping("/elderly/medicationExecution")
@Validated
public class MedicationExecutionController {

    @Resource
    private MedicationExecutionService medicationExecutionService;


    @PostMapping("/createMedicationExecution")
    @Operation(summary = "创建长者用药执行记录")
    public CommonResult<Boolean> createMedicationExecution(@Valid @RequestBody MedicationExecutionDO createReqVO) {
        medicationExecutionService.createMedicationExecution(createReqVO);
        return success(true);
    }


    @GetMapping("/getMedicationExecutionRecords")
    @Operation(summary = "查询执行用药记录")
    public CommonResult<MedicationExecutionRecordResVO> getMedicationExecutionRecords(@RequestParam("elderId") Long elderId, @RequestParam("medicationId") Long medicationId) {
        return success(medicationExecutionService.getMedicationExecutionRecords(elderId, medicationId));
    }


    @GetMapping("/getMedicationExecutionPage")
    @Operation(summary = "详情-获取用药记录分页")
    public CommonResult<PageResult<MedicationExecutionDO>> getMedicationExecutionPage(@Valid MedicationExecutionPageReqVO pageReqVO) {
        return success(medicationExecutionService.getMedicationExecutionPage(pageReqVO));
    }


    @GetMapping("/page")
    @Operation(summary = "获得长者用药执行分页")
    @TenantIgnore
    public CommonResult<PageResult<ElderlyMedicationExecutionRespVO>> getElderlyMedicationExecutionPage(@Valid MedicationExecutionPageReqVO pageReqVO) {
        pageReqVO.setTenantId(Objects.isNull(pageReqVO.getTenantId()) ? TenantContextHolder.getTenantId() : pageReqVO.getTenantId());
        PageResult<ElderlyMedicationExecutionRespVO> pageResult = medicationExecutionService.getElderlyMedicationExecutionPage(pageReqVO);
        return success(pageResult);
    }


    @GetMapping("/changeStatus")
    @Operation(summary = "确认用药")
    public CommonResult<Boolean> changeStatus(@RequestParam("elderId") Long elderId) {
        medicationExecutionService.changeStatus(elderId);
        return success(true);
    }


    @GetMapping("/exportMedicationExecutionExcel")
    @Operation(summary = "导出长者用药执行记录 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMedicationExecutionExcel(@Valid MedicationExecutionPageReqVO pageReqVO, HttpServletResponse response) throws IOException {
//        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MedicationExecutionDO> list = medicationExecutionService.getMedicationExecutionPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "长者用药执行记录.xls", "数据", MedicationExecutionDO.class, list);
    }

}