package com.muyu.rule.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.muyu.common.core.domain.Result;

import com.muyu.rule.common.domain.RuleEngine;
import com.muyu.rule.common.domain.RuleRegion;
import com.muyu.rule.common.domain.req.EtlRuleAddReq;
import com.muyu.rule.common.domain.req.EtlRuleListReq;
import com.muyu.rule.common.domain.req.EtlRuleUpdReq;
import com.muyu.rule.common.domain.resp.EtlRuleResp;
import com.muyu.rule.server.service.EtlRuleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author：张承志
 * @Package：com.muyu.etl.controller
 * @Project：cloud-etl
 * @name：EtlRuleController
 * @Date：2024/8/21 14:17
 */
@Log4j2
@RestController
@RequestMapping("/rule")
@RequiredArgsConstructor
@Tag(name = "etl规则控制层", description = "进行etl规则管理,查看等相关操作")
public class EtlRuleController {

    @Autowired
    private EtlRuleService etlRuleService;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @GetMapping("/{id}")
    @Operation(summary = "规则引擎的查看", description = "通过Id对规则引擎的查看,并对规则引擎的逻辑模块进行设置")
    public Result<RuleEngine> queryById(@PathVariable("id") int id){
        RuleEngine etlRule = etlRuleService.queryById(id);
        return Result.success(etlRule);
    }
    @PostMapping("/list")
    @Operation(summary = "查看规则引擎", description = "根据规则引擎的名称,类型,是否激活,状态等可以进行对规则的筛选")
    public Result<List<EtlRuleResp>>  selectList(@Validated @RequestBody EtlRuleListReq req){

     List<EtlRuleResp> list = etlRuleService.selectList(req);


        return Result.success(list);
    }


    /**
     * 新增数据
     *
     * @param etlRule 实例对象
     * @return 实例对象
     */

    @PostMapping
    @Operation(summary = "规则引擎的添加", description = "添加规则引擎的,添加成功后可以对规则引擎进行维护")
    public Result<RuleEngine> add(@RequestBody EtlRuleAddReq etlRule){

        etlRuleService.save(RuleEngine.addBuild(etlRule));
        return Result.success();
    }

    /**
     * 更新数据
     *
     * @param etlRule 实例对象
     * @return 实例对象
     */

    @PutMapping("/{id}")
    @Operation(summary = "规则引擎的修改", description = "通过Id修改规则引擎的信息")
    public Result<RuleEngine> edit(@PathVariable("id") Long  id, @RequestBody @Validated EtlRuleUpdReq etlRule){
        etlRuleService.updateById(RuleEngine.updBuild(etlRule,()->id));
        return Result.success();
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "规则引擎的删除", description = "通过Id删除规则信息")
    public Result<Boolean> deleteById(@PathVariable("id") int id){
        etlRuleService.deleteById(id);
        return Result.success();
    }


    @PostMapping("/findAllRuleRegion")
    @Operation(summary = "规则作用域列表", description = "查询所有作用域的信息")
    public Result<List<RuleRegion>> findAllRuleRegion(){

   List<RuleRegion> list = etlRuleService.findAllRuleRegion();

        return Result.success(list);
    }

/**
 * 查看维护规则版本
 */

@PostMapping("/selectRuleAndVersionById/{id}")
@Operation(summary = "规则引擎的维护", description = "通过Id查询规则引擎和规则引擎的版本")
    public Result<EtlRuleResp> selectRuleAndVersionById(@PathVariable("id") Long id){

         EtlRuleResp etlRuleResp =  etlRuleService.selectRuleAndVersionById(id);

    return Result.success(etlRuleResp);
}

    /**
     * 开启引擎请求
     * @param id
     * @return
     */
@PostMapping("/openEngine/{id}")
@Operation(summary = "规则引擎的开启", description = "通过Id开启规则引擎")
    public Result openEngine(@PathVariable("id") Long id){

    etlRuleService.openEngine(id);

    return Result.success();
}

/**
 * 关闭引擎请求
 */


@PostMapping("/closeEngine/{id}")
@Operation(summary = "规则引擎的关闭", description = "通过Id关闭规则引擎")
public Result closeEngine(@PathVariable("id") Long id){

    etlRuleService.closeEngine(id);

    return Result.success();
}



    /**
     * 激活引擎请求
     * @param id
     * @return
     */
    @PostMapping("/activate/{id}")
    @Operation(summary = "规则引擎的激活", description = "通过Id激活规则引擎")
    public Result activate(@PathVariable("id") Long id){

        etlRuleService.activate(id);

        return Result.success();
    }

    /**
     * 取消引擎请求
     */


    @PostMapping("/disable/{id}")
    @Operation(summary = "规则引擎的取消", description = "通过Id取消规则引擎")
    public Result disable(@PathVariable("id") Long id){

        etlRuleService.disable(id);

        return Result.success();
    }



}






