package com.happy.service.impl;

import com.happy.Model.*;
import com.happy.dao.ArticleTweetDao;
import com.happy.dto.IPage;
import com.happy.service.ArticleTweetService;
import com.happy.vo.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service("ArticleTweetService")
public class ArticleTweetImplService implements ArticleTweetService {

    @Resource
    public ArticleTweetDao articleTweetDao;

    @Override
    public ArticleTweet queryArticleById(String id, Integer userId) {
        ArticleTweet num = articleTweetDao.queryArticleById(id, userId);
        return num;
    }

    @Override
    public int insert(ArticleTweet articleTweet) {
        int num = articleTweetDao.insert(articleTweet);
        return num;
    }

    public int update(ArticleTweet articleTweet){
        return articleTweetDao.update(articleTweet);
    }

    @Override
    public int updateArticleApprove(ArticleTweet articleTweet) {
        int num = articleTweetDao.updateArticleApprove(articleTweet);
        return num;
    }

    @Override
    public int insertArticleFileBatch(List<ArticleFileInfo> articleFileInfos) {
        int num = articleTweetDao.insertArticleFileBatch(articleFileInfos);
        return num;
    }

    public int insertArticleFile(ArticleFileInfo articleFileInfos){
        return articleTweetDao.insertArticleFile(articleFileInfos);
    }

    public int delArticleFileBatch(int id){
        return articleTweetDao.delArticleFileBatch(id);
    }

    public int delArticle(int id){
        return articleTweetDao.delArticle(id);
    }

    public int delArticleFile(int link_id){
        return articleTweetDao.delArticleFile(link_id);
    }

    public int updateArticleTop(ArticleTweet articleTweet){
        return articleTweetDao.updateArticleTop(articleTweet);
    }

    @Override
    public IPage<HotelVo> queryHotelPage(String sqlx, int page, int rows) {
        IPage<HotelVo> iPage = new IPage();
        List<HotelVo> hotelList = articleTweetDao.queryHotelPage(sqlx, page, rows);
        int total = articleTweetDao.queryHotelTotal(sqlx);
        iPage.setPageList(hotelList == null ? new ArrayList<>() : hotelList);
        iPage.setPage(page);
        iPage.setTotalPage((int) Math.ceil((double) total / rows));
        iPage.setRows(rows);
        iPage.setTotal(total);
        return iPage;
    }

    @Override
    public List<HotelVo> queryHotels(String sqlx) {
        List<HotelVo> hotels = articleTweetDao.queryHotels(sqlx);
        return hotels;
    }

    @Override
    public IPage<HotelVo> queryHotelPageByHotleId(String sqlx, int page, int rows) {
        IPage<HotelVo> iPage = new IPage();
        List<HotelVo> hotelList = articleTweetDao.queryHotelPageByHotleId(sqlx, page, rows);
        int total = articleTweetDao.queryHotelTotalByHotleId(sqlx);
        iPage.setPageList(hotelList == null ? new ArrayList<>() : hotelList);
        iPage.setPage(page);
        iPage.setTotalPage((int) Math.ceil((double) total / rows));
        iPage.setRows(rows);
        iPage.setTotal(total);
        return iPage;
    }

    @Override
    public List<FileInfo> queryList(String sqlx) {
        List<FileInfo> result = articleTweetDao.queryList(sqlx);
        return result;
    }

    @Override
    public UserCollect queryUserCollect(Integer parentId, Integer userId) {
        UserCollect result = articleTweetDao.queryUserCollect(parentId, userId);
        return result;
    }

    @Override
    public List<LikeListVo> queryArticleLikes(Integer articleId) {
        List<LikeListVo> result = articleTweetDao.queryArticleLikes(articleId);
        return result;
    }

    @Override
    public List<ArticleCommentVo> queryArticleComment(Integer articleId) {
        List<ArticleCommentVo> result = articleTweetDao.queryArticleComment(articleId);
        return result;
    }

    @Override
    public int queryArticleCommentTotal(Integer articleId) {
        int result = articleTweetDao.queryArticleCommentTotal(articleId);
        return result;
    }

    @Override
    public IPage<ArticleCommentVo> queryCommentPageByArticle(Integer articleId, int page, int rows) {
        IPage<ArticleCommentVo> iPage = new IPage();
        List<ArticleCommentVo> commentList = articleTweetDao.queryCommentPageByArticle(articleId, page, rows);
        int total = articleTweetDao.queryCommentTotalByArticle(articleId);
        iPage.setPageList(commentList == null ? new ArrayList<>() : commentList);
        iPage.setPage(page);
        iPage.setTotalPage((int) Math.ceil((double) total / rows));
        iPage.setRows(rows);
        iPage.setTotal(total);
        return iPage;
    }

    @Override
    public List<ArticleCommentVo> queryCommentsByArticle(Integer articleId) {
        List<ArticleCommentVo> result = articleTweetDao.queryCommentsByArticle(articleId);
        return result;
    }

    @Override
    public articleUserVo queryUserInfo(Integer userId) {
        articleUserVo result = articleTweetDao.queryUserInfo(userId);
        return result;
    }

    @Override
    public IPage<OwnerArticleVo> queryOwnerArticlePage(Integer userId, String sqlWhere, int page, int rows) {
        IPage<OwnerArticleVo> iPage = new IPage();
        List<OwnerArticleVo> commentList = articleTweetDao.queryOwnerArticlePage(userId, sqlWhere, page, rows);
        int total = articleTweetDao.queryOwnerArticleTotal(userId, sqlWhere);
        iPage.setPageList(commentList == null ? new ArrayList<>() : commentList);
        iPage.setPage(page);
        iPage.setTotalPage((int) Math.ceil((double) total / rows));
        iPage.setRows(rows);
        iPage.setTotal(total);
        return iPage;
    }

