package com.huimv.admin.system.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huimv.admin.system.entity.ClientConfig;
import com.huimv.admin.system.service.IClientConfigService;
import com.huimv.common.utils.Result;
import com.huimv.common.utils.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/clientConfig")
public class ClientConfigController {
    @Autowired
    private IClientConfigService clientConfigService;

    @PostMapping("/add")
    public Result add(@RequestBody ClientConfig clientConfig){
        clientConfigService.save(clientConfig);
        return new Result(10000,"添加成功",true);
    }

    @PostMapping("/update")
    public Result update(@RequestBody ClientConfig clientConfig){
        QueryWrapper<ClientConfig> farmId = new QueryWrapper<ClientConfig>().eq("farm_id", clientConfig.getFarmId());
        ClientConfig one = clientConfigService.getOne(farmId);
        if (ObjectUtil.isEmpty(one)){
            clientConfigService.save(clientConfig);

        }else {
            clientConfigService.update(clientConfig,farmId);
        }

        return new Result(10000,"修改成功",true);
    }

    @GetMapping("/remove")
    public Result remove(@RequestParam(name = "id") Integer id){
        clientConfigService.removeById(id);
        return new Result(10000,"删除成功",true);
    }

    @GetMapping("/list")
    public Result list(@RequestParam(name = "farmId") Integer farmId){

        return new Result(ResultCode.SUCCESS,  clientConfigService.getOne(new QueryWrapper<ClientConfig>().eq("farm_id", farmId)));
    }
}
