package com.ruoyi.mall.service.impl;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.mall.domain.MallMemberUser;
import com.ruoyi.mall.domain.Vo.BusinessArticleVo;
import com.ruoyi.mall.mapper.ArticleTagMapper;
import org.springframework.stereotype.Service;
import com.ruoyi.mall.mapper.BusinessArticleMapper;
import com.ruoyi.mall.domain.BusinessArticle;
import com.ruoyi.mall.service.IBusinessArticleService;

import javax.annotation.Resource;

/**
 * 帖子管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-10-17
 */
@Service
public class BusinessArticleServiceImpl implements IBusinessArticleService 
{
    @Resource
    private BusinessArticleMapper businessArticleMapper;
    @Resource
    private ArticleTagMapper articleTagMapper;

    /**
     * 查询帖子管理
     * 
     * @param articleId 帖子管理主键
     * @return 帖子管理
     */
    @Override
    public BusinessArticleVo selectBusinessArticleByArticleId(Long articleId)
    {
        return businessArticleMapper.selectBusinessArticleByArticleId(articleId);
    }

    /**
     * 查询帖子管理列表
     * 
     * @param businessArticle 帖子管理
     * @return 帖子管理
     */
    @Override
    public List<BusinessArticleVo> selectBusinessArticleList(BusinessArticle businessArticle)
    {
        return businessArticleMapper.selectBusinessArticleList(businessArticle);
    }

    /**
     * 查询帖子管理列表(xcx,结果包含likeFlag//用户点赞状态）
     * @param content 搜索内容
     * @param orderByColumn　需要排序的字段
     * @param descFlag　时间排序desc/asc
     * @return 帖子管理集合
     */
    public List<BusinessArticleVo> selectListForXcx(Long userId,String content,String orderByColumn,String descFlag){
        return businessArticleMapper.selectListForXcx(userId,content,orderByColumn,descFlag);
    }
    /**查询帖子详情
     *
     * @param userId 登录者id
     * @param articleId 帖子id
     * @return
     */
    @Override
    public BusinessArticleVo selectDetailForXcx(Long userId,Long articleId){
        return businessArticleMapper.selectDetailForXcx(userId,articleId);
    }
    /**xcx 热度话题列表*/
    @Override
    public String[] hotTagList(){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        //过去七天
        c.setTime(new Date());
        c.add(Calendar.DATE, - 7);
        Date d = c.getTime();
        String day = format.format(d);
        return businessArticleMapper.hotTagList(day);
    }

/**xcx 我的 帖子列表*/
    /**
     * 查询帖子管理列表(xcx,结果包含likeFlag//用户点赞状态）
     * @return 帖子管理集合
     */
    @Override
    public List<BusinessArticleVo> selectListForXcxMy(Long userId){

        return businessArticleMapper.selectListForXcxMy(userId);
    }
    /**查询帖子详情
     *
     * @param userId 我的id
     * @param articleId 帖子id
     * @return
     */
    @Override
    public BusinessArticleVo selectDetailForXcxMy(Long userId,Long articleId){
        return businessArticleMapper.selectDetailForXcxMy(userId,articleId);
    }
    /**
     * 新增帖子管理
     * 
     * @param businessArticle 帖子管理
     * @return 结果
     */
    @Override
    public int insertBusinessArticle(BusinessArticle businessArticle)
    {
        //修改帖子热度
        if(StringUtils.isNotBlank(businessArticle.getArticleTag())){
            String tags = businessArticle.getArticleTag();
            String[] split = tags.split (",");
            int i = articleTagMapper.updateHotByTags(split);
        }
        businessArticle.setCreateTime(DateUtils.getNowDate());
        return businessArticleMapper.insertBusinessArticle(businessArticle);
    }

    /**
     * 修改帖子管理
     * 
     * @param businessArticle 帖子管理
     * @return 结果
     */
    @Override
    public int updateBusinessArticle(BusinessArticle businessArticle)
    {
        //修改帖子热度
        if(StringUtils.isNotBlank(businessArticle.getArticleTag())){
            String tags = businessArticle.getArticleTag();
            String[] split = tags.split (",");
            int i = articleTagMapper.updateHotByTags(split);
        }
        businessArticle.setUpdateTime(DateUtils.getNowDate());
        return businessArticleMapper.updateBusinessArticle(businessArticle);
    }

    /**
     * 逻辑批量删除帖子管理
     * 
     * @param articleIds 需要删除的帖子管理主键
     * @return 结果
     */
    @Override
    public int deleteBusinessArticleByArticleIds(Long[] articleIds)
    {   BusinessArticle businessArticle = new BusinessArticle();
        businessArticle.setUpdateTime(DateUtils.getNowDate());
        return businessArticleMapper.deleteBusinessArticleByArticleIds(articleIds,businessArticle);
    }

    /**
     * 逻辑删除帖子管理信息
     * 
     * @param articleId 帖子管理主键
     * @return 结果
     */
    @Override
    public int deleteBusinessArticleByArticleId(Long articleId)
    {
        return businessArticleMapper.deleteBusinessArticleByArticleId(articleId);
    }
}
