package com.ruoyi.cai.controller.app;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.cai.dto.app.query.DynamicAddReq;
import com.ruoyi.cai.dto.app.query.DynamicQuery;
import com.ruoyi.cai.dto.app.query.IdRes;
import com.ruoyi.cai.dto.app.vo.DynamicListVo;
import com.ruoyi.cai.service.DynamicService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.helper.LoginHelper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/dynamic")
@Tag(name = "广场动态相关接口")
public class UserDynamicAppController {

    @Autowired
    private DynamicService dynamicService;

    @GetMapping("/page")
    @Operation(summary = "动态查询动态")
    @Log(title = "动态查询动态", businessType = BusinessType.OTHER, isSaveDb = false)
    public R<List<DynamicListVo>> page(PageQuery pageQuery, DynamicQuery query){
        query.setCurrentUserId(LoginHelper.getUserId());
        Page<DynamicListVo> resp =  dynamicService.pageApp(pageQuery,query);
        return R.ok(resp.getRecords());
    }

    @PostMapping("/push")
    @Operation(summary = "发布动态")
    @Log(title = "发布动态", businessType = BusinessType.OTHER, isSaveDb = true)
    public R<Void> push(@RequestBody DynamicAddReq res){
        res.setUserId(LoginHelper.getUserId());
        dynamicService.saveDynamic(res);
        return R.ok();
    }

    @PostMapping("/delete")
    @Operation(summary = "删除动态")
    @Log(title = "删除动态", businessType = BusinessType.OTHER, isSaveDb = true)
    public R<Void> delete(@RequestBody IdRes res){
        dynamicService.deleteDynamic(res.getId());
        return R.ok();
    }

}
