package com.foreverwin.mesnac.equip.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.equip.service.ResourceRunTimeService;
import com.foreverwin.mesnac.equip.model.ResourceRunTime;
import java.util.List;

/**
 *
 * @author pavel.liu
 * @since 2021-10-09
 */
@RestController
@RequestMapping("/Z-RESOURCE-RUN-TIME")
public class ResourceRunTimeController {

    @Autowired
    public ResourceRunTimeService resourceRunTimeService;

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

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

    /**
     * 分页查询数据
     *
     * @param frontPage  分页信息
     * @return
     */
    @ResponseBody
    @GetMapping("/page")
    public R page(FrontPage<ResourceRunTime> frontPage, ResourceRunTime resourceRunTime){
        IPage result;
        QueryWrapper<ResourceRunTime> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(resourceRunTime);
        if (frontPage.getGlobalQuery() != null && !"".equals(frontPage.getGlobalQuery().trim())) {
            //TODO modify global query
            queryWrapper.lambda().and(wrapper -> wrapper
                .like(ResourceRunTime::getHandle, frontPage.getGlobalQuery())
                .or().like(ResourceRunTime::getSite, frontPage.getGlobalQuery())
                .or().like(ResourceRunTime::getSendDate, frontPage.getGlobalQuery())
                .or().like(ResourceRunTime::getResrce, frontPage.getGlobalQuery())
                .or().like(ResourceRunTime::getCreatedUser, frontPage.getGlobalQuery())
                .or().like(ResourceRunTime::getModifiedUser, frontPage.getGlobalQuery())
    );
        }
        result = resourceRunTimeService.page(frontPage.getPagePlus(), queryWrapper);
        return R.ok(result);
    }

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

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

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