package com.huimv.video.controller;

import com.huimv.video.domain.PastureArea;
import com.huimv.video.result.Result;
import com.huimv.video.service.PastureAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Project : huimv.shiwan
 * @Package : com.huimv.video.controller
 * @Description : TODO
 * @Author : yuxuexuan
 * @Create : 2021/1/7 0007 10:34
 **/

@CrossOrigin
@RestController
@RequestMapping("/pastureArea")
public class PastureAreaController {

    @Autowired
    private PastureAreaService pastureAreaService;

    @RequestMapping("/add")
    private Result addArea(PastureArea pastureArea){
        try {
            if (pastureArea.getParent() != null){
                pastureAreaService.save(pastureArea);
                return new Result(10000,"添加成功");
            }else {
                return new Result(10002,"请选择父级ID");
            }
        }catch (Exception e){
            return new Result(10001,"添加失败");
        }
    }

    @RequestMapping("/update")
    private Result updateArea(PastureArea pastureArea){
        try {
            if (pastureArea.getId()==0){
                return new Result(10002,"请选择修改数据");
            }
            if (pastureArea.getParent() != null){
                pastureAreaService.update(pastureArea);
                return new Result(10000,"修改成功");
            }else {
                return new Result(10002,"请选择父级ID");
            }

        }catch (Exception e){
            return new Result(10001,"修改失败");
        }
    }

    @RequestMapping("/delete")
    private Result deleteArea(@RequestParam(required = false)List<Integer> ids){
        try {
            pastureAreaService.delete(ids);
            return new Result(10000,"删除成功");
        }catch (Exception e){
            return new Result(10001,"删除失败");
        }
    }

    @RequestMapping("/findAll")
    private Result findAll(){
        try {
            List list = pastureAreaService.findAll();
            if (list == null){
                return new Result(10000,"暂时没有数据");
            }
            return new Result(10000,"查询成功",list);
        }catch (Exception e){
            return new Result(10001,"查询失败");
        }
    }



}
