package com.gxwebsoft.cms.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.cms.service.ArticleLikeService;
import com.gxwebsoft.cms.entity.ArticleLike;
import com.gxwebsoft.cms.param.ArticleLikeParam;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.BatchParam;
import com.gxwebsoft.common.core.annotation.OperationLog;
import com.gxwebsoft.common.system.entity.User;
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 2023-10-22 15:47:09
 */
@Api(tags = "文章点赞")
@RestController
@RequestMapping("/api/cms/article-like")
public class ArticleLikeController extends BaseController {
    @Resource
    private ArticleLikeService articleLikeService;

    @ApiOperation("分页查询点赞文章")
    @GetMapping("/page")
    public ApiResult<PageResult<ArticleLike>> page(ArticleLikeParam param) {
        PageParam<ArticleLike, ArticleLikeParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(articleLikeService.page(page, page.getWrapper()));
        // 使用关联查询
        //return success(articleLikeService.pageRel(param));
    }

    @ApiOperation("查询全部点赞文章")
    @GetMapping()
    public ApiResult<List<ArticleLike>> list(ArticleLikeParam param) {
        PageParam<ArticleLike, ArticleLikeParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(articleLikeService.list(page.getOrderWrapper()));
        // 使用关联查询
        //return success(articleLikeService.listRel(param));
    }

    @ApiOperation("根据id查询点赞文章")
    @GetMapping("/{id}")
    public ApiResult<ArticleLike> get(@PathVariable("id") Integer id) {
        return success(articleLikeService.getById(id));
        // 使用关联查询
        //return success(articleLikeService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('cms:articleLike:save')")
    @ApiOperation("添加点赞文章")
    @PostMapping()
    public ApiResult<?> save(@RequestBody ArticleLike articleLike) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
          articleLike.setUserId(loginUser.getUserId());
        }
        if (articleLikeService.save(articleLike)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('cms:articleLike:update')")
    @ApiOperation("修改点赞文章")
    @PutMapping()
    public ApiResult<?> update(@RequestBody ArticleLike articleLike) {
        if (articleLikeService.updateById(articleLike)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('cms:articleLike:remove')")
    @ApiOperation("删除点赞文章")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (articleLikeService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('cms:articleLike:save')")
    @ApiOperation("批量添加点赞文章")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<ArticleLike> list) {
        if (articleLikeService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('cms:articleLike:update')")
    @ApiOperation("批量修改点赞文章")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<ArticleLike> batchParam) {
        if (batchParam.update(articleLikeService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('cms:articleLike:remove')")
    @ApiOperation("批量删除点赞文章")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (articleLikeService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
