package com.mvc.controller.alarm;

import com.mvc.model.ResPageModel;
import com.mvc.model.ResultGeekerModel;
import com.mvc.model.alarm.TAlarmSubscriptionModel;
import com.mvc.service.alarm.TAlarmSubscriptionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Api(tags = "报警通订阅接口")
@RequestMapping("/TAlarmSubscriptionAction")
@Controller
public class TAlarmSubscriptionAction {

    @Resource
    private TAlarmSubscriptionService tAlarmSubscription;

    @ApiOperation("查询所有报警订阅记录")
    @RequestMapping(value = "/findAllTAlarmSubscription", method = RequestMethod.POST)
    @ResponseBody
    public ResultGeekerModel findAllTAlarmSubscription(@RequestBody TAlarmSubscriptionModel model) {
        ResultGeekerModel resultModel;
        try {
            ResPageModel res = tAlarmSubscription.findAllTAlarmSubscription(model);
            resultModel = new ResultGeekerModel(res);
        } catch (Exception e) {
            e.printStackTrace();
            resultModel = new ResultGeekerModel(e.getMessage());
        }
        return resultModel;
    }

    @ApiOperation("新增报警订阅")
    @RequestMapping(value = "/saveTAlarmSubscription", method = RequestMethod.POST)
    @ResponseBody
    public ResultGeekerModel saveTAlarmSubscription(@RequestBody TAlarmSubscriptionModel model) {
        ResultGeekerModel resultModel;
        try {
            String msg = tAlarmSubscription.checkAlarmSubscription(model);
            if (StringUtils.isNotEmpty(msg)) {
                resultModel = new ResultGeekerModel(msg);
            } else {
                TAlarmSubscriptionModel res = tAlarmSubscription.saveTAlarmSubscription(model);
                resultModel = new ResultGeekerModel(res);
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultModel = new ResultGeekerModel(e.getMessage());
        }
        return resultModel;
    }

    @ApiOperation("更新报警订阅")
    @RequestMapping(value = "/updateTAlarmSubscription", method = RequestMethod.POST)
    @ResponseBody
    public ResultGeekerModel updateTAlarmSubscription(@RequestBody TAlarmSubscriptionModel model) {
        ResultGeekerModel resultModel;
        try {
            String msg = tAlarmSubscription.checkAlarmSubscription(model);
            if (StringUtils.isNotEmpty(msg)) {
                resultModel = new ResultGeekerModel(msg);
            } else {
                TAlarmSubscriptionModel res = tAlarmSubscription.updateTAlarmSubscription(model);
                resultModel = new ResultGeekerModel(res);
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultModel = new ResultGeekerModel(e.getMessage());
        }
        return resultModel;
    }

    @ApiOperation("删除报警订阅")
    @RequestMapping(value = "/deleteTAlarmSubscription", method = RequestMethod.GET)
    @ResponseBody
    public ResultGeekerModel deleteTAlarmSubscription(int id) {
        ResultGeekerModel resultModel;
        try {
            int res = tAlarmSubscription.deleteTAlarmSubscription(id);
            resultModel = new ResultGeekerModel(res);
        } catch (Exception e) {
            e.printStackTrace();
            resultModel = new ResultGeekerModel(e.getMessage());
        }
        return resultModel;
    }
}
