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.ResourceInspectTaskParamService;
import com.foreverwin.mesnac.equip.model.ResourceInspectTaskParam;
import java.util.List;

/**
 *
 * @author pavel.Liu
 * @since 2021-07-01
 */
@RestController
@RequestMapping("/Z-RESOURCE-INSPECT-TASK-PARAM")
public class ResourceInspectTaskParamController {

    @Autowired
    public ResourceInspectTaskParamService resourceInspectTaskParamService;

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

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

    /**
     * 分页查询数据
     *
     * @param frontPage  分页信息
     * @return
     */
    @ResponseBody
    @GetMapping("/page")
    public R page(FrontPage<ResourceInspectTaskParam> frontPage, ResourceInspectTaskParam resourceInspectTaskParam){
        IPage result;
        QueryWrapper<ResourceInspectTaskParam> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(resourceInspectTaskParam);
        if (frontPage.getGlobalQuery() != null && !"".equals(frontPage.getGlobalQuery().trim())) {
            //TODO modify global query
            queryWrapper.lambda().and(wrapper -> wrapper
                .like(ResourceInspectTaskParam::getHandle, frontPage.getGlobalQuery())
                .or().like(ResourceInspectTaskParam::getInspectNo, frontPage.getGlobalQuery())
                .or().like(ResourceInspectTaskParam::getResourceInspectTaskBo, frontPage.getGlobalQuery())
                .or().like(ResourceInspectTaskParam::getDescription, frontPage.getGlobalQuery())
                .or().like(ResourceInspectTaskParam::getInspectMethod, frontPage.getGlobalQuery())
                .or().like(ResourceInspectTaskParam::getInspectDataType, frontPage.getGlobalQuery())
                .or().like(ResourceInspectTaskParam::getParamUnit, frontPage.getGlobalQuery())
                .or().like(ResourceInspectTaskParam::getRequired, frontPage.getGlobalQuery())
                .or().like(ResourceInspectTaskParam::getStandardValue, frontPage.getGlobalQuery())
                .or().like(ResourceInspectTaskParam::getResultValue, frontPage.getGlobalQuery())
                .or().like(ResourceInspectTaskParam::getResult, frontPage.getGlobalQuery())
                .or().like(ResourceInspectTaskParam::getRemark, frontPage.getGlobalQuery())
                .or().like(ResourceInspectTaskParam::getCreateUser, frontPage.getGlobalQuery())
                .or().like(ResourceInspectTaskParam::getModifyUser, frontPage.getGlobalQuery())
    );
        }
        result = resourceInspectTaskParamService.page(frontPage.getPagePlus(), queryWrapper);
        return R.ok(result);
    }

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

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

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