package com.mvc.controller.taizhang;

import com.mvc.interceptor.Debounce;
import com.mvc.model.CorrectMap;
import com.mvc.model.ErrorMap;
import com.mvc.model.BatteryAccModel;
import com.mvc.service.taizhang.BatteryAccService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Api(tags = "电池台账接口")
@RequestMapping("/BatteryAccAction")
@Controller
public class BatteryAccAction {

    @Autowired
    private BatteryAccService batteryAccService;

    @ApiOperation("电池台账分页查询")
    @RequestMapping(value = "/findAllBatteryAcc", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> findAllBatteryAcc(@RequestBody BatteryAccModel model) {
        try {
            Map<String, Object> res = batteryAccService.findAllBatteryAcc(model);
            Map<String, Object> resMap = new CorrectMap();
            resMap.put("res", res);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> resMap = new ErrorMap();
            resMap.put("error", e.getMessage());
            return resMap;
        }
    }

    @ApiOperation("新增电池台账")
    @RequestMapping(value = "/saveBatteryAcc", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> saveBatteryAcc(@RequestBody BatteryAccModel model) {
        try {
            String errMsg = batteryAccService.checkDataLegitimacy(model);
            if (StringUtils.isNotEmpty(errMsg)) {
                Map<String, Object> resMap = new ErrorMap();
                resMap.put("error", errMsg);
                return resMap;
            }
            BatteryAccModel resModel = batteryAccService.saveBatteryAcc(model);
            Map<String, Object> resMap = new CorrectMap();
            resMap.put("res", resModel);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> resMap = new ErrorMap();
            resMap.put("error", e.getMessage());
            return resMap;
        }
    }

    @ApiOperation("更新电池台账")
    @RequestMapping(value = "/updateBatteryAcc", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> updateBatteryAcc(@RequestBody BatteryAccModel model) {
        try {
            BatteryAccModel resModel = batteryAccService.updateBatteryAcc(model);
            Map<String, Object> resMap = new CorrectMap();
            resMap.put("res", resModel);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> resMap = new ErrorMap();
            resMap.put("error", e.getMessage());
            return resMap;
        }
    }

    @ApiOperation("删除电池台账")
    @RequestMapping(value = "/deleteBatteryAcc/{id}", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> deleteBatteryAcc(@PathVariable("id") int id) {
        try {
            int res = batteryAccService.deleteBatteryAcc(id);
            Map<String, Object> resMap = new CorrectMap();
            resMap.put("res", res);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> resMap = new ErrorMap();
            resMap.put("error", e.getMessage());
            return resMap;
        }
    }
}
