package com.huimv.manage.sys.controller;


import com.huimv.manage.common.token.TokenSign;
import com.huimv.manage.sys.entity.Farm;
import com.huimv.manage.sys.service.IFarmService;
import com.huimv.manage.common.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author astupidcoder
 * @since 2021-10-14
 */
@CrossOrigin
@RestController
@RequestMapping("/farm")
public class FarmController {


    @Autowired
    private IFarmService farmService;

    @GetMapping("/getFarm")
    public Result getFarm(HttpServletRequest request){
        Integer userId = TokenSign.getMemberIdByJwtToken(request);
        return  farmService.getFarm(userId);
    }


    @PostMapping("/add")
    public Result add(@RequestBody Farm farm,HttpServletRequest request){
        Integer userId = TokenSign.getMemberIdByJwtToken(request);
        if (userId != 1){
            return new Result(10001,"该账号不能操作牧场",false);
        }
        return  farmService.add(farm);
    }

    @PostMapping("/edit")
    public Result edit(@RequestBody Farm farm,HttpServletRequest request){
        Integer userId = TokenSign.getMemberIdByJwtToken(request);
        if (userId != 1){
            return new Result(10001,"该账号不能操作牧场",false);
        }
        farmService.updateById(farm);
        return new Result(10000,"修改成功",true);
    }

    @GetMapping("/remove")
    public Result remove(@RequestParam(name = "ids") List<Integer> ids, HttpServletRequest request){
        Integer userId = TokenSign.getMemberIdByJwtToken(request);
        if (userId != 1){
            return new Result(10001,"该账号不能操作牧场",false);
        }
        farmService.removeByIds(ids);
        return new Result(10000,"删除成功",true);
    }
}
