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.FormulaPageReqVO;
import cn.iocoder.yudao.module.iot.controller.admin.formula.vo.FormulaRespVO;
import cn.iocoder.yudao.module.iot.controller.admin.formula.vo.FormulaSaveReqVO;
import cn.iocoder.yudao.module.iot.dal.dataobject.formula.FormulaDO;
import cn.iocoder.yudao.module.iot.dal.dataobject.formula.FormulaDetailDO;
import cn.iocoder.yudao.module.iot.service.formula.FormulaService;
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")
@Validated
public class FormulaController {

    @Resource
    private FormulaService formulaService;

    @PostMapping("/create")
    @Operation(summary = "创建计算公式")
    @PreAuthorize("@ss.hasPermission('iot:formula:create')")
    public CommonResult<Long> createFormula(@Valid @RequestBody FormulaSaveReqVO createReqVO) {
        return success(formulaService.createFormula(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新计算公式")
    @PreAuthorize("@ss.hasPermission('iot:formula:update')")
    public CommonResult<Boolean> updateFormula(@Valid @RequestBody FormulaSaveReqVO updateReqVO) {
        formulaService.updateFormula(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除计算公式")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('iot:formula:delete')")
    public CommonResult<Boolean> deleteFormula(@RequestParam("id") Long id) {
        formulaService.deleteFormula(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得计算公式")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('iot:formula:query')")
    public CommonResult<FormulaRespVO> getFormula(@RequestParam("id") Long id) {
        FormulaDO formula = formulaService.getFormula(id);
        return success(BeanUtils.toBean(formula, FormulaRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得计算公式分页")
    @PreAuthorize("@ss.hasPermission('iot:formula:query')")
    public CommonResult<PageResult<FormulaRespVO>> getFormulaPage(@Valid FormulaPageReqVO pageReqVO) {
        PageResult<FormulaDO> pageResult = formulaService.getFormulaPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, FormulaRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出计算公式 Excel")
    @PreAuthorize("@ss.hasPermission('iot:formula:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportFormulaExcel(@Valid FormulaPageReqVO pageReqVO,
                                   HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<FormulaDO> list = formulaService.getFormulaPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "计算公式.xls", "数据", FormulaRespVO.class,
                BeanUtils.toBean(list, FormulaRespVO.class));
    }

    // ==================== 子表（计算公式明细） ====================

    @GetMapping("/formula-detail/page")
    @Operation(summary = "获得计算公式明细分页")
    @Parameter(name = "formulaId", description = "计算公式ID")
    @PreAuthorize("@ss.hasPermission('iot:formula:query')")
    public CommonResult<PageResult<FormulaDetailDO>> getFormulaDetailPage(PageParam pageReqVO,
                                                                          @RequestParam("formulaId") Long formulaId) {
        return success(formulaService.getFormulaDetailPage(pageReqVO, formulaId));
    }

    @PostMapping("/formula-detail/create")
    @Operation(summary = "创建计算公式明细")
    @PreAuthorize("@ss.hasPermission('iot:formula:create')")
    public CommonResult<Long> createFormulaDetail(@Valid @RequestBody FormulaDetailDO formulaDetail) {
        return success(formulaService.createFormulaDetail(formulaDetail));
    }

    @PutMapping("/formula-detail/update")
    @Operation(summary = "更新计算公式明细")
    @PreAuthorize("@ss.hasPermission('iot:formula:update')")
    public CommonResult<Boolean> updateFormulaDetail(@Valid @RequestBody FormulaDetailDO formulaDetail) {
        formulaService.updateFormulaDetail(formulaDetail);
        return success(true);
    }

    @DeleteMapping("/formula-detail/delete")
    @Parameter(name = "id", description = "编号", required = true)
    @Operation(summary = "删除计算公式明细")
    @PreAuthorize("@ss.hasPermission('iot:formula:delete')")
    public CommonResult<Boolean> deleteFormulaDetail(@RequestParam("id") Long id) {
        formulaService.deleteFormulaDetail(id);
        return success(true);
    }

    @GetMapping("/formula-detail/get")
    @Operation(summary = "获得计算公式明细")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('iot:formula:query')")
    public CommonResult<FormulaDetailDO> getFormulaDetail(@RequestParam("id") Long id) {
        return success(formulaService.getFormulaDetail(id));
    }

}