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

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.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.module.system.controller.admin.sys.vo.DrugDictionaryPageReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.sys.SysDrugDictionaryDO;
import cn.iocoder.yudao.module.system.service.sys.SysDrugDictionaryService;
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 org.springframework.validation.annotation.Validated;
import io.swagger.v3.oas.annotations.tags.Tag;

import javax.annotation.Resource;
import javax.validation.Valid;

import java.util.List;
import java.util.Objects;

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


@Tag(name = "管理后台 - 药品字典")
@RestController
@RequestMapping("/sys/drugDictionary")
@Validated
public class SysDrugDictionaryController {

    @Resource
    private SysDrugDictionaryService sysDrugDictionaryService;


    @PostMapping("/create")
    @Operation(summary = "创建药品字典")
    public CommonResult<Boolean> createDrugDictionary(@Valid @RequestBody SysDrugDictionaryDO sysDrugDictionaryDO) {
        sysDrugDictionaryService.createDrugDictionary(sysDrugDictionaryDO);
        return success(true);
    }

    @PutMapping("/update")
    @Operation(summary = "更新药品字典")
    public CommonResult<Boolean> updateDrugDictionary(@Valid @RequestBody SysDrugDictionaryDO sysDrugDictionaryDO) {
        sysDrugDictionaryService.updateDrugDictionary(sysDrugDictionaryDO);
        return success(true);
    }

    @PostMapping("/delete")
    @Operation(summary = "删除药品字典")
    public CommonResult<Boolean> deleteDrugDictionary(@RequestBody List<Long> ids) {
        sysDrugDictionaryService.deleteDrugDictionary(ids);
        return success(true);
    }

    @GetMapping("/getDrugDictionary")
    @Operation(summary = "获得药品字典详情")
    @Parameter(name = "drugId", description = "药品字典ID", required = true, example = "1024")
    public CommonResult<SysDrugDictionaryDO> getDrugDictionary(@RequestParam("drugId") Long drugId) {
        return success(sysDrugDictionaryService.getDrugDictionary(drugId));
    }

    @GetMapping("/getDrugDictionaryPage")
    @Operation(summary = "获得药品字典分页")
    @TenantIgnore
    public CommonResult<PageResult<SysDrugDictionaryDO>> getDrugDictionaryPage(@Valid DrugDictionaryPageReqVO pageReqVO) {
        pageReqVO.setTenantId(Objects.isNull(pageReqVO.getTenantId()) ? TenantContextHolder.getTenantId() : pageReqVO.getTenantId());
        PageResult<SysDrugDictionaryDO> pageResult = sysDrugDictionaryService.getDrugDictionaryPage(pageReqVO);
        return success(pageResult);
    }
//
//    @GetMapping("/export-excel")
//    @Operation(summary = "导出药品字典 Excel")
//    @PreAuthorize("@ss.hasPermission('sys:drug-dictionary:export')")
//    @ApiAccessLog(operateType = EXPORT)
//    public void exportDrugDictionaryExcel(@Valid DrugDictionaryPageReqVO pageReqVO,
//              HttpServletResponse response) throws OException {
//        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
//        List<DrugDictionaryDO> list = drugDictionaryService.getDrugDictionaryPage(pageReqVO).getList();
//        // 导出 Excel
//        ExcelUtils.write(response, "药品字典.xls", "数据", DrugDictionaryRespVO.class,
//                        BeanUtils.toBean(list, DrugDictionaryRespVO.class));
//    }

}