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.common.system.entity.User;
import com.eleadmin.oa.entity.Link;
import com.eleadmin.oa.param.LinkParam;
import com.eleadmin.oa.server.LinkService;
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/link")
public class LinkController extends BaseController {
    @Resource
    private LinkService linkService;

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

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

    @PreAuthorize("hasAuthority('sys:link:list')")
    @OperationLog
    @ApiOperation("根据id查询链接")
    @GetMapping("/{id}")
    public ApiResult<Link> get(@PathVariable("id") Integer id) {
        return success(linkService.getById(id));
    }

    @PreAuthorize("hasAuthority('sys:link:save')")
    @ApiOperation("添加链接")
    @PostMapping()
    public ApiResult<?> add(@RequestBody Link link) {
        if (linkService.save(link)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('sys:link:update')")
    @OperationLog
    @ApiOperation("修改链接")
    @PutMapping()
    public ApiResult<?> update(@RequestBody Link link) {
        if (linkService.updateById(link)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

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

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

}
