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.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.ByodRegistrationDrugRespVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.ByodRegistrationPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.ByodRegistrationRespVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.ByodRegistrationSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ByodRegistrationDrugDO;
import cn.iocoder.yudao.module.system.service.biz.ByodRegistrationService;
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.Map;
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/byodRgistration")
@Validated
public class ByodRegistrationController {

    @Resource
    private ByodRegistrationService byodRegistrationService;

    @PostMapping("/createByodRegistration")
    @Operation(summary = "创建长者自带药登记")
    public CommonResult<Long> createByodRegistration(@Valid @RequestBody ByodRegistrationSaveReqVO createReqVO) {
        return success(byodRegistrationService.createByodRegistration(createReqVO));
    }

    @PutMapping("/updateByodRegistration")
    @Operation(summary = "更新长者自带药登记")
    public CommonResult<Boolean> updateByodRegistration(@Valid @RequestBody ByodRegistrationSaveReqVO updateReqVO) {
        byodRegistrationService.updateByodRegistration(updateReqVO);
        return success(true);
    }

    @PostMapping("/deleteByodRegistration")
    @Operation(summary = "删除长者自带药登记")
    public CommonResult<Boolean> deleteByodRegistration(@RequestBody List<Long> ids) {
        byodRegistrationService.deleteByodRegistration(ids);
        return success(true);
    }

    @GetMapping("/getByodRegistration")
    @Operation(summary = "获得长者自带药登记详情")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<ByodRegistrationRespVO> getByodRegistration(@RequestParam("id") Long id) {
        ByodRegistrationRespVO byodRegistration = byodRegistrationService.getByodRegistration(id);
        return success(byodRegistration);
    }

    @GetMapping("/getByodRegistrationPage")
    @Operation(summary = "获得长者自带药登记分页")
    @TenantIgnore
    public CommonResult<PageResult<ByodRegistrationRespVO>> getByodRegistrationPage(@Valid ByodRegistrationPageReqVO pageReqVO) {
        pageReqVO.setTenantId(Objects.isNull(pageReqVO.getTenantId()) ? TenantContextHolder.getTenantId() : pageReqVO.getTenantId());
        PageResult<ByodRegistrationRespVO> pageResult = byodRegistrationService.getByodRegistrationPage(pageReqVO);
        return success(pageResult);
    }

    @PostMapping("/validDrugs")
    @Operation(summary = "校验自带药是否存在药品字典中")
    public CommonResult<Map<String, Object>> validDrugs(@RequestBody List<ByodRegistrationDrugDO> drugList) {
        return success(byodRegistrationService.validDrugs(drugList));
    }


    @GetMapping("/getByodRegistrationDrugPage")
    @Operation(summary = "自带药台账分页")
    @TenantIgnore
    public CommonResult<PageResult<ByodRegistrationDrugRespVO>> getByodRegistrationDrugPage(@Valid ByodRegistrationPageReqVO pageReqVO) {
        pageReqVO.setTenantId(Objects.isNull(pageReqVO.getTenantId()) ? TenantContextHolder.getTenantId() : pageReqVO.getTenantId());
        return success(byodRegistrationService.getByodRegistrationDrugPage(pageReqVO));
    }


    @GetMapping("/exportByodRegistrationDrugExcel")
    @Operation(summary = "导出长者自带药台账Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportByodRegistrationDrugExcel(@Valid ByodRegistrationPageReqVO pageReqVO,
                                                HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ByodRegistrationDrugRespVO> list = byodRegistrationService.getByodRegistrationDrugPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "长者自带药台账.xls", "数据", ByodRegistrationDrugRespVO.class, list);
    }


    @GetMapping("/getByodRegistrationDrugList")
    @Operation(summary = "根据长者ID查询长者自带药")
    public CommonResult<List<ByodRegistrationDrugDO>> getByodRegistrationDrugList(@RequestParam("elderId") Long elderId ){
        return success(byodRegistrationService.getByodRegistrationDrugList(elderId));
    }

}