package com.mvc.controller.sthjb;

import com.mvc.interceptor.Debounce;
import com.mvc.model.ResPageModel;
import com.mvc.model.ResultGeekerModel;
import com.mvc.model.sthjb.TPipelineLeakageModel;
import com.mvc.service.sthjb.TPipelineLeakageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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("/TPipelineLeakageController")
@Controller
public class TPipelineLeakageController {

    @Resource
    private TPipelineLeakageService tPipelineLeakageService;

    @ApiOperation("查询所有管网漏损检测任务")
    @RequestMapping(value = "/findAllTPipelineLeakage", method = RequestMethod.POST)
    @ResponseBody
    public ResultGeekerModel findAllTPipelineLeakage(@RequestBody TPipelineLeakageModel model) {
        ResultGeekerModel resultModel;
        try {
            ResPageModel res = tPipelineLeakageService.findAllTPipelineLeakage(model);
            resultModel = new ResultGeekerModel(res);
        } catch (Exception e) {
            e.printStackTrace();
            resultModel = new ResultGeekerModel(e.getMessage());
        }
        return resultModel;
    }

    @ApiOperation("新增管网漏损检测任务")
    @RequestMapping(value = "/saveTPipelineLeakage", method = RequestMethod.POST)
    @ResponseBody
    @Debounce
    public ResultGeekerModel saveTPipelineLeakage(@RequestBody TPipelineLeakageModel model) {
        ResultGeekerModel resultModel;
        try {
            String error = tPipelineLeakageService.checkDataLegitimacy(model);
            if (error != null) {
                return new ResultGeekerModel(error);
            }

            TPipelineLeakageModel resModel = tPipelineLeakageService.saveTPipelineLeakage(model);
            resultModel = new ResultGeekerModel(resModel);
        } catch (Exception e) {
            e.printStackTrace();
            resultModel = new ResultGeekerModel(e.getMessage());
        }
        return resultModel;
    }

    @ApiOperation("更新管网漏损检测任务")
    @RequestMapping(value = "/updateTPipelineLeakage", method = RequestMethod.POST)
    @ResponseBody
    public ResultGeekerModel updateTPipelineLeakage(@RequestBody TPipelineLeakageModel model) {
        ResultGeekerModel resultModel;
        try {
            TPipelineLeakageModel resModel = tPipelineLeakageService.updateTPipelineLeakage(model);
            resultModel = new ResultGeekerModel(resModel);
        } catch (Exception e) {
            e.printStackTrace();
            resultModel = new ResultGeekerModel(e.getMessage());
        }
        return resultModel;
    }

    @ApiOperation("删除管网漏损检测任务")
    @RequestMapping(value = "/deleteTPipelineLeakage", method = RequestMethod.GET)
    @ResponseBody
    public ResultGeekerModel deleteTPipelineLeakage(int id) {
        ResultGeekerModel resultModel;
        try {
            int res = tPipelineLeakageService.deleteTPipelineLeakage(id);
            resultModel = new ResultGeekerModel(res);
        } catch (Exception e) {
            e.printStackTrace();
            resultModel = new ResultGeekerModel(e.getMessage());
        }
        return resultModel;
    }

    @ApiOperation("手动触发计算漏损")
    @RequestMapping(value = "/manualCalcPipelineLeakage", method = RequestMethod.GET)
    @ResponseBody
    public ResultGeekerModel manualCalcPipelineLeakage(int id) {
        ResultGeekerModel resultModel;
        try {
            tPipelineLeakageService.manualCalcPipelineLeakage(id);
            resultModel = new ResultGeekerModel(1);
        } catch (Exception e) {
            e.printStackTrace();
            resultModel = new ResultGeekerModel(e.getMessage());
        }
        return resultModel;
    }
}
