package com.ruoyi.project.business.controller;

import com.ruoyi.common.utils.jep.JEPUtils;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.business.domain.bo.validation.ElevatorQueryBO;
import com.ruoyi.project.business.dto.TpParaValidationDto;
import com.ruoyi.project.business.dto.TpParaValidationElevatorDto;
import com.ruoyi.project.business.service.ITpParaValidationElevatorService;
import com.ruoyi.project.business.service.ITpParaValidationService;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 校验规则配置Controller
 *
 * @author ruoyi
 * @date 2024-04-29
 */
@RestController
@RequestMapping("/business/validation")
@Slf4j
public class TpParaValidationController extends BaseController {
    @Resource
    private ITpParaValidationService tpParaValidationService;

    @Resource
    private ITpParaValidationElevatorService iTpParaValidationElevatorService;

    @GetMapping("/list")
    public TableDataInfo getTableList(TpParaValidationDto param) {
        startPage();
        List<TpParaValidationDto> list = tpParaValidationService.getTableList(param);
        return getDataTable(list);
    }

    @PostMapping("/testFormula")
    public AjaxResult testFormula(@RequestBody TpParaValidationDto param) {
        String str = param.getFormulaValueStr();
        log.info("前端公式传入:{}", str);
        Map<String, Object> result = JEPUtils.calculate(str, param.getFormulaValueMap(), true);
        return AjaxResult.success(result);
    }


    @PostMapping("saveOrUpdate")
    public AjaxResult saveOrUpdate(@RequestBody TpParaValidationDto param) {
        return AjaxResult.success(tpParaValidationService.saveOrUpdate(param));
    }

    @GetMapping("/getInfoById/{id}")
    public AjaxResult getInfoById(@PathVariable("id") Long id) {
        return AjaxResult.success(tpParaValidationService.getInfoById(id));
    }

    @GetMapping("/initRelation")
    public TableDataInfo initRelation(TpParaValidationDto param) {
        startPage();
        List<TpParaValidationElevatorDto> relationEnters = iTpParaValidationElevatorService.initData(param.getId());
        return getDataTable(relationEnters);
    }

    @DeleteMapping("/delete")
    public AjaxResult delete(@RequestParam("id") Long id, @RequestParam("updateTime") Date updateTime) {
        return AjaxResult.success(tpParaValidationService.deleteById(id, updateTime));
    }


    @PostMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody TpParaValidationDto param) {
        return AjaxResult.success(tpParaValidationService.changeStatus(param));
    }

    /**
     * 电梯参数验证
     *
     * @param queryBO
     * @return
     */
    @PostMapping("/elevator/check")
    public AjaxResult checkElevatorParam(@RequestBody ElevatorQueryBO queryBO) {
        return AjaxResult.success(tpParaValidationService.checkElevatorParam(queryBO));
    }

}
