package com.njzscloud.supervisory.resource.cotroller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.njzscloud.supervisory.resource.pojo.SysResourceEntity;
import com.njzscloud.common.core.utils.R;
import com.njzscloud.common.mp.support.PageParam;
import com.njzscloud.common.mp.support.PageResult;
import com.njzscloud.supervisory.resource.service.SysResourceService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统资源表
 */
@Slf4j
@RestController
@RequestMapping("/sys_resource")
@RequiredArgsConstructor
public class SysResourceController {

    private final SysResourceService sysResourceService;

    /**
     * 新增
     *
     * @param sysResourceEntity 数据
     */
    @PostMapping("/add")
    public R<?> add(@RequestBody SysResourceEntity sysResourceEntity) {
        sysResourceService.add(sysResourceEntity);
        return R.success();
    }

    /**
     * 修改
     *
     * @param sysResourceEntity 数据
     */
    @PostMapping("/modify")
    public R<?> modify(@RequestBody SysResourceEntity sysResourceEntity) {
        sysResourceService.modify(sysResourceEntity);
        return R.success();
    }

    /**
     * 删除
     *
     * @param ids Ids
     */
    @PostMapping("/del")
    public R<?> del(@RequestBody List<Long> ids) {
        sysResourceService.del(ids);
        return R.success();
    }

    /**
     * 详情
     *
     * @param id Id
     * @return SysResourceEntity 结果
     */
    @GetMapping("/detail")
    public R<SysResourceEntity> detail(@RequestParam Long id) {
        return R.success(sysResourceService.detail(id));
    }

    /**
     * 分页查询
     *
     * @param sysResourceEntity 筛选条件
     * @param pageParam         分页参数
     * @return PageResult&lt;SysResourceEntity&gt; 分页结果
     */
    @GetMapping("/paging")
    public R<PageResult<SysResourceEntity>> paging(PageParam pageParam, SysResourceEntity sysResourceEntity) {
        return R.success(sysResourceService.paging(pageParam, sysResourceEntity));
    }

    @GetMapping("/list")
    public R<List<SysResourceEntity>> list(
            @RequestParam(value = "tableName", required = false) String tableName,
            @RequestParam(value = "keywords", required = false) String keywords
    ) {
        return R.success(sysResourceService.list(Wrappers.<SysResourceEntity>lambdaQuery()
                .eq(StrUtil.isNotBlank(tableName), SysResourceEntity::getTableName, tableName)
                .and(StrUtil.isNotBlank(keywords), ew -> ew.like(SysResourceEntity::getSn, keywords)
                        .or().like(SysResourceEntity::getMemo, keywords))
        ));
    }

}
