package com.eleadmin.oa.controller;

import com.eleadmin.common.core.annotation.OperationLog;
import com.eleadmin.common.core.web.ApiResult;
import com.eleadmin.common.core.web.BaseController;
import com.eleadmin.common.core.web.PageParam;
import com.eleadmin.common.core.web.PageResult;
import com.eleadmin.oa.entity.App;
import com.eleadmin.oa.param.LinkParam;
import com.eleadmin.oa.server.AppService;
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 gxwebsoft.com
 * @since 2020-03-14 11:29:03
 */
@Api(tags = "应用管理")
@RestController
@RequestMapping("/api/app")
public class AppController extends BaseController {
    @Resource
    private AppService appService;

    @PreAuthorize("hasAuthority('sys:app:list')")
    @OperationLog
    @ApiOperation("分页查询应用")
    @GetMapping("/page")
    public ApiResult<PageResult<App>> page(LinkParam param) {
        PageParam<App, LinkParam> page = new PageParam<>(param);
        page.setDefaultOrder("sort_number, clicks desc");
        return success(appService.page(page, page.getWrapper()));
    }

    @PreAuthorize("hasAuthority('sys:app:list')")
    @OperationLog
    @ApiOperation("查询全部应用")
    @GetMapping()
    public ApiResult<List<App>> list(LinkParam param) {
        PageParam<App, LinkParam> page = new PageParam<>(param);
        page.setDefaultOrder("sort_number, clicks desc");
        return success(appService.list(page.getOrderWrapper()));
    }

    @PreAuthorize("hasAuthority('sys:app:list')")
    @OperationLog
    @ApiOperation("根据id查询应用")
    @GetMapping("/{id}")
    public ApiResult<App> get(@PathVariable("id") Integer id) {
        return success(appService.getById(id));
    }

    @PreAuthorize("hasAuthority('sys:app:save')")
    @ApiOperation("添加应用")
    @PostMapping()
    public ApiResult<?> add(@RequestBody App app) {
        if (appService.save(app)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('sys:app:update')")
    @OperationLog
    @ApiOperation("修改应用")
    @PutMapping()
    public ApiResult<?> update(@RequestBody App app) {
        if (appService.updateById(app)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('sys:app:remove')")
    @OperationLog
    @ApiOperation("删除应用")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (appService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('sys:app:save')")
    @OperationLog
    @ApiOperation("批量添加应用")
    @PostMapping("/batch")
    public ApiResult<List<String>> saveBatch(@RequestBody List<App> list) {
        if (appService.saveBatch(list)) {
            return success("添加成功", null);
        }
        return fail("添加失败", null);
    }

    @PreAuthorize("hasAuthority('sys:app:remove')")
    @OperationLog
    @ApiOperation("批量删除应用")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (appService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
