package com.foreverwin.mesnac.equip.controller;

import com.foreverwin.mesnac.common.enums.HandleEnum;
import com.foreverwin.mesnac.meapi.model.ResourceType;
import com.foreverwin.mesnac.meapi.service.ResrceService;
import com.foreverwin.modular.core.util.CommonMethods;
import com.foreverwin.modular.core.util.R;
import com.foreverwin.modular.core.util.FrontPage;
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.ResourceInspectResourceService;
import com.foreverwin.mesnac.equip.model.ResourceInspectResource;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author pavel.Liu
 * @since 2021-06-11
 */
@RestController
@RequestMapping("/Z-RESOURCE-INSPECT-RESOURCE")
public class ResourceInspectResourceController {

    @Autowired
    public ResourceInspectResourceService resourceInspectResourceService;



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

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

    /**
     * 分页查询数据
     *
     * @param frontPage  分页信息
     * @return
     */
    @ResponseBody
    @GetMapping("/page")
    public R page(FrontPage<ResourceInspectResource> frontPage, ResourceInspectResource resourceInspectResource){
        IPage result;
        QueryWrapper<ResourceInspectResource> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(resourceInspectResource);
        if (frontPage.getGlobalQuery() != null && !"".equals(frontPage.getGlobalQuery().trim())) {
            //TODO modify global query
            queryWrapper.lambda().and(wrapper -> wrapper
                .like(ResourceInspectResource::getHandle, frontPage.getGlobalQuery())
                .or().like(ResourceInspectResource::getResourceInspectPlanBo, frontPage.getGlobalQuery())
                .or().like(ResourceInspectResource::getResourceBo, frontPage.getGlobalQuery())
                .or().like(ResourceInspectResource::getResourceNo, frontPage.getGlobalQuery())
                .or().like(ResourceInspectResource::getDescription, frontPage.getGlobalQuery())
                .or().like(ResourceInspectResource::getStatus, frontPage.getGlobalQuery())
                .or().like(ResourceInspectResource::getCreateUser, frontPage.getGlobalQuery())
                .or().like(ResourceInspectResource::getModifyUser, frontPage.getGlobalQuery())
    );
        }
        result = resourceInspectResourceService.page(frontPage.getPagePlus(), queryWrapper);
        return R.ok(result);
    }

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

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

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

    /**
     * 获取同一类型的所有设备信息
     * @param resourceTypeNo
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getResourceListByResourceType")
    public R getResourceListByResourceType(String resourceTypeNo) {
        try {
            return R.ok(resourceInspectResourceService.getResourceListByResourceType(resourceTypeNo));
        } catch (Exception e) {
            return R.failed("获取设备信息失败：" + e.getMessage());
        }
    }

    /**
     * 新增或修改
     * @param resourceInspectResource  传递的实体
     * @return  null 失败  实体成功
     */
    @PostMapping("/saveOrUpdate")
    public R saveOrUpdate(@RequestBody ResourceInspectResource resourceInspectResource) {
        return R.ok(resourceInspectResourceService.save(resourceInspectResource));
    }


}