package com.huimv.admin.system.controller;


import com.huimv.admin.system.entity.Farm;
import com.huimv.admin.system.service.IFarmService;
import com.huimv.common.token.TokenSign;
import com.huimv.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
 */
@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);
    }

    @GetMapping("/getFarm2")
    public Result getFarm2(HttpServletRequest request){
        Integer userId = TokenSign.getMemberIdByJwtToken(request);
        return  farmService.getFarm2(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);
    }
}
