package com.foreverwin.mesnac.meapi.controller;

import com.foreverwin.modular.core.util.CommonMethods;
import com.foreverwin.modular.core.util.R;
import com.foreverwin.modular.core.util.FrontPage;
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.meapi.service.InventoryService;
import com.foreverwin.mesnac.meapi.model.Inventory;

import java.math.BigDecimal;
import java.util.List;

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

    @Autowired
    public InventoryService inventoryService;

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

    /**
     * 查询所有数据
     *
     * @return
     */
    @ResponseBody
    @GetMapping("")
    public R getInventoryList(Inventory inventory){
        List<Inventory> result;
        QueryWrapper<Inventory> queryWrapper = new QueryWrapper<>();
        inventory.setSite(CommonMethods.getSite());
        queryWrapper.setEntity(inventory);
        result = inventoryService.list(queryWrapper);
        return R.ok(result);
    }

    /**
     * 分页查询数据
     *
     * @param frontPage  分页信息
     * @return
     */
    @ResponseBody
    @GetMapping("/page")
    public R page(FrontPage<Inventory> frontPage, Inventory inventory){
        IPage result;
        QueryWrapper<Inventory> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(inventory);
        if (frontPage.getGlobalQuery() != null && !"".equals(frontPage.getGlobalQuery().trim())) {
            //TODO modify global query
            queryWrapper.lambda().and(wrapper -> wrapper
                .like(Inventory::getHandle, frontPage.getGlobalQuery())
                .or().like(Inventory::getSite, frontPage.getGlobalQuery())
                .or().like(Inventory::getInventoryId, frontPage.getGlobalQuery())
                .or().like(Inventory::getItemBo, frontPage.getGlobalQuery())
                .or().like(Inventory::getInventoryContextGbo, frontPage.getGlobalQuery())
                .or().like(Inventory::getDescription, frontPage.getGlobalQuery())
                .or().like(Inventory::getAssyDataTypeBo, frontPage.getGlobalQuery())
                .or().like(Inventory::getStatusBo, frontPage.getGlobalQuery())
                .or().like(Inventory::getWorkCenterLocBo, frontPage.getGlobalQuery())
                .or().like(Inventory::getWorkCenterLocRes, frontPage.getGlobalQuery())
                .or().like(Inventory::getOperationLocBo, frontPage.getGlobalQuery())
                .or().like(Inventory::getOperationLocRes, frontPage.getGlobalQuery())
                .or().like(Inventory::getResourceLocBo, frontPage.getGlobalQuery())
                .or().like(Inventory::getResourceLocRes, frontPage.getGlobalQuery())
                .or().like(Inventory::getShopOrderLocBo, frontPage.getGlobalQuery())
                .or().like(Inventory::getShopOrderLocRes, frontPage.getGlobalQuery())
                .or().like(Inventory::getShopOrderSetByErp, frontPage.getGlobalQuery())
                .or().like(Inventory::getOriginalUserBo, frontPage.getGlobalQuery())
                .or().like(Inventory::getStorageLocationBo, frontPage.getGlobalQuery())
                .or().like(Inventory::getHasBeenUsed, frontPage.getGlobalQuery())
                .or().like(Inventory::getParentInventoryBo, frontPage.getGlobalQuery())
                .or().like(Inventory::getErpInventory, frontPage.getGlobalQuery())
    );
        }
        result = inventoryService.page(frontPage.getPagePlus(), queryWrapper);
        return R.ok(result);
    }

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

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

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

    /**
     * 根据条码号，返回库存数量
     * 使用场景：扫描物料条码，返回库存数量
     * @param inventory
     * @return
     */
    @ResponseBody
    @GetMapping("/getInventoryOne")
    public R getInventoryOne(Inventory inventory){
        List<Inventory> inventoryList = inventoryService.selectList(inventory);
        if (inventoryList.size() < 1){
            inventory.setQtyOnHand(new BigDecimal(0));
            return R.ok(inventory);
        }
        if (inventoryList.size() == 1){
            return R.ok(inventoryList.get(0));
        }
        return R.failed("一个条码匹配多个数据！");
    }

}