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

/**
 *
 * @author Leon.L
 * @since 2021-05-26
 */
@RestController
@RequestMapping("/RESOURCE-TYPE")
public class ResourceTypeController {

    @Autowired
    public ResourceTypeService resourceTypeService;


    /**
     * 查询所有数据
     *
     * @return
     */
    @ResponseBody
    @GetMapping("")
    public R getResourceTypeList(ResourceType resourceType){
        String site = CommonMethods.getSite();
        resourceType.setSite(site);
        List<ResourceType> result;
        QueryWrapper<ResourceType> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(resourceType);
        result = resourceTypeService.list(queryWrapper);
        return R.ok(result);
    }

    /**
     * 分页查询数据
     *
     * @param frontPage  分页信息
     * @return
     */
    @ResponseBody
    @GetMapping("/page")
    public R page(FrontPage<ResourceType> frontPage, ResourceType resourceType){
        IPage result;
        QueryWrapper<ResourceType> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(resourceType);
        resourceType.setSite(CommonMethods.getSite());
        if (frontPage.getGlobalQuery() != null && !"".equals(frontPage.getGlobalQuery().trim())) {
            queryWrapper.lambda().and(wrapper -> wrapper
                .like(ResourceType::getHandle, frontPage.getGlobalQuery())
                .or().like(ResourceType::getSite, frontPage.getGlobalQuery())
                .or().like(ResourceType::getResourceType, frontPage.getGlobalQuery())
                .or().like(ResourceType::getDescription, frontPage.getGlobalQuery()));
        }
        result = resourceTypeService.page(frontPage.getPagePlus(), queryWrapper);
        return R.ok(result);
    }

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

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

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