package com.huimv.management.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.huimv.management.service.FarmService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.huimv.management.entity.FarmEntity;
import com.huimv.common.utils.PageUtils;
import com.huimv.common.utils.R;


/**
 * 牧场表
 *
 * @author yinhao
 * @version 1.0
 * @date 2021-05-07 15:32:42
 */
@Api(tags = "牧场表")
@RestController
@RequestMapping("management/farm")
public class FarmController {
    @Autowired
    private FarmService farmService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = farmService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id) {
        FarmEntity farm = farmService.getById(id);

        return R.ok().put("farm", farm);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody FarmEntity farm) {
//        Boolean isExist = farmService.findByName(farm);
//        if (isExist){
        farmService.save(farm);
        return R.ok();

    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody FarmEntity farm) {
        farmService.updateById(farm);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids) {
//		pigstyService.removeByIds(Arrays.asList(ids));
        Integer delete = farmService.delete(ids);
        if (delete == 1) {
            farmService.removeByIds(Arrays.asList(ids));
            return R.ok();
        } else {
            return R.error().put("msg", "牧场中有未清空猪舍");
        }


    }

    /**
     * 查询全部
     */
    @RequestMapping("/findAll")
    public R findAll() {
        List list = farmService.findAll();

        return R.ok().put("all", list);
    }

}
