package com.gxwebsoft.cms.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.cms.service.FollowService;
import com.gxwebsoft.cms.entity.Follow;
import com.gxwebsoft.cms.param.FollowParam;
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 2024-12-11 18:25:28
 */
@Api(tags = "关注管理")
@RestController
@RequestMapping("/api/cms/follow")
public class FollowController extends BaseController {
    @Resource
    private FollowService followService;

    @GetMapping("/check")
    public ApiResult<Follow> check(FollowParam param) {
        // 使用关联查询
        return success(followService.check(getLoginUserId(), param.getFollowUserId()));
    }

    @ApiOperation("分页查询关注")
    @GetMapping("/page")
    public ApiResult<PageResult<Follow>> page(FollowParam param) {
        // 使用关联查询
        return success(followService.pageRel(param));
    }

    @ApiOperation("查询全部关注")
    @GetMapping()
    public ApiResult<List<Follow>> list(FollowParam param) {
        // 使用关联查询
        return success(followService.listRel(param));
    }

    @PreAuthorize("hasAuthority('cms:follow:list')")
    @ApiOperation("根据id查询关注")
    @GetMapping("/{id}")
    public ApiResult<Follow> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(followService.getByIdRel(id));
    }

    @ApiOperation("添加关注")
    @PostMapping()
    public ApiResult<?> save(@RequestBody Follow follow) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
            follow.setUserId(loginUser.getUserId());
            Follow follow1 = followService.check(loginUser.getUserId(), follow.getId());
            if (follow1 != null) {
                followService.removeById(follow1.getId());
                return success("取消成功");
            }
        }
        if (followService.save(follow)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @ApiOperation("修改关注")
    @PutMapping()
    public ApiResult<?> update(@RequestBody Follow follow) {
        if (followService.updateById(follow)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @ApiOperation("删除关注")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (followService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @ApiOperation("批量添加关注")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<Follow> list) {
        if (followService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @ApiOperation("批量修改关注")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<Follow> batchParam) {
        if (batchParam.update(followService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @ApiOperation("批量删除关注")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (followService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
