package com.foreverwin.mesnac.meapi.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.meapi.service.BomService;
import com.foreverwin.mesnac.meapi.model.Bom;
import java.util.List;

/**
 *
 * @author Leon.L
 * @since 2021-06-27
 */
@RestController
@RequestMapping("/BOM")
public class BomController {

    @Autowired
    public BomService bomService;

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

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

    /**
     * 分页查询数据
     *
     * @param frontPage  分页信息
     * @return
     */
    @ResponseBody
    @GetMapping("/page")
    public R page(FrontPage<Bom> frontPage, Bom bom){
        IPage result;
        QueryWrapper<Bom> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(bom);
        if (frontPage.getGlobalQuery() != null && !"".equals(frontPage.getGlobalQuery().trim())) {
            //TODO modify global query
            queryWrapper.lambda().and(wrapper -> wrapper
                .like(Bom::getHandle, frontPage.getGlobalQuery())
                .or().like(Bom::getSite, frontPage.getGlobalQuery())
                .or().like(Bom::getBom, frontPage.getGlobalQuery())
                .or().like(Bom::getBomType, frontPage.getGlobalQuery())
                .or().like(Bom::getDescription, frontPage.getGlobalQuery())
                .or().like(Bom::getStatusBo, frontPage.getGlobalQuery())
                .or().like(Bom::getTmpBom, frontPage.getGlobalQuery())
                .or().like(Bom::getCopiedFromBomBo, frontPage.getGlobalQuery())
                .or().like(Bom::getRevision, frontPage.getGlobalQuery())
                .or().like(Bom::getCurrentRevision, frontPage.getGlobalQuery())
                .or().like(Bom::getBomTemplate, frontPage.getGlobalQuery())
                .or().like(Bom::getHasBeenReleased, frontPage.getGlobalQuery())
                .or().like(Bom::getEffectivityControl, frontPage.getGlobalQuery())
                .or().like(Bom::getPrevSite, frontPage.getGlobalQuery())
                .or().like(Bom::getOriginalTransferKey, frontPage.getGlobalQuery())
                .or().like(Bom::getErpBillOfMaterial, frontPage.getGlobalQuery())
                .or().like(Bom::getModel, frontPage.getGlobalQuery())
    );
        }
        result = bomService.page(frontPage.getPagePlus(), queryWrapper);
        return R.ok(result);
    }

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

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

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