package com.huimv.env.admin.controller;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;

import com.huimv.env.admin.common.token.TokenSign;
import com.huimv.env.admin.common.utils.Result;
import com.huimv.env.admin.common.utils.ResultCode;
import com.huimv.env.admin.entity.Farm;
import com.huimv.env.admin.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);
    }

    @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);
        }
        farm.setLocation(farm.getFrontLocation());
        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());
    }

//    @PostMapping("/getWeather")
//    public Result getWeather(@RequestBody Map<String,Integer> map) {
//        int farmId =  map.get("farmId");
//        Farm byId = farmService.getById(farmId);
//        String cityCode = byId.getLocation();
//        if (ObjectUtil.isEmpty(cityCode)){
//            cityCode = "330782";
//        }
//        String url="https://restapi.amap.com/v3/weather/weatherInfo?parameters&key=c1d8179ed2dda7ed48c56bed06c0c86e&city="+cityCode+"&extensions=base&output=JSON";
//        String result = HttpUtil.get(url);
//        WeatherDto weatherDto = JSONUtil.toBean(result, WeatherDto.class);
//        return new Result(ResultCode.SUCCESS,weatherDto);
//    }
}
