package com.fjhx.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fjhx.entity.Warehouse;
import com.fjhx.service.WarehouseService;
import org.springblade.core.tool.api.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 仓库 前端控制器
 */
@RestController
@RequestMapping("/warehouse")
public class WarehouseController {

    @Autowired
    private WarehouseService warehouseService;

    @PostMapping("/page")
    public R page(@RequestBody Map<String, String> condition) {

        String type = condition.get("type");
        String keyword = condition.get("keyword");

        Page<Warehouse> result = warehouseService.page(
                warehouseService.createPage(condition),
                Wrappers.<Warehouse>lambdaQuery()
                        .eq(ObjectUtil.isNotEmpty(type), Warehouse::getType, type)
                        .like(ObjectUtil.isNotEmpty(keyword), Warehouse::getName, keyword)
        );

        return R.success(result);
    }

    @PostMapping("/add")
    public R add(@RequestBody Warehouse warehouse) {
        warehouseService.addOrEdit(warehouse);
        return R.success();
    }

    @PostMapping("/edit")
    public R edit(@RequestBody Warehouse warehouse) {
        warehouseService.addOrEdit(warehouse);
        return R.success();
    }

    @PostMapping("/delete")
    public R delete(@RequestBody Warehouse warehouse) {
        warehouseService.removeById(warehouse.getId());
        return R.success();
    }

    @PostMapping("/list")
    public R list() {
        return R.success(warehouseService.list());
    }

}
