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.Operation;
import com.foreverwin.mesnac.meapi.service.OperationService;
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 Robert
 * @since 2021-06-03
 */
@RestController
@RequestMapping("/OPERATION")
public class OperationController {

    @Autowired
    public OperationService operationService;

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

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


    @ResponseBody
    @GetMapping("/getOperationPageList")
    public R getItemPageList(FrontPage<Operation> frontPage, Operation operation){
        IPage result;
        try {
            QueryWrapper<Operation> queryWrapper = new QueryWrapper<>();
            operation.setSite(CommonMethods.getSite());
            queryWrapper.setEntity(operation);

            if (frontPage.getGlobalQuery() != null && !"".equals(frontPage.getGlobalQuery().trim())) {
                queryWrapper
                        .like(Operation.OPERATION, frontPage.getGlobalQuery())
                        .or().like(Operation.REVISION, frontPage.getGlobalQuery())
                        .or().like("DESCRIPTION", frontPage.getGlobalQuery());
            }
            result = operationService.selectPage(frontPage.getPagePlus(), queryWrapper);
        } catch (Exception e) {
            return R.failed(e.getMessage());
        }

        return R.ok(result);
    }

    @ResponseBody
    @GetMapping("/getOperationStep")
    public R getOperationStep(FrontPage<Operation> frontPage, Operation operation){
        IPage result;
        try {
            QueryWrapper<Operation> queryWrapper = new QueryWrapper<>();
            operation.setSite(CommonMethods.getSite());
            operation.setOperation(frontPage.getGlobalQuery());
            queryWrapper.setEntity(operation);
            result = operationService.selectOperationStep(frontPage.getPagePlus(), queryWrapper);
        } catch (Exception e) {
            return R.failed(e.getMessage());
        }

        return R.ok(result);
    }


}