package com.zksy.web.controller.gasbasic;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zksy.base.service.DrillSimulationService;
import com.zksy.base.domain.*;
import com.zksy.base.mapper.*;
import com.zksy.common.annotation.Anonymous;
import com.zksy.common.core.domain.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/drill/sim")
@Api(tags = "演练管理-模拟演练")
public class DrillSimulationController {

    @Autowired
    private DrillSimulationProjectMapper projectMapper;
    @Autowired
    private DrillSimulationSceneMapper sceneMapper;
    @Autowired
    private DrillSimulationEventMapper eventMapper;
    @Autowired
    private DrillSimulationRoleMapper roleMapper;
    @Autowired
    private DrillSimulationTaskMapper taskMapper;
    @Autowired
    private DrillSimulationLogMapper logMapper;
    @Autowired
    private DrillSimulationResultMapper resultMapper;
    @Autowired
    private DrillSimulationService drillSimulationService;

    @GetMapping("/project")
    @Anonymous
    public AjaxResult projects() {
        return AjaxResult.success(projectMapper.selectList(new LambdaQueryWrapper<DrillSimulationProject>().orderByDesc(DrillSimulationProject::getCreateTime)));
    }

    @PostMapping("/project")
    @Anonymous
    public AjaxResult saveProject(@RequestBody DrillSimulationProject p) {
        projectMapper.insert(p);
        return AjaxResult.success(p.getId());
    }

    @DeleteMapping("/project/{id}")
    @Anonymous
    public AjaxResult deleteProject(@PathVariable Long id) {
        projectMapper.deleteById(id);
        return AjaxResult.success();
    }

    @GetMapping("/scene/{projectId}")
    @Anonymous
    public AjaxResult scene(@PathVariable Long projectId) {
        return AjaxResult.success(sceneMapper.selectList(new LambdaQueryWrapper<DrillSimulationScene>().eq(DrillSimulationScene::getProjectId, projectId)));
    }

    @PostMapping("/scene")
    @Anonymous
    public AjaxResult saveScene(@RequestBody DrillSimulationScene s) {
        if (s.getId() != null) sceneMapper.updateById(s);
        else sceneMapper.insert(s);
        return AjaxResult.success();
    }

    @DeleteMapping("/scene/{id}")
    @Anonymous
    public AjaxResult deleteScene(@PathVariable Long id) {
        sceneMapper.deleteById(id);
        return AjaxResult.success();
    }

    @GetMapping("/event/{projectId}")
    @Anonymous
    public AjaxResult event(@PathVariable Long projectId) {
        return AjaxResult.success(eventMapper.selectList(new LambdaQueryWrapper<DrillSimulationEvent>().eq(DrillSimulationEvent::getProjectId, projectId)));
    }

    @PostMapping("/event")
    @Anonymous
    public AjaxResult saveEvent(@RequestBody DrillSimulationEvent e) {
        eventMapper.insert(e);
        return AjaxResult.success();
    }

    @DeleteMapping("/event/{id}")
    @Anonymous
    public AjaxResult deleteEvent(@PathVariable Long id) {
        eventMapper.deleteById(id);
        return AjaxResult.success();
    }

    @GetMapping("/role/{projectId}")
    @Anonymous
    public AjaxResult role(@PathVariable Long projectId) {
        return AjaxResult.success(roleMapper.selectList(new LambdaQueryWrapper<DrillSimulationRole>().eq(DrillSimulationRole::getProjectId, projectId)));
    }

    @PostMapping("/role")
    @Anonymous
    public AjaxResult saveRole(@RequestBody DrillSimulationRole r) {
        roleMapper.insert(r);
        return AjaxResult.success();
    }

    @DeleteMapping("/role/{id}")
    @Anonymous
    public AjaxResult deleteRole(@PathVariable Long id) {
        roleMapper.deleteById(id);
        return AjaxResult.success();
    }

    @GetMapping("/task/{projectId}")
    @Anonymous
    public AjaxResult task(@PathVariable Long projectId) {
        return AjaxResult.success(taskMapper.selectList(new LambdaQueryWrapper<DrillSimulationTask>().eq(DrillSimulationTask::getProjectId, projectId)));
    }

    @PostMapping("/task")
    @Anonymous
    public AjaxResult saveTask(@RequestBody DrillSimulationTask t) {
        taskMapper.insert(t);
        return AjaxResult.success();
    }

    @DeleteMapping("/task/{id}")
    @Anonymous
    public AjaxResult deleteTask(@PathVariable Long id) {
        taskMapper.deleteById(id);
        return AjaxResult.success();
    }

    @GetMapping("/log/{projectId}")
    @Anonymous
    public AjaxResult log(@PathVariable Long projectId) {
        return AjaxResult.success(logMapper.selectList(new LambdaQueryWrapper<DrillSimulationLog>().eq(DrillSimulationLog::getProjectId, projectId).orderByAsc(DrillSimulationLog::getLogTime)));
    }

    @PostMapping("/log")
    @Anonymous
    public AjaxResult addLog(@RequestBody DrillSimulationLog log) {
        logMapper.insert(log);
        return AjaxResult.success();
    }

    @GetMapping("/result/{projectId}")
    @Anonymous
    public AjaxResult result(@PathVariable Long projectId) {
        return AjaxResult.success(resultMapper.selectOne(new LambdaQueryWrapper<DrillSimulationResult>().eq(DrillSimulationResult::getProjectId, projectId)));
    }

    @PostMapping("/process")
    @Anonymous
    @ApiOperation("根据项目类型批量触发设备告警")
    public AjaxResult process(@RequestParam Long projectId, @RequestParam String action, @RequestParam String category) {
        return AjaxResult.success(drillSimulationService.triggerAlarm(category));
    }
}
