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.NutritionRecordPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.NutritionRecordRespVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.NutritionRecordDO;
import cn.iocoder.yudao.module.system.service.biz.NutritionRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
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.*;


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("/nutrition/record")
@Validated
public class NutritionRecordController {

    @Autowired
    private NutritionRecordService recordService;

    @PostMapping("/create")
    @Operation(summary = "创建营养记录")
    @TenantIgnore
    public CommonResult<Boolean> createRecord( @RequestBody NutritionRecordDO createReqVO) {
        recordService.createRecord(createReqVO);
        return success(true);
    }

    @PutMapping("/update")
    @Operation(summary = "更新营养记录")
    @TenantIgnore
    public CommonResult<Boolean> updateRecord(@RequestBody NutritionRecordDO updateReqVO) {
        recordService.updateRecord(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得营养记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @TenantIgnore
    public CommonResult<NutritionRecordDO> getRecord(@RequestParam("id") Integer id) {
        NutritionRecordDO record = recordService.getRecord(id);
        return success(record);
    }

    @GetMapping("/page")
    @Operation(summary = "获得营养记录分页")
    @TenantIgnore
    public CommonResult<PageResult<NutritionRecordRespVO>> getRecordPage(@Valid NutritionRecordPageReqVO pageReqVO) {
        PageResult<NutritionRecordRespVO> pageResult = recordService.getRecordPage(pageReqVO);
        return success(pageResult);
    }

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

    @PostMapping("/import")
    @Operation(summary = "导入营养记录")
    @TenantIgnore
    public CommonResult<ImportResultVO> importCareRecords(
            @RequestParam("file") MultipartFile file) throws Exception {
        ImportResultVO result = recordService.importNutritionRecord(file);
        return success(result);
    }
//    @GetMapping("/export-excel")
//    @Operation(summary = "导出营养记录 Excel")
//    @PreAuthorize("@ss.hasPermission('nutrition:record:export')")
//    @ApiAccessLog(operateType = EXPORT)
//    public void exportRecordExcel(@Valid RecordPageReqVO pageReqVO,
//              HttpServletResponse response) throws IOException {
//        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
//        List<RecordDO> list = recordService.getRecordPage(pageReqVO).getList();
//        // 导出 Excel
//        ExcelUtils.write(response, "营养记录.xls", "数据", RecordRespVO.class,
//                        BeanUtils.toBean(list, RecordRespVO.class));
//    }

}