package com.foreverwin.mesnac.dispatch.controller;

import com.foreverwin.modular.core.util.R;
import com.foreverwin.modular.core.util.FrontPage;
import com.foreverwin.modular.core.util.CommonMethods;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.foreverwin.mesnac.dispatch.service.SurplusInventoryService;
import com.foreverwin.mesnac.dispatch.model.SurplusInventory;
import java.util.List;

/**
 *
 * @author Leon.L
 * @since 2021-07-19
 */
@RestController
@RequestMapping("/Z-SURPLUS-INVENTORY")
public class SurplusInventoryController {

    @Autowired
    public SurplusInventoryService surplusInventoryService;

    /**
    * 根据id查询
    *
    * @param id 主键
    * @return
    */
    @ResponseBody
    @GetMapping("/{id:.+}")
    public R getSurplusInventoryById(@PathVariable String id) {
        return R.ok( surplusInventoryService.getById(id));
    }

    /**
     * 查询所有数据
     *
     * @return
     */
    @ResponseBody
    @GetMapping("")
    public R getSurplusInventoryList(SurplusInventory surplusInventory){
        List<SurplusInventory> result;
        QueryWrapper<SurplusInventory> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(surplusInventory);
        result = surplusInventoryService.list(queryWrapper);
        return R.ok(result);
    }

    /**
     * 分页查询数据
     *
     * @param frontPage  分页信息
     * @return
     */
    @ResponseBody
    @GetMapping("/page")
    public R page(FrontPage<SurplusInventory> frontPage, SurplusInventory surplusInventory){
        IPage result;
        QueryWrapper<SurplusInventory> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(surplusInventory);
        if (frontPage.getGlobalQuery() != null && !"".equals(frontPage.getGlobalQuery().trim())) {
            //TODO modify global query
            queryWrapper.lambda().and(wrapper -> wrapper
                .like(SurplusInventory::getHandle, frontPage.getGlobalQuery())
                .or().like(SurplusInventory::getSite, frontPage.getGlobalQuery())
                .or().like(SurplusInventory::getParentInventory, frontPage.getGlobalQuery())
                .or().like(SurplusInventory::getInventory, frontPage.getGlobalQuery())
                .or().like(SurplusInventory::getItemBo, frontPage.getGlobalQuery())
                .or().like(SurplusInventory::getLenght, frontPage.getGlobalQuery())
                .or().like(SurplusInventory::getWidth, frontPage.getGlobalQuery())
                .or().like(SurplusInventory::getCreateUser, frontPage.getGlobalQuery())
                .or().like(SurplusInventory::getModifyUser, frontPage.getGlobalQuery())
    );
        }
        result = surplusInventoryService.page(frontPage.getPagePlus(), queryWrapper);
        return R.ok(result);
    }

    /**
     * 新增
     * @param surplusInventory  传递的实体
     * @return  null 失败  实体成功
     */
    @PostMapping
    public R save(@RequestBody SurplusInventory surplusInventory) {
        return R.ok(surplusInventoryService.save(surplusInventory));
    }

    /**
     * 修改
     * @param surplusInventory  传递的实体
     * @return  null 失败  实体成功
     */
    @PutMapping
    public R updateById(@RequestBody SurplusInventory surplusInventory) {
        return R.ok(surplusInventoryService.updateById(surplusInventory));
    }

    /**
     * 根据id删除对象
     * @param id  实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.DELETE, value = "/{id:.+}")
    public R removeById(@PathVariable("id") String id){
        return R.ok(surplusInventoryService.removeById(id));
    }

    /**
     * 批量删除对象
     * @param ids 实体集合ID
     * @return  0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/delete-batch")
    public R removeByIds(List<String> ids){
        return R.ok(surplusInventoryService.removeByIds(ids));
    }
}