package cn.iocoder.yudao.module.iot.controller.admin.formula;

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.iot.controller.admin.formula.vo.FormulaRecordPageReqVO;
import cn.iocoder.yudao.module.iot.controller.admin.formula.vo.FormulaRecordRespVO;
import cn.iocoder.yudao.module.iot.controller.admin.formula.vo.FormulaRecordSaveReqVO;
import cn.iocoder.yudao.module.iot.dal.dataobject.formula.FormulaRecordDO;
import cn.iocoder.yudao.module.iot.service.formula.FormulaRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
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("/iot/formula-record")
@Validated
public class FormulaRecordController {

    @Resource
    private FormulaRecordService formulaRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建变量记录")
    @PreAuthorize("@ss.hasPermission('iot:formula-record:create')")
    public CommonResult<Long> createFormulaRecord(@Valid @RequestBody FormulaRecordSaveReqVO createReqVO) {
        return success(formulaRecordService.createFormulaRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新变量记录")
    @PreAuthorize("@ss.hasPermission('iot:formula-record:update')")
    public CommonResult<Boolean> updateFormulaRecord(@Valid @RequestBody FormulaRecordSaveReqVO updateReqVO) {
        formulaRecordService.updateFormulaRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除变量记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('iot:formula-record:delete')")
    public CommonResult<Boolean> deleteFormulaRecord(@RequestParam("id") Long id) {
        formulaRecordService.deleteFormulaRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得变量记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('iot:formula-record:query')")
    public CommonResult<FormulaRecordRespVO> getFormulaRecord(@RequestParam("id") Long id) {
        FormulaRecordDO formulaRecord = formulaRecordService.getFormulaRecord(id);
        return success(BeanUtils.toBean(formulaRecord, FormulaRecordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得变量记录分页")
    @PreAuthorize("@ss.hasPermission('iot:formula-record:query')")
    public CommonResult<PageResult<FormulaRecordRespVO>> getFormulaRecordPage(@Valid FormulaRecordPageReqVO pageReqVO) {
        PageResult<FormulaRecordDO> pageResult = formulaRecordService.getFormulaRecordPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, FormulaRecordRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出变量记录 Excel")
    @PreAuthorize("@ss.hasPermission('iot:formula-record:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportFormulaRecordExcel(@Valid FormulaRecordPageReqVO pageReqVO,
                                         HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<FormulaRecordDO> list = formulaRecordService.getFormulaRecordPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "变量记录.xls", "数据", FormulaRecordRespVO.class,
                BeanUtils.toBean(list, FormulaRecordRespVO.class));
    }

}