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

/**
 *
 * @author Philip
 * @since 2021-08-12
 */
@RestController
@RequestMapping("/ROUTER-STEP")
public class RouterStepController {

    @Autowired
    public RouterStepService routerStepService;

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

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

    /**
     * 分页查询数据
     *
     * @param frontPage  分页信息
     * @return
     */
    @ResponseBody
    @GetMapping("/page")
    public R page(FrontPage<RouterStep> frontPage, RouterStep routerStep){
        IPage result;
        QueryWrapper<RouterStep> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(routerStep);
        if (frontPage.getGlobalQuery() != null && !"".equals(frontPage.getGlobalQuery().trim())) {
            //TODO modify global query
            queryWrapper.lambda().and(wrapper -> wrapper
                .like(RouterStep::getHandle, frontPage.getGlobalQuery())
                .or().like(RouterStep::getRouterBo, frontPage.getGlobalQuery())
                .or().like(RouterStep::getStepId, frontPage.getGlobalQuery())
                .or().like(RouterStep::getDescription, frontPage.getGlobalQuery())
                .or().like(RouterStep::getRework, frontPage.getGlobalQuery())
                .or().like(RouterStep::getQueueDecisionType, frontPage.getGlobalQuery())
                .or().like(RouterStep::getRouterCompGbo, frontPage.getGlobalQuery())
                .or().like(RouterStep::getReportingStep, frontPage.getGlobalQuery())
                .or().like(RouterStep::getReportingCenterBo, frontPage.getGlobalQuery())
                .or().like(RouterStep::getTabularStepType, frontPage.getGlobalQuery())
                .or().like(RouterStep::getScrapReportingStep, frontPage.getGlobalQuery())
                .or().like(RouterStep::getIsLastReportingStep, frontPage.getGlobalQuery())
                .or().like(RouterStep::getErpSequence, frontPage.getGlobalQuery())
                .or().like(RouterStep::getErpControlKeyBo, frontPage.getGlobalQuery())
                .or().like(RouterStep::getErpWorkCenterBo, frontPage.getGlobalQuery())
                .or().like(RouterStep::getErpInspectionComplete, frontPage.getGlobalQuery())
                .or().like(RouterStep::getErpOperation, frontPage.getGlobalQuery())
                .or().like(RouterStep::getOperationActivityId, frontPage.getGlobalQuery())
    );
        }
        result = routerStepService.page(frontPage.getPagePlus(), queryWrapper);
        return R.ok(result);
    }

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

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

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