package cn.com.ty.lift.quan.news.controller;

import cn.com.ty.lift.common.model.PushMessage;
import cn.com.ty.lift.quan.news.dao.entity.QuanNews;
import cn.com.ty.lift.quan.news.dao.entity.model.QuanNewsReq;
import cn.com.ty.lift.quan.news.service.impl.QuanNewsServiceImpl;
import cn.com.xwy.boot.web.dto.RestResponse;
import lombok.AllArgsConstructor;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/**
 * <p>
 * 电梯圈新闻公告 前端控制器
 * </p>
 *
 * @author wang-hai-cheng
 * @since 2019-12-02
 */
@RestController
@AllArgsConstructor
@RequestMapping("/news")
public class QuanNewsController {
    private final QuanNewsServiceImpl quanNewsService;
    private final JmsMessagingTemplate jmsMessagingTemplate;

    /**
     * 分页展示置顶新闻+普通新闻
     *
     * @param quanNews [页码, 页大小]
     * @return cn.com.xwy.boot.web.dto.RestResponse
     * @since 2019/12/2 15:39
     */
    @PostMapping("app/list")
    public RestResponse list(@RequestBody QuanNewsReq quanNews) {
        return quanNewsService.appNews(quanNews);
    }

    /**
     * @param quanNews pageNum,pageSize
     * @return 新闻列表
     */
    @PostMapping("/list")
    public RestResponse newsList(@RequestBody QuanNewsReq quanNews) {
        return quanNewsService.news(quanNews);
    }

    /**
     * 获取新闻
     *
     * @param quanNews 新闻id，用户id
     * @return 新闻信息
     */
    @PostMapping
    public RestResponse news(@RequestBody QuanNewsReq quanNews) {
        return quanNewsService.getNews(quanNews);
    }

    /**
     * 新增新闻
     *
     * @param quanNews [新闻标题, 新闻内容, 视频文件, 发布人id]
     * @return org.springframework.http.ResponseEntity
     * @since 2019/12/2 15:48
     */
    @PostMapping("/add")
    public RestResponse add(@RequestBody QuanNews quanNews) {
        //推送消息给全平台
        PushMessage.newsNotice(quanNews.getTitle()).sendAllOnPlatform(jmsMessagingTemplate);
        return RestResponse.success(quanNewsService.save(quanNews));
    }

    /**
     * 根据id更新新闻信息
     *
     * @param quanNews [新闻id, 新闻标题, 新闻内容]
     * @return org.springframework.http.ResponseEntity
     * @since 2019/12/2 15:49
     */
    @PostMapping("/modify")
    public RestResponse updateNews(@RequestBody QuanNews quanNews) {
        return RestResponse.success(quanNewsService.updateById(quanNews));
    }

    /**
     * 根据id删除新闻
     *
     * @param quanNews [新闻id]
     * @return org.springframework.http.ResponseEntity
     * @since 2019/12/2 15:49
     */
    @PostMapping("/delete")
    public RestResponse delete(@RequestBody QuanNews quanNews) {
        return RestResponse.success(quanNewsService.removeById(quanNews));
    }

    /**
     * 点赞新闻
     *
     * @param request [用户id,新闻id]
     * @return org.springframework.http.ResponseEntity
     */
    @PostMapping("/like")
    public RestResponse like(@RequestBody QuanNewsReq request) {
        return quanNewsService.like(request);
    }

    /**
     * 市列表
     *
     * @return 市列表
     */
    @PostMapping("/area")
    public RestResponse area() {
        return quanNewsService.areas();
    }

    /**
     * 根据经纬度定位市名称
     *
     * @param point 经纬度，如:39.990912172420714,116.32715863448607
     * @return 市名称
     * @throws IOException jackson反序列化解析错误
     */
    @GetMapping("/location")
    public RestResponse location(@RequestParam String point) throws IOException {
        return quanNewsService.location(point);
    }
}