package com.dgtis.dorm.manager.db.service;

import com.github.pagehelper.PageHelper;
import com.dgtis.dorm.manager.db.dao.NewsCommentMapper;
import com.dgtis.dorm.manager.db.dao.NewsMapper;
import com.dgtis.dorm.manager.db.dao.NewsOrganizeMapper;
import com.dgtis.dorm.manager.db.dao.NewsZanMapper;
import com.dgtis.dorm.manager.db.domain.News;
import org.springframework.stereotype.Service;

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

@Service
public class NewsService {

    @Resource
    private NewsMapper newsMapper;
    @Resource
    private NewsZanMapper newsZanMapper;
    @Resource
    private NewsOrganizeMapper newsOrganizeMapper;
    @Resource
    private NewsCommentMapper newsCommentMapper;

    /**
     * 查询息交互内容
     * @param news
     * @return
     */
    public List<News> findNews(News news) {

        List<News> lstNews = null;
        lstNews = newsMapper.selectByExample(news);

        return lstNews;
    }

    /**
     * 分页查询 息交互内容
     * @param news
     * @return
     */
    public List<News> querySelective(News news) {

        List<News> lstNews = null;
        int page = news.getNewsCriterion().getPage();
        int limit = news.getNewsCriterion().getLimit();
        PageHelper.startPage(page, limit);
        lstNews = newsMapper.selectByExample(news);

        return lstNews;
    }

    /**
     * 获取交互内容，并带评论数量、点赞数量
     */
    public List<News> querySelctiveWithComment(News news) {

        List<News> lstNews = null;

        int page = news.getNewsCriterion().getPage();
        int limit = news.getNewsCriterion().getLimit();
        PageHelper.startPage(page, limit);

        lstNews = newsMapper.selectByExampleWithComment(news);

        return lstNews;
    }

    /**
     * 获取活动新闻 并获取关联活动的 活动开始时间、活动地址、活动人数
     */
    public List<News> querySelectiveForActivityNews(News news) {
        List<News> lstNews = null;

        int page = news.getNewsCriterion().getPage();
        int limit = news.getNewsCriterion().getLimit();
        PageHelper.startPage(page, limit);

        lstNews = newsMapper.selectByExampleForActivityNews(news);

        return lstNews;
    }

    /**
     * 获取圈子的动态，新闻、活动 ...
     */
    public List<News> queryByOrgIdForNews(Integer orgId, Integer userIdForZan, int page, int limit) {

        List<News> lstNews = null;
        PageHelper.startPage(page, limit);
        lstNews = newsMapper.selectByOrgIdForActivityNews(orgId, userIdForZan);

        return lstNews;

    }

    /**
     * 获取圈子动态列表数量
     */
    public Integer countByOrgIdForNews(Integer orgId) {
        return newsMapper.countForActivityNews(orgId);
    }

    /**
     * 查询关注好友的动态，新闻、活动 ...
     */
    public List<News> queryByFriendNews(Integer userId, int page, int limit) {

        List<News> lstNews = null;
        PageHelper.startPage(page, limit);
        lstNews = newsMapper.selectForFriendNews(userId);

        return lstNews;

    }

    /**
     * 查询关注好友的动态列表数量
     */
    public Integer countByFriendForNews(Integer userId) {
        return newsMapper.countForFriendNews(userId);
    }

    /**
     * 获取查询结果总数量
     * @param news
     * @return
     */
    public Integer countSelective(News news) {

        Integer total = (int)newsMapper.countByExample(news);
        return total;
    }

    /**
     * 获取息交互内容详情
     * @param id
     * @return
     */
    public News findById(int id) {

        News news = newsMapper.selectByPrimaryKey(id);
        return news;
    }

    /**
     * 获取交互内容详情，包含:评论数量、点赞数量、参加活动人员数量
     * @param id
     * @return
     */
    public News findByIdWithCommon(int id, int userIdForZan) {
        News news = newsMapper.selectByPrimaryKeyWithComment(id, userIdForZan);
        return news;
    }

    /**
     * 录入息交互内容信息
     * @param news
     */
    public void add(News news) {

        newsMapper.insertSelective(news);
    }

    /**
     * 更新息交互内容信息
     * @param news
     */
    public void updateById(News news) {

        newsMapper.updateByPrimaryKeySelective(news);
    }

    /**
     * 删除息交互内容
     * @param id
     */
    public void deleteById(int id) {

        newsMapper.deleteByPrimaryKey(id);
    }

    /**
     * 批量删除交互内容
     * @param list
     */
    public void deleteQuantity(List<String> list ) {
        for (String id:list) {
             newsMapper.deleteByPrimaryKey(Integer.parseInt(id));
        }
    }
    /**
     * 获取参加的活动列表
     * @param news
     * @return
     */
    public List<News> querySelectiveForMyJoin(News news) {
        List<News> lstNews = null;
        int page = news.getNewsCriterion().getPage();
        int limit = news.getNewsCriterion().getLimit();
        PageHelper.startPage(page, limit);

        lstNews = newsMapper.selectByExampleForMyJoin(news);

        return lstNews;
    }

    /**
     * 获取参加活动的数量
     */
    public Integer countSelectiveForMyJoin(News news) {
        return newsMapper.countForMyJoin(news);
    }

    /**
     * 删除圈子动态
     * @param newsId
     */
    public void deleteDynamicByNewId(int newsId) {
        //删除原动态
        newsMapper.deleteByPrimaryKey(newsId);
        //删除动态和圈子的关联
        newsOrganizeMapper.deleteByActivityId(newsId);
        //删除点赞
        newsZanMapper.deleteByNewId(newsId);
        //删除评论
        newsCommentMapper.deleteByNewId(newsId);


    }

    /**
     * 根据活动id和圈子id删除圈子关联的活动，只删除关联圈子的所在活动
     * @param newsId
     * @param organizeId
     */
    public void deleteOrganizeNewsByActivity(String newsId, String organizeId) {
        newsOrganizeMapper.deleteOrganizeNewsByActivity(newsId, organizeId);
    }
}
