package com.huimv.beeboxs.controller;


import com.huimv.beeboxs.common.token.TokenSign;
import com.huimv.beeboxs.common.utils.Result;
import com.huimv.beeboxs.entity.BaseHiveFarm;
import com.huimv.beeboxs.service.IBaseHiveFarmService;
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 author
 * @since 2022-04-22
 */
@RestController
@RequestMapping("/admin/farm")
@CrossOrigin
public class BaseHiveFarmController {

    @Autowired
    private IBaseHiveFarmService farmService;

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


    @PostMapping("/add")
    public Result add(@RequestBody BaseHiveFarm 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 BaseHiveFarm 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);
    }


}
