package com.ruoyi.project.business.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
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.TpCaseElevator;
import com.ruoyi.project.business.domain.TpParaValidationElevator;
import com.ruoyi.project.business.domain.bo.validation.ComputeQueryBO;
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.enums.DelFlagEnum;
import com.ruoyi.project.business.service.ITpCaseElevatorService;
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 java.util.stream.Collectors;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
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) {
        List<Long> relatedModelIds = param.getRelatedModelIds();
        if (CollectionUtils.isNotEmpty(relatedModelIds)){
            param.setIds(iTpParaValidationElevatorService.list(Wrappers.<TpParaValidationElevator>lambdaQuery()
                            .in(TpParaValidationElevator::getElevatorModelId, relatedModelIds)
                            .eq(TpParaValidationElevator::getDelFlag, DelFlagEnum.NORMAL.getValue()))
                    .stream().map(TpParaValidationElevator::getParaValidationId).distinct()
                    .collect(Collectors.toList()));
            if (CollectionUtils.isEmpty(param.getIds())) {
                param.setIds(Lists.newArrayList(-1L));
            }
        }
        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(),param.getFormulaValueTypeMap(), 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);
    }

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

    @DeleteMapping("/delete")
    public AjaxResult delete(@RequestParam("id") Long id,
        @RequestParam(value = "updateTime", required = false) 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));
    }

    /**
     * 电梯参数验证
     *
     * @param queryBO
     * @return
     */
    @PostMapping("/compute/{id}")
    public AjaxResult compute(@PathVariable("id") Long id, @RequestBody ComputeQueryBO queryBO) {
        return AjaxResult.success(tpParaValidationService.computeElevatorParam(queryBO));
    }

}
