package com.ruoyi.web.controller.system;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.web.domain.dto.warning.WarningReleaseRequest;
import com.ruoyi.web.domain.dto.warning.drowningWarning.DrowningWarningAddRequest;
import com.ruoyi.web.domain.dto.warning.earthquakeWarning.EarthquakeWarningAddRequest;
import com.ruoyi.web.domain.dto.warning.environmentWarning.EnvironmentWarningAddRequest;
import com.ruoyi.web.service.DrowningWarningService;
import com.ruoyi.web.service.EarthquakeWarningService;
import com.ruoyi.web.service.EnvironmentWarningService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
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.RestController;

import static com.ruoyi.common.core.domain.AjaxResult.error;
import static com.ruoyi.common.core.domain.AjaxResult.success;

@Api(tags = "预警信息发布")
@RestController
@RequestMapping("/system/warning")
public class WarningController {

    @Autowired
    private EarthquakeWarningService earthquakeWarningService;

    @Autowired
    private EnvironmentWarningService environmentWarningService;

    @Autowired
    private DrowningWarningService drowningWarningService;

    /**
     * 发布预警信息
     *
     * @param warningReleaseRequest
     * @return
     */
    @ApiOperation("发布预警信息")
    @PostMapping("/release")
    public AjaxResult addDrowningWarning(@RequestBody WarningReleaseRequest warningReleaseRequest) {
        if (warningReleaseRequest == null) {
            return error("请求参数为空");
        }
        String type = warningReleaseRequest.getType();
        if (type == null) {
            return error("类别为空");
        }
        //类别 0-地震预警、1-人居环境预警、2-防溺水预警
        switch (type) {
            case "0":
                EarthquakeWarningAddRequest earthquakeWarningAddRequest = new EarthquakeWarningAddRequest();
                BeanUtil.copyProperties(warningReleaseRequest, earthquakeWarningAddRequest);
                earthquakeWarningService.addEarthquakeWarning(earthquakeWarningAddRequest);
                break;
            case "1":
                EnvironmentWarningAddRequest environmentWarningAddRequest = new EnvironmentWarningAddRequest();
                BeanUtil.copyProperties(warningReleaseRequest, environmentWarningAddRequest);
                environmentWarningService.addEnvironmentWarning(environmentWarningAddRequest);
                break;
            case "2":
                DrowningWarningAddRequest drowningWarningAddRequest = new DrowningWarningAddRequest();
                BeanUtil.copyProperties(warningReleaseRequest, drowningWarningAddRequest);
                drowningWarningService.addDrowningWarning(drowningWarningAddRequest);
                break;
            default:
                return error("发布失败");
        }
        return success("发布成功");
    }
}