    @Override
    public IPage<ArticleListVo> queryArticlesPage(String keyWord, String townId, Integer userId, Integer type, int page, int rows) {
        IPage<ArticleListVo> iPage = new IPage();
        List<ArticleListVo> commentList = articleTweetDao.queryArticlesPage(keyWord, townId, userId, type, page, rows);
        int total = articleTweetDao.queryArticlesTotal(keyWord, townId, userId, type);
        iPage.setPageList(commentList == null ? new ArrayList<>() : commentList);
        iPage.setPage(page);
        iPage.setTotalPage((int) Math.ceil((double) total / rows));
        iPage.setRows(rows);
        iPage.setTotal(total);
        return iPage;
    }

    public IPage<ArticleListVo> queryManageArticlesPage(String keyWord, String townId, int page, int rows) {
        IPage<ArticleListVo> iPage = new IPage();
        List<ArticleListVo> commentList = articleTweetDao.queryManageArticlesPage(keyWord, townId, page, rows);
        int total = articleTweetDao.queryManageArticlesTotal(keyWord, townId);
        iPage.setPageList(commentList == null ? new ArrayList<>() : commentList);
        iPage.setPage(page);
        iPage.setTotalPage((int) Math.ceil((double) total / rows));
        iPage.setRows(rows);
        iPage.setTotal(total);
        return iPage;
    }

    @Override
    public IPage<ArticleListVo> relatedTweetPage(Integer userId, String townId, Integer articleId, int page, int rows) {
        IPage<ArticleListVo> iPage = new IPage();
        List<ArticleListVo> commentList = articleTweetDao.relatedTweetPage(userId, townId, articleId, page, rows);
        int total = articleTweetDao.relatedTweetTotal(townId, articleId);
        iPage.setPageList(commentList == null ? new ArrayList<>() : commentList);
        iPage.setPage(page);
        iPage.setTotalPage((int) Math.ceil((double) total / rows));
        iPage.setRows(rows);
        iPage.setTotal(total);
        return iPage;
    }

    @Override
    public ArticleCollect queryArticleCollect(Integer articleId, Integer userId) {
        ArticleCollect result = articleTweetDao.queryArticleCollect(articleId, userId);
        return result;
    }

    @Override
    public int updateArticleCollect(ArticleCollect articleCollect) {
        int result = articleTweetDao.updateArticleCollect(articleCollect);
        return result;
    }

    ;

    @Override
    public int insertArticleComment(ArticleComment articleComment) {
        int result = articleTweetDao.insertArticleComment(articleComment);
        return result;
    }

    /**
     * 查询用户关注记录数据
     *
     * @param authorId
     * @param userId
     * @return
     */
    public UserCollect queryUserCollectNoLose(Integer authorId, Integer userId) {
        UserCollect result = articleTweetDao.queryUserCollectNoLose(authorId, userId);
        return result;
    }

    /**
     * 关注或取消关注操作
     *
     * @param userCollect
     * @return
     */
    public int updateUserCollect(UserCollect userCollect) {
        int result = articleTweetDao.updateUserCollect(userCollect);
        return result;
    }

    /**
     * 查询用户点赞记录数据
     *
     * @param articleId
     * @param userId
     * @return
     */
    public ArticleLikes queryArticleLike(Integer articleId, Integer userId) {
        ArticleLikes result = articleTweetDao.queryArticleLike(articleId, userId);
        return result;
    }

    /**
     * 点赞文章
     *
     * @param articleLikes
     * @return
     */
    public int updateArticleLike(ArticleLikes articleLikes) {
        int num = articleTweetDao.updateArticleLike(articleLikes);
        return num;
    }

    /**
     * 更新用户简介
     *
     * @param authorId
     * @param descript
     * @return
     */
    public int updateDescript(Integer authorId, String descript) {
        int num = articleTweetDao.updateDescript(authorId, descript);
        return num;
    }


    @Override
    public IPage<LikeListVo> queryLikesPage(Integer articleId, int page, int rows) {
        IPage<LikeListVo> iPage = new IPage();
        List<LikeListVo> commentList = articleTweetDao.queryLikesPage(articleId, page, rows);
        int total = articleTweetDao.queryLikesTotal(articleId);
        iPage.setPageList(commentList == null ? new ArrayList<>() : commentList);
        iPage.setPage(page);
        iPage.setTotalPage((int) Math.ceil((double) total / rows));
        iPage.setRows(rows);
        iPage.setTotal(total);
        return iPage;
    }


    @Override
    public ArticleLikeMaxVo queryMaxArticleLike() {
        ArticleLikeMaxVo result = articleTweetDao.queryMaxArticleLike();
        return result;
    }

    @Override
    public IPage<WalkthroughVo> walkthroughPage(String strSql, Integer page, Integer rows) {
        IPage<WalkthroughVo> iPage = new IPage();
        List<WalkthroughVo> commentList = articleTweetDao.walkthroughPage(strSql, page, rows);
        int total = articleTweetDao.walkthroughTotal(strSql);
        iPage.setPageList(commentList == null ? new ArrayList<>() : commentList);
        iPage.setPage(page);
        iPage.setTotalPage((int) Math.ceil((double) total / rows));
        iPage.setRows(rows);
        iPage.setTotal(total);
        return iPage;
    }

    @Override
    public List<ArticleLikeMaxVo> queryTop3ArticleLike() {
        List<ArticleLikeMaxVo> result = articleTweetDao.queryTop3ArticleLike();
        return result;
    }

    public List<ArticleTweet> queryTops(){
        return articleTweetDao.queryTops();
    }

}
