package com.ruoyi.system.service.impl;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.service.ISysDictTypeService;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.BomanNewsMapper;
import com.ruoyi.system.domain.BomanNews;
import com.ruoyi.system.service.IBomanNewsService;

/**
 * boman_newsService业务层处理
 *
 * @author boman
 * @date 2023-11-02
 */
@Service
public class BomanNewsServiceImpl implements IBomanNewsService {
    @Autowired
    private BomanNewsMapper bomanNewsMapper;

    @Autowired
    private ISysDictTypeService dictTypeService;

    /**
     * 查询boman_news
     *
     * @param newsId boman_news主键
     * @return boman_news
     */
    @Override
    public BomanNews selectBomanNewsByNewsId(Long newsId) {
        return bomanNewsMapper.selectBomanNewsByNewsId(newsId);
    }

    /**
     * 查询boman_news列表
     *
     * @param bomanNews boman_news
     * @return boman_news
     */
    @Override
    public List<BomanNews> selectBomanNewsList(BomanNews bomanNews) {
        return bomanNewsMapper.selectBomanNewsList(bomanNews);
    }

    /**
     * 新增boman_news
     *
     * @param bomanNews boman_news
     * @return 结果
     */
    @Override
    public int insertBomanNews(BomanNews bomanNews) {
        bomanNews.setCreateTime(DateUtils.getNowDate());
        return bomanNewsMapper.insertBomanNews(bomanNews);
    }

    /**
     * 修改boman_news
     *
     * @param bomanNews boman_news
     * @return 结果
     */
    @Override
    public int updateBomanNews(BomanNews bomanNews) {
        bomanNews.setUpdateTime(DateUtils.getNowDate());
        return bomanNewsMapper.updateBomanNews(bomanNews);
    }

    /**
     * 批量删除boman_news
     *
     * @param newsIds 需要删除的boman_news主键
     * @return 结果
     */
    @Override
    public int deleteBomanNewsByNewsIds(Long[] newsIds) {
        return bomanNewsMapper.deleteBomanNewsByNewsIds(newsIds);
    }

    /**
     * 删除boman_news信息
     *
     * @param newsId boman_news主键
     * @return 结果
     */
    @Override
    public int deleteBomanNewsByNewsId(Long newsId) {
        return bomanNewsMapper.deleteBomanNewsByNewsId(newsId);
    }

    /**
     * 发布首页统计
     */
    @Override
    public AjaxResult indexStatisticsInFor() {
        Map<String, Object> map = new HashMap<>();
        map.put("year", 0);
        map.put("yearMonth", 0);
        JSONArray jsonArray = new JSONArray();
        //查询字典值
        List<SysDictData> dictDataList = dictTypeService.selectDictDataByType("wenzl");
        if (dictDataList != null && dictDataList.size() > 0) {
            for (SysDictData sysDictData : dictDataList) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("id", sysDictData.getDictValue());
                jsonObject.put("this", 0);
                jsonObject.put("last", 0);
                jsonObject.put("status", 0);
                jsonArray.add(Integer.parseInt(sysDictData.getDictValue()), jsonObject);
            }
        }
        //查询本月发布的文章数据
        BomanNews bomanNews = new BomanNews();
        bomanNews.setReleaseTime(DateUtils.getNowDate());
        //本月数据
        List<BomanNews> bomanNewsList = bomanNewsMapper.selectBomanNewsList(bomanNews);
        //查询上月数据
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, -1); //得到前一个月
        bomanNews.setReleaseTime(DateUtils.parseDate(format.format(c.getTime())));
        //todo 上月数据放到redis中
        List<BomanNews> bomanNewsListLast = bomanNewsMapper.selectBomanNewsList(bomanNews);
        if (bomanNewsList != null && bomanNewsList.size() > 0) {
            map.put("yearMonth", bomanNewsList.size());
            //进行数据比对
            Map<String, List<BomanNews>> collect = bomanNewsList.stream().collect(Collectors.groupingBy(BomanNews::getType));
            if (collect != null && collect.size() > 0) {
                for (String key : collect.keySet()) {
                    JSONObject jsonObject = (JSONObject) jsonArray.get(Integer.parseInt(key));
                    jsonObject.put("this", collect.get(key).size());
                }
            }
            if (bomanNewsListLast != null && bomanNewsListLast.size() > 0) {
                //计算上月数据
                Map<String, List<BomanNews>> collectLast = bomanNewsListLast.stream().collect(Collectors.groupingBy(BomanNews::getType));
                if (collect != null && collect.size() > 0) {
                    for (String key : collect.keySet()) {
                        //本月数据
                        JSONObject jsonObject = (JSONObject) jsonArray.get(Integer.parseInt(key));
                        int num = (int) jsonObject.get("this");
                        int numLast = 0;
                        if (collectLast.get(key) != null && collectLast.get(key).size() > 0) {
                            //上月数据
                            numLast = collectLast.get(key).size();
                        }
                        //计算 x=y=0 x<y=-1 x>y=1
                        int compare = NumberUtils.compare(num, numLast);
                        jsonObject.put("last", num - numLast);
                        jsonObject.put("status", compare);
                    }
                }
            }
        }
        map.put("arrayList", jsonArray);
        return AjaxResult.success(map);
    }
}
