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.UnitWaterDataModel;
import com.mvc.service.sthjb.UnitWaterDataService;
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("/UnitWaterDataController")
@Controller
public class UnitWaterDataController {

    @Resource
    private UnitWaterDataService unitWaterDataService;

    @ApiOperation("查询所有单位用水基本信息")
    @RequestMapping(value = "/findAllUnitWaterData", method = RequestMethod.POST)
    @ResponseBody
    public ResultGeekerModel findAllUnitWaterData(@RequestBody UnitWaterDataModel model) {
        ResultGeekerModel resultModel;
        try {
            ResPageModel res = unitWaterDataService.findAllUnitWaterData(model);
            resultModel = new ResultGeekerModel(res);
        } catch (Exception e) {
            e.printStackTrace();
            resultModel = new ResultGeekerModel(e.getMessage());
        }
        return resultModel;
    }

    @ApiOperation("新增单位用水基本信息")
    @RequestMapping(value = "/saveUnitWaterData", method = RequestMethod.POST)
    @ResponseBody
    @Debounce
    public ResultGeekerModel saveUnitWaterData(@RequestBody UnitWaterDataModel model) {
        ResultGeekerModel resultModel;
        try {
            String error = unitWaterDataService.checkDataLegitimacy(model);
            if (error != null) {
                return new ResultGeekerModel(error);
            }

            UnitWaterDataModel resModel = unitWaterDataService.saveUnitWaterData(model);
            resultModel = new ResultGeekerModel(resModel);
        } catch (Exception e) {
            e.printStackTrace();
            resultModel = new ResultGeekerModel(e.getMessage());
        }
        return resultModel;
    }

    @ApiOperation("更新单位用水基本信息")
    @RequestMapping(value = "/updateUnitWaterData", method = RequestMethod.POST)
    @ResponseBody
    public ResultGeekerModel updateUnitWaterData(@RequestBody UnitWaterDataModel model) {
        ResultGeekerModel resultModel;
        try {
            UnitWaterDataModel resModel = unitWaterDataService.updateUnitWaterData(model);
            resultModel = new ResultGeekerModel(resModel);
        } catch (Exception e) {
            e.printStackTrace();
            resultModel = new ResultGeekerModel(e.getMessage());
        }
        return resultModel;
    }

    @ApiOperation("删除单位用水基本信息")
    @RequestMapping(value = "/deleteUnitWaterData", method = RequestMethod.GET)
    @ResponseBody
    public ResultGeekerModel deleteUnitWaterData(int id) {
        ResultGeekerModel resultModel;
        try {
            int res = unitWaterDataService.deleteUnitWaterData(id);
            resultModel = new ResultGeekerModel(res);
        } catch (Exception e) {
            e.printStackTrace();
            resultModel = new ResultGeekerModel(e.getMessage());
        }
        return resultModel;
    }
}
