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.RouterService;
import com.foreverwin.mesnac.meapi.model.Router;
import java.util.List;

/**
 *
 * @author Leon.L
 * @since 2021-06-02
 */
@RestController
@RequestMapping("/ROUTER")
public class RouterController {

    @Autowired
    public RouterService routerService;

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

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

    /**
     * 分页查询数据
     *
     * @param frontPage  分页信息
     * @return
     */
    @ResponseBody
    @GetMapping("/page")
    public R page(FrontPage<Router> frontPage, Router router){
        IPage result;
        QueryWrapper<Router> queryWrapper = new QueryWrapper<>();
        String site = CommonMethods.getSite();
        router.setSite(site);
        queryWrapper.setEntity(router);
        if (frontPage.getGlobalQuery() != null && !"".equals(frontPage.getGlobalQuery().trim())) {
            //TODO modify global query
            queryWrapper.lambda().and(wrapper -> wrapper
                .like(Router::getHandle, frontPage.getGlobalQuery())
                .or().like(Router::getSite, frontPage.getGlobalQuery())
                .or().like(Router::getRouter, frontPage.getGlobalQuery())
                .or().like(Router::getRouterType, frontPage.getGlobalQuery())
                .or().like(Router::getDescription, frontPage.getGlobalQuery())
                .or().like(Router::getTemporaryRouter, frontPage.getGlobalQuery())
                .or().like(Router::getStatusBo, frontPage.getGlobalQuery())
                .or().like(Router::getEntryRouterStepBo, frontPage.getGlobalQuery())
                .or().like(Router::getCopiedFromRouterBo, frontPage.getGlobalQuery())
                .or().like(Router::getRevision, frontPage.getGlobalQuery())
                .or().like(Router::getCurrentRevision, frontPage.getGlobalQuery())
                .or().like(Router::getHasBeenReleased, frontPage.getGlobalQuery())
                .or().like(Router::getGuiRepresentation, frontPage.getGlobalQuery())
                .or().like(Router::getOriginalStatusBo, frontPage.getGlobalQuery())
                .or().like(Router::getDispositionGroupBo, frontPage.getGlobalQuery())
                .or().like(Router::getPrevSite, frontPage.getGlobalQuery())
                .or().like(Router::getOriginalTransferKey, frontPage.getGlobalQuery())
                .or().like(Router::getDisplayType, frontPage.getGlobalQuery())
                .or().like(Router::getSendAsShared, frontPage.getGlobalQuery())
                .or().like(Router::getSentToErp, frontPage.getGlobalQuery())
                .or().like(Router::getErpChangeNumber, frontPage.getGlobalQuery())
                .or().like(Router::getRelaxedFlow, frontPage.getGlobalQuery())
                .or().like(Router::getBomBo, frontPage.getGlobalQuery())
                .or().like(Router::getOrigin, frontPage.getGlobalQuery())
    );
        }
        result = routerService.page(frontPage.getPagePlus(), queryWrapper);
        return R.ok(result);
    }

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

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

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