package com.foreverwin.mesnac.meapi.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.foreverwin.mesnac.meapi.model.Item;
import com.foreverwin.mesnac.meapi.service.ItemService;
import com.foreverwin.modular.core.util.CommonMethods;
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;

/**
 * @author Ervin Chen
 * @since 2020-10-19
 */
@RestController
@RequestMapping("/ITEM")
public class ItemController {

    @Autowired
    public ItemService itemService;

    /**
     * 查询所有数据
     *
     * @return
     */
    @ResponseBody
    @GetMapping("")
    public R getItemList(Item item) {
        String site = CommonMethods.getSite();
        item.setSite(site);
        List<Item> result;
        item.setSite(CommonMethods.getSite());
        QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(item);
        result = itemService.list(queryWrapper);
        return R.ok(result);
    }

    /**
     * 分页查询数据
     *
     * @param frontPage  分页信息
     * @return
     */
    @ResponseBody
    @GetMapping("/getItemPageList")
    public R getItemPageList(FrontPage<Item> frontPage, Item item){
        IPage result;
        try {
            String site = CommonMethods.getSite();

            item.setSite(site);
            item.setCurrentRevision("true");
            QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
            queryWrapper.setEntity(item);
            if (frontPage.getGlobalQuery() != null && !"".equals(frontPage.getGlobalQuery().trim())) {
                queryWrapper
                        .like(Item.ITEM, frontPage.getGlobalQuery())
                        .or().like("DESCRIPTION", frontPage.getGlobalQuery())
                        .or().like(Item.STATUS_BO, frontPage.getGlobalQuery())
                        .or().like(Item.ITEM_TYPE, frontPage.getGlobalQuery());
            }
            result = itemService.selectPage(frontPage.getPagePlus(), queryWrapper);
        } catch (Exception e) {
            return R.failed(e.getMessage());
        }
        return R.ok(result);
    }

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

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

    @GetMapping("selectById")
    public R selectById( String itemBo){
        return R.ok(itemService.selectById(itemBo));
    }

    /**
     * 根据物料找出图号
     */
    @GetMapping("/findMapByItemBo")
    public R findMapByItemBo(String itemBo){
        return R.ok(itemService.findMapByItemBo(itemBo));
    }

}