package com.eleadmin.test.controller;

import com.eleadmin.common.core.web.BaseController;
import com.eleadmin.test.service.TenantSettingService;
import com.eleadmin.test.entity.TenantSetting;
import com.eleadmin.test.param.TenantSettingParam;
import com.eleadmin.common.core.web.ApiResult;
import com.eleadmin.common.core.web.PageResult;
import com.eleadmin.common.core.web.PageParam;
import com.eleadmin.common.core.web.BatchParam;
import com.eleadmin.common.core.annotation.OperationLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 系统设置表控制器
 *
 * @author 科技小王子
 * @since 2022-08-29 09:27:05
 */
@Api(tags = "系统设置表管理")
@RestController
@RequestMapping("/api/test/tenant-setting")
public class TenantSettingController extends BaseController {
    @Resource
    private TenantSettingService tenantSettingService;

    @PreAuthorize("hasAuthority('test:tenantSetting:list')")
    @OperationLog
    @ApiOperation("分页查询系统设置表")
    @GetMapping("/page")
    public ApiResult<PageResult<TenantSetting>> page(TenantSettingParam param) {
        PageParam<TenantSetting, TenantSettingParam> page = new PageParam<>(param);
        //page.setDefaultOrder("create_time desc");
        return success(tenantSettingService.page(page, page.getWrapper()));
        // 使用关联查询
        //return success(tenantSettingService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('test:tenantSetting:list')")
    @OperationLog
    @ApiOperation("查询全部系统设置表")
    @GetMapping()
    public ApiResult<List<TenantSetting>> list(TenantSettingParam param) {
        PageParam<TenantSetting, TenantSettingParam> page = new PageParam<>(param);
        //page.setDefaultOrder("create_time desc");
        return success(tenantSettingService.list(page.getOrderWrapper()));
        // 使用关联查询
        //return success(tenantSettingService.listRel(param));
    }

    @PreAuthorize("hasAuthority('test:tenantSetting:list')")
    @OperationLog
    @ApiOperation("根据id查询系统设置表")
    @GetMapping("/{id}")
    public ApiResult<TenantSetting> get(@PathVariable("id") Integer id) {
        return success(tenantSettingService.getById(id));
        // 使用关联查询
        //return success(tenantSettingService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('test:tenantSetting:save')")
    @OperationLog
    @ApiOperation("添加系统设置表")
    @PostMapping()
    public ApiResult<?> save(@RequestBody TenantSetting tenantSetting) {
        if (tenantSettingService.save(tenantSetting)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('test:tenantSetting:update')")
    @OperationLog
    @ApiOperation("修改系统设置表")
    @PutMapping()
    public ApiResult<?> update(@RequestBody TenantSetting tenantSetting) {
        if (tenantSettingService.updateById(tenantSetting)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('test:tenantSetting:remove')")
    @OperationLog
    @ApiOperation("删除系统设置表")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (tenantSettingService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('test:tenantSetting:save')")
    @OperationLog
    @ApiOperation("批量添加系统设置表")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<TenantSetting> list) {
        if (tenantSettingService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('test:tenantSetting:update')")
    @OperationLog
    @ApiOperation("批量修改系统设置表")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<TenantSetting> batchParam) {
        if (batchParam.update(tenantSettingService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('test:tenantSetting:remove')")
    @OperationLog
    @ApiOperation("批量删除系统设置表")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (tenantSettingService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
