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

    @Resource
    private WaterMeterInventoryService waterMeterInventoryService;

    @ApiOperation("查询所有水计量器具信息")
    @RequestMapping(value = "/findAllWaterMeterInventory", method = RequestMethod.POST)
    @ResponseBody
    public ResultGeekerModel findAllWaterMeterInventory(@RequestBody WaterMeterInventoryModel model) {
        ResultGeekerModel resultModel;
        try {
            ResPageModel res = waterMeterInventoryService.findAllWaterMeterInventory(model);
            resultModel = new ResultGeekerModel(res);
        } catch (Exception e) {
            e.printStackTrace();
            resultModel = new ResultGeekerModel(e.getMessage());
        }
        return resultModel;
    }

    @ApiOperation("新增水计量器具信息")
    @RequestMapping(value = "/saveWaterMeterInventory", method = RequestMethod.POST)
    @ResponseBody
    @Debounce
    public ResultGeekerModel saveWaterMeterInventory(@RequestBody WaterMeterInventoryModel model) {
        ResultGeekerModel resultModel;
        try {
            String error = waterMeterInventoryService.checkDataLegitimacy(model);
            if (error != null) {
                return new ResultGeekerModel(error);
            }

            WaterMeterInventoryModel resModel = waterMeterInventoryService.saveWaterMeterInventory(model);
            resultModel = new ResultGeekerModel(resModel);
        } catch (Exception e) {
            e.printStackTrace();
            resultModel = new ResultGeekerModel(e.getMessage());
        }
        return resultModel;
    }

    @ApiOperation("更新水计量器具信息")
    @RequestMapping(value = "/updateWaterMeterInventory", method = RequestMethod.POST)
    @ResponseBody
    public ResultGeekerModel updateWaterMeterInventory(@RequestBody WaterMeterInventoryModel model) {
        ResultGeekerModel resultModel;
        try {
            WaterMeterInventoryModel resModel = waterMeterInventoryService.updateWaterMeterInventory(model);
            resultModel = new ResultGeekerModel(resModel);
        } catch (Exception e) {
            e.printStackTrace();
            resultModel = new ResultGeekerModel(e.getMessage());
        }
        return resultModel;
    }

    @ApiOperation("删除水计量器具信息")
    @RequestMapping(value = "/deleteWaterMeterInventory", method = RequestMethod.GET)
    @ResponseBody
    public ResultGeekerModel deleteWaterMeterInventory(int id) {
        ResultGeekerModel resultModel;
        try {
            int res = waterMeterInventoryService.deleteWaterMeterInventory(id);
            resultModel = new ResultGeekerModel(res);
        } catch (Exception e) {
            e.printStackTrace();
            resultModel = new ResultGeekerModel(e.getMessage());
        }
        return resultModel;
    }
}
