package com.foreverwin.mesnac.production.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.foreverwin.mesnac.meapi.dto.WorkCenterDto;
import com.foreverwin.mesnac.production.dto.LoadInventoryDto;
import com.foreverwin.mesnac.production.model.LoadInventory;
import com.foreverwin.mesnac.production.service.LoadInventoryService;
import com.foreverwin.modular.core.util.FrontPage;
import com.foreverwin.modular.core.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 *
 * @author Philip
 * @since 2021-07-09
 */
@RestController
@RequestMapping("/Z-LOAD-INVENTORY")
public class LoadInventoryController {

    @Autowired
    public LoadInventoryService loadInventoryService;

    /**
     * 查询资源上料记录
     *
     * @return
     */
    @ResponseBody
    @GetMapping("/loadList")
    public R getLoadList(WorkCenterDto workCenterDto){
        Map<String, Object> result;
        result = loadInventoryService.getLoadInventoryList(workCenterDto);
        return R.ok(result);
    }

    /**
     * 上料
     *
     * @return
     */
    @ResponseBody
    @GetMapping("/loadInventory")
    public R loadInventory(LoadInventory loadInventory){
        loadInventoryService.loadInventory(loadInventory);
        return R.ok("上料成功");
    }
    /**
     * 卸料
     *
     * @return
     */
    @ResponseBody
    @PostMapping("/unLoadInventory")
    public R loadInventory(@RequestBody List<LoadInventoryDto> inventoryDtoList){
        loadInventoryService.unLoadInventory(inventoryDtoList);
        return R.ok();
    }

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

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

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

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

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

    /**
     * 根据id删除对象
     * @param id  实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.DELETE, value = "/{id:.+}")
    public R removeById(@PathVariable("id") String id){
        return R.ok(loadInventoryService.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(loadInventoryService.removeByIds(ids));
    }
}