package cn.com.taiji.beidou.web.controller.ship;

import cn.com.taiji.beidou.common.core.controller.BaseController;
import cn.com.taiji.beidou.common.core.domain.R;
import cn.com.taiji.beidou.common.core.page.TableDataInfo;
import cn.com.taiji.beidou.ship.warning.model.domain.*;
import cn.com.taiji.beidou.ship.warning.model.mapper.ShipWarningModelMapper;
import cn.com.taiji.beidou.ship.warning.model.service.ShipWarningService;
import cn.com.taiji.beidou.ship.warning.rule.domain.UpdateEnabledReq;
import cn.com.taiji.beidou.ship.warning.rule.domain.WarningRuleResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author CHEN
 * @Date 2022/11/15 17:33
 */
@Api(tags = "预警规则接口")
@RestController
public class ShipWarningRuleController extends BaseController {

    @Resource
    private ShipWarningModelMapper shipWarningModelMapper;

    @Resource
    private ShipWarningService shipWarningService;

    @ApiOperation(value = "新增预警规则",notes = "新增预警规则时，无需传规则ID（系统会自动生成），但需要配置预警模型时，" +
            "所传的预警区域，预警参数都是绑定在预警模型上的，才会有效，如果只传预警区域或者预警参数不传预警模型ID，则是无效的")
    @PostMapping("/addWarningRule")
    public R addWarningRule(@RequestBody WarningRuleReq warningRuleReq){
        try {
            return R.ok(shipWarningService.addOrUpdateWarningRule(warningRuleReq));
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
    }

    @ApiOperation(value = "编辑预警规则",notes = "参数中必须传预警规则ID，否则将是新增预警规则,所有参数均为必传，从规则详情中获取")
    @PostMapping("/modifyWarningRule")
    public R modifyWarningRule(@RequestBody WarningRuleReq warningRuleReq){
        try {
            return R.ok(shipWarningService.addOrUpdateWarningRule(warningRuleReq));
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
    }

    @ApiOperation("新增预警模型")
    @PostMapping("/addWarningModel")
    public R addWarningModel(@RequestBody WarningModelReq warningModelReq){
        try {
            return R.ok(shipWarningService.addOrUpdateWarningModel(warningModelReq));
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
    }

    @ApiOperation(value = "编辑预警模型",notes = "参数中必须传预警模型ID，否则将是新增预警模型。所有参数均为必传，从模型详情中获取")
    @PostMapping("/modifyWarningModel")
    public R modifyWarningModel(@RequestBody WarningModelReq warningModelReq){
        try {
            return R.ok(shipWarningService.addOrUpdateWarningModel(warningModelReq));
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
    }

    @ApiOperation("预警规则列表")
    @GetMapping("/warn-rule/list")
    public TableDataInfo getWarningRuleList(String ruleName, Integer pageNum, Integer pageSize){
        startPage(pageNum,pageSize);
        List<WarningRuleResult> results = shipWarningModelMapper.selectRuleList(ruleName,null,null);
        return getDataTable(results);
    }

    @ApiOperation("预警规则列表(预警接入)")
    @GetMapping("/warn-rule/lists")
    public R<List<WarningRuleResult>> lists(){
        List<WarningRuleResult> results = shipWarningModelMapper.selectRuleList(null,null,null);
        if (results==null||results.size()<0){
            return R.fail();
        }
        return R.ok(results);
    }

    @ApiOperation("预警规则详情")
    @GetMapping("/warn-rule/get-by-id")
    public R<WarningRuleResult> getWarningRuleById(String id){
        List<WarningRuleResult> results = shipWarningModelMapper.selectRuleList(null,null,id);
        if(results==null||results.size()<1){
            return R.fail();
        }
        return R.ok(results.get(0));
    }


    @ApiOperation("预警模型列表")
    @GetMapping("/getWarningModelList")
    public TableDataInfo getWarningModelList(String modelName, Integer pageNum, Integer pageSize){
        startPage(pageNum,pageSize);
        List<WarningModelResult> results = shipWarningModelMapper.selectModelList(modelName,null);
        TableDataInfo dataTable = getDataTable(results);
        return dataTable;
    }

    @ApiOperation(value = "预警模型详情",notes = "参数是预警模型ID，返回该模型关联的预警参数和预警区域")
    @GetMapping("/WarningModelById")
    public R<WarningModelResult> WarningModelById(String id){
        List<WarningModelResult> results = shipWarningModelMapper.selectModelList(null,id);
        if(results==null||results.size()<1){
            return R.fail();
        }
        return R.ok(results.get(0));
    }

    @ApiOperation("启用停用")
    @PostMapping("/warn_rule/isEnabled")
    public R UpdateEnabled(@RequestBody UpdateEnabledReq req){
        try {
            shipWarningService.updateEnabledById(req);
            return R.ok();
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
    }

    @ApiOperation("获取参数列表")
    @GetMapping("/getParamList")
    public R<List<WarningParamDictData>> getParamList(){
        try {
            return R.ok(shipWarningModelMapper.selectParamDict());
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
    }

    @ApiOperation("获取区域列表")
    @GetMapping("/getAreaList")
    public R<List<WarningAreaVo>> getAreaList(String areaName){
        try {
            return R.ok(shipWarningModelMapper.selectAreaList(areaName));
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
    }

    @ApiOperation("删除预警规则")
    @GetMapping("/removeWarningRule")
    public R removeWarningRule(String ruleId){
        try {
            return R.ok(shipWarningService.delRule(ruleId));
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
    }

    @ApiOperation("删除预警模型")
    @GetMapping("/removeWarningModel")
    public R removeWarningModel(String modelId){
        try {
            return R.ok(shipWarningService.delModel(modelId));
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
    }

    @ApiOperation(value = "勾选区域",notes = "参数ids是字符串，多个区域id以英文符号逗号隔开")
    @PostMapping("/getAreaByIds")
    public R getWarningAreaById(String ids){
        try {
            return R.ok(shipWarningService.findInId(ids));
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
    }
}
