package com.huimv.receive.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huimv.receive.common.token.TokenSign;
import com.huimv.receive.common.utils.Result;
import com.huimv.receive.common.utils.ResultCode;
import com.huimv.receive.entity.Farm;
import com.huimv.receive.service.IFarmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author astupidcoder
 * @since 2021-10-14
 */
@CrossOrigin
@RestController
@RequestMapping("/admin/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("/listByRowStatus")
    public Result listByRowStatus(){

        return  new Result(ResultCode.SUCCESS,farmService.list(new QueryWrapper<Farm>().eq("row_status",1).orderByAsc("farm_name")));
    }

    @GetMapping("/getFarm2")
    public Result getFarm2(HttpServletRequest request){
        Integer userId = TokenSign.getMemberIdByJwtToken(request);
        return  farmService.getFarm2(userId);
    }
    @PostMapping("/list")
    public Result list(HttpServletRequest request ,@RequestBody Map paramMap){
        Integer userId = TokenSign.getMemberIdByJwtToken(request);
        return  farmService.listPage(userId,paramMap);
    }

    @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);
    }

    @PostMapping("/listFunction")
    public Result listFunction(@RequestBody Map<String,String> paramsMap){
        String farmId = paramsMap.get("farmId");
        return  new Result(ResultCode.SUCCESS,farmService.getById(farmId).getFunction());
    }


}
