package com.app.controller.admin.info;

import com.app.annotation.platform.OperationLog;
import com.app.dto.PageResult;
import com.app.dto.Result;
import com.app.dto.SearchDto;
import com.app.dto.basics.operation.NewsDto;
import com.app.dto.basics.search.SearchNewsDto;
import com.app.entity.info.News;
import com.app.service.info.NewsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 功能描述：新闻资讯
 *
 * @author 禤润峰
 * @date 2021年1月15日09点59分
 */
@Api(description = "新闻资讯")
@Slf4j
@Validated
@RestController("AdminNewsController")
@RequestMapping("/admin/info/news")
public class NewsController {

    @Resource
    private NewsService newsService;

    @ApiOperation(value = "查询[新闻详情]")
    @PostMapping("/{newsId}")
    public ResponseEntity<Result<News>> findNewsById(@PathVariable("newsId") String newsId) {
        return new ResponseEntity(new Result().ok(newsService.findNewsById(newsId)), HttpStatus.OK);
    }

    /**
     * 查询【新闻资讯】列表
     *
     * @param params 搜索dto
     * @return 响应结果
     */
    @ApiOperation(value = "查询[新闻资讯]列表")
    @PostMapping("/page")
    public ResponseEntity<Result<PageResult<News>>> page(@Valid @RequestBody SearchNewsDto params) {
        return new ResponseEntity(new Result<>().ok(newsService.page(params)), HttpStatus.OK);
    }

    /**
     * 保存/更新[新闻资讯]
     *
     * @param dto 查询dto
     * @return 成功响应
     */
    @OperationLog("更新 - 新闻资讯")
    @ApiOperation(value = "保存/更新[新闻资讯]")
    @PostMapping("/save-or-update")
    public ResponseEntity<Result> saveOrUpdate(@Valid @RequestBody NewsDto dto) {
        newsService.saveOrUpdate(dto);
        return new ResponseEntity(new Result().ok(), HttpStatus.OK);
    }

    @OperationLog("替换删除状态 - 新闻资讯")
    @ApiOperation(value = "替换[新闻资讯]删除状态")
    @PostMapping("/handle-is-del")
    public ResponseEntity<Result> handleIsDel(@Valid @RequestBody SearchDto searchdto) {
        newsService.handleIsDel(searchdto.getId());
        return new ResponseEntity(new Result().ok(), HttpStatus.OK);
    }

}
