package com.cool.store.controller.webb;

import com.cool.store.dto.content.*;
import com.cool.store.entity.HyContentInfoDO;
import com.cool.store.exception.ApiException;
import com.cool.store.response.ResponseResult;
import com.cool.store.service.ContentService;
import com.cool.store.vo.HyContentInfoVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("pc/news")
@Api(tags = "PC动态")
@Slf4j
public class ContentController {

    @Autowired
    private ContentService contentService;

    @PostMapping("/queryTitles")
    @ApiOperation("搜索标题是否重复")
    public ResponseResult<Boolean> queryTitles(@RequestBody ContentQueryTitlesDto title) {
        return ResponseResult.success(contentService.queryTitles(title.getTitle()));
    }

    @PostMapping("/add")
    @ApiOperation("新增动态")
    public ResponseResult<String> addContent(@RequestBody ContentAddDto dto) throws ApiException {
        return ResponseResult.success(contentService.addNews(dto));
    }

    @PostMapping("/delete")
    @ApiOperation("删除动态")
    public ResponseResult deleteContent(@RequestBody ContentDelDto dto) {
        contentService.deleteContent(dto.getContentId());
        return ResponseResult.success();
    }

    @PostMapping("/modify")
    @ApiOperation("修改动态")
    public ResponseResult updateContent(@RequestBody ContentUpdateDto dto) throws ApiException {
        contentService.updateContent(dto);
        return ResponseResult.success();
    }

    @PostMapping("/queryContentList")
    @ApiOperation("查询动态列表")
    public ResponseResult<PageInfo<HyContentInfoVO>> queryContentList(@RequestBody ContentQueryListDto dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        List<HyContentInfoVO> list = contentService.queryContentList(dto);
        PageInfo<HyContentInfoVO> page = new PageInfo<>(list);
        return ResponseResult.success(page);
    }

    @PostMapping("/detail")
    @ApiOperation("动态详情")
    public ResponseResult<HyContentInfoDO> queryContentInfo(@RequestBody ContentQueryDetailDto dto) {
        return ResponseResult.success(contentService.queryContentInfo(dto.getContentId()));
    }

}
