package com.mvc.controller.dic;

import com.mvc.model.CorrectMap;
import com.mvc.model.ErrorMap;
import com.mvc.model.DicStkroomModel;
import com.mvc.service.dic.DicStkroomService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags = "库房接口")
@RequestMapping("/DicStkroomAction")
@Controller
public class DicStkroomAction {

    @Autowired
    private DicStkroomService dicStkroomService;

    @ApiOperation("查询所有库房")
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> findAllDicStkroom(){

        try {
            List<DicStkroomModel> resList = dicStkroomService.findAllDicStkroom();
            Map<String,Object> resMap=new CorrectMap();
            resMap.put("res",resList);
            return resMap;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String,Object> resMap=new ErrorMap();
            resMap.put("error",e.getMessage());
            return resMap;
        }
    }

    @ApiOperation("新增库房")
    @RequestMapping(value = "/saveDicStkroom",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> saveDicStkroom(@RequestBody DicStkroomModel model){
        try {
            DicStkroomModel temp= dicStkroomService.findStkroomByStkroomName(model.getStkroomName());
            if(temp!=null){
                Map<String, Object> resMap = new ErrorMap();
                resMap.put("error", "库房名称已经存在");
                return resMap;
            }

            DicStkroomModel resModel= dicStkroomService.saveDicStkroom(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 = "/updateDicStkroom",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> updateDeviceAcc(@RequestBody DicStkroomModel model){
        try {
            DicStkroomModel temp= dicStkroomService.findStkroomByStkroomName(model.getStkroomName());
            if(temp!=null&&model.getStkroomId()!=temp.getStkroomId()){
                Map<String, Object> resMap = new ErrorMap();
                resMap.put("error", "库房名称已经存在");
                return resMap;
            }

            DicStkroomModel resModel= dicStkroomService.updateDicStkroom(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 = "/deleteDicStkroom/{id}",method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> deleteDicStkroom(@PathVariable("id") int id){
        try {
            int  res= dicStkroomService.deleteDicStkroom(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;
        }
    }
}
